root / trunk / web / dojo / dojox / drawing / manager / Stencil.js @ 13
History | View | Annotate | Download (7.22 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.manager.Stencil"]){ | ||
| 9 | dojo._hasResource["dojox.drawing.manager.Stencil"]=true; | ||
| 10 | dojo.provide("dojox.drawing.manager.Stencil");
 | ||
| 11 | (function(){
 | ||
| 12 | var _1,_2;
 | ||
| 13 | dojox.drawing.manager.Stencil=dojox.drawing.util.oo.declare(function(_3){
 | ||
| 14 | _1=_3.surface; | ||
| 15 | this.canvas=_3.canvas;
 | ||
| 16 | this.defaults=dojox.drawing.defaults.copy();
 | ||
| 17 | this.undo=_3.undo;
 | ||
| 18 | this.mouse=_3.mouse;
 | ||
| 19 | this.keys=_3.keys;
 | ||
| 20 | this.anchors=_3.anchors;
 | ||
| 21 | this.stencils={};
 | ||
| 22 | this.selectedStencils={};
 | ||
| 23 | this._mouseHandle=this.mouse.register(this); | ||
| 24 | dojo.connect(this.keys,"onArrow",this,"onArrow"); | ||
| 25 | dojo.connect(this.keys,"onEsc",this,"deselect"); | ||
| 26 | dojo.connect(this.keys,"onDelete",this,"onDelete"); | ||
| 27 | },{_dragBegun:false,_wasDragged:false,_secondClick:false,_isBusy:false,register:function(_4){
 | ||
| 28 | if(_4.isText&&!_4.editMode&&_4.deleteEmptyCreate&&!_4.getText()){
 | ||
| 29 | console.warn("EMPTY CREATE DELETE",_4);
 | ||
| 30 | _4.destroy(); | ||
| 31 | return false; | ||
| 32 | } | ||
| 33 | this.stencils[_4.id]=_4;
 | ||
| 34 | if(_4.execText){
 | ||
| 35 | if(_4._text&&!_4.editMode){
 | ||
| 36 | this.selectItem(_4);
 | ||
| 37 | } | ||
| 38 | _4.connect("execText",this,function(){ | ||
| 39 | if(_4.isText&&_4.deleteEmptyModify&&!_4.getText()){
 | ||
| 40 | console.warn("EMPTY MOD DELETE",_4);
 | ||
| 41 | this.deleteItem(_4);
 | ||
| 42 | }else{
 | ||
| 43 | if(_4.selectOnExec){
 | ||
| 44 | this.selectItem(_4);
 | ||
| 45 | } | ||
| 46 | } | ||
| 47 | }); | ||
| 48 | } | ||
| 49 | _4.connect("deselect",this,function(){ | ||
| 50 | if(!this._isBusy&&this.isSelected(_4)){ | ||
| 51 | this.deselectItem(_4);
 | ||
| 52 | } | ||
| 53 | }); | ||
| 54 | _4.connect("select",this,function(){ | ||
| 55 | if(!this._isBusy&&!this.isSelected(_4)){ | ||
| 56 | this.selectItem(_4);
 | ||
| 57 | } | ||
| 58 | }); | ||
| 59 | return _4;
 | ||
| 60 | },unregister:function(_5){ | ||
| 61 | if(_5){
 | ||
| 62 | _5.selected&&this.onDeselect(_5);
 | ||
| 63 | delete this.stencils[_5.id]; | ||
| 64 | } | ||
| 65 | },onArrow:function(_6){ | ||
| 66 | if(this.hasSelected()){ | ||
| 67 | this.saveThrottledState();
 | ||
| 68 | this.group.applyTransform({dx:_6.x,dy:_6.y}); | ||
| 69 | } | ||
| 70 | },_throttleVrl:null,_throttle:false,throttleTime:400,_lastmxx:-1,_lastmxy:-1,saveMoveState:function(){ | ||
| 71 | var mx=this.group.getTransform(); | ||
| 72 | if(mx.dx==this._lastmxx&&mx.dy==this._lastmxy){ | ||
| 73 | return;
 | ||
| 74 | } | ||
| 75 | this._lastmxx=mx.dx;
 | ||
| 76 | this._lastmxy=mx.dy;
 | ||
| 77 | this.undo.add({before:dojo.hitch(this.group,"setTransform",mx)}); | ||
| 78 | },saveThrottledState:function(){ | ||
| 79 | clearTimeout(this._throttleVrl);
 | ||
| 80 | clearInterval(this._throttleVrl);
 | ||
| 81 | this._throttleVrl=setTimeout(dojo.hitch(this,function(){ | ||
| 82 | this._throttle=false; | ||
| 83 | this.saveMoveState();
 | ||
| 84 | }),this.throttleTime);
 | ||
| 85 | if(this._throttle){ | ||
| 86 | return;
 | ||
| 87 | } | ||
| 88 | this._throttle=true; | ||
| 89 | this.saveMoveState();
 | ||
| 90 | },unDelete:function(_7){ | ||
| 91 | for(var s in _7){ | ||
| 92 | _7[s].render(); | ||
| 93 | this.onSelect(_7[s]);
 | ||
| 94 | } | ||
| 95 | },onDelete:function(_8){ | ||
| 96 | if(_8!==true){ | ||
| 97 | this.undo.add({before:dojo.hitch(this,"unDelete",this.selectedStencils),after:dojo.hitch(this,"onDelete",true)}); | ||
| 98 | } | ||
| 99 | this.withSelected(function(m){ | ||
| 100 | this.anchors.remove(m);
 | ||
| 101 | var id=m.id;
 | ||
| 102 | m.destroy(); | ||
| 103 | delete this.stencils[id]; | ||
| 104 | }); | ||
| 105 | this.selectedStencils={};
 | ||
| 106 | },deleteItem:function(_9){ | ||
| 107 | if(this.hasSelected()){ | ||
| 108 | var _a=[];
 | ||
| 109 | for(var m in this.selectedStencils){ | ||
| 110 | if(this.selectedStencils.id==_9.id){ | ||
| 111 | if(this.hasSelected()==1){ | ||
| 112 | this.onDelete();
 | ||
| 113 | return;
 | ||
| 114 | } | ||
| 115 | }else{
 | ||
| 116 | _a.push(this.selectedStencils.id);
 | ||
| 117 | } | ||
| 118 | } | ||
| 119 | this.deselect();
 | ||
| 120 | this.selectItem(_9);
 | ||
| 121 | this.onDelete();
 | ||
| 122 | dojo.forEach(_a,function(id){
 | ||
| 123 | this.selectItem(id);
 | ||
| 124 | },this);
 | ||
| 125 | }else{
 | ||
| 126 | this.selectItem(_9);
 | ||
| 127 | this.onDelete();
 | ||
| 128 | } | ||
| 129 | },removeAll:function(){ | ||
| 130 | this.selectAll();
 | ||
| 131 | this._isBusy=true; | ||
| 132 | this.onDelete();
 | ||
| 133 | this.stencils={};
 | ||
| 134 | this._isBusy=false; | ||
| 135 | },setSelectionGroup:function(){ | ||
| 136 | this.withSelected(function(m){ | ||
| 137 | this.onDeselect(m,true); | ||
| 138 | }); | ||
| 139 | if(this.group){ | ||
| 140 | _1.remove(this.group);
 | ||
| 141 | this.group.removeShape();
 | ||
| 142 | } | ||
| 143 | this.group=_1.createGroup();
 | ||
| 144 | this.group.setTransform({dx:0,dy:0}); | ||
| 145 | this.withSelected(function(m){ | ||
| 146 | this.group.add(m.container);
 | ||
| 147 | m.select(); | ||
| 148 | }); | ||
| 149 | },setConstraint:function(){ | ||
| 150 | var t=Infinity,l=Infinity; | ||
| 151 | this.withSelected(function(m){ | ||
| 152 | var o=m.getBounds();
 | ||
| 153 | t=Math.min(o.y1,t); | ||
| 154 | l=Math.min(o.x1,l); | ||
| 155 | }); | ||
| 156 | this.constrain={l:-l,t:-t}; | ||
| 157 | },onDeselect:function(_b,_c){ | ||
| 158 | if(!_c){
 | ||
| 159 | delete this.selectedStencils[_b.id]; | ||
| 160 | } | ||
| 161 | this.anchors.remove(_b);
 | ||
| 162 | _1.add(_b.container); | ||
| 163 | _b.selected&&_b.deselect(); | ||
| 164 | _b.applyTransform(this.group.getTransform());
 | ||
| 165 | },deselectItem:function(_d){ | ||
| 166 | this.onDeselect(_d);
 | ||
| 167 | },deselect:function(){ | ||
| 168 | this.withSelected(function(m){ | ||
| 169 | this.onDeselect(m);
 | ||
| 170 | }); | ||
| 171 | this._dragBegun=false; | ||
| 172 | this._wasDragged=false; | ||
| 173 | },onSelect:function(_e){ | ||
| 174 | if(!_e){
 | ||
| 175 | console.error("null stencil is not selected:",this.stencils); | ||
| 176 | } | ||
| 177 | if(this.selectedStencils[_e.id]){ | ||
| 178 | return;
 | ||
| 179 | } | ||
| 180 | this.selectedStencils[_e.id]=_e;
 | ||
| 181 | this.group.add(_e.container);
 | ||
| 182 | _e.select(); | ||
| 183 | if(this.hasSelected()==1){ | ||
| 184 | this.anchors.add(_e,this.group); | ||
| 185 | } | ||
| 186 | },selectAll:function(){ | ||
| 187 | this._isBusy=true; | ||
| 188 | for(var m in this.stencils){ | ||
| 189 | this.selectItem(m);
 | ||
| 190 | } | ||
| 191 | this._isBusy=false; | ||
| 192 | },selectItem:function(_f){ | ||
| 193 | var id=typeof (_f)=="string"?_f:_f.id; | ||
| 194 | var _10=this.stencils[id]; | ||
| 195 | this.setSelectionGroup();
 | ||
| 196 | this.onSelect(_10);
 | ||
| 197 | this.group.moveToFront();
 | ||
| 198 | this.setConstraint();
 | ||
| 199 | },onStencilDoubleClick:function(obj){ | ||
| 200 | if(this.selectedStencils[obj.id]){ | ||
| 201 | if(this.selectedStencils[obj.id].edit){ | ||
| 202 | var m=this.selectedStencils[obj.id]; | ||
| 203 | m.editMode=true;
 | ||
| 204 | this.deselect();
 | ||
| 205 | m.edit(); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | },onAnchorUp:function(){ | ||
| 209 | this.setConstraint();
 | ||
| 210 | },onStencilDown:function(obj,evt){ | ||
| 211 | if(!this.stencils[obj.id]){ | ||
| 212 | return;
 | ||
| 213 | } | ||
| 214 | this._isBusy=true; | ||
| 215 | if(this.selectedStencils[obj.id]&&this.keys.meta){ | ||
| 216 | if(dojo.isMac&&this.keys.cmmd){ | ||
| 217 | } | ||
| 218 | this.onDeselect(this.selectedStencils[obj.id]); | ||
| 219 | if(this.hasSelected()==1){ | ||
| 220 | this.withSelected(function(m){ | ||
| 221 | this.anchors.add(m,this.group); | ||
| 222 | }); | ||
| 223 | } | ||
| 224 | this.group.moveToFront();
 | ||
| 225 | this.setConstraint();
 | ||
| 226 | return;
 | ||
| 227 | }else{
 | ||
| 228 | if(this.selectedStencils[obj.id]){ | ||
| 229 | var mx=this.group.getTransform(); | ||
| 230 | this._offx=obj.x-mx.dx;
 | ||
| 231 | this._offy=obj.y-mx.dy;
 | ||
| 232 | return;
 | ||
| 233 | }else{
 | ||
| 234 | if(!this.keys.meta){ | ||
| 235 | this.deselect();
 | ||
| 236 | }else{
 | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | this.selectItem(obj.id);
 | ||
| 241 | mx=this.group.getTransform();
 | ||
| 242 | this._offx=obj.x-mx.dx;
 | ||
| 243 | this._offy=obj.y-mx.dx;
 | ||
| 244 | this.orgx=obj.x;
 | ||
| 245 | this.orgy=obj.y;
 | ||
| 246 | this._isBusy=false; | ||
| 247 | this.undo.add({before:function(){ | ||
| 248 | },after:function(){ | ||
| 249 | }}); | ||
| 250 | },onStencilUp:function(obj){ | ||
| 251 | },onStencilDrag:function(obj){ | ||
| 252 | if(!this._dragBegun){ | ||
| 253 | this.onBeginDrag(obj);
 | ||
| 254 | this._dragBegun=true; | ||
| 255 | }else{
 | ||
| 256 | this.saveThrottledState();
 | ||
| 257 | var x=obj.x-obj.last.x,y=obj.y-obj.last.y,c=this.constrain,mz=this.defaults.anchors.marginZero; | ||
| 258 | x=obj.x-this._offx;
 | ||
| 259 | y=obj.y-this._offy;
 | ||
| 260 | if(x<c.l+mz){
 | ||
| 261 | x=c.l+mz; | ||
| 262 | } | ||
| 263 | if(y<c.t+mz){
 | ||
| 264 | y=c.t+mz; | ||
| 265 | } | ||
| 266 | this.group.setTransform({dx:x,dy:y}); | ||
| 267 | } | ||
| 268 | },onDragEnd:function(obj){ | ||
| 269 | this._dragBegun=false; | ||
| 270 | },onBeginDrag:function(obj){ | ||
| 271 | this._wasDragged=true; | ||
| 272 | },onDown:function(obj){ | ||
| 273 | this.deselect();
 | ||
| 274 | },onStencilOver:function(obj){ | ||
| 275 | dojo.style(obj.id,"cursor","move"); | ||
| 276 | },onStencilOut:function(obj){ | ||
| 277 | dojo.style(obj.id,"cursor","crosshair"); | ||
| 278 | },exporter:function(){ | ||
| 279 | var _11=[];
 | ||
| 280 | for(var m in this.stencils){ | ||
| 281 | this.stencils[m].enabled&&_11.push(this.stencils[m].exporter()); | ||
| 282 | } | ||
| 283 | return _11;
 | ||
| 284 | },listStencils:function(){ | ||
| 285 | return this.stencils; | ||
| 286 | },toSelected:function(_12){ | ||
| 287 | var _13=Array.prototype.slice.call(arguments).splice(1); | ||
| 288 | for(var m in this.selectedStencils){ | ||
| 289 | var _14=this.selectedStencils[m]; | ||
| 290 | _14[_12].apply(_14,_13); | ||
| 291 | } | ||
| 292 | },withSelected:function(_15){ | ||
| 293 | var f=dojo.hitch(this,_15); | ||
| 294 | for(var m in this.selectedStencils){ | ||
| 295 | f(this.selectedStencils[m]);
 | ||
| 296 | } | ||
| 297 | },withUnselected:function(_16){ | ||
| 298 | var f=dojo.hitch(this,_16); | ||
| 299 | for(var m in this.stencils){ | ||
| 300 | !this.stencils[m].selected&&f(this.stencils[m]); | ||
| 301 | } | ||
| 302 | },withStencils:function(_17){ | ||
| 303 | var f=dojo.hitch(this,_17); | ||
| 304 | for(var m in this.stencils){ | ||
| 305 | f(this.stencils[m]);
 | ||
| 306 | } | ||
| 307 | },hasSelected:function(){ | ||
| 308 | var ln=0; | ||
| 309 | for(var m in this.selectedStencils){ | ||
| 310 | ln++; | ||
| 311 | } | ||
| 312 | return ln;
 | ||
| 313 | },isSelected:function(_18){ | ||
| 314 | return !!this.selectedStencils[_18.id]; | ||
| 315 | }}); | ||
| 316 | })(); | ||
| 317 | } |