]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/dnd/Manager.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / dnd / Manager.js
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");
4
5 dojo.require("dojo.dnd.common");
6 dojo.require("dojo.dnd.autoscroll");
7 dojo.require("dojo.dnd.Avatar");
8
9 dojo.declare("dojo.dnd.Manager", null, {
10         // summary: the manager of DnD operations (usually a singleton)
11         constructor: function(){
12                 this.avatar  = null;
13                 this.source = null;
14                 this.nodes = [];
15                 this.copy  = true;
16                 this.target = null;
17                 this.canDropFlag = false;
18                 this.events = [];
19         },
20
21         // avatar's offset from the mouse
22         OFFSET_X: 16,
23         OFFSET_Y: 16,
24         
25         // methods
26         overSource: function(source){
27                 // summary: called when a source detected a mouse-over conditiion
28                 // source: Object: the reporter
29                 if(this.avatar){
30                         this.target = (source && source.targetState != "Disabled") ? source : null;
31                         this.avatar.update();
32                 }
33                 dojo.publish("/dnd/source/over", [source]);
34         },
35         outSource: function(source){
36                 // summary: called when a source detected a mouse-out conditiion
37                 // source: Object: the reporter
38                 if(this.avatar){
39                         if(this.target == source){
40                                 this.target = null;
41                                 this.canDropFlag = false;
42                                 this.avatar.update();
43                                 dojo.publish("/dnd/source/over", [null]);
44                         }
45                 }else{
46                         dojo.publish("/dnd/source/over", [null]);
47                 }
48         },
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
54                 this.source = source;
55                 this.nodes  = nodes;
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]);
60                 this.events = [
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")
65                 ];
66                 var c = "dojoDnd" + (copy ? "Copy" : "Move");
67                 dojo.addClass(dojo.body(), c); 
68         },
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;
74                         this.avatar.update();
75                 }
76         },
77         stopDrag: function(){
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);
82                 this.events = [];
83                 this.avatar.destroy();
84                 this.avatar = null;
85                 this.source = null;
86                 this.nodes = [];
87         },
88         makeAvatar: function(){
89                 // summary: makes the avatar, it is separate to be overwritten dynamically, if needed
90                 return new dojo.dnd.Avatar(this);
91         },
92         updateAvatar: function(){
93                 // summary: updates the avatar, it is separate to be overwritten dynamically, if needed
94                 this.avatar.update();
95         },
96         // mouse event processors
97         onMouseMove: function(e){
98                 // summary: event processor for onmousemove
99                 // e: Event: mouse event
100                 var a = this.avatar;
101                 if(a){
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);
110                         }
111                 }
112         },
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);
121                         }else{
122                                 dojo.publish("/dnd/cancel");
123                         }
124                         this.stopDrag();
125                 }
126         },
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
132                 if(this.avatar){
133                         switch(e.keyCode){
134                                 case dojo.keys.CTRL:
135                                         var copy = Boolean(this.source.copyState(true));
136                                         if(this.copy != copy){ 
137                                                 this._setCopyStatus(copy);
138                                         }
139                                         break;
140                                 case dojo.keys.ESCAPE:
141                                         dojo.publish("/dnd/cancel");
142                                         this.stopDrag();
143                                         break;
144                         }
145                 }
146         },
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);
154                         }
155                 }
156         },
157         // utilities
158         _setCopyStatus: function(copy){
159                 // summary: changes the copy status
160                 // copy: Boolean: the copy status
161                 this.copy = copy;
162                 this.source._markDndStatus(this.copy);
163                 this.updateAvatar();
164                 dojo.removeClass(dojo.body(), "dojoDnd" + (this.copy ? "Move" : "Copy"));
165                 dojo.addClass(dojo.body(), "dojoDnd" + (this.copy ? "Copy" : "Move"));
166         }
167 });
168
169 // summary: the manager singleton variable, can be overwritten, if needed
170 dojo.dnd._manager = null;
171
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();
176         }
177         return dojo.dnd._manager;       // Object
178 };
179
180 }