| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/dnd/common", |
| 5 |
"dojo/dom-class", |
| 6 |
"dojo/dom-geometry", |
| 7 |
"dojo/_base/lang", |
| 8 |
"dojo/mouse", |
| 9 |
"dojo/on", |
| 10 |
"dojo/touch", |
| 11 |
"dojo/topic", |
| 12 |
"dojo/dnd/Manager", |
| 13 |
"./_dndSelector" |
| 14 |
], function(array, declare, dndCommon, domClass, domGeometry, lang, mouse, on, touch, topic, DNDManager, _dndSelector){ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
var dndSource = declare("dijit.tree.dndSource", _dndSelector, { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
isSource: true, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
accept: ["text", "treeNode"], |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
copyOnly: false, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
dragThreshold: 5, |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
betweenThreshold: 0, |
| 54 |
|
| 55 |
|
| 56 |
generateText: true, |
| 57 |
|
| 58 |
constructor: function( tree, params){ |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
if(!params){ |
| 64 |
params = {}; |
| 65 |
} |
| 66 |
lang.mixin(this, params); |
| 67 |
var type = params.accept instanceof Array ? params.accept : ["text", "treeNode"]; |
| 68 |
this.accept = null; |
| 69 |
if(type.length){ |
| 70 |
this.accept = {}; |
| 71 |
for(var i = 0; i < type.length; ++i){ |
| 72 |
this.accept[type[i]] = 1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
this.isDragging = false; |
| 78 |
this.mouseDown = false; |
| 79 |
this.targetAnchor = null; |
| 80 |
this.targetBox = null; |
| 81 |
this.dropPosition = ""; |
| 82 |
this._lastX = 0; |
| 83 |
this._lastY = 0; |
| 84 |
|
| 85 |
|
| 86 |
this.sourceState = ""; |
| 87 |
if(this.isSource){ |
| 88 |
domClass.add(this.node, "dojoDndSource"); |
| 89 |
} |
| 90 |
this.targetState = ""; |
| 91 |
if(this.accept){ |
| 92 |
domClass.add(this.node, "dojoDndTarget"); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
this.topics = [ |
| 97 |
topic.subscribe("/dnd/source/over", lang.hitch(this, "onDndSourceOver")), |
| 98 |
topic.subscribe("/dnd/start", lang.hitch(this, "onDndStart")), |
| 99 |
topic.subscribe("/dnd/drop", lang.hitch(this, "onDndDrop")), |
| 100 |
topic.subscribe("/dnd/cancel", lang.hitch(this, "onDndCancel")) |
| 101 |
]; |
| 102 |
}, |
| 103 |
|
| 104 |
|
| 105 |
checkAcceptance: function(){ |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
return true; |
| 116 |
}, |
| 117 |
|
| 118 |
copyState: function(keyPressed){ |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
return this.copyOnly || keyPressed; |
| 127 |
}, |
| 128 |
destroy: function(){ |
| 129 |
|
| 130 |
|
| 131 |
this.inherited(arguments); |
| 132 |
var h; |
| 133 |
while(h = this.topics.pop()){ |
| 134 |
h.remove(); |
| 135 |
} |
| 136 |
this.targetAnchor = null; |
| 137 |
}, |
| 138 |
|
| 139 |
_onDragMouse: function(e, firstTime){ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
var m = DNDManager.manager(), |
| 151 |
oldTarget = this.targetAnchor, |
| 152 |
newTarget = this.current, |
| 153 |
oldDropPosition = this.dropPosition; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
var newDropPosition = "Over"; |
| 158 |
if(newTarget && this.betweenThreshold > 0){ |
| 159 |
|
| 160 |
if(!this.targetBox || oldTarget != newTarget){ |
| 161 |
this.targetBox = domGeometry.position(newTarget.rowNode, true); |
| 162 |
} |
| 163 |
if((e.pageY - this.targetBox.y) <= this.betweenThreshold){ |
| 164 |
newDropPosition = "Before"; |
| 165 |
}else if((e.pageY - this.targetBox.y) >= (this.targetBox.h - this.betweenThreshold)){ |
| 166 |
newDropPosition = "After"; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if(firstTime || newTarget != oldTarget || newDropPosition != oldDropPosition){ |
| 171 |
if(oldTarget){ |
| 172 |
this._removeItemClass(oldTarget.rowNode, oldDropPosition); |
| 173 |
} |
| 174 |
if(newTarget){ |
| 175 |
this._addItemClass(newTarget.rowNode, newDropPosition); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
if(!newTarget){ |
| 180 |
m.canDrop(false); |
| 181 |
}else if(newTarget == this.tree.rootNode && newDropPosition != "Over"){ |
| 182 |
|
| 183 |
m.canDrop(false); |
| 184 |
}else{ |
| 185 |
|
| 186 |
|
| 187 |
var dropOntoSelf = false, |
| 188 |
dropOntoParent = false; |
| 189 |
if(m.source == this){ |
| 190 |
dropOntoParent = (newDropPosition === "Over"); |
| 191 |
for(var dragId in this.selection){ |
| 192 |
var dragNode = this.selection[dragId]; |
| 193 |
if(dragNode.item === newTarget.item){ |
| 194 |
dropOntoSelf = true; |
| 195 |
break; |
| 196 |
} |
| 197 |
if(dragNode.getParent().id !== newTarget.id){ |
| 198 |
dropOntoParent = false; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
m.canDrop( |
| 203 |
!dropOntoSelf && !dropOntoParent && |
| 204 |
!this._isParentChildDrop(m.source, newTarget.rowNode) && |
| 205 |
this.checkItemAcceptance(newTarget.rowNode, m.source, newDropPosition.toLowerCase()) |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
this.targetAnchor = newTarget; |
| 210 |
this.dropPosition = newDropPosition; |
| 211 |
} |
| 212 |
}, |
| 213 |
|
| 214 |
onMouseMove: function(e){ |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if(this.isDragging && this.targetState == "Disabled"){ |
| 222 |
return; |
| 223 |
} |
| 224 |
this.inherited(arguments); |
| 225 |
var m = DNDManager.manager(); |
| 226 |
if(this.isDragging){ |
| 227 |
this._onDragMouse(e); |
| 228 |
}else{ |
| 229 |
if(this.mouseDown && this.isSource && |
| 230 |
(Math.abs(e.pageX - this._lastX) >= this.dragThreshold || Math.abs(e.pageY - this._lastY) >= this.dragThreshold)){ |
| 231 |
var nodes = this.getSelectedTreeNodes(); |
| 232 |
if(nodes.length){ |
| 233 |
if(nodes.length > 1){ |
| 234 |
|
| 235 |
var seen = this.selection, i = 0, r = [], n, p; |
| 236 |
nextitem: while((n = nodes[i++])){ |
| 237 |
for(p = n.getParent(); p && p !== this.tree; p = p.getParent()){ |
| 238 |
if(seen[p.id]){ |
| 239 |
continue nextitem; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
r.push(n); |
| 244 |
} |
| 245 |
nodes = r; |
| 246 |
} |
| 247 |
nodes = array.map(nodes, function(n){ |
| 248 |
return n.domNode |
| 249 |
}); |
| 250 |
m.startDrag(this, nodes, this.copyState(dndCommon.getCopyKeyState(e))); |
| 251 |
this._onDragMouse(e, true); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
}, |
| 256 |
|
| 257 |
onMouseDown: function(e){ |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
if(e.type == "touchstart" || mouse.isLeft(e)){ |
| 266 |
this.mouseDown = true; |
| 267 |
this.mouseButton = e.button; |
| 268 |
this._lastX = e.pageX; |
| 269 |
this._lastY = e.pageY; |
| 270 |
} |
| 271 |
|
| 272 |
this.inherited(arguments); |
| 273 |
}, |
| 274 |
|
| 275 |
onMouseUp: function(e){ |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if(this.mouseDown){ |
| 283 |
this.mouseDown = false; |
| 284 |
this.inherited(arguments); |
| 285 |
} |
| 286 |
}, |
| 287 |
|
| 288 |
onMouseOut: function(){ |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
this.inherited(arguments); |
| 294 |
this._unmarkTargetAnchor(); |
| 295 |
}, |
| 296 |
|
| 297 |
checkItemAcceptance: function(){ |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
return true; |
| 314 |
}, |
| 315 |
|
| 316 |
|
| 317 |
onDndSourceOver: function(source){ |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
if(this != source){ |
| 325 |
this.mouseDown = false; |
| 326 |
this._unmarkTargetAnchor(); |
| 327 |
}else if(this.isDragging){ |
| 328 |
var m = DNDManager.manager(); |
| 329 |
m.canDrop(false); |
| 330 |
} |
| 331 |
}, |
| 332 |
onDndStart: function(source, nodes, copy){ |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
if(this.isSource){ |
| 345 |
this._changeState("Source", this == source ? (copy ? "Copied" : "Moved") : ""); |
| 346 |
} |
| 347 |
var accepted = this.checkAcceptance(source, nodes); |
| 348 |
|
| 349 |
this._changeState("Target", accepted ? "" : "Disabled"); |
| 350 |
|
| 351 |
if(this == source){ |
| 352 |
DNDManager.manager().overSource(this); |
| 353 |
} |
| 354 |
|
| 355 |
this.isDragging = true; |
| 356 |
}, |
| 357 |
|
| 358 |
itemCreator: function(nodes ){ |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
return array.map(nodes, function(node){ |
| 379 |
return { |
| 380 |
"id": node.id, |
| 381 |
"name": node.textContent || node.innerText || "" |
| 382 |
}; |
| 383 |
}); |
| 384 |
}, |
| 385 |
|
| 386 |
onDndDrop: function(source, nodes, copy){ |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
if(this.containerState == "Over"){ |
| 401 |
var tree = this.tree, |
| 402 |
model = tree.model, |
| 403 |
target = this.targetAnchor, |
| 404 |
doExpand = false; |
| 405 |
|
| 406 |
this.isDragging = false; |
| 407 |
|
| 408 |
|
| 409 |
var newParentItem; |
| 410 |
var insertIndex; |
| 411 |
var before; |
| 412 |
newParentItem = (target && target.item) || tree.item; |
| 413 |
if(this.dropPosition == "Before" || this.dropPosition == "After"){ |
| 414 |
|
| 415 |
|
| 416 |
newParentItem = (target.getParent() && target.getParent().item) || tree.item; |
| 417 |
|
| 418 |
insertIndex = target.getIndexInParent(); |
| 419 |
if(this.dropPosition == "After"){ |
| 420 |
insertIndex = target.getIndexInParent() + 1; |
| 421 |
before = target.getNextSibling() && target.getNextSibling().item; |
| 422 |
}else{ |
| 423 |
before = target.item; |
| 424 |
} |
| 425 |
}else{ |
| 426 |
newParentItem = (target && target.item) || tree.item; |
| 427 |
doExpand = true; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
var newItemsParams; |
| 433 |
|
| 434 |
array.forEach(nodes, function(node, idx){ |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
var sourceItem = source.getItem(node.id); |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
if(array.indexOf(sourceItem.type, "treeNode") != -1){ |
| 444 |
var childTreeNode = sourceItem.data, |
| 445 |
childItem = childTreeNode.item, |
| 446 |
oldParentItem = childTreeNode.getParent().item; |
| 447 |
} |
| 448 |
|
| 449 |
if(source == this){ |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
if(typeof insertIndex == "number"){ |
| 456 |
if(newParentItem == oldParentItem && childTreeNode.getIndexInParent() < insertIndex){ |
| 457 |
insertIndex -= 1; |
| 458 |
} |
| 459 |
} |
| 460 |
model.pasteItem(childItem, oldParentItem, newParentItem, copy, insertIndex, before); |
| 461 |
}else if(model.isItem(childItem)){ |
| 462 |
|
| 463 |
|
| 464 |
model.pasteItem(childItem, oldParentItem, newParentItem, copy, insertIndex, before); |
| 465 |
}else{ |
| 466 |
|
| 467 |
|
| 468 |
if(!newItemsParams){ |
| 469 |
newItemsParams = this.itemCreator(nodes, target.rowNode, source); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
model.newItem(newItemsParams[idx], newParentItem, insertIndex, before); |
| 474 |
} |
| 475 |
}, this); |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if(doExpand) { |
| 480 |
this.tree._expandNode(target); |
| 481 |
} |
| 482 |
} |
| 483 |
this.onDndCancel(); |
| 484 |
}, |
| 485 |
|
| 486 |
onDndCancel: function(){ |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
this._unmarkTargetAnchor(); |
| 492 |
this.isDragging = false; |
| 493 |
this.mouseDown = false; |
| 494 |
delete this.mouseButton; |
| 495 |
this._changeState("Source", ""); |
| 496 |
this._changeState("Target", ""); |
| 497 |
}, |
| 498 |
|
| 499 |
|
| 500 |
onOverEvent: function(){ |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
this.inherited(arguments); |
| 506 |
DNDManager.manager().overSource(this); |
| 507 |
}, |
| 508 |
onOutEvent: function(){ |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
this._unmarkTargetAnchor(); |
| 514 |
var m = DNDManager.manager(); |
| 515 |
if(this.isDragging){ |
| 516 |
m.canDrop(false); |
| 517 |
} |
| 518 |
m.outSource(this); |
| 519 |
|
| 520 |
this.inherited(arguments); |
| 521 |
}, |
| 522 |
|
| 523 |
_isParentChildDrop: function(source, targetRow){ |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
if(!source.tree || source.tree != this.tree){ |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
var root = source.tree.domNode; |
| 545 |
var ids = source.selection; |
| 546 |
|
| 547 |
var node = targetRow.parentNode; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
while(node != root && !ids[node.id]){ |
| 552 |
node = node.parentNode; |
| 553 |
} |
| 554 |
|
| 555 |
return node.id && ids[node.id]; |
| 556 |
}, |
| 557 |
|
| 558 |
_unmarkTargetAnchor: function(){ |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
if(!this.targetAnchor){ |
| 564 |
return; |
| 565 |
} |
| 566 |
this._removeItemClass(this.targetAnchor.rowNode, this.dropPosition); |
| 567 |
this.targetAnchor = null; |
| 568 |
this.targetBox = null; |
| 569 |
this.dropPosition = null; |
| 570 |
}, |
| 571 |
|
| 572 |
_markDndStatus: function(copy){ |
| 573 |
|
| 574 |
|
| 575 |
this._changeState("Source", copy ? "Copied" : "Moved"); |
| 576 |
} |
| 577 |
}); |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
return dndSource; |
| 584 |
}); |