Debian Codegraph @

dojox/drawing/stencil/_Base.js

raw symbols SCIP
1 define(["dojo", "dojo/fx/easing", "../util/oo", "../annotations/BoxShadow",
2 "../annotations/Angle", "../annotations/Label", "../defaults"],
3 function(dojo, easing, oo, BoxShadow, Angle, Label, defaults){
4
5 /*=====
6 var StencilArgs = {
7 // container: [readonly] dojo.gfx.group
8 // The parent shape that contains all
9 // shapes used in a Stencil
10 container:null,
11
12 // anchorType: String
13 // Optionally blank or 'group'. 'group' tells
14 // an anchor point that it must constrain
15 // itself to other anchor points.
16 anchorType:"",
17
18 // isText: Boolean
19 // Whether this is a text object or not
20 // (either stencil.text or tools.TextBlock)
21 isText:false,
22
23 // shortType: String
24 // The type of stencil that corresponds with the types and
25 // constructors used in Drawing.registerTool
26 shortType:"",
27
28 // annotation: Boolean
29 // A Stencil used within a Stencil. An annotation
30 // is not selectable or clickable. A Label would
31 // be one example.
32 annotation:false,
33
34 // subShape: Boolean
35 // A Stencil used within a Stencil. A subShape
36 // is clickable. An arrow head would be an example.
37 subShape:false,
38
39 // style: Object
40 // An instance of the styles and defaults used within
41 // the Stencil.
42 style:null,
43
44 // util: Object
45 // Pointer to util.common
46 util:null,
47
48 // mouse: Object
49 // Pointer to the mouse instance
50 mouse:null,
51
52 // keys: Object
53 // Pointer to the keys class
54 keys:null,
55
56 // points: StencilPoints
57 // Points is an array of objects that make up the
58 // description of a Stencil. The points to a Rect
59 // that is 100x100 and at x:10 and y:10 would look like:
60 // [{x:10,y:10}, {x:110, y:10}, {x:110, y:110}, {x:10, y:110}]
61 // Points go clockwise from the top left. In the case of Paths,
62 // they would go in the order that the Stencil would be drawn.
63 // Always when the points Array is set, a data Object is created
64 // as well. So never set points directly, always use setPoints().
65 //
66 // See: setPoints()
67 points:[],
68
69 // data: StencilData
70 // A data object typically (but not always) resembles the data
71 // that is used to create the dojox.gfx Shape. The same Rect
72 // example shown in points above would look like:
73 // {x:10, y:10, width:100, height:100}
74 // And an Ellipse with the same coordinates:
75 // {cx:55, cy:55, rx:50, ry:50}
76 // The only Stencil that does not support data (at this time)
77 // is the Path. While x1,x2,x3... culd be used in a data object
78 // it doesn't provide much benefit.
79 // Always when a data object is set, a set of points is created
80 // as well. So never set data directly, always use setData().
81 //
82 // See: setData()
83 data:null,
84
85 // marginZero [readonly] Number
86 // How closely shape can get to y:0 or x:0. Less than zero has
87 // bugs in VML. This is set with defaults, and should be equal
88 // to half the size of an anchor point (5 px)
89 marginZero:0,
90
91 // created: [readonly] Boolean
92 // Whether the Stencil has been rendered for the first time or
93 // not.
94 created: false,
95
96 // highlighted: [readonly] Boolean
97 // Whether the Stencil is highlighted or not.
98 highlighted:false,
99
100 // selected: [readonly] Boolean
101 // Whether the Stencil is selected or not.
102 selected:false,
103
104 // draws: [readonly] Boolean
105 // Whether the Stencil can draw with a mouse drag or can just
106 // be created programmtically. If the Stencil comes from the
107 // stencil package, it should be draw:false. If it comes from
108 // the tools package it should be draw:true.
109 draws:false
110 };
111
112 var StencilPoint = {
113 // summary:
114 // One point Object in the points Array
115 // x: Number
116 // x position of point
117 // y: Number
118 // y position of point
119 };
120
121 var ToolsSetup = {
122 // summary:
123 // An object attached to a Tool's constructor
124 // used to inform the toolbar of its information
125 // and properties.
126 // description:
127 // This object is inserted into the *function* of
128 // a tool (not a stencil). Like: function.ToolsSetup
129 // It must be attached after constructr creation, so
130 // this object is found at the bottom of the file.
131
132 // name: String
133 // Fully qualified name of constructor
134 // tooltip: String
135 // Text to display on toolbar button hover
136 // iconClass: String
137 // CSS class with icon information to attach
138 // to toolbar button.
139 };
140 =====*/
141
142 var Base = oo.declare(
143 function(options){
144 //console.log("______Base", this.type, options)
145 // clone style so changes are reflected in future shapes
146 dojo.mixin(this, options);
147 this.style = options.style || defaults.copy();
148 if(options.stencil){
149 this.stencil = options.stencil;
150 this.util = options.stencil.util;
151 this.mouse = options.stencil.mouse;
152 this.container = options.stencil.container;
153 this.style = options.stencil.style;
154 }
155
156 // don't use the 'g' on these, it affects
157 // the global RegExp
158 var lineTypes = /Line|Vector|Axes|Arrow/;
159 var textTypes = /Text/;
160
161 this.shortType = this.util.abbr(this.type);
162 this.isText = textTypes.test(this.type);
163 this.isLine = lineTypes.test(this.type);
164
165 this.renderHit = this.style.renderHitLayer;
166 if(!this.renderHit && this.style.renderHitLines && this.isLine){
167 this.renderHit = true;
168 }
169 if(!this.renderHit && this.style.useSelectedStyle){
170 this.useSelectedStyle = true;
171 this.selCopy = dojo.clone(this.style.selected);
172 for(var nm in this.style.norm){
173 if(this.style.selected[nm]===undefined){
174 this.style.selected[nm] = this.style.norm[nm];
175 }
176 }
177 this.textSelected = dojo.clone(this.style.text);
178 this.textSelected.color = this.style.selected.fill;
179
180 }
181
182
183 this.angleSnap = this.style.angleSnap || 1;
184
185 this.marginZero = options.marginZero || this.style.anchors.marginZero;
186 this.id = options.id || this.util.uid(this.type);
187 this._cons = [];
188
189 if(!this.annotation && !this.subShape){
190 this.util.attr(this.container, "id", this.id);
191 }
192
193 this.connect(this, "onBeforeRender", "preventNegativePos");
194
195 this._offX = this.mouse.origin.x;
196 this._offY = this.mouse.origin.y;
197
198 if(this.isText){
199 this.align = options.align || this.align;
200 this.valign = options.valign || this.valign;
201 if(options.data && options.data.makeFit){
202 var textObj = this.makeFit(options.data.text, options.data.width);
203 this.textSize = this.style.text.size = textObj.size;
204 this._lineHeight = textObj.box.h;
205 }else{
206 this.textSize = parseInt(this.style.text.size, 10);
207 this._lineHeight = this.textSize * 1.4;
208 }
209
210
211 // TODO: thinner text selection
212 //this.style.hitSelected.width *= 0.5;
213
214 // ouch. how verbose. My mixin is weak....
215 this.deleteEmptyCreate = options.deleteEmptyCreate!==undefined ? options.deleteEmptyCreate : this.style.text.deleteEmptyCreate;
216 this.deleteEmptyModify = options.deleteEmptyModify!==undefined ? options.deleteEmptyModify : this.style.text.deleteEmptyModify;
217 }
218
219 //this.drawingType
220
221 this.attr(options.data);
222
223 // make truthy
224 // add to renders below
225 // this.baseRender && render()
226 //if(this.type == "dojox.drawing.tools.TextBlock"){
227 if(this.noBaseRender){
228 // TextBlock will handle rendering itself
229 return;
230 }
231
232 //console.log("BASE OPTS:", options)
233 if(options.points){
234 //console.log("__________Base.constr >>>> ", this.type, "points", options.points)
235 if(options.data && options.data.closePath===false){
236 this.closePath = false;
237 }
238 this.setPoints(options.points);
239 this.connect(this, "render", this, "onRender", true);
240 this.baseRender && this.enabled && this.render();
241 options.label && this.setLabel(options.label);
242 options.shadow && this.addShadow(options.shadow);
243
244 }else if(options.data){
245 //console.log("___________Base.constr", this.type, "options data", options.data)
246 options.data.width = options.data.width ? options.data.width : this.style.text.minWidth;
247 options.data.height = options.data.height ? options.data.height : this._lineHeight;
248 this.setData(options.data);
249 this.connect(this, "render", this, "onRender", true);
250 this.baseRender && this.enabled && this.render(options.data.text);
251 this.baseRender && options.label && this.setLabel(options.label);
252 this.baseRender && options.shadow && this.addShadow(options.shadow);
253
254 }else if(this.draws){
255 //console.log("_____________Base.constr", this.type, "draws")
256 this.points = [];
257 this.data = {};
258 this.connectMouse();
259 this._postRenderCon = dojo.connect(this, "render", this, "_onPostRender");
260 }
261 if(this.showAngle){
262 this.angleLabel = new Angle({stencil:this});
263 }
264
265 if(!this.enabled){
266 this.disable();
267 this.moveToBack();
268 // some things render some don't...
269 this.render(options.data.text);
270 }
271
272 },
273 {
274 // summary:
275 // The base class used for all Stencils.
276 // description:
277 // All stencils extend this base class.
278 // Most methods and events can be found here.
279
280 // type: String
281 // The type of Stencil this is. Should be overridden
282 // by extending classes.
283 // FIXME: should this be declaredClass?
284 type:"dojox.drawing.stencil",
285
286 // minimumSize: Number
287 // The minimum size allowed for a render. If the size
288 // is less, the shape is destroyed.
289 minimumSize:10,
290
291 // enabled: [readonly] Boolean
292 // Whether the Stencil is enabled or not.
293 enabled:true,
294
295
296 drawingType:"stencil",
297
298 //points:[],
299
300 setData: function(/*StencilData*/data){
301 // summary:
302 // Setter for Stencil data; also converts
303 // data to points. See individual Stencils
304 // for specific data properties.
305 this.data = data;
306 this.points = this.dataToPoints();
307 },
308
309 setPoints: function(/*StencilPoints*/points){
310 // summary:
311 // Setter for Stencil points; also converts
312 // points to data. See individual Stencils
313 // for specific points properties.
314 this.points = points;
315 // Path doesn't do data
316 if(this.pointsToData){
317 this.data = this.pointsToData();
318 }
319 },
320
321 onDelete: function(/* Stencil */ stencil){
322 // summary:
323 // Stub - fires before this is destroyed
324 console.info("onDelete", this.id);
325 },
326
327 onBeforeRender: function(/*Object*/ stencil){
328 // summary:
329 // Stub - Fires before render occurs.
330 },
331
332 onModify: function(/*Object*/stencil){
333 // summary:
334 // Stub - fires on change of any property,
335 // including style properties
336
337 },
338
339 onChangeData: function(/*Object*/ stencil){
340 // summary:
341 // Stub - fires on change of dimensional
342 // properties or a text change
343 },
344
345 onChangeText: function(value){ // value or 'this' ?
346 // summary:
347 // Stub - fires on change of text in a
348 // TextBlock tool only
349 },
350
351 onRender: function(/*Object*/ stencil){
352 // summary:
353 // Stub - Fires on creation.
354 // Drawing connects to this (once!) to be
355 // notified of drag completion. But only if it
356 // was registered as a Tool. Creating Stencil in and of
357 // itself does not register it.
358 //
359 // This should fire
360 // at the *end* of creation (not during drag)
361
362 // FIXME:
363 // This should probably be onCreate. It should
364 // only fire once. But the mechanism for determining
365 // this is more complicated than it sounds.
366
367 this._postRenderCon = dojo.connect(this, "render", this, "_onPostRender");
368 this.created = true;
369 this.disconnectMouse();
370
371 // for Silverlight
372 if(this.shape){
373 this.shape.superClass = this;
374 }else{
375 this.container.superClass = this;
376 }
377 this._setNodeAtts(this);
378 //console.warn("ONRENDER", this.id, this)
379 },
380
381 onChangeStyle: function(/*Object*/stencil){
382 // summary:
383 // Fires when styles of shape has changed
384
385 this._isBeingModified = true; // need this to prevent onRender
386 if(!this.enabled){
387 this.style.current = this.style.disabled;
388 this.style.currentText = this.style.textDisabled;
389 this.style.currentHit = this.style.hitNorm;
390
391 }else{
392 this.style.current = this.style.norm;
393 this.style.currentHit = this.style.hitNorm;
394 this.style.currentText = this.style.text;
395 }
396
397 if(this.selected){
398 if(this.useSelectedStyle){
399 this.style.current = this.style.selected;
400 this.style.currentText = this.textSelected;
401 }
402 this.style.currentHit = this.style.hitSelected;
403
404 }else if(this.highlighted){
405 //this.style.current = this.style.highlighted;
406 this.style.currentHit = this.style.hitHighlighted;
407 //this.style.currentText = this.style.textHighlighted;
408 }
409
410 // NOTE: Can't just change props like setStroke
411 // because Silverlight throws error
412 this.render();
413 },
414
415 animate: function(options, create){
416 console.warn("ANIMATE..........................")
417 var d = options.d || options.duration || 1000;
418 var ms = options.ms || 20;
419 var ease = options.ease || easing.linear;
420 var steps = options.steps;
421 var ts = new Date().getTime();
422 var w = 100;
423 var cnt = 0;
424 var isArray = true;
425 var sp, ep;
426
427 if(dojo.isArray(options.start)){
428 sp = options.start;
429 ep = options.end;
430
431 }else if(dojo.isObject(options.start)){
432 sp = options.start;
433 ep = options.end;
434 isArray = false;
435 }else{
436
437 console.warn("No data provided to animate")
438 }
439
440 var v = setInterval(dojo.hitch(this, function(){
441 var t = new Date().getTime() - ts;
442 var p = ease(1-t/d);
443 if(t > d || cnt++ > 100){
444 clearInterval(v);
445 return;
446 }
447
448 if(isArray){
449 var pnts = [];
450 dojo.forEach(sp, function(pt, i){
451
452 var o = {
453 x: (ep[i].x-sp[i].x)*p + sp[i].x,
454 y: (ep[i].y-sp[i].y)*p + sp[i].y
455 };
456 pnts.push(o);
457 });
458 this.setPoints(pnts);
459 this.render();
460
461 }else{
462
463 var o = {};
464 for(var nm in sp){
465 o[nm] = (ep[nm] - sp[nm]) * p + sp[nm];
466 }
467
468 this.attr(o);
469
470 }
471 //console.dir(pnts)
472
473
474 //this.attr("height", w);
475 ////console.log("W:", w)
476 //w += 5;
477
478 }), ms);
479 },
480
481 attr: function(/*String|Object*/ key, /*String|Number?*/value){
482 // summary:
483 // Changes properties in the style or disabled styles,
484 // depending on whether the object is enabled.
485 // Also can be used to change most position and size props.
486
487 // NOTE: JUST A SETTER!! TODO!
488
489 // WARNING:
490 // Not doing any Stencil-type checking here. Setting a height
491 // on a line or an angle on a rectangle will just not render.
492
493 // FIXME
494 // 'width' attr is used for line width. How to change the width of a stencil?
495 var n = this.enabled?this.style.norm:this.style.disabled;
496 var t = this.enabled?this.style.text:this.style.textDisabled;
497 var ts = this.textSelected || {},
498 o,
499 nm,
500 width,
501 styleWas = dojo.toJson(n),
502 textWas = dojo.toJson(t);
503
504 var coords = {
505 x:true,
506 y:true,
507 r:true,
508 height:true,
509 width:true,
510 radius:true,
511 angle:true
512 };
513 var propChange = false;
514 if(typeof(key)!="object"){
515 o = {};
516 o[key] = value;
517 }else{
518 // prevent changing actual data
519 o = dojo.clone(key);
520 }
521
522 if(o.width){
523 // using width for size,
524 // borderWidth should be used
525 // for line thickness
526 width = o.width;
527 delete o.width;
528 }
529
530 for(nm in o){
531 if(nm in n){ n[nm] = o[nm]; }
532 if(nm in t){ t[nm] = o[nm]; }
533 if(nm in ts){ ts[nm] = o[nm]; }
534
535 if(nm in coords){
536 coords[nm] = o[nm];
537 propChange = true;
538 if(nm == "radius" && o.angle===undefined){
539 o.angle = coords.angle = this.getAngle();
540 }else if(nm == "angle" && o.radius===undefined){
541 o.radius = coords.radius = this.getRadius();
542 }
543
544 }
545 if(nm == "text"){
546 this.setText(o.text);
547 }
548 if(nm == "label"){
549 this.setLabel(o.label);
550 }
551 }
552 if(o.borderWidth!==undefined){
553 n.width = o.borderWidth;
554 }
555
556 if(this.useSelectedStyle){
557 // using the original selected style copy as
558 // a reference map of what props to copy
559 for(nm in this.style.norm){
560 if(this.selCopy[nm]===undefined){
561 this.style.selected[nm] = this.style.norm[nm];
562 }
563 }
564 this.textSelected.color = this.style.selected.color;
565
566 }
567
568 if(!this.created){
569 return;
570 }
571
572 // basic transform
573 if(o.x!==undefined || o.y!==undefined){
574 var box = this.getBounds(true);
575 var mx = { dx:0, dy:0 };
576 for(nm in o){
577 if(nm=="x" || nm =="y" || nm =="r"){
578 mx["d"+nm] = o[nm] - box[nm];
579 }
580 }
581 this.transformPoints(mx);
582 }
583
584
585 var p = this.points;
586 if(o.angle!==undefined){
587 this.dataToPoints({
588 x:this.data.x1,
589 y:this.data.y1,
590 angle:o.angle,
591 radius:o.radius
592 });
593
594 } else if(width!==undefined){
595 p[1].x = p[2].x = p[0].x + width;
596 this.pointsToData(p);
597 }
598
599 if(o.height!==undefined && o.angle===undefined){
600 console.log("Doing P2D-2");
601 p[2].y = p[3].y = p[0].y + o.height;
602 this.pointsToData(p);
603 }
604
605 if(o.r!==undefined){
606 this.data.r = Math.max(0, o.r);
607 }
608
609 //console.dir(this.data);
610 if(propChange || textWas!=dojo.toJson(t) || styleWas != dojo.toJson(n)){
611 // to trigger the render
612 // other events will be called post render
613 this.onChangeStyle(this);
614 }
615 o.width = width;
616
617 if(o.cosphi!=undefined){
618 !this.data? this.data = {cosphi:o.cosphi} : this.data.cosphi = o.cosphi;
619 this.style.zAxis = o.cosphi!=0 ? true : false;
620 }
621 },
622
623 exporter: function(){
624 // summary:
625 // Exports Stencil data
626
627 var type = this.type.substring(this.type.lastIndexOf(".")+1).charAt(0).toLowerCase()
628 + this.type.substring(this.type.lastIndexOf(".")+2);
629 var o = dojo.clone(this.style.norm);
630 o.borderWidth = o.width;
631 delete o.width;
632 if(type=="path"){
633 o.points = this.points;
634 }else{
635 o = dojo.mixin(o, this.data);
636 }
637 o.type = type;
638 if(this.isText){
639 o.text = this.getText();
640 o = dojo.mixin(o, this.style.text);
641 delete o.minWidth;
642 delete o.deleteEmptyCreate;
643 delete o.deleteEmptyModify;
644 }
645 var lbl = this.getLabel();
646 if(lbl){
647 o.label = lbl;
648 }
649 return o;
650 },
651
652
653 // TODO:
654 // Makes these all called by att()
655 // Should points and data be?
656
657 disable: function(){
658 // summary:
659 // Disables Stencil so it is not selectable.
660 // Changes the color to the disabled style.
661 this.enabled = false;
662 this.renderHit = false;
663 this.onChangeStyle(this);
664 },
665
666 enable: function(){
667 // summary:
668 // Enables Stencil so it is not selectable (if
669 // it was selectable to begin with). Changes the
670 // color to the current style.
671 this.enabled = true;
672 this.renderHit = true;
673 this.onChangeStyle(this);
674 },
675
676 select: function(){
677 // summary:
678 // Called when the Stencil is selected.
679 // NOTE: Calling this will not select the Stencil
680 // calling this just sets the style to the 'selected'
681 // theme. 'manager.Stencil' should be used for selecting
682 // Stencils.
683
684 this.selected = true;
685 this.onChangeStyle(this);
686 },
687
688 deselect: function(/*Boolean*/useDelay){
689 // summary:
690 // Called when the Stencil is deselected.
691 // NOTE: Calling this will not deselect the Stencil
692 // calling this just sets the style to the current
693 // theme. 'manager.Stencil' should be used for selecting
694 // and deselecting Stencils.
695 // useDelay: Boolean
696 // Adds slight delay before the style is set.
697
698 // should not have to render here because the deselection
699 // re-renders after the transform
700 // but... oh well.
701 if(useDelay){
702 setTimeout(dojo.hitch(this, function(){
703 this.selected = false;
704 this.onChangeStyle(this);
705 }),200);
706 }else{
707 this.selected = false;
708 this.onChangeStyle(this);
709 }
710 },
711 _toggleSelected: function(){
712 if(!this.selected){ return; }
713 this.deselect();
714 setTimeout(dojo.hitch(this, "select"), 0);
715 },
716
717 highlight: function(){
718 // summary:
719 // Changes style to the highlight theme.
720 this.highlighted = true;
721 this.onChangeStyle(this);
722 },
723
724 unhighlight: function(){
725 // summary:
726 // Changes style to the current theme.
727 this.highlighted = false;
728 this.onChangeStyle(this);
729 },
730
731 moveToFront: function(){
732 // summary:
733 // Moves Stencil to the front of all other items
734 // on the canvas.
735 this.container && this.container.moveToFront();
736 },
737
738 moveToBack: function(){
739 // summary:
740 // Moves Stencil to the back of all other items
741 // on the canvas.
742 this.container && this.container.moveToBack();
743 },
744
745 onTransformBegin: function(/* ? manager.Anchor */anchor){
746 // summary:
747 // Fired at the start of a transform. This would be
748 // an anchor drag or a selection.
749
750 this._isBeingModified = true;
751 },
752
753 onTransformEnd: function(/* manager.Anchor */anchor){
754 // summary:
755 // Called from anchor point up mouse up
756 this._isBeingModified = false;
757 this.onModify(this);
758 },
759
760 onTransform: function(/* ? manager.Anchor */anchor){
761 // summary:
762 // Called from anchor point mouse drag
763 // also called from plugins.Pan.checkBounds
764 if(!this._isBeingModified){
765 this.onTransformBegin();
766 }
767 // this is not needed for anchor moves, but it
768 // is for stencil move:
769 this.setPoints(this.points);
770 this.render();
771 },
772
773 transformPoints: function(mx){
774 // summary:
775 // Moves object to a new X Y location
776 // mx is additive. So mx.dx=1 will move the stencil
777 // 1 pixel to the right from wherever it was.
778
779 // An attempt is made to prevent < 0 errors, but
780 // this won't work on all shapes (like Axes)
781 if(!mx.dx && !mx.dy){
782 // no change
783 return;
784 }
785 var backup = dojo.clone(this.points), abort = false;
786 dojo.forEach(this.points, function(o){
787 o.x += mx.dx;
788 o.y += mx.dy;
789 if(o.x<this.marginZero || o.y<this.marginZero){
790 abort = true;
791 }
792 });
793 if(abort){
794 this.points = backup;
795 console.error("Attempt to set object '"+this.id+"' to less than zero.");
796 return;
797 }
798 this.onTransform();
799 this.onTransformEnd();
800 },
801
802 applyTransform: function(mx){
803 // summary:
804 // Applies the transform to the stencil
805 //
806 // NOTE: PARTIALLY IMPLEMENTED. Only applies x y coords.
807 this.transformPoints(mx);
808 },
809
810 setTransform: function(/*Object*/mx){
811 // summary:
812 // Sets the transform to the stencil
813 //
814 // NOTE: PARTIALLY IMPLEMENTED. Only applies x y coords.
815 this.attr({
816 x:mx.dx,
817 y:mx.dy
818 });
819 },
820
821 getTransform: function(){
822 // summary:
823 // Returns the current transform (position) of the Stencil's
824 // container
825 return this.selected ? this.container.getParent().getTransform() : {dx:0, dy:0}; // Object
826 },
827
828 addShadow: function(/*Object*/args){
829 args = args===true ? {} : args;
830 args.stencil = this;
831 this.shadow = new BoxShadow(args);
832 },
833
834 removeShadow: function(){
835 this.shadow.destroy();
836 },
837
838 setLabel: function(/*String*/text){
839 // summary:
840 // Creates and sets a label annotation for the Stencil.
841 // If Stencil contains a labelPosition method, that will
842 // be used for positioning. Otherwise
843 // dojox.drawing.util.positioning.label is used.
844 // text: String
845 // The text to set as the label.
846 if(!this._label){
847 this._label = new Label.Label({
848 text:text,
849 util:this.util,
850 mouse:this.mouse,
851 stencil:this,
852 annotation:true,
853 container:this.container,
854 labelPosition:this.labelPosition
855 });
856 }else if(text!=undefined){
857 this._label.setLabel(text);
858 }
859 },
860
861 getLabel: function(){
862 // summary:
863 // Get the text of the label.
864 if(this._label){
865 return this._label.getText(); // String
866 }
867 return null; //null
868 },
869
870 getAngle: function(){
871 // summary:
872 // Gets angle of Stencil
873 // NOTE: Only works for Lines, Arrows, Vectors and Axes
874 // (works on points, not transforms)
875 var d = this.pointsToData();
876 var obj = {
877 start:{
878 x:d.x1,
879 y:d.y1
880 },
881 x:d.x2,
882 y:d.y2
883 };
884 var angle = this.util.angle(obj, this.angleSnap);
885 // converting the angle for display: -180 -> 180, -90 -> 270
886 angle<0 ? angle = 360 + angle : angle;
887 return angle;
888 },
889 getRadius: function(){
890 // summary:
891 // Gets radius (length) of Stencil.
892 //
893 // NOTE: Only works for Lines, Arrows and Vectors
894 // (not for Ellipse, Axes has its own version)
895 var box = this.getBounds(true);
896 var line = {start:{x:box.x1, y:box.y1}, x:box.x2, y:box.y2};
897 return this.util.length(line);
898 },
899 getBounds: function(/* ? Boolean*/absolute){
900 // summary:
901 // Returns the coordinates of the Stencil. This is often
902 // different than the data or the points.
903 //
904 // NOTE: Won't work for paths or annotations (labels, Axes, arrow tips)
905 // They should overwrite.
906 //
907 // NOTE: Primarily used for checking for if shape is off
908 // canvas. Therefore Lines could get flipped. Use absolute
909 // to prevent this.
910 // absolute: Boolean
911 // Keeps lines from flipping (see note).
912
913 var p = this.points, x1, y1, x2, y2;
914 if(p.length==2){
915 if(absolute){
916 x1 = p[0].x;
917 y1 = p[0].y;
918 x2 = p[1].x;
919 y2 = p[1].y
920 }else{
921 x1 = p[0].x < p[1].x ? p[0].x : p[1].x;
922 y1 = p[0].y < p[1].y ? p[0].y : p[1].y;
923 x2 = p[0].x < p[1].x ? p[1].x : p[0].x;
924 y2 = p[0].y < p[1].y ? p[1].y : p[0].y;
925 }
926 return {
927 x1:x1,
928 y1:y1,
929 x2:x2,
930 y2:y2,
931 x:x1,
932 y:y1,
933 w:x2-x1,
934 h:y2-y1
935 }; // Object
936 }else{
937 return {
938 x1:p[0].x,
939 y1:p[0].y,
940 x2:p[2].x,
941 y2:p[2].y,
942 x:p[0].x,
943 y:p[0].y,
944 w:p[2].x - p[0].x,
945 h:p[2].y - p[0].y
946 }; // Object
947 }
948 },
949
950
951 preventNegativePos: function(){
952 // summary:
953 // Internal. Prevent item from being drawn/rendered less
954 // than zero on the X or Y.
955
956 // if being modified anchors will prevent less than zero.
957 if(this._isBeingModified){ return; }
958 // FIXME: why is this sometimes empty?
959 if(!this.points || !this.points.length){ return; }
960
961 if(this.type=="dojox.drawing.tools.custom.Axes"){
962 // this scenario moves all points if < 0
963 var minY = this.marginZero, minX = this.marginZero;
964 dojo.forEach(this.points, function(p){ minY = Math.min(p.y, minY); });
965 dojo.forEach(this.points, function(p){ minX = Math.min(p.x, minX); });
966
967 if(minY<this.marginZero){
968 dojo.forEach(this.points, function(p, i){
969 p.y = p.y + (this.marginZero-minY)
970 }, this);
971 }
972 if(minX<this.marginZero){
973 dojo.forEach(this.points, function(p){
974 p.x += (this.marginZero-minX)
975 }, this);
976 }
977
978 }else{
979 // this scenario moves just the one point that is < 0
980 dojo.forEach(this.points, function(p){
981 p.x = p.x < 0 ? this.marginZero : p.x;
982 p.y = p.y < 0 ? this.marginZero : p.y;
983 });
984 }
985 this.setPoints(this.points);
986 },
987
988 _onPostRender: function(/*Object*/data){
989 // summary:
990 // Drag-create or programmatic create calls onRender
991 // and afterwards, _onPostRender is called and
992 // manages further events.
993
994 // TODO: can this be onModify? Is that more clear?
995 //
996 //console.info("...........post render.....");
997
998 if(this._isBeingModified){
999 this.onModify(this);
1000 this._isBeingModified = false;
1001 }else if(!this.created){
1002 //this.onCreate(this);
1003 //this.onRender(this);
1004 }
1005
1006 if(!this.editMode && !this.selected && this._prevData && dojo.toJson(this._prevData) != dojo.toJson(this.data)){
1007 //console.info("_Base data changed ----> : this.editMode:", this.editMode)
1008 this.onChangeData(this);
1009 this._prevData = dojo.clone(this.data);
1010
1011 }else if(!this._prevData && (!this.isText || this.getText())){
1012 //console.info("_Base no prevData..........................");
1013 this._prevData = dojo.clone(this.data);
1014
1015 }
1016
1017 },
1018
1019 _setNodeAtts: function(shape){
1020 // summary:
1021 // Internal. Sets the rawNode attribute. (Or in Silverlight
1022 // an "object attribute". "stencil" is
1023 // used by the application to determine if
1024 // something is selectable or not. This also
1025 // sets the mouse custom events like:
1026 // "onStencilUp". To disable the selectability,
1027 // make the att "", which causes a standard
1028 // mouse event.
1029 // Labels are special and used to select master stencils.
1030 var att = this.enabled && (!this.annotation || this.drawingType=="label") ? this.drawingType : "";
1031 this.util.attr(shape, "drawingType", att);
1032 },
1033
1034
1035 destroy: function(){
1036 // summary:
1037 // Destroys this Stencil
1038
1039 // Note:
1040 // Can connect to this, but it's better to
1041 // connect to onDelete
1042
1043 // prevent loops:
1044 if(this.destroyed){ return; }
1045 if(this.data || this.points && this.points.length){
1046 this.onDelete(this);
1047 }
1048
1049 this.disconnectMouse();
1050 this.disconnect(this._cons);
1051 dojo.disconnect(this._postRenderCon);
1052 this.remove(this.shape, this.hit);
1053 this.destroyed = true;
1054 },
1055
1056 remove: function(/*Shape...*/){
1057 // summary:
1058 // Removes shape(s), typically before a re-render
1059 // No args defaults to this.shape
1060 // Pass in multiple args to remove multiple shapes
1061
1062 // FIXME: Use an Array of all shapes
1063
1064 var a = arguments;
1065 if(!a.length){
1066 if(!this.shape){ return; }
1067 a = [this.shape];
1068 }
1069 for(var i=0;i<a.length;i++){
1070 if(a[i]){
1071 a[i].removeShape();
1072 }
1073 }
1074 },
1075
1076 connectMult: function(/*dojo.connect args */){
1077 // summary:
1078 // Convenience method for batches of quick connects
1079 // Handles are not returned and therefore cannot be
1080 // disconnected until Shape destroy time
1081
1082 if(arguments.length>1){
1083 // arguments are the connect params
1084 this._cons.push(this.connect.apply(this, arguments));
1085 }else if(dojo.isArray(arguments[0][0])){
1086 // an array of arrays of params
1087 dojo.forEach(arguments[0], function(ar){
1088 this._cons.push(this.connect.apply(this, ar));
1089 }, this);
1090 }else{
1091 //one array of params
1092 this._cons.push(this.connect.apply(this, arguments[0]));
1093 }
1094
1095 },
1096
1097 // TODO: connect to a Shape event from outside class
1098 connect: function(o, e, s, m, /* Boolean*/once){
1099 // summary:
1100 // Convenience method for quick connects
1101 // See comments below for possiblities
1102 // functions can be strings
1103 // once:
1104 // If true, the connection happens only
1105 // once then disconnects. Five args are required
1106 // for this functionality.
1107
1108 var c;
1109 if(typeof(o)!="object"){
1110 if(s){
1111 // ** function object function **
1112 m = s; s = e; e=o; o = this;
1113 }else{
1114 // ** function function **
1115 m = e; e = o; o = s = this;
1116 }
1117 }else if(!m){
1118 // ** object function function **
1119 m = s; s = this;
1120 }else if(once){
1121 // ** object function object function Boolean **
1122 c = dojo.connect(o, e, function(evt){
1123 dojo.hitch(s, m)(evt);
1124 dojo.disconnect(c);
1125 });
1126 this._cons.push(c);
1127 return c;
1128 }else{
1129 // ** object function object function **
1130 }
1131 c = dojo.connect(o, e, s, m);
1132 this._cons.push(c);
1133 return c;
1134 },
1135
1136 disconnect: function(/*Handle|Array*/handles){
1137 // summary:
1138 // Removes connections based on passed
1139 // handles arguments
1140 if(!handles){ return }
1141 if(!dojo.isArray(handles)){ handles=[handles]; }
1142 dojo.forEach(handles, dojo.disconnect, dojo);
1143 },
1144
1145 connectMouse: function(){
1146 // summary:
1147 // Internal. Registers this Stencil to receive
1148 // mouse events.
1149 this._mouseHandle = this.mouse.register(this);
1150 },
1151 disconnectMouse: function(){
1152 // summary:
1153 // Internal. Unregisters this Stencil from receiving
1154 // mouse events.
1155 this.mouse.unregister(this._mouseHandle);
1156 },
1157
1158 // Should be overwritten by sub class:
1159 render: function(){
1160 // summary:
1161 // This Stencil's render description. Often
1162 // calls 'sub render' methods.
1163 },
1164 //renderOutline: function(){},
1165 dataToPoints: function(/*Object*/data){
1166 // summary:
1167 // Converts data to points.
1168 },
1169 pointsToData: function(/*Array*/points){
1170 // summary:
1171 // Converts points to data
1172 },
1173 onDown: function(/*EventObject*/obj){
1174 // summary:
1175 // Mouse event, fired on mousedown on canvas
1176
1177 // by default, object is ready to accept data
1178 // turn this off for dragging or onRender will
1179 // keep firing and register the shape
1180 // NOTE: Not needed for all stencils. Axes needs it.
1181 this._downOnCanvas = true;
1182 dojo.disconnect(this._postRenderCon);
1183 this._postRenderCon = null;
1184 },
1185 onMove: function(/*EventObject*/obj){
1186 // summary:
1187 // Mouse event, fired on mousemove while mouse
1188 // is not down.
1189 // NOTE: Not currently implemented
1190 },
1191 onDrag: function(/*EventObject*/obj){
1192 // summary:
1193 // Mouse event, fired on mousemove while mouse
1194 // is down on canvas
1195 },
1196 onUp: function(/*EventObject*/obj){
1197 // summary:
1198 // Mouse event, fired on mouseup
1199 }
1200 }
1201 );
1202 dojo.setObject("dojox.drawing.stencil._Base", Base);
1203 return Base;
1204 });