| 1 |
define(["dojo", "./defaults", "./manager/_registry", "./manager/keys", "./manager/Mouse", |
| 2 |
"./manager/Canvas","./manager/Undo","./manager/Anchors","./manager/Stencil","./manager/StencilUI", |
| 3 |
"./util/common"], |
| 4 |
function(dojo, defaults, registry, keys, Mouse, Canvas, Undo, Anchors, Stencil, StencilUI, utilCommon){ |
| 5 |
return dojo.declare("dojox.drawing.Drawing", [], { |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
ready:false, |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
mode: "", |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
width:0, |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
height:0, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
constructor: function(props, node){ |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
var def = dojo.attr(node, "defaults"); |
| 104 |
this.defaults = def ? (typeof def === 'string' ? dojo.getObject(def) : def) : defaults; |
| 105 |
|
| 106 |
this.id = node.id || dijit.getUniqueId('dojox_drawing_Drawing'); |
| 107 |
registry.register(this, "drawing"); |
| 108 |
this.mode = (props.mode || dojo.attr(node, "mode") || "").toLowerCase(); |
| 109 |
var box = dojo.contentBox(node); |
| 110 |
this.width = props.width || box.w; |
| 111 |
this.height = props.height || box.h; |
| 112 |
utilCommon.register(this); |
| 113 |
this.mouse = new Mouse({util:utilCommon, keys:keys, id:this.mode=="ui"?"MUI":"mse"}); |
| 114 |
this.mouse.setEventMode(this.mode); |
| 115 |
|
| 116 |
this.tools = {}; |
| 117 |
this.stencilTypes = {}; |
| 118 |
this.stencilTypeMap = {}; |
| 119 |
this.srcRefNode = node; |
| 120 |
this.domNode = node; |
| 121 |
if(props.plugins){ |
| 122 |
this.plugins = eval(props.plugins); |
| 123 |
}else{ |
| 124 |
this.plugins = []; |
| 125 |
} |
| 126 |
|
| 127 |
this.widgetId = this.id; |
| 128 |
dojo.attr(this.domNode, "widgetId", this.widgetId); |
| 129 |
|
| 130 |
if(dijit && dijit.registry){ |
| 131 |
dijit.registry.add(this); |
| 132 |
console.log("using dijit") |
| 133 |
}else{ |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
dijit.registry = { |
| 138 |
objs:{}, |
| 139 |
add:function(obj){ |
| 140 |
this.objs[obj.id] = obj; |
| 141 |
} |
| 142 |
}; |
| 143 |
dijit.byId = function(id){ |
| 144 |
return dijit.registry.objs[id]; |
| 145 |
}; |
| 146 |
dijit.registry.add(this); |
| 147 |
} |
| 148 |
|
| 149 |
var stencils = registry.getRegistered("stencil"); |
| 150 |
for(var nm in stencils){ |
| 151 |
this.registerTool(stencils[nm].name); |
| 152 |
} |
| 153 |
var tools = registry.getRegistered("tool"); |
| 154 |
for(nm in tools){ |
| 155 |
this.registerTool(tools[nm].name); |
| 156 |
} |
| 157 |
var plugs = registry.getRegistered("plugin"); |
| 158 |
for(nm in plugs){ |
| 159 |
this.registerTool(plugs[nm].name); |
| 160 |
} |
| 161 |
this._createCanvas(); |
| 162 |
|
| 163 |
}, |
| 164 |
|
| 165 |
_createCanvas: function(){ |
| 166 |
console.info("drawing create canvas..."); |
| 167 |
this.canvas = new Canvas({ |
| 168 |
srcRefNode:this.domNode, |
| 169 |
util:utilCommon, |
| 170 |
mouse:this.mouse, |
| 171 |
width: this.width, height: this.height, |
| 172 |
callback: dojo.hitch(this, "onSurfaceReady") |
| 173 |
}); |
| 174 |
this.initPlugins(); |
| 175 |
}, |
| 176 |
|
| 177 |
resize: function(box){ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
box && dojo.style(this.domNode, { |
| 184 |
width:box.w+"px", |
| 185 |
height:box.h+"px" |
| 186 |
}); |
| 187 |
if(!this.canvas){ |
| 188 |
this._createCanvas(); |
| 189 |
}else if(box){ |
| 190 |
this.canvas.resize(box.w, box.h); |
| 191 |
} |
| 192 |
}, |
| 193 |
|
| 194 |
startup: function(){ |
| 195 |
|
| 196 |
}, |
| 197 |
|
| 198 |
getShapeProps: function(data, mode){ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
var surface = data.stencilType; |
| 204 |
var ui = this.mode=="ui" || mode=="ui"; |
| 205 |
return dojo.mixin({ |
| 206 |
container: ui && !surface ? this.canvas.overlay.createGroup() : this.canvas.surface.createGroup(), |
| 207 |
util:utilCommon, |
| 208 |
keys:keys, |
| 209 |
mouse:this.mouse, |
| 210 |
drawing:this, |
| 211 |
drawingType: ui && !surface ? "ui" : "stencil", |
| 212 |
style:this.defaults.copy() |
| 213 |
}, data || {}); |
| 214 |
}, |
| 215 |
|
| 216 |
addPlugin: function(plugin){ |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
this.plugins.push(plugin); |
| 221 |
if(this.canvas.surfaceReady){ |
| 222 |
this.initPlugins(); |
| 223 |
} |
| 224 |
}, |
| 225 |
|
| 226 |
initPlugins: function(){ |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if(!this.canvas || !this.canvas.surfaceReady){ |
| 232 |
var c = dojo.connect(this, "onSurfaceReady", this, function(){ |
| 233 |
dojo.disconnect(c); |
| 234 |
this.initPlugins(); |
| 235 |
}); |
| 236 |
return; |
| 237 |
} |
| 238 |
dojo.forEach(this.plugins, function(p, i){ |
| 239 |
var props = dojo.mixin({ |
| 240 |
util:utilCommon, |
| 241 |
keys:keys, |
| 242 |
mouse:this.mouse, |
| 243 |
drawing:this, |
| 244 |
stencils:this.stencils, |
| 245 |
anchors:this.anchors, |
| 246 |
canvas:this.canvas |
| 247 |
}, p.options || {}); |
| 248 |
|
| 249 |
this.registerTool(p.name, dojo.getObject(p.name)); |
| 250 |
try{ |
| 251 |
this.plugins[i] = new this.tools[p.name](props); |
| 252 |
}catch(e){ |
| 253 |
console.error("Failed to initilaize plugin: " +p.name + ". Did you require it?"); |
| 254 |
} |
| 255 |
}, this); |
| 256 |
this.plugins = []; |
| 257 |
|
| 258 |
|
| 259 |
this.mouse.setCanvas(); |
| 260 |
}, |
| 261 |
|
| 262 |
onSurfaceReady: function(){ |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
this.ready = true; |
| 268 |
|
| 269 |
this.mouse.init(this.canvas.domNode); |
| 270 |
this.undo = new Undo({keys:keys}); |
| 271 |
this.anchors = new Anchors({drawing:this, mouse:this.mouse, undo:this.undo, util:utilCommon}); |
| 272 |
if(this.mode == "ui"){ |
| 273 |
this.uiStencils = new StencilUI({canvas:this.canvas, surface:this.canvas.surface, mouse:this.mouse, keys:keys}); |
| 274 |
}else{ |
| 275 |
this.stencils = new Stencil({canvas:this.canvas, surface:this.canvas.surface, mouse:this.mouse, undo:this.undo, keys:keys, anchors:this.anchors}); |
| 276 |
this.uiStencils = new StencilUI({canvas:this.canvas, surface:this.canvas.surface, mouse:this.mouse, keys:keys}); |
| 277 |
} |
| 278 |
if(dojox.gfx.renderer=="silverlight"){ |
| 279 |
try{ |
| 280 |
new dojox.drawing.plugins.drawing.Silverlight({util:utilCommon, mouse:this.mouse, stencils:this.stencils, anchors:this.anchors, canvas:this.canvas}); |
| 281 |
}catch(e){ |
| 282 |
throw new Error("Attempted to install the Silverlight plugin, but it was not found."); |
| 283 |
} |
| 284 |
} |
| 285 |
dojo.forEach(this.plugins, function(p){ |
| 286 |
p.onSurfaceReady && p.onSurfaceReady(); |
| 287 |
}); |
| 288 |
|
| 289 |
}, |
| 290 |
|
| 291 |
addUI: function(type, options){ |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if(!this.ready){ |
| 309 |
var c = dojo.connect(this, "onSurfaceReady", this, function(){ |
| 310 |
dojo.disconnect(c); |
| 311 |
this.addUI(type, options); |
| 312 |
}); |
| 313 |
return false; |
| 314 |
} |
| 315 |
if(options && !options.data && !options.points){ |
| 316 |
options = {data:options} |
| 317 |
} |
| 318 |
if(!this.stencilTypes[type]){ |
| 319 |
if(type != "tooltip"){ |
| 320 |
console.warn("Not registered:", type); |
| 321 |
} |
| 322 |
return null; |
| 323 |
} |
| 324 |
var s = this.uiStencils.register( new this.stencilTypes[type](this.getShapeProps(options, "ui"))); |
| 325 |
return s; |
| 326 |
}, |
| 327 |
|
| 328 |
|
| 329 |
addStencil: function(type, options){ |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
if(!this.ready){ |
| 345 |
var c = dojo.connect(this, "onSurfaceReady", this, function(){ |
| 346 |
dojo.disconnect(c); |
| 347 |
this.addStencil(type, options); |
| 348 |
}); |
| 349 |
return false; |
| 350 |
} |
| 351 |
if(options && !options.data && !options.points){ |
| 352 |
options = {data:options} |
| 353 |
} |
| 354 |
var s = this.stencils.register( new this.stencilTypes[type](this.getShapeProps(options))); |
| 355 |
|
| 356 |
|
| 357 |
this.currentStencil && this.currentStencil.moveToFront(); |
| 358 |
return s; |
| 359 |
}, |
| 360 |
|
| 361 |
removeStencil: function(stencil){ |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
this.stencils.unregister(stencil); |
| 368 |
stencil.destroy(); |
| 369 |
}, |
| 370 |
|
| 371 |
removeAll: function(){ |
| 372 |
|
| 373 |
|
| 374 |
this.stencils.removeAll(); |
| 375 |
}, |
| 376 |
|
| 377 |
selectAll: function(){ |
| 378 |
|
| 379 |
|
| 380 |
this.stencils.selectAll(); |
| 381 |
}, |
| 382 |
|
| 383 |
toSelected: function(func ){ |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
this.stencils.toSelected.apply(this.stencils, arguments); |
| 391 |
}, |
| 392 |
|
| 393 |
exporter: function(){ |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
console.log("this.stencils", this.stencils); |
| 398 |
return this.stencils.exporter(); |
| 399 |
}, |
| 400 |
|
| 401 |
importer: function(objects){ |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
dojo.forEach(objects, function(m){ |
| 406 |
this.addStencil(m.type, m); |
| 407 |
}, this); |
| 408 |
}, |
| 409 |
|
| 410 |
changeDefaults: function(newStyle,value){ |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if(value!=undefined && value){ |
| 434 |
for(var nm in newStyle){ |
| 435 |
this.defaults[nm] = newStyle[nm]; |
| 436 |
} |
| 437 |
}else{ |
| 438 |
for(var nm in newStyle){ |
| 439 |
for(var n in newStyle[nm]){ |
| 440 |
|
| 441 |
this.defaults[nm][n] = newStyle[nm][n]; |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if(this.currentStencil!=undefined && (!this.currentStencil.created || this.defaults.clickMode)){ |
| 447 |
this.unSetTool(); |
| 448 |
this.setTool(this.currentType); |
| 449 |
} |
| 450 |
}, |
| 451 |
|
| 452 |
onRenderStencil: function(stencil){ |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
this.stencils.register(stencil); |
| 460 |
this.unSetTool(); |
| 461 |
if(!this.defaults.clickMode){ |
| 462 |
this.setTool(this.currentType); |
| 463 |
}else{ |
| 464 |
this.defaults.clickable = true; |
| 465 |
} |
| 466 |
}, |
| 467 |
|
| 468 |
onDeleteStencil: function(stencil){ |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
this.stencils.unregister(stencil); |
| 475 |
}, |
| 476 |
|
| 477 |
registerTool: function(type){ |
| 478 |
|
| 479 |
|
| 480 |
if(this.tools[type]){ return; } |
| 481 |
var constr = dojo.getObject(type); |
| 482 |
|
| 483 |
this.tools[type] = constr; |
| 484 |
var abbr = utilCommon.abbr(type); |
| 485 |
this.stencilTypes[abbr] = constr; |
| 486 |
this.stencilTypeMap[abbr] = type; |
| 487 |
}, |
| 488 |
|
| 489 |
getConstructor: function(abbr){ |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
return this.stencilTypes[abbr]; |
| 494 |
}, |
| 495 |
|
| 496 |
setTool: function(type){ |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if(this.mode=="ui"){ return; } |
| 503 |
if(!this.canvas || !this.canvas.surface){ |
| 504 |
var c = dojo.connect(this, "onSurfaceReady", this, function(){ |
| 505 |
dojo.disconnect(c); |
| 506 |
this.setTool(type); |
| 507 |
}); |
| 508 |
return; |
| 509 |
} |
| 510 |
if(this.currentStencil){ |
| 511 |
this.unSetTool(); |
| 512 |
} |
| 513 |
|
| 514 |
this.currentType = this.tools[type] ? type : this.stencilTypeMap[type]; |
| 515 |
|
| 516 |
|
| 517 |
try{ |
| 518 |
this.currentStencil = new this.tools[this.currentType]({container:this.canvas.surface.createGroup(), util:utilCommon, mouse:this.mouse, keys:keys}); |
| 519 |
console.log("new tool is:", this.currentStencil.id, this.currentStencil); |
| 520 |
if(this.defaults.clickMode){ this.defaults.clickable = false; } |
| 521 |
this.currentStencil.connect(this.currentStencil, "onRender", this, "onRenderStencil"); |
| 522 |
this.currentStencil.connect(this.currentStencil, "destroy", this, "onDeleteStencil"); |
| 523 |
}catch(e){ |
| 524 |
console.error("dojox.drawing.setTool Error:", e); |
| 525 |
console.error(this.currentType + " is not a constructor: ", this.tools[this.currentType]); |
| 526 |
|
| 527 |
} |
| 528 |
}, |
| 529 |
|
| 530 |
set: function(name, value){ |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
console.info("Attempting to set ",name," to: ",value,". Set currently not fully supported in Drawing"); |
| 535 |
}, |
| 536 |
|
| 537 |
get: function(name){ |
| 538 |
return; |
| 539 |
}, |
| 540 |
|
| 541 |
unSetTool: function(){ |
| 542 |
|
| 543 |
|
| 544 |
if(!this.currentStencil.created){ |
| 545 |
this.currentStencil.destroy(); |
| 546 |
} |
| 547 |
|
| 548 |
} |
| 549 |
}); |
| 550 |
|
| 551 |
}); |