Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / drawing / stencil / _Base.js

History | View | Annotate | Download (13.3 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dojox.drawing.stencil._Base"]){
9
dojo._hasResource["dojox.drawing.stencil._Base"]=true;
10
dojo.provide("dojox.drawing.stencil._Base");
11
dojo.require("dojo.fx.easing");
12
dojox.drawing.stencil._Base=dojox.drawing.util.oo.declare(function(_1){
13
dojo.mixin(this,_1);
14
this.style=_1.style||dojox.drawing.defaults.copy();
15
if(_1.stencil){
16
this.stencil=_1.stencil;
17
this.util=_1.stencil.util;
18
this.mouse=_1.stencil.mouse;
19
this.container=_1.stencil.container;
20
this.style=_1.stencil.style;
21
}
22
var _2=/Line|Vector|Axes|Arrow/;
23
var _3=/Text/;
24
this.shortType=this.util.abbr(this.type);
25
this.isText=_3.test(this.type);
26
this.isLine=_2.test(this.type);
27
this.renderHit=this.style.renderHitLayer;
28
if(!this.renderHit&&this.style.renderHitLines&&this.isLine){
29
this.renderHit=true;
30
}
31
if(!this.renderHit&&this.style.useSelectedStyle){
32
this.useSelectedStyle=true;
33
this.selCopy=dojo.clone(this.style.selected);
34
for(var nm in this.style.norm){
35
if(this.style.selected[nm]===undefined){
36
this.style.selected[nm]=this.style.norm[nm];
37
}
38
}
39
this.textSelected=dojo.clone(this.style.text);
40
this.textSelected.color=this.style.selected.fill;
41
}
42
this.angleSnap=this.style.angleSnap||1;
43
this.marginZero=_1.marginZero||this.style.anchors.marginZero;
44
this.id=_1.id||this.util.uid(this.type);
45
this._cons=[];
46
if(!this.annotation&&!this.subShape){
47
this.util.attr(this.container,"id",this.id);
48
}
49
this.connect(this,"onBeforeRender","preventNegativePos");
50
this._offX=this.mouse.origin.x;
51
this._offY=this.mouse.origin.y;
52
if(this.isText){
53
this.align=_1.align||this.align;
54
this.valign=_1.valign||this.valign;
55
if(_1.data&&_1.data.makeFit){
56
var _4=this.makeFit(_1.data.text,_1.data.width);
57
this.textSize=this.style.text.size=_4.size;
58
this._lineHeight=_4.box.h;
59
}else{
60
this.textSize=parseInt(this.style.text.size,10);
61
this._lineHeight=this.textSize*1.4;
62
}
63
this.deleteEmptyCreate=_1.deleteEmptyCreate!==undefined?_1.deleteEmptyCreate:this.style.text.deleteEmptyCreate;
64
this.deleteEmptyModify=_1.deleteEmptyModify!==undefined?_1.deleteEmptyModify:this.style.text.deleteEmptyModify;
65
}
66
this.attr(_1.data);
67
if(this.noBaseRender){
68
return;
69
}
70
if(_1.points){
71
if(_1.data&&_1.data.closePath===false){
72
this.closePath=false;
73
}
74
this.setPoints(_1.points);
75
this.connect(this,"render",this,"onRender",true);
76
this.baseRender&&this.enabled&&this.render();
77
_1.label&&this.setLabel(_1.label);
78
_1.shadow&&this.addShadow(_1.shadow);
79
}else{
80
if(_1.data){
81
_1.data.width=_1.data.width?_1.data.width:this.style.text.minWidth;
82
_1.data.height=_1.data.height?_1.data.height:this._lineHeight;
83
this.setData(_1.data);
84
this.connect(this,"render",this,"onRender",true);
85
this.baseRender&&this.enabled&&this.render(_1.data.text);
86
this.baseRender&&_1.label&&this.setLabel(_1.label);
87
this.baseRender&&_1.shadow&&this.addShadow(_1.shadow);
88
}else{
89
if(this.draws){
90
this.points=[];
91
this.data={};
92
this.connectMouse();
93
this._postRenderCon=dojo.connect(this,"render",this,"_onPostRender");
94
}
95
}
96
}
97
if(this.showAngle){
98
this.angleLabel=new dojox.drawing.annotations.Angle({stencil:this});
99
}
100
if(!this.enabled){
101
this.disable();
102
this.moveToBack();
103
this.render(_1.data.text);
104
}
105
},{type:"dojox.drawing.stencil",minimumSize:10,enabled:true,drawingType:"stencil",setData:function(_5){
106
this.data=_5;
107
this.points=this.dataToPoints();
108
},setPoints:function(_6){
109
this.points=_6;
110
if(this.pointsToData){
111
this.data=this.pointsToData();
112
}
113
},onDelete:function(_7){
114
},onBeforeRender:function(_8){
115
},onModify:function(_9){
116
},onChangeData:function(_a){
117
},onChangeText:function(_b){
118
},onRender:function(_c){
119
this._postRenderCon=dojo.connect(this,"render",this,"_onPostRender");
120
this.created=true;
121
this.disconnectMouse();
122
if(this.shape){
123
this.shape.superClass=this;
124
}else{
125
this.container.superClass=this;
126
}
127
this._setNodeAtts(this);
128
},onChangeStyle:function(_d){
129
this._isBeingModified=true;
130
if(!this.enabled){
131
this.style.current=this.style.disabled;
132
this.style.currentText=this.style.textDisabled;
133
this.style.currentHit=this.style.hitNorm;
134
}else{
135
this.style.current=this.style.norm;
136
this.style.currentHit=this.style.hitNorm;
137
this.style.currentText=this.style.text;
138
}
139
if(this.selected){
140
if(this.useSelectedStyle){
141
this.style.current=this.style.selected;
142
this.style.currentText=this.textSelected;
143
}
144
this.style.currentHit=this.style.hitSelected;
145
}else{
146
if(this.highlighted){
147
this.style.currentHit=this.style.hitHighlighted;
148
}
149
}
150
this.render();
151
},animate:function(_e,_f){
152
console.warn("ANIMATE..........................");
153
var d=_e.d||_e.duration||1000;
154
var ms=_e.ms||20;
155
var _10=_e.ease||dojo.fx.easing.linear;
156
var _11=_e.steps;
157
var ts=new Date().getTime();
158
var w=100;
159
var cnt=0;
160
var _12=true;
161
var sp,ep;
162
if(dojo.isArray(_e.start)){
163
sp=_e.start;
164
ep=_e.end;
165
}else{
166
if(dojo.isObject(_e.start)){
167
sp=_e.start;
168
ep=_e.end;
169
_12=false;
170
}else{
171
console.warn("No data provided to animate");
172
}
173
}
174
var v=setInterval(dojo.hitch(this,function(){
175
var t=new Date().getTime()-ts;
176
var p=_10(1-t/d);
177
if(t>d||cnt++>100){
178
clearInterval(v);
179
return;
180
}
181
if(_12){
182
var _13=[];
183
dojo.forEach(sp,function(pt,i){
184
var o={x:(ep[i].x-sp[i].x)*p+sp[i].x,y:(ep[i].y-sp[i].y)*p+sp[i].y};
185
_13.push(o);
186
});
187
this.setPoints(_13);
188
this.render();
189
}else{
190
var o={};
191
for(var nm in sp){
192
o[nm]=(ep[nm]-sp[nm])*p+sp[nm];
193
}
194
this.attr(o);
195
}
196
}),ms);
197
},attr:function(key,_14){
198
var n=this.enabled?this.style.norm:this.style.disabled;
199
var t=this.enabled?this.style.text:this.style.textDisabled;
200
var ts=this.textSelected||{},o,nm,_15,_16=dojo.toJson(n),_17=dojo.toJson(t);
201
var _18={x:true,y:true,r:true,height:true,width:true,radius:true,angle:true};
202
var _19=false;
203
if(typeof (key)!="object"){
204
o={};
205
o[key]=_14;
206
}else{
207
o=dojo.clone(key);
208
}
209
if(o.width){
210
_15=o.width;
211
delete o.width;
212
}
213
for(nm in o){
214
if(nm in n){
215
n[nm]=o[nm];
216
}
217
if(nm in t){
218
t[nm]=o[nm];
219
}
220
if(nm in ts){
221
ts[nm]=o[nm];
222
}
223
if(nm in _18){
224
_18[nm]=o[nm];
225
_19=true;
226
if(nm=="radius"&&o.angle===undefined){
227
o.angle=_18.angle=this.getAngle();
228
}else{
229
if(nm=="angle"&&o.radius===undefined){
230
o.radius=_18.radius=this.getRadius();
231
}
232
}
233
}
234
if(nm=="text"){
235
this.setText(o.text);
236
}
237
if(nm=="label"){
238
this.setLabel(o.label);
239
}
240
}
241
if(o.borderWidth!==undefined){
242
n.width=o.borderWidth;
243
}
244
if(this.useSelectedStyle){
245
for(nm in this.style.norm){
246
if(this.selCopy[nm]===undefined){
247
this.style.selected[nm]=this.style.norm[nm];
248
}
249
}
250
this.textSelected.color=this.style.selected.color;
251
}
252
if(!this.created){
253
return;
254
}
255
if(o.x!==undefined||o.y!==undefined){
256
var box=this.getBounds(true);
257
var mx={dx:0,dy:0};
258
for(nm in o){
259
if(nm=="x"||nm=="y"||nm=="r"){
260
mx["d"+nm]=o[nm]-box[nm];
261
}
262
}
263
this.transformPoints(mx);
264
}
265
var p=this.points;
266
if(o.angle!==undefined){
267
this.dataToPoints({x:this.data.x1,y:this.data.y1,angle:o.angle,radius:o.radius});
268
}else{
269
if(_15!==undefined){
270
p[1].x=p[2].x=p[0].x+_15;
271
this.pointsToData(p);
272
}
273
}
274
if(o.height!==undefined&&o.angle===undefined){
275
p[2].y=p[3].y=p[0].y+o.height;
276
this.pointsToData(p);
277
}
278
if(o.r!==undefined){
279
this.data.r=Math.max(0,o.r);
280
}
281
if(_19||_17!=dojo.toJson(t)||_16!=dojo.toJson(n)){
282
this.onChangeStyle(this);
283
}
284
o.width=_15;
285
if(o.cosphi!=undefined){
286
!this.data?this.data={cosphi:o.cosphi}:this.data.cosphi=o.cosphi;
287
this.style.zAxis=o.cosphi!=0?true:false;
288
}
289
},exporter:function(){
290
var _1a=this.type.substring(this.type.lastIndexOf(".")+1).charAt(0).toLowerCase()+this.type.substring(this.type.lastIndexOf(".")+2);
291
var o=dojo.clone(this.style.norm);
292
o.borderWidth=o.width;
293
delete o.width;
294
if(_1a=="path"){
295
o.points=this.points;
296
}else{
297
o=dojo.mixin(o,this.data);
298
}
299
o.type=_1a;
300
if(this.isText){
301
o.text=this.getText();
302
o=dojo.mixin(o,this.style.text);
303
delete o.minWidth;
304
delete o.deleteEmptyCreate;
305
delete o.deleteEmptyModify;
306
}
307
var lbl=this.getLabel();
308
if(lbl){
309
o.label=lbl;
310
}
311
return o;
312
},disable:function(){
313
this.enabled=false;
314
this.renderHit=false;
315
this.onChangeStyle(this);
316
},enable:function(){
317
this.enabled=true;
318
this.renderHit=true;
319
this.onChangeStyle(this);
320
},select:function(){
321
this.selected=true;
322
this.onChangeStyle(this);
323
},deselect:function(_1b){
324
if(_1b){
325
setTimeout(dojo.hitch(this,function(){
326
this.selected=false;
327
this.onChangeStyle(this);
328
}),200);
329
}else{
330
this.selected=false;
331
this.onChangeStyle(this);
332
}
333
},_toggleSelected:function(){
334
if(!this.selected){
335
return;
336
}
337
this.deselect();
338
setTimeout(dojo.hitch(this,"select"),0);
339
},highlight:function(){
340
this.highlighted=true;
341
this.onChangeStyle(this);
342
},unhighlight:function(){
343
this.highlighted=false;
344
this.onChangeStyle(this);
345
},moveToFront:function(){
346
this.container&&this.container.moveToFront();
347
},moveToBack:function(){
348
this.container&&this.container.moveToBack();
349
},onTransformBegin:function(_1c){
350
this._isBeingModified=true;
351
},onTransformEnd:function(_1d){
352
this._isBeingModified=false;
353
this.onModify(this);
354
},onTransform:function(_1e){
355
if(!this._isBeingModified){
356
this.onTransformBegin();
357
}
358
this.setPoints(this.points);
359
this.render();
360
},transformPoints:function(mx){
361
if(!mx.dx&&!mx.dy){
362
return;
363
}
364
var _1f=dojo.clone(this.points),_20=false;
365
dojo.forEach(this.points,function(o){
366
o.x+=mx.dx;
367
o.y+=mx.dy;
368
if(o.x<this.marginZero||o.y<this.marginZero){
369
_20=true;
370
}
371
});
372
if(_20){
373
this.points=_1f;
374
console.error("Attempt to set object '"+this.id+"' to less than zero.");
375
return;
376
}
377
this.onTransform();
378
this.onTransformEnd();
379
},applyTransform:function(mx){
380
this.transformPoints(mx);
381
},setTransform:function(mx){
382
this.attr({x:mx.dx,y:mx.dy});
383
},getTransform:function(){
384
return this.selected?this.container.getParent().getTransform():{dx:0,dy:0};
385
},addShadow:function(_21){
386
_21=_21===true?{}:_21;
387
_21.stencil=this;
388
this.shadow=new dojox.drawing.annotations.BoxShadow(_21);
389
},removeShadow:function(){
390
this.shadow.destroy();
391
},setLabel:function(_22){
392
if(!this._label){
393
this._label=new dojox.drawing.annotations.Label({text:_22,util:this.util,mouse:this.mouse,stencil:this,annotation:true,container:this.container,labelPosition:this.labelPosition});
394
}else{
395
if(_22){
396
this._label.setLabel(_22);
397
}
398
}
399
},getLabel:function(){
400
if(this._label){
401
return this._label.getText();
402
}
403
return null;
404
},getAngle:function(){
405
var d=this.pointsToData();
406
var obj={start:{x:d.x1,y:d.y1},x:d.x2,y:d.y2};
407
var _23=this.util.angle(obj,this.angleSnap);
408
_23<0?_23=360+_23:_23;
409
return _23;
410
},getRadius:function(){
411
var box=this.getBounds(true);
412
var _24={start:{x:box.x1,y:box.y1},x:box.x2,y:box.y2};
413
return this.util.length(_24);
414
},getBounds:function(_25){
415
var p=this.points,x1,y1,x2,y2;
416
if(p.length==2){
417
if(_25){
418
x1=p[0].x;
419
y1=p[0].y;
420
x2=p[1].x;
421
y2=p[1].y;
422
}else{
423
x1=p[0].x<p[1].x?p[0].x:p[1].x;
424
y1=p[0].y<p[1].y?p[0].y:p[1].y;
425
x2=p[0].x<p[1].x?p[1].x:p[0].x;
426
y2=p[0].y<p[1].y?p[1].y:p[0].y;
427
}
428
return {x1:x1,y1:y1,x2:x2,y2:y2,x:x1,y:y1,w:x2-x1,h:y2-y1};
429
}else{
430
return {x1:p[0].x,y1:p[0].y,x2:p[2].x,y2:p[2].y,x:p[0].x,y:p[0].y,w:p[2].x-p[0].x,h:p[2].y-p[0].y};
431
}
432
},preventNegativePos:function(){
433
if(this._isBeingModified){
434
return;
435
}
436
if(!this.points||!this.points.length){
437
return;
438
}
439
if(this.type=="dojox.drawing.tools.custom.Axes"){
440
var _26=this.marginZero,_27=this.marginZero;
441
dojo.forEach(this.points,function(p){
442
_26=Math.min(p.y,_26);
443
});
444
dojo.forEach(this.points,function(p){
445
_27=Math.min(p.x,_27);
446
});
447
if(_26<this.marginZero){
448
dojo.forEach(this.points,function(p,i){
449
p.y=p.y+(this.marginZero-_26);
450
},this);
451
}
452
if(_27<this.marginZero){
453
dojo.forEach(this.points,function(p){
454
p.x+=(this.marginZero-_27);
455
},this);
456
}
457
}else{
458
dojo.forEach(this.points,function(p){
459
p.x=p.x<0?this.marginZero:p.x;
460
p.y=p.y<0?this.marginZero:p.y;
461
});
462
}
463
this.setPoints(this.points);
464
},_onPostRender:function(_28){
465
if(this._isBeingModified){
466
this.onModify(this);
467
this._isBeingModified=false;
468
}else{
469
if(!this.created){
470
}
471
}
472
if(!this.editMode&&!this.selected&&this._prevData&&dojo.toJson(this._prevData)!=dojo.toJson(this.data)){
473
this.onChangeData(this);
474
this._prevData=dojo.clone(this.data);
475
}else{
476
if(!this._prevData&&(!this.isText||this.getText())){
477
this._prevData=dojo.clone(this.data);
478
}
479
}
480
},_setNodeAtts:function(_29){
481
var att=this.enabled&&!this.annotation?this.drawingType:"";
482
this.util.attr(_29,"drawingType",att);
483
},destroy:function(){
484
if(this.destroyed){
485
return;
486
}
487
if(this.data||this.points&&this.points.length){
488
this.onDelete(this);
489
}
490
this.disconnectMouse();
491
this.disconnect(this._cons);
492
dojo.disconnect(this._postRenderCon);
493
this.remove(this.shape,this.hit);
494
this.destroyed=true;
495
},remove:function(){
496
var a=arguments;
497
if(!a.length){
498
if(!this.shape){
499
return;
500
}
501
a=[this.shape];
502
}
503
for(var i=0;i<a.length;i++){
504
if(a[i]){
505
a[i].removeShape();
506
}
507
}
508
},connectMult:function(){
509
if(arguments.length>1){
510
this._cons.push(this.connect.apply(this,arguments));
511
}else{
512
if(dojo.isArray(arguments[0][0])){
513
dojo.forEach(arguments[0],function(ar){
514
this._cons.push(this.connect.apply(this,ar));
515
},this);
516
}else{
517
this._cons.push(this.connect.apply(this,arguments[0]));
518
}
519
}
520
},connect:function(o,e,s,m,_2a){
521
var c;
522
if(typeof (o)!="object"){
523
if(s){
524
m=s;
525
s=e;
526
e=o;
527
o=this;
528
}else{
529
m=e;
530
e=o;
531
o=s=this;
532
}
533
}else{
534
if(!m){
535
m=s;
536
s=this;
537
}else{
538
if(_2a){
539
c=dojo.connect(o,e,function(evt){
540
dojo.hitch(s,m)(evt);
541
dojo.disconnect(c);
542
});
543
this._cons.push(c);
544
return c;
545
}else{
546
}
547
}
548
}
549
c=dojo.connect(o,e,s,m);
550
this._cons.push(c);
551
return c;
552
},disconnect:function(_2b){
553
if(!_2b){
554
return;
555
}
556
if(!dojo.isArray(_2b)){
557
_2b=[_2b];
558
}
559
dojo.forEach(_2b,dojo.disconnect,dojo);
560
},connectMouse:function(){
561
this._mouseHandle=this.mouse.register(this);
562
},disconnectMouse:function(){
563
this.mouse.unregister(this._mouseHandle);
564
},render:function(){
565
},dataToPoints:function(_2c){
566
},pointsToData:function(_2d){
567
},onDown:function(obj){
568
this._downOnCanvas=true;
569
dojo.disconnect(this._postRenderCon);
570
this._postRenderCon=null;
571
},onMove:function(obj){
572
},onDrag:function(obj){
573
},onUp:function(obj){
574
}});
575
}