Debian Codegraph @

dojox/mdnd/Moveable.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/array",
4 "dojo/_base/connect",
5 "dojo/_base/event",
6 "dojo/_base/sniff",
7 "dojo/touch",
8 "dojo/dom",
9 "dojo/dom-geometry",
10 "dojo/dom-style",
11 "./AutoScroll"
12 ],function(declare, array, connect, event, sniff, touch, dom, geom, domStyle, AutoScroll){
13 return declare(
14 "dojox.mdnd.Moveable",
15 null,
16 {
17 // summary:
18 // Allow end-users to track a DOM node into the web page
19
20 // handle: DOMNode
21 // The node on which the user clicks to drag the main node.
22 handle: null,
23
24 // skip: Boolean
25 // A flag to control a drag action if a form element has been focused.
26 // If true, the drag action is not executed.
27 skip: true,
28
29 // dragDistance: Integer
30 // The user clicks on the handle, but the drag action will really begin
31 // if he tracks the main node to more than 3 pixels.
32 dragDistance: 3,
33
34 constructor: function(/*Object*/params, /*DOMNode*/node){
35 // summary:
36 // Configure parameters and listen to mousedown events from handle
37 // node.
38 // params:
39 // Hash of parameters
40 // node:
41 // The draggable node
42
43 //console.log("dojox.mdnd.Moveable ::: constructor");
44 this.node = dom.byId(node);
45
46 this.d = this.node.ownerDocument;
47
48 if(!params){ params = {}; }
49 this.handle = params.handle ? dom.byId(params.handle) : null;
50 if(!this.handle){ this.handle = this.node; }
51 this.skip = params.skip;
52 this.events = [
53 connect.connect(this.handle, touch.press, this, "onMouseDown")
54 ];
55 if(AutoScroll.autoScroll){
56 this.autoScroll = AutoScroll.autoScroll;
57 }
58
59 },
60
61 isFormElement: function(/*DOMEvent*/ e){
62 // summary:
63 // identify the type of target node associated with a DOM event.
64 // e:
65 // a DOM event
66 // returns:
67 // if true, the target is one of those specific nodes.
68
69 //console.log("dojox.mdnd.Moveable ::: isFormElement");
70 var t = e.target;
71 if(t.nodeType == 3 /*TEXT_NODE*/){
72 t = t.parentNode;
73 }
74 return " a button textarea input select option ".indexOf(" " + t.tagName.toLowerCase() + " ") >= 0; // Boolean
75 },
76
77 onMouseDown: function(/*DOMEvent*/e){
78 // summary:
79 // Occurs when the user clicks on the handle node.
80 // Skip the drag action if a specific node is targeted.
81 // Listens to mouseup and mousemove events on to the HTML document.
82 // e:
83 // a DOM event
84 // tags:
85 // callback
86
87 //console.log("dojox.mdnd.Moveable ::: onMouseDown");
88 if(this._isDragging){ return;}
89 var isLeftButton = (e.which || e.button) == 1;
90 if(!isLeftButton){
91 return;
92 }
93 if(this.skip && this.isFormElement(e)){ return; }
94 if(this.autoScroll){
95 this.autoScroll.setAutoScrollNode(this.node);
96 this.autoScroll.setAutoScrollMaxPage();
97 }
98 this.events.push(connect.connect(this.d, touch.release, this, "onMouseUp"));
99 this.events.push(connect.connect(this.d, touch.move, this, "onFirstMove"));
100 this._selectStart = connect.connect(dojo.body(), "onselectstart", event.stop);
101 this._firstX = e.clientX;
102 this._firstY = e.clientY;
103 event.stop(e);
104 },
105
106 onFirstMove: function(/*DOMEvent*/e){
107 // summary:
108 // Occurs when the user moves the mouse after clicking on the
109 // handle.
110 // Determinate when the drag action will have to begin (see
111 // dragDistance).
112 // e:
113 // A DOM event
114 // tags:
115 // callback
116
117 //console.log("dojox.mdnd.Moveable ::: onFirstMove");
118 event.stop(e);
119 var d = (this._firstX - e.clientX) * (this._firstX - e.clientX)
120 + (this._firstY - e.clientY) * (this._firstY - e.clientY);
121 if(d > this.dragDistance * this.dragDistance){
122 this._isDragging = true;
123 connect.disconnect(this.events.pop());
124 domStyle.set(this.node, "width", geom.getContentBox(this.node).w + "px");
125 this.initOffsetDrag(e);
126 this.events.push(connect.connect(this.d, touch.move, this, "onMove"));
127 }
128 },
129
130 initOffsetDrag: function(/*DOMEvent*/e){
131 // summary:
132 // Initialize the gap between main node coordinates and the clicked point.
133 // Call the onDragStart method.
134 // e:
135 // A DOM event
136
137 //console.log("dojox.mdnd.Moveable ::: initOffsetDrag");
138 this.offsetDrag = { 'l': e.pageX, 't': e.pageY };
139 var s = this.node.style;
140 var position = geom.position(this.node, true);
141 /*if(s.position == "relative" || s.position == ""){
142 s.position = "absolute"; // enforcing the absolute mode
143 }*/
144 this.offsetDrag.l = position.x - this.offsetDrag.l;
145 this.offsetDrag.t = position.y - this.offsetDrag.t;
146 var coords = {
147 'x': position.x,
148 'y': position.y
149 };
150 this.size = {
151 'w': position.w,
152 'h': position.h
153 };
154 // method to catch
155 this.onDragStart(this.node, coords, this.size);
156 },
157
158 onMove: function(/*DOMEvent*/e){
159 // summary:
160 // Occurs when the user moves the mouse.
161 // Calls the onDrag method.
162 // e:
163 // a DOM event
164 // tags:
165 // callback
166
167 //console.log("dojox.mdnd.Moveable ::: onMove");
168 event.stop(e);
169 // hack to avoid too many calls to onMove in IE8 causing sometimes slowness
170 if(sniff("ie") == 8 && new Date() - this.date < 20){
171 return;
172 }
173 if(this.autoScroll){
174 this.autoScroll.checkAutoScroll(e);
175 }
176 var coords = {
177 'x': this.offsetDrag.l + e.pageX,
178 'y': this.offsetDrag.t + e.pageY
179 };
180 var s = this.node.style;
181 s.left = coords.x + "px";
182 s.top = coords.y + "px";
183
184 // method to catch
185 this.onDrag(this.node, coords, this.size, {'x':e.pageX, 'y':e.pageY});
186 if(sniff("ie") == 8){
187 this.date = new Date();
188 }
189 },
190
191 onMouseUp: function(/*DOMEvent*/e){
192 // summary:
193 // Occurs when the user releases the mouse
194 // Calls the onDragEnd method.
195 // e:
196 // a DOM event
197
198 if (this._isDragging){
199 event.stop(e);
200 this._isDragging = false;
201 if(this.autoScroll){
202 this.autoScroll.stopAutoScroll();
203 }
204 delete this.onMove;
205 this.onDragEnd(this.node);
206 this.node.focus();
207 }
208 connect.disconnect(this.events.pop());
209 connect.disconnect(this.events.pop());
210 connect.disconnect(this._selectStart);
211 this._selectStart = null;
212 },
213
214 onDragStart: function(/*DOMNode*/node, /*Object*/coords, /*Object*/size){
215 // summary:
216 // Stub function.
217 // Notes : border box model
218 // node:
219 // a DOM node
220 // coords:
221 // absolute position of the main node
222 // size:
223 // an object encapsulating width an height values
224 // tags:
225 // callback
226
227 },
228
229 onDragEnd: function(/*DOMNode*/node){
230 // summary:
231 // Stub function
232 // Notes : Coordinates don't contain margins
233 // node:
234 // a DOM node
235 // tags:
236 // callback
237
238 },
239
240 onDrag: function(/*DOMNode*/node, /*Object*/coords, /*Object*/size, /*Object*/mousePosition){
241 // summary:
242 // Stub function.
243 // Notes : border box model for size value, margin box model for coordinates
244 // node:
245 // a DOM node
246 // coords:
247 // position of the main node (equals to css left/top properties)
248 // size:
249 // an object encapsulating width and height values
250 // mousePosition:
251 // coordiantes of mouse
252 // tags:
253 // callback
254
255 },
256
257 destroy: function(){
258 // summary:
259 // Delecte associated events
260
261 // console.log("dojox.mdnd.Moveable ::: destroy");
262 array.forEach(this.events, connect.disconnect);
263 this.events = this.node = null;
264 }
265 });
266 });