1 if(!dojo._hasResource["dojo.dnd.Manager"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.dnd.Manager"] = true;
3 dojo.provide("dojo.dnd.Manager");
5 dojo.require("dojo.dnd.common");
6 dojo.require("dojo.dnd.autoscroll");
7 dojo.require("dojo.dnd.Avatar");
9 dojo.declare("dojo.dnd.Manager", null, {
10 // summary: the manager of DnD operations (usually a singleton)
11 constructor: function(){
17 this.canDropFlag = false;
21 // avatar's offset from the mouse
26 overSource: function(source){
27 // summary: called when a source detected a mouse-over conditiion
28 // source: Object: the reporter
30 this.target = (source && source.targetState != "Disabled") ? source : null;
33 dojo.publish("/dnd/source/over", [source]);
35 outSource: function(source){
36 // summary: called when a source detected a mouse-out conditiion
37 // source: Object: the reporter
39 if(this.target == source){
41 this.canDropFlag = false;
43 dojo.publish("/dnd/source/over", [null]);
46 dojo.publish("/dnd/source/over", [null]);
49 startDrag: function(source, nodes, copy){
50 // summary: called to initiate the DnD operation
51 // source: Object: the source which provides items
52 // nodes: Array: the list of transferred items
53 // copy: Boolean: copy items, if true, move items otherwise
56 this.copy = Boolean(copy); // normalizing to true boolean
57 this.avatar = this.makeAvatar();
58 dojo.body().appendChild(this.avatar.node);
59 dojo.publish("/dnd/start", [source, nodes, this.copy]);
61 dojo.connect(dojo.doc, "onmousemove", this, "onMouseMove"),
62 dojo.connect(dojo.doc, "onmouseup", this, "onMouseUp"),
63 dojo.connect(dojo.doc, "onkeydown", this, "onKeyDown"),
64 dojo.connect(dojo.doc, "onkeyup", this, "onKeyUp")
66 var c = "dojoDnd" + (copy ? "Copy" : "Move");
67 dojo.addClass(dojo.body(), c);
69 canDrop: function(flag){
70 // summary: called to notify if the current target can accept items
71 var canDropFlag = Boolean(this.target && flag);
72 if(this.canDropFlag != canDropFlag){
73 this.canDropFlag = canDropFlag;
78 // summary: stop the DnD in progress
79 dojo.removeClass(dojo.body(), "dojoDndCopy");
80 dojo.removeClass(dojo.body(), "dojoDndMove");
81 dojo.forEach(this.events, dojo.disconnect);
83 this.avatar.destroy();
88 makeAvatar: function(){
89 // summary: makes the avatar, it is separate to be overwritten dynamically, if needed
90 return new dojo.dnd.Avatar(this);
92 updateAvatar: function(){
93 // summary: updates the avatar, it is separate to be overwritten dynamically, if needed
96 // mouse event processors
97 onMouseMove: function(e){
98 // summary: event processor for onmousemove
99 // e: Event: mouse event
102 //dojo.dnd.autoScrollNodes(e);
103 dojo.dnd.autoScroll(e);
104 var s = a.node.style;
105 s.left = (e.pageX + this.OFFSET_X) + "px";
106 s.top = (e.pageY + this.OFFSET_Y) + "px";
107 var copy = Boolean(this.source.copyState(dojo.dnd.getCopyKeyState(e)));
108 if(this.copy != copy){
109 this._setCopyStatus(copy);
113 onMouseUp: function(e){
114 // summary: event processor for onmouseup
115 // e: Event: mouse event
116 if(this.avatar && (!("mouseButton" in this.source) || this.source.mouseButton == e.button)){
117 if(this.target && this.canDropFlag){
118 var params = [this.source, this.nodes, Boolean(this.source.copyState(dojo.dnd.getCopyKeyState(e))), this.target];
119 dojo.publish("/dnd/drop/before", params);
120 dojo.publish("/dnd/drop", params);
122 dojo.publish("/dnd/cancel");
127 // keyboard event processors
128 onKeyDown: function(e){
129 // summary: event processor for onkeydown:
130 // watching for CTRL for copy/move status, watching for ESCAPE to cancel the drag
131 // e: Event: keyboard event
135 var copy = Boolean(this.source.copyState(true));
136 if(this.copy != copy){
137 this._setCopyStatus(copy);
140 case dojo.keys.ESCAPE:
141 dojo.publish("/dnd/cancel");
147 onKeyUp: function(e){
148 // summary: event processor for onkeyup, watching for CTRL for copy/move status
149 // e: Event: keyboard event
150 if(this.avatar && e.keyCode == dojo.keys.CTRL){
151 var copy = Boolean(this.source.copyState(false));
152 if(this.copy != copy){
153 this._setCopyStatus(copy);
158 _setCopyStatus: function(copy){
159 // summary: changes the copy status
160 // copy: Boolean: the copy status
162 this.source._markDndStatus(this.copy);
164 dojo.removeClass(dojo.body(), "dojoDnd" + (this.copy ? "Move" : "Copy"));
165 dojo.addClass(dojo.body(), "dojoDnd" + (this.copy ? "Copy" : "Move"));
169 // summary: the manager singleton variable, can be overwritten, if needed
170 dojo.dnd._manager = null;
172 dojo.dnd.manager = function(){
173 // summary: returns the current DnD manager, creates one if it is not created yet
174 if(!dojo.dnd._manager){
175 dojo.dnd._manager = new dojo.dnd.Manager();
177 return dojo.dnd._manager; // Object