root / trunk / web / dojo / dojox / drawing / Drawing.js @ 12
History | View | Annotate | Download (7.04 KB)
| 1 |
/*
|
|---|---|
| 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.Drawing"]){ |
| 9 |
dojo._hasResource["dojox.drawing.Drawing"]=true; |
| 10 |
dojo.provide("dojox.drawing.Drawing");
|
| 11 |
(function(){
|
| 12 |
var _1=false; |
| 13 |
dojo.declare("dojox.drawing.Drawing",[],{ready:false,mode:"",width:0,height:0,constructor:function(_2,_3){ |
| 14 |
var _4=dojo.attr(_3,"defaults"); |
| 15 |
if(_4){
|
| 16 |
dojox.drawing.defaults=dojo.getObject(_4); |
| 17 |
} |
| 18 |
this.defaults=dojox.drawing.defaults;
|
| 19 |
this.id=_3.id;
|
| 20 |
dojox.drawing.register(this,"drawing"); |
| 21 |
this.mode=(_2.mode||dojo.attr(_3,"mode")||"").toLowerCase(); |
| 22 |
var _5=dojo.contentBox(_3);
|
| 23 |
this.width=_5.w;
|
| 24 |
this.height=_5.h;
|
| 25 |
this.util=dojox.drawing.util.common;
|
| 26 |
this.util.register(this); |
| 27 |
this.keys=dojox.drawing.manager.keys;
|
| 28 |
this.mouse=new dojox.drawing.manager.Mouse({util:this.util,keys:this.keys,id:this.mode=="ui"?"MUI":"mse"}); |
| 29 |
this.mouse.setEventMode(this.mode); |
| 30 |
this.tools={};
|
| 31 |
this.stencilTypes={};
|
| 32 |
this.stencilTypeMap={};
|
| 33 |
this.srcRefNode=_3;
|
| 34 |
this.domNode=_3;
|
| 35 |
var _6=dojo.attr(_3,"plugins"); |
| 36 |
if(_6){
|
| 37 |
this.plugins=eval(_6);
|
| 38 |
}else{
|
| 39 |
this.plugins=[];
|
| 40 |
} |
| 41 |
this.widgetId=this.id; |
| 42 |
dojo.attr(this.domNode,"widgetId",this.widgetId); |
| 43 |
if(dijit&&dijit.registry){
|
| 44 |
dijit.registry.add(this);
|
| 45 |
}else{
|
| 46 |
dijit.registry={objs:{},add:function(_7){
|
| 47 |
this.objs[_7.id]=_7;
|
| 48 |
}}; |
| 49 |
dijit.byId=function(id){ |
| 50 |
return dijit.registry.objs[id];
|
| 51 |
}; |
| 52 |
dijit.registry.add(this);
|
| 53 |
} |
| 54 |
var _8=dojox.drawing.getRegistered("stencil"); |
| 55 |
for(var nm in _8){ |
| 56 |
this.registerTool(_8[nm].name);
|
| 57 |
} |
| 58 |
var _9=dojox.drawing.getRegistered("tool"); |
| 59 |
for(nm in _9){ |
| 60 |
this.registerTool(_9[nm].name);
|
| 61 |
} |
| 62 |
var _a=dojox.drawing.getRegistered("plugin"); |
| 63 |
for(nm in _a){ |
| 64 |
this.registerTool(_a[nm].name);
|
| 65 |
} |
| 66 |
this._createCanvas();
|
| 67 |
},_createCanvas:function(){ |
| 68 |
this.canvas=new dojox.drawing.manager.Canvas({srcRefNode:this.domNode,util:this.util,mouse:this.mouse,callback:dojo.hitch(this,"onSurfaceReady")}); |
| 69 |
this.initPlugins();
|
| 70 |
},resize:function(_b){ |
| 71 |
_b&&dojo.style(this.domNode,{width:_b.w+"px",height:_b.h+"px"}); |
| 72 |
if(!this.canvas){ |
| 73 |
this._createCanvas();
|
| 74 |
}else{
|
| 75 |
if(_b){
|
| 76 |
this.canvas.resize(_b.w,_b.h);
|
| 77 |
} |
| 78 |
} |
| 79 |
},startup:function(){ |
| 80 |
},getShapeProps:function(_c,_d){ |
| 81 |
var _e=_c.stencilType;
|
| 82 |
var ui=this.mode=="ui"||_d=="ui"; |
| 83 |
return dojo.mixin({container:ui&&!_e?this.canvas.overlay.createGroup():this.canvas.surface.createGroup(),util:this.util,keys:this.keys,mouse:this.mouse,drawing:this,drawingType:ui&&!_e?"ui":"stencil",style:this.defaults.copy()},_c||{}); |
| 84 |
},addPlugin:function(_f){ |
| 85 |
this.plugins.push(_f);
|
| 86 |
if(this.canvas.surfaceReady){ |
| 87 |
this.initPlugins();
|
| 88 |
} |
| 89 |
},initPlugins:function(){ |
| 90 |
if(!this.canvas||!this.canvas.surfaceReady){ |
| 91 |
var c=dojo.connect(this,"onSurfaceReady",this,function(){ |
| 92 |
dojo.disconnect(c); |
| 93 |
this.initPlugins();
|
| 94 |
}); |
| 95 |
return;
|
| 96 |
} |
| 97 |
dojo.forEach(this.plugins,function(p,i){ |
| 98 |
var _10=dojo.mixin({util:this.util,keys:this.keys,mouse:this.mouse,drawing:this,stencils:this.stencils,anchors:this.anchors,canvas:this.canvas},p.options||{}); |
| 99 |
this.registerTool(p.name,dojo.getObject(p.name));
|
| 100 |
try{
|
| 101 |
this.plugins[i]=new this.tools[p.name](_10); |
| 102 |
} |
| 103 |
catch(e){
|
| 104 |
console.error("Failed to initilaize plugin:\t"+p.name+". Did you require it?"); |
| 105 |
} |
| 106 |
},this);
|
| 107 |
this.plugins=[];
|
| 108 |
_1=true;
|
| 109 |
this.mouse.setCanvas();
|
| 110 |
},onSurfaceReady:function(){ |
| 111 |
this.ready=true; |
| 112 |
this.mouse.init(this.canvas.domNode); |
| 113 |
this.undo=new dojox.drawing.manager.Undo({keys:this.keys}); |
| 114 |
this.anchors=new dojox.drawing.manager.Anchors({drawing:this,mouse:this.mouse,undo:this.undo,util:this.util}); |
| 115 |
if(this.mode=="ui"){ |
| 116 |
this.uiStencils=new dojox.drawing.manager.StencilUI({canvas:this.canvas,surface:this.canvas.surface,mouse:this.mouse,keys:this.keys}); |
| 117 |
}else{
|
| 118 |
this.stencils=new dojox.drawing.manager.Stencil({canvas:this.canvas,surface:this.canvas.surface,mouse:this.mouse,undo:this.undo,keys:this.keys,anchors:this.anchors}); |
| 119 |
this.uiStencils=new dojox.drawing.manager.StencilUI({canvas:this.canvas,surface:this.canvas.surface,mouse:this.mouse,keys:this.keys}); |
| 120 |
} |
| 121 |
if(dojox.gfx.renderer=="silverlight"){ |
| 122 |
try{
|
| 123 |
new dojox.drawing.plugins.drawing.Silverlight({util:this.util,mouse:this.mouse,stencils:this.stencils,anchors:this.anchors,canvas:this.canvas}); |
| 124 |
} |
| 125 |
catch(e){
|
| 126 |
throw new Error("Attempted to install the Silverlight plugin, but it was not found."); |
| 127 |
} |
| 128 |
} |
| 129 |
dojo.forEach(this.plugins,function(p){ |
| 130 |
p.onSurfaceReady&&p.onSurfaceReady(); |
| 131 |
}); |
| 132 |
},addUI:function(_11,_12){ |
| 133 |
if(!this.ready){ |
| 134 |
var c=dojo.connect(this,"onSurfaceReady",this,function(){ |
| 135 |
dojo.disconnect(c); |
| 136 |
this.addUI(_11,_12);
|
| 137 |
}); |
| 138 |
return false; |
| 139 |
} |
| 140 |
if(_12&&!_12.data&&!_12.points){
|
| 141 |
_12={data:_12};
|
| 142 |
} |
| 143 |
if(!this.stencilTypes[_11]){ |
| 144 |
if(_11!="tooltip"){ |
| 145 |
console.warn("Not registered:",_11);
|
| 146 |
} |
| 147 |
return null; |
| 148 |
} |
| 149 |
var s=this.uiStencils.register(new this.stencilTypes[_11](this.getShapeProps(_12,"ui"))); |
| 150 |
return s;
|
| 151 |
},addStencil:function(_13,_14){ |
| 152 |
if(!this.ready){ |
| 153 |
var c=dojo.connect(this,"onSurfaceReady",this,function(){ |
| 154 |
dojo.disconnect(c); |
| 155 |
this.addStencil(_13,_14);
|
| 156 |
}); |
| 157 |
return false; |
| 158 |
} |
| 159 |
if(_14&&!_14.data&&!_14.points){
|
| 160 |
_14={data:_14};
|
| 161 |
} |
| 162 |
var s=this.stencils.register(new this.stencilTypes[_13](this.getShapeProps(_14))); |
| 163 |
this.currentStencil&&this.currentStencil.moveToFront(); |
| 164 |
return s;
|
| 165 |
},removeStencil:function(_15){ |
| 166 |
this.stencils.unregister(_15);
|
| 167 |
_15.destroy(); |
| 168 |
},removeAll:function(){ |
| 169 |
this.stencils.removeAll();
|
| 170 |
},selectAll:function(){ |
| 171 |
this.stencils.selectAll();
|
| 172 |
},toSelected:function(_16){ |
| 173 |
this.stencils.toSelected.apply(this.stencils,arguments); |
| 174 |
},exporter:function(){ |
| 175 |
return this.stencils.exporter(); |
| 176 |
},importer:function(_17){ |
| 177 |
dojo.forEach(_17,function(m){
|
| 178 |
this.addStencil(m.type,m);
|
| 179 |
},this);
|
| 180 |
},changeDefaults:function(_18){ |
| 181 |
for(var nm in _18){ |
| 182 |
for(var n in _18[nm]){ |
| 183 |
this.defaults[nm][n]=_18[nm][n];
|
| 184 |
} |
| 185 |
} |
| 186 |
this.unSetTool();
|
| 187 |
this.setTool(this.currentType); |
| 188 |
},onRenderStencil:function(_19){ |
| 189 |
this.stencils.register(_19);
|
| 190 |
this.unSetTool();
|
| 191 |
if(!this.defaults.clickMode){ |
| 192 |
this.setTool(this.currentType); |
| 193 |
}else{
|
| 194 |
if(this.defaults.clickMode){ |
| 195 |
this.defaults.clickable=true; |
| 196 |
} |
| 197 |
} |
| 198 |
},onDeleteStencil:function(_1a){ |
| 199 |
this.stencils.unregister(_1a);
|
| 200 |
},registerTool:function(_1b){ |
| 201 |
if(this.tools[_1b]){ |
| 202 |
return;
|
| 203 |
} |
| 204 |
var _1c=dojo.getObject(_1b);
|
| 205 |
this.tools[_1b]=_1c;
|
| 206 |
var _1d=this.util.abbr(_1b); |
| 207 |
this.stencilTypes[_1d]=_1c;
|
| 208 |
this.stencilTypeMap[_1d]=_1b;
|
| 209 |
},getConstructor:function(_1e){ |
| 210 |
return this.stencilTypes[_1e]; |
| 211 |
},setTool:function(_1f){ |
| 212 |
if(this.mode=="ui"){ |
| 213 |
return;
|
| 214 |
} |
| 215 |
if(!this.canvas||!this.canvas.surface){ |
| 216 |
var c=dojo.connect(this,"onSurfaceReady",this,function(){ |
| 217 |
dojo.disconnect(c); |
| 218 |
this.setTool(_1f);
|
| 219 |
}); |
| 220 |
return;
|
| 221 |
} |
| 222 |
if(this.currentStencil){ |
| 223 |
this.unSetTool();
|
| 224 |
} |
| 225 |
this.currentType=this.tools[_1f]?_1f:this.stencilTypeMap[_1f]; |
| 226 |
try{
|
| 227 |
this.currentStencil=new this.tools[this.currentType]({container:this.canvas.surface.createGroup(),util:this.util,mouse:this.mouse,keys:this.keys}); |
| 228 |
if(this.defaults.clickMode){ |
| 229 |
this.defaults.clickable=false; |
| 230 |
} |
| 231 |
this.currentStencil.connect(this.currentStencil,"onRender",this,"onRenderStencil"); |
| 232 |
this.currentStencil.connect(this.currentStencil,"destroy",this,"onDeleteStencil"); |
| 233 |
} |
| 234 |
catch(e){
|
| 235 |
console.error("dojox.drawing.setTool Error:",e);
|
| 236 |
console.error(this.currentType+" is not a constructor: ",this.tools[this.currentType]); |
| 237 |
} |
| 238 |
},unSetTool:function(){ |
| 239 |
if(!this.currentStencil.created){ |
| 240 |
this.currentStencil.destroy();
|
| 241 |
} |
| 242 |
}}); |
| 243 |
})(); |
| 244 |
} |