| 1 |
define(["dojo", "../util/oo", "../defaults"], |
| 2 |
function(dojo, oo, defaults){ |
| 3 |
|
| 4 |
var surface, surfaceNode; |
| 5 |
|
| 6 |
return oo.declare( |
| 7 |
function(options){ |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
surface = options.surface; |
| 12 |
this.canvas = options.canvas; |
| 13 |
|
| 14 |
|
| 15 |
this.undo = options.undo; |
| 16 |
this.mouse = options.mouse; |
| 17 |
this.keys = options.keys; |
| 18 |
this.anchors = options.anchors; |
| 19 |
this.stencils = {}; |
| 20 |
this.selectedStencils = {}; |
| 21 |
this._mouseHandle = this.mouse.register(this); |
| 22 |
|
| 23 |
dojo.connect(this.keys, "onArrow", this, "onArrow"); |
| 24 |
dojo.connect(this.keys, "onEsc", this, "deselect"); |
| 25 |
dojo.connect(this.keys, "onDelete", this, "onDelete"); |
| 26 |
|
| 27 |
}, |
| 28 |
{ |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
_dragBegun: false, |
| 38 |
_wasDragged:false, |
| 39 |
_secondClick:false, |
| 40 |
_isBusy:false, |
| 41 |
|
| 42 |
setRecentStencil: function(stencil){ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
this.recent = stencil; |
| 47 |
}, |
| 48 |
|
| 49 |
getRecentStencil: function(){ |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
return this.recent; |
| 54 |
}, |
| 55 |
|
| 56 |
register: function(stencil){ |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
console.log("Selection.register ::::::", stencil.id); |
| 64 |
if(stencil.isText && !stencil.editMode && stencil.deleteEmptyCreate && !stencil.getText()){ |
| 65 |
|
| 66 |
|
| 67 |
console.warn("EMPTY CREATE DELETE", stencil); |
| 68 |
stencil.destroy(); |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
this.stencils[stencil.id] = stencil; |
| 73 |
this.setRecentStencil(stencil); |
| 74 |
|
| 75 |
if(stencil.execText){ |
| 76 |
if(stencil._text && !stencil.editMode){ |
| 77 |
console.log("select text"); |
| 78 |
this.selectItem(stencil); |
| 79 |
} |
| 80 |
stencil.connect("execText", this, function(){ |
| 81 |
if(stencil.isText && stencil.deleteEmptyModify && !stencil.getText()){ |
| 82 |
console.warn("EMPTY MOD DELETE", stencil); |
| 83 |
|
| 84 |
|
| 85 |
this.deleteItem(stencil); |
| 86 |
}else if(stencil.selectOnExec){ |
| 87 |
this.selectItem(stencil); |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
stencil.connect("deselect", this, function(){ |
| 93 |
if(!this._isBusy && this.isSelected(stencil)){ |
| 94 |
|
| 95 |
this.deselectItem(stencil); |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
stencil.connect("select", this, function(){ |
| 100 |
if(!this._isBusy && !this.isSelected(stencil)){ |
| 101 |
|
| 102 |
this.selectItem(stencil); |
| 103 |
} |
| 104 |
}); |
| 105 |
|
| 106 |
return stencil; |
| 107 |
}, |
| 108 |
unregister: function(stencil){ |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
console.log("Selection.unregister ::::::", stencil.id, "sel:", stencil.selected); |
| 115 |
if(stencil){ |
| 116 |
stencil.selected && this.onDeselect(stencil); |
| 117 |
delete this.stencils[stencil.id]; |
| 118 |
} |
| 119 |
}, |
| 120 |
|
| 121 |
onArrow: function(evt){ |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if(this.hasSelected()){ |
| 127 |
this.saveThrottledState(); |
| 128 |
this.group.applyTransform({dx:evt.x, dy: evt.y}); |
| 129 |
} |
| 130 |
}, |
| 131 |
|
| 132 |
_throttleVrl:null, |
| 133 |
_throttle: false, |
| 134 |
throttleTime:400, |
| 135 |
_lastmxx:-1, |
| 136 |
_lastmxy:-1, |
| 137 |
saveMoveState: function(){ |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
var mx = this.group.getTransform(); |
| 143 |
if(mx.dx == this._lastmxx && mx.dy == this._lastmxy){ return; } |
| 144 |
this._lastmxx = mx.dx; |
| 145 |
this._lastmxy = mx.dy; |
| 146 |
|
| 147 |
this.undo.add({ |
| 148 |
before:dojo.hitch(this.group, "setTransform", mx) |
| 149 |
}); |
| 150 |
}, |
| 151 |
|
| 152 |
saveThrottledState: function(){ |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
clearTimeout(this._throttleVrl); |
| 159 |
clearInterval(this._throttleVrl); |
| 160 |
this._throttleVrl = setTimeout(dojo.hitch(this, function(){ |
| 161 |
this._throttle = false; |
| 162 |
this.saveMoveState(); |
| 163 |
}), this.throttleTime); |
| 164 |
if(this._throttle){ return; } |
| 165 |
this._throttle = true; |
| 166 |
|
| 167 |
this.saveMoveState(); |
| 168 |
|
| 169 |
}, |
| 170 |
unDelete: function(stencils){ |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
console.log("unDelete:", stencils); |
| 175 |
for(var s in stencils){ |
| 176 |
stencils[s].render(); |
| 177 |
this.onSelect(stencils[s]); |
| 178 |
} |
| 179 |
}, |
| 180 |
onDelete: function(noundo){ |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
console.log("Stencil onDelete", noundo); |
| 185 |
if(noundo!==true){ |
| 186 |
this.undo.add({ |
| 187 |
before:dojo.hitch(this, "unDelete", this.selectedStencils), |
| 188 |
after:dojo.hitch(this, "onDelete", true) |
| 189 |
}); |
| 190 |
} |
| 191 |
this.withSelected(function(m){ |
| 192 |
this.anchors.remove(m); |
| 193 |
var id = m.id; |
| 194 |
console.log("delete:", m); |
| 195 |
m.destroy(); |
| 196 |
delete this.stencils[id]; |
| 197 |
}); |
| 198 |
this.selectedStencils = {}; |
| 199 |
}, |
| 200 |
|
| 201 |
deleteItem: function(stencil){ |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if(this.hasSelected()){ |
| 208 |
|
| 209 |
var sids = []; |
| 210 |
for(var m in this.selectedStencils){ |
| 211 |
if(this.selectedStencils.id == stencil.id){ |
| 212 |
if(this.hasSelected()==1){ |
| 213 |
|
| 214 |
this.onDelete(); |
| 215 |
return; |
| 216 |
} |
| 217 |
}else{ |
| 218 |
sids.push(this.selectedStencils.id); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
this.deselect(); |
| 223 |
this.selectItem(stencil); |
| 224 |
this.onDelete(); |
| 225 |
dojo.forEach(sids, function(id){ |
| 226 |
this.selectItem(id); |
| 227 |
}, this); |
| 228 |
}else{ |
| 229 |
|
| 230 |
this.selectItem(stencil); |
| 231 |
|
| 232 |
this.onDelete(); |
| 233 |
} |
| 234 |
}, |
| 235 |
|
| 236 |
removeAll: function(){ |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
this.selectAll(); |
| 241 |
this._isBusy = true; |
| 242 |
this.onDelete(); |
| 243 |
this.stencils = {}; |
| 244 |
this._isBusy = false; |
| 245 |
}, |
| 246 |
|
| 247 |
setSelectionGroup: function(){ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
this.withSelected(function(m){ |
| 253 |
this.onDeselect(m, true); |
| 254 |
}); |
| 255 |
|
| 256 |
if(this.group){ |
| 257 |
surface.remove(this.group); |
| 258 |
this.group.removeShape(); |
| 259 |
} |
| 260 |
this.group = surface.createGroup(); |
| 261 |
this.group.setTransform({dx:0, dy: 0}); |
| 262 |
|
| 263 |
this.withSelected(function(m){ |
| 264 |
this.group.add(m.container); |
| 265 |
m.select(); |
| 266 |
}); |
| 267 |
}, |
| 268 |
|
| 269 |
setConstraint: function(){ |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
var t = Infinity, l = Infinity; |
| 276 |
this.withSelected(function(m){ |
| 277 |
var o = m.getBounds(); |
| 278 |
t = Math.min(o.y1, t); |
| 279 |
l = Math.min(o.x1, l); |
| 280 |
}); |
| 281 |
this.constrain = {l:-l, t:-t}; |
| 282 |
}, |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
onDeselect: function(stencil, keepObject){ |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if(!keepObject){ |
| 291 |
delete this.selectedStencils[stencil.id]; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
this.anchors.remove(stencil); |
| 296 |
|
| 297 |
surface.add(stencil.container); |
| 298 |
stencil.selected && stencil.deselect(); |
| 299 |
stencil.applyTransform(this.group.getTransform()); |
| 300 |
}, |
| 301 |
|
| 302 |
deselectItem: function(stencil){ |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
this.onDeselect(stencil); |
| 308 |
}, |
| 309 |
|
| 310 |
deselect: function(){ |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
this.withSelected(function(m){ |
| 315 |
this.onDeselect(m); |
| 316 |
}); |
| 317 |
this._dragBegun = false; |
| 318 |
this._wasDragged = false; |
| 319 |
}, |
| 320 |
|
| 321 |
onSelect: function(stencil){ |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if(!stencil){ |
| 327 |
console.error("null stencil is not selected:", this.stencils) |
| 328 |
} |
| 329 |
if(this.selectedStencils[stencil.id]){ return; } |
| 330 |
this.selectedStencils[stencil.id] = stencil; |
| 331 |
this.group.add(stencil.container); |
| 332 |
stencil.select(); |
| 333 |
if(this.hasSelected()==1){ |
| 334 |
this.anchors.add(stencil, this.group); |
| 335 |
} |
| 336 |
}, |
| 337 |
|
| 338 |
selectAll: function(){ |
| 339 |
|
| 340 |
|
| 341 |
this._isBusy = true; |
| 342 |
for(var m in this.stencils){ |
| 343 |
|
| 344 |
this.selectItem(m); |
| 345 |
|
| 346 |
} |
| 347 |
this._isBusy = false; |
| 348 |
}, |
| 349 |
|
| 350 |
selectItem: function( idOrItem){ |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
var id = typeof(idOrItem)=="string" ? idOrItem : idOrItem.id; |
| 355 |
var stencil = this.stencils[id]; |
| 356 |
this.setSelectionGroup(); |
| 357 |
this.onSelect(stencil); |
| 358 |
this.group.moveToFront(); |
| 359 |
this.setConstraint(); |
| 360 |
}, |
| 361 |
|
| 362 |
onLabelDoubleClick: function(obj){ |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
console.info("mgr.onLabelDoubleClick:", obj); |
| 367 |
if(this.selectedStencils[obj.id]){ |
| 368 |
this.deselect(); |
| 369 |
} |
| 370 |
}, |
| 371 |
|
| 372 |
onStencilDoubleClick: function(obj){ |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
console.info("mgr.onStencilDoubleClick:", obj); |
| 377 |
if(this.selectedStencils[obj.id]){ |
| 378 |
if(this.selectedStencils[obj.id].edit){ |
| 379 |
console.info("Mgr Stencil Edit -> ", this.selectedStencils[obj.id]); |
| 380 |
var m = this.selectedStencils[obj.id]; |
| 381 |
|
| 382 |
|
| 383 |
m.editMode = true; |
| 384 |
this.deselect(); |
| 385 |
m.edit(); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
}, |
| 390 |
|
| 391 |
onAnchorUp: function(){ |
| 392 |
|
| 393 |
|
| 394 |
this.setConstraint(); |
| 395 |
}, |
| 396 |
|
| 397 |
onStencilDown: function(obj, evt){ |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
console.info(" >>> onStencilDown:", obj.id, this.keys.meta); |
| 402 |
if(!this.stencils[obj.id]){ return; } |
| 403 |
this.setRecentStencil(this.stencils[obj.id]); |
| 404 |
this._isBusy = true; |
| 405 |
|
| 406 |
|
| 407 |
if(this.selectedStencils[obj.id] && this.keys.meta){ |
| 408 |
if(dojo.isMac && this.keys.cmmd){ |
| 409 |
|
| 410 |
|
| 411 |
} |
| 412 |
console.log(" shift remove"); |
| 413 |
this.onDeselect(this.selectedStencils[obj.id]); |
| 414 |
if(this.hasSelected()==1){ |
| 415 |
this.withSelected(function(m){ |
| 416 |
this.anchors.add(m, this.group); |
| 417 |
}); |
| 418 |
} |
| 419 |
this.group.moveToFront(); |
| 420 |
this.setConstraint(); |
| 421 |
return; |
| 422 |
|
| 423 |
}else if(this.selectedStencils[obj.id]){ |
| 424 |
console.log(" clicked on selected"); |
| 425 |
|
| 426 |
|
| 427 |
var mx = this.group.getTransform(); |
| 428 |
this._offx = obj.x - mx.dx; |
| 429 |
this._offy = obj.y - mx.dy; |
| 430 |
return; |
| 431 |
|
| 432 |
}else if(!this.keys.meta){ |
| 433 |
|
| 434 |
console.log(" deselect all"); |
| 435 |
this.deselect(); |
| 436 |
|
| 437 |
}else{ |
| 438 |
|
| 439 |
|
| 440 |
} |
| 441 |
console.log(" add stencil to selection"); |
| 442 |
|
| 443 |
this.selectItem(obj.id); |
| 444 |
|
| 445 |
mx = this.group.getTransform(); |
| 446 |
this._offx = obj.x - mx.dx; |
| 447 |
this._offy = obj.y - mx.dx; |
| 448 |
|
| 449 |
this.orgx = obj.x; |
| 450 |
this.orgy = obj.y; |
| 451 |
|
| 452 |
this._isBusy = false; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
this.undo.add({ |
| 459 |
before:function(){ |
| 460 |
|
| 461 |
}, |
| 462 |
after: function(){ |
| 463 |
|
| 464 |
} |
| 465 |
}); |
| 466 |
}, |
| 467 |
|
| 468 |
onLabelDown: function(obj, evt){ |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
this.onStencilDown(obj,evt); |
| 476 |
}, |
| 477 |
|
| 478 |
onStencilUp: function(obj){ |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
}, |
| 483 |
|
| 484 |
onLabelUp: function(obj){ |
| 485 |
this.onStencilUp(obj); |
| 486 |
}, |
| 487 |
|
| 488 |
onStencilDrag: function(obj){ |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
if(!this._dragBegun){ |
| 493 |
|
| 494 |
|
| 495 |
this.onBeginDrag(obj); |
| 496 |
this._dragBegun = true; |
| 497 |
}else{ |
| 498 |
this.saveThrottledState(); |
| 499 |
|
| 500 |
var x = obj.x - obj.last.x, |
| 501 |
y = obj.y - obj.last.y, |
| 502 |
c = this.constrain, |
| 503 |
mz = defaults.anchors.marginZero; |
| 504 |
|
| 505 |
|
| 506 |
x = obj.x - this._offx; |
| 507 |
y = obj.y - this._offy; |
| 508 |
|
| 509 |
if(x < c.l + mz){ |
| 510 |
x = c.l + mz; |
| 511 |
} |
| 512 |
if(y < c.t + mz){ |
| 513 |
y = c.t + mz; |
| 514 |
} |
| 515 |
|
| 516 |
this.group.setTransform({ |
| 517 |
dx: x, |
| 518 |
dy: y |
| 519 |
}); |
| 520 |
|
| 521 |
|
| 522 |
} |
| 523 |
}, |
| 524 |
|
| 525 |
onLabelDrag: function(obj){ |
| 526 |
this.onStencilDrag(obj); |
| 527 |
}, |
| 528 |
|
| 529 |
onDragEnd: function(obj){ |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
this._dragBegun = false; |
| 534 |
}, |
| 535 |
onBeginDrag: function(obj){ |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
this._wasDragged = true; |
| 540 |
}, |
| 541 |
|
| 542 |
onDown: function(obj){ |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
this.deselect(); |
| 547 |
}, |
| 548 |
|
| 549 |
|
| 550 |
onStencilOver: function(obj){ |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
dojo.style(obj.id, "cursor", "move"); |
| 557 |
}, |
| 558 |
|
| 559 |
onStencilOut: function(obj){ |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
dojo.style(obj.id, "cursor", "crosshair"); |
| 565 |
}, |
| 566 |
|
| 567 |
exporter: function(){ |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
var items = []; |
| 572 |
for(var m in this.stencils){ |
| 573 |
this.stencils[m].enabled && items.push(this.stencils[m].exporter()); |
| 574 |
} |
| 575 |
return items; |
| 576 |
}, |
| 577 |
|
| 578 |
listStencils: function(){ |
| 579 |
return this.stencils; |
| 580 |
}, |
| 581 |
|
| 582 |
toSelected: function(func){ |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
var args = Array.prototype.slice.call(arguments).splice(1); |
| 587 |
for(var m in this.selectedStencils){ |
| 588 |
var item = this.selectedStencils[m]; |
| 589 |
item[func].apply(item, args); |
| 590 |
} |
| 591 |
}, |
| 592 |
|
| 593 |
withSelected: function(func){ |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
var f = dojo.hitch(this, func); |
| 598 |
for(var m in this.selectedStencils){ |
| 599 |
f(this.selectedStencils[m]); |
| 600 |
} |
| 601 |
}, |
| 602 |
|
| 603 |
withUnselected: function(func){ |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
var f = dojo.hitch(this, func); |
| 608 |
for(var m in this.stencils){ |
| 609 |
!this.stencils[m].selected && f(this.stencils[m]); |
| 610 |
} |
| 611 |
}, |
| 612 |
|
| 613 |
withStencils: function(func){ |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
var f = dojo.hitch(this, func); |
| 618 |
for(var m in this.stencils){ |
| 619 |
f(this.stencils[m]); |
| 620 |
} |
| 621 |
}, |
| 622 |
|
| 623 |
hasSelected: function(){ |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
var ln = 0; |
| 630 |
for(var m in this.selectedStencils){ ln++; } |
| 631 |
return ln; |
| 632 |
}, |
| 633 |
|
| 634 |
isSelected: function(stencil){ |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
return !!this.selectedStencils[stencil.id]; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
); |
| 644 |
}); |