123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- define("dijit/tree/dndSource", [
- "dojo/_base/array",
- "dojo/_base/connect",
- "dojo/_base/declare",
- "dojo/dom-class",
- "dojo/dom-geometry",
- "dojo/_base/lang",
- "dojo/on",
- "dojo/touch",
- "dojo/topic",
- "dojo/dnd/Manager",
- "./_dndSelector"
- ], function(array, connect, declare, domClass, domGeometry, lang, on, touch, topic, DNDManager, _dndSelector){
- return declare("dijit.tree.dndSource", _dndSelector, {
-
-
-
-
- isSource: true,
-
-
-
- accept: ["text", "treeNode"],
-
-
- copyOnly: false,
-
-
- dragThreshold: 5,
-
-
- betweenThreshold: 0,
- constructor: function(/*dijit.Tree*/ tree, /*dijit.tree.__SourceArgs*/ params){
-
-
-
-
- if(!params){ params = {}; }
- lang.mixin(this, params);
- this.isSource = typeof params.isSource == "undefined" ? true : params.isSource;
- var type = params.accept instanceof Array ? params.accept : ["text", "treeNode"];
- this.accept = null;
- if(type.length){
- this.accept = {};
- for(var i = 0; i < type.length; ++i){
- this.accept[type[i]] = 1;
- }
- }
-
- this.isDragging = false;
- this.mouseDown = false;
- this.targetAnchor = null;
- this.targetBox = null;
- this.dropPosition = "";
- this._lastX = 0;
- this._lastY = 0;
-
- this.sourceState = "";
- if(this.isSource){
- domClass.add(this.node, "dojoDndSource");
- }
- this.targetState = "";
- if(this.accept){
- domClass.add(this.node, "dojoDndTarget");
- }
-
- this.topics = [
- topic.subscribe("/dnd/source/over", lang.hitch(this, "onDndSourceOver")),
- topic.subscribe("/dnd/start", lang.hitch(this, "onDndStart")),
- topic.subscribe("/dnd/drop", lang.hitch(this, "onDndDrop")),
- topic.subscribe("/dnd/cancel", lang.hitch(this, "onDndCancel"))
- ];
- },
-
- checkAcceptance: function(/*===== source, nodes =====*/){
-
-
-
-
-
-
-
-
-
- return true;
- },
- copyState: function(keyPressed){
-
-
-
-
-
-
-
- return this.copyOnly || keyPressed;
- },
- destroy: function(){
-
-
- this.inherited(arguments);
- var h;
- while(h = this.topics.pop()){ h.remove(); }
- this.targetAnchor = null;
- },
- _onDragMouse: function(e){
-
-
-
- var m = DNDManager.manager(),
- oldTarget = this.targetAnchor,
- newTarget = this.current,
- oldDropPosition = this.dropPosition;
-
-
- var newDropPosition = "Over";
- if(newTarget && this.betweenThreshold > 0){
-
- if(!this.targetBox || oldTarget != newTarget){
- this.targetBox = domGeometry.position(newTarget.rowNode, true);
- }
- if((e.pageY - this.targetBox.y) <= this.betweenThreshold){
- newDropPosition = "Before";
- }else if((e.pageY - this.targetBox.y) >= (this.targetBox.h - this.betweenThreshold)){
- newDropPosition = "After";
- }
- }
- if(newTarget != oldTarget || newDropPosition != oldDropPosition){
- if(oldTarget){
- this._removeItemClass(oldTarget.rowNode, oldDropPosition);
- }
- if(newTarget){
- this._addItemClass(newTarget.rowNode, newDropPosition);
- }
-
- if(!newTarget){
- m.canDrop(false);
- }else if(newTarget == this.tree.rootNode && newDropPosition != "Over"){
-
- m.canDrop(false);
- }else{
-
- var model = this.tree.model,
- sameId = false;
- if(m.source == this){
- for(var dragId in this.selection){
- var dragNode = this.selection[dragId];
- if(dragNode.item === newTarget.item){
- sameId = true;
- break;
- }
- }
- }
- if(sameId){
- m.canDrop(false);
- }else if(this.checkItemAcceptance(newTarget.rowNode, m.source, newDropPosition.toLowerCase())
- && !this._isParentChildDrop(m.source, newTarget.rowNode)){
- m.canDrop(true);
- }else{
- m.canDrop(false);
- }
- }
- this.targetAnchor = newTarget;
- this.dropPosition = newDropPosition;
- }
- },
- onMouseMove: function(e){
-
-
-
-
-
-
- if(this.isDragging && this.targetState == "Disabled"){ return; }
- this.inherited(arguments);
- var m = DNDManager.manager();
- if(this.isDragging){
- this._onDragMouse(e);
- }else{
- if(this.mouseDown && this.isSource &&
- (Math.abs(e.pageX-this._lastX)>=this.dragThreshold || Math.abs(e.pageY-this._lastY)>=this.dragThreshold)){
- var nodes = this.getSelectedTreeNodes();
- if(nodes.length){
- if(nodes.length > 1){
-
- var seen = this.selection, i = 0, r = [], n, p;
- nextitem: while((n = nodes[i++])){
- for(p = n.getParent(); p && p !== this.tree; p = p.getParent()){
- if(seen[p.id]){
- continue nextitem;
- }
- }
-
- r.push(n);
- }
- nodes = r;
- }
- nodes = array.map(nodes, function(n){return n.domNode});
- m.startDrag(this, nodes, this.copyState(connect.isCopyKey(e)));
- }
- }
- }
- },
- onMouseDown: function(e){
-
-
-
-
-
-
- this.mouseDown = true;
- this.mouseButton = e.button;
- this._lastX = e.pageX;
- this._lastY = e.pageY;
- this.inherited(arguments);
- },
- onMouseUp: function(e){
-
-
-
-
-
-
- if(this.mouseDown){
- this.mouseDown = false;
- this.inherited(arguments);
- }
- },
- onMouseOut: function(){
-
-
-
-
- this.inherited(arguments);
- this._unmarkTargetAnchor();
- },
- checkItemAcceptance: function(/*===== target, source, position =====*/){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return true;
- },
-
- onDndSourceOver: function(source){
-
-
-
-
-
-
- if(this != source){
- this.mouseDown = false;
- this._unmarkTargetAnchor();
- }else if(this.isDragging){
- var m = DNDManager.manager();
- m.canDrop(false);
- }
- },
- onDndStart: function(source, nodes, copy){
-
-
-
-
-
-
-
-
-
-
- if(this.isSource){
- this._changeState("Source", this == source ? (copy ? "Copied" : "Moved") : "");
- }
- var accepted = this.checkAcceptance(source, nodes);
- this._changeState("Target", accepted ? "" : "Disabled");
- if(this == source){
- DNDManager.manager().overSource(this);
- }
- this.isDragging = true;
- },
- itemCreator: function(nodes /*===== , target, source =====*/){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return array.map(nodes, function(node){
- return {
- "id": node.id,
- "name": node.textContent || node.innerText || ""
- };
- });
- },
- onDndDrop: function(source, nodes, copy){
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(this.containerState == "Over"){
- var tree = this.tree,
- model = tree.model,
- target = this.targetAnchor;
- this.isDragging = false;
-
- var newParentItem;
- var insertIndex;
- newParentItem = (target && target.item) || tree.item;
- if(this.dropPosition == "Before" || this.dropPosition == "After"){
-
-
- newParentItem = (target.getParent() && target.getParent().item) || tree.item;
-
- insertIndex = target.getIndexInParent();
- if(this.dropPosition == "After"){
- insertIndex = target.getIndexInParent() + 1;
- }
- }else{
- newParentItem = (target && target.item) || tree.item;
- }
-
-
- var newItemsParams;
- array.forEach(nodes, function(node, idx){
-
-
-
- var sourceItem = source.getItem(node.id);
-
-
-
- if(array.indexOf(sourceItem.type, "treeNode") != -1){
- var childTreeNode = sourceItem.data,
- childItem = childTreeNode.item,
- oldParentItem = childTreeNode.getParent().item;
- }
- if(source == this){
-
-
-
-
- if(typeof insertIndex == "number"){
- if(newParentItem == oldParentItem && childTreeNode.getIndexInParent() < insertIndex){
- insertIndex -= 1;
- }
- }
- model.pasteItem(childItem, oldParentItem, newParentItem, copy, insertIndex);
- }else if(model.isItem(childItem)){
-
-
- model.pasteItem(childItem, oldParentItem, newParentItem, copy, insertIndex);
- }else{
-
-
- if(!newItemsParams){
- newItemsParams = this.itemCreator(nodes, target.rowNode, source);
- }
-
- model.newItem(newItemsParams[idx], newParentItem, insertIndex);
- }
- }, this);
-
-
- this.tree._expandNode(target);
- }
- this.onDndCancel();
- },
- onDndCancel: function(){
-
-
-
-
- this._unmarkTargetAnchor();
- this.isDragging = false;
- this.mouseDown = false;
- delete this.mouseButton;
- this._changeState("Source", "");
- this._changeState("Target", "");
- },
-
- onOverEvent: function(){
-
-
-
-
- this.inherited(arguments);
- DNDManager.manager().overSource(this);
- },
- onOutEvent: function(){
-
-
-
-
- this._unmarkTargetAnchor();
- var m = DNDManager.manager();
- if(this.isDragging){
- m.canDrop(false);
- }
- m.outSource(this);
- this.inherited(arguments);
- },
- _isParentChildDrop: function(source, targetRow){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if(!source.tree || source.tree != this.tree){
- return false;
- }
- var root = source.tree.domNode;
- var ids = source.selection;
- var node = targetRow.parentNode;
-
-
- while(node != root && !ids[node.id]){
- node = node.parentNode;
- }
- return node.id && ids[node.id];
- },
- _unmarkTargetAnchor: function(){
-
-
-
-
- if(!this.targetAnchor){ return; }
- this._removeItemClass(this.targetAnchor.rowNode, this.dropPosition);
- this.targetAnchor = null;
- this.targetBox = null;
- this.dropPosition = null;
- },
- _markDndStatus: function(copy){
-
-
- this._changeState("Source", copy ? "Copied" : "Moved");
- }
- });
- });
|