|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0b1 |
|
6 build: 1163 |
|
7 */ |
|
8 YUI.add('dd-ddm-base', function(Y) { |
|
9 |
|
10 |
|
11 /** |
|
12 * Provides the base Drag Drop Manger required for making a Node draggable. |
|
13 * @module dd |
|
14 * @submodule dd-ddm-base |
|
15 */ |
|
16 /** |
|
17 * Provides the base Drag Drop Manger required for making a Node draggable. |
|
18 * @class DDM |
|
19 * @extends Base |
|
20 * @constructor |
|
21 * @namespace DD |
|
22 */ |
|
23 |
|
24 var DDMBase = function() { |
|
25 DDMBase.superclass.constructor.apply(this, arguments); |
|
26 }; |
|
27 |
|
28 DDMBase.NAME = 'ddm'; |
|
29 |
|
30 DDMBase.ATTRS = { |
|
31 /** |
|
32 * @attribute dragCursor |
|
33 * @description The cursor to apply when dragging, if shimmed the shim will get the cursor. |
|
34 * @type String |
|
35 */ |
|
36 dragCursor: { |
|
37 value: 'move' |
|
38 }, |
|
39 /** |
|
40 * @attribute clickPixelThresh |
|
41 * @description The number of pixels to move to start a drag operation, default is 3. |
|
42 * @type Number |
|
43 */ |
|
44 clickPixelThresh: { |
|
45 value: 3 |
|
46 }, |
|
47 /** |
|
48 * @attribute clickTimeThresh |
|
49 * @description The number of milliseconds a mousedown has to pass to start a drag operation, default is 1000. |
|
50 * @type Number |
|
51 */ |
|
52 clickTimeThresh: { |
|
53 value: 1000 |
|
54 }, |
|
55 /** |
|
56 * @attribute dragMode |
|
57 * @description This attribute only works if the dd-drop module is active. It will set the dragMode (point, intersect, strict) of all future Drag instances. |
|
58 * @type String |
|
59 */ |
|
60 dragMode: { |
|
61 value: 'point', |
|
62 setter: function(mode) { |
|
63 this._setDragMode(mode); |
|
64 return mode; |
|
65 } |
|
66 } |
|
67 |
|
68 }; |
|
69 |
|
70 Y.extend(DDMBase, Y.Base, { |
|
71 /** |
|
72 * @property _active |
|
73 * @description flag set when we activate our first drag, so DDM can start listening for events. |
|
74 * @type {Boolean} |
|
75 */ |
|
76 _active: null, |
|
77 /** |
|
78 * @private |
|
79 * @method _setDragMode |
|
80 * @description Handler for dragMode attribute setter. |
|
81 * @param String/Number The Number value or the String for the DragMode to default all future drag instances to. |
|
82 * @return Number The Mode to be set |
|
83 */ |
|
84 _setDragMode: function(mode) { |
|
85 if (mode === null) { |
|
86 mode = Y.DD.DDM.get('dragMode'); |
|
87 } |
|
88 switch (mode) { |
|
89 case 1: |
|
90 case 'intersect': |
|
91 return 1; |
|
92 case 2: |
|
93 case 'strict': |
|
94 return 2; |
|
95 case 0: |
|
96 case 'point': |
|
97 return 0; |
|
98 } |
|
99 return 0; |
|
100 }, |
|
101 /** |
|
102 * @property CSS_PREFIX |
|
103 * @description The PREFIX to attach to all DD CSS class names |
|
104 * @type {String} |
|
105 */ |
|
106 CSS_PREFIX: 'yui-dd', |
|
107 _activateTargets: function() {}, |
|
108 /** |
|
109 * @private |
|
110 * @property _drags |
|
111 * @description Holder for all registered drag elements. |
|
112 * @type {Array} |
|
113 */ |
|
114 _drags: [], |
|
115 /** |
|
116 * @property activeDrag |
|
117 * @description A reference to the currently active draggable object. |
|
118 * @type {Drag} |
|
119 */ |
|
120 activeDrag: false, |
|
121 /** |
|
122 * @private |
|
123 * @method _regDrag |
|
124 * @description Adds a reference to the drag object to the DDM._drags array, called in the constructor of Drag. |
|
125 * @param {Drag} d The Drag object |
|
126 */ |
|
127 _regDrag: function(d) { |
|
128 this._drags[this._drags.length] = d; |
|
129 if (!this._active) { |
|
130 this._setupListeners(); |
|
131 } |
|
132 }, |
|
133 /** |
|
134 * @private |
|
135 * @method _unregDrag |
|
136 * @description Remove this drag object from the DDM._drags array. |
|
137 * @param {Drag} d The drag object. |
|
138 */ |
|
139 _unregDrag: function(d) { |
|
140 var tmp = []; |
|
141 Y.each(this._drags, function(n, i) { |
|
142 if (n !== d) { |
|
143 tmp[tmp.length] = n; |
|
144 } |
|
145 }); |
|
146 this._drags = tmp; |
|
147 }, |
|
148 /** |
|
149 * @private |
|
150 * @method _setupListeners |
|
151 * @description Add the document listeners. |
|
152 */ |
|
153 _setupListeners: function() { |
|
154 this._active = true; |
|
155 var doc = Y.get(document); |
|
156 doc.on('mousemove', Y.bind(this._move, this)); |
|
157 //Y.Event.nativeAdd(document, 'mousemove', Y.bind(this._move, this)); |
|
158 doc.on('mouseup', Y.bind(this._end, this)); |
|
159 }, |
|
160 /** |
|
161 * @private |
|
162 * @method _start |
|
163 * @description Internal method used by Drag to signal the start of a drag operation |
|
164 * @param {Number} x The x position of the drag element |
|
165 * @param {Number} y The y position of the drag element |
|
166 * @param {Number} w The width of the drag element |
|
167 * @param {Number} h The height of the drag element |
|
168 */ |
|
169 _start: function(x, y, w, h) { |
|
170 this.fire('ddm:start'); |
|
171 this._startDrag.apply(this, arguments); |
|
172 }, |
|
173 /** |
|
174 * @private |
|
175 * @method _startDrag |
|
176 * @description Factory method to be overwritten by other DDM's |
|
177 * @param {Number} x The x position of the drag element |
|
178 * @param {Number} y The y position of the drag element |
|
179 * @param {Number} w The width of the drag element |
|
180 * @param {Number} h The height of the drag element |
|
181 */ |
|
182 _startDrag: function() {}, |
|
183 /** |
|
184 * @private |
|
185 * @method _endDrag |
|
186 * @description Factory method to be overwritten by other DDM's |
|
187 */ |
|
188 _endDrag: function() {}, |
|
189 _dropMove: function() {}, |
|
190 /** |
|
191 * @private |
|
192 * @method _end |
|
193 * @description Internal method used by Drag to signal the end of a drag operation |
|
194 */ |
|
195 _end: function() { |
|
196 //@TODO - Here we can get a (click - drag - click - release) interaction instead of a (mousedown - drag - mouseup - release) interaction |
|
197 //Add as a config option?? |
|
198 if (this.activeDrag) { |
|
199 this._endDrag(); |
|
200 this.fire('ddm:end'); |
|
201 this.activeDrag.end.call(this.activeDrag); |
|
202 this.activeDrag = null; |
|
203 } |
|
204 }, |
|
205 /** |
|
206 * @method stopDrag |
|
207 * @description Method will forcefully stop a drag operation. For example calling this from inside an ESC keypress handler will stop this drag. |
|
208 * @return {Self} |
|
209 * @chainable |
|
210 */ |
|
211 stopDrag: function() { |
|
212 if (this.activeDrag) { |
|
213 this._end(); |
|
214 } |
|
215 return this; |
|
216 }, |
|
217 /** |
|
218 * @private |
|
219 * @method _move |
|
220 * @description Internal listener for the mousemove DOM event to pass to the Drag's move method. |
|
221 * @param {Event.Facade} ev The Dom mousemove Event |
|
222 */ |
|
223 _move: function(ev) { |
|
224 if (this.activeDrag) { |
|
225 this.activeDrag._move.call(this.activeDrag, ev); |
|
226 this._dropMove(); |
|
227 } |
|
228 }, |
|
229 /** |
|
230 * //TODO Private, rename??... |
|
231 * @private |
|
232 * @method cssSizestoObject |
|
233 * @description Helper method to use to set the gutter from the attribute setter. |
|
234 * @param {String} gutter CSS style string for gutter: '5 0' (sets top and bottom to 5px, left and right to 0px), '1 2 3 4' (top 1px, right 2px, bottom 3px, left 4px) |
|
235 * @return {Object} The gutter Object Literal. |
|
236 */ |
|
237 cssSizestoObject: function(gutter) { |
|
238 var x = gutter.split(' '); |
|
239 |
|
240 switch (x.length) { |
|
241 case 1: x[1] = x[2] = x[3] = x[0]; break; |
|
242 case 2: x[2] = x[0]; x[3] = x[1]; break; |
|
243 case 3: x[3] = x[1]; break; |
|
244 } |
|
245 |
|
246 return { |
|
247 top : parseInt(x[0],10), |
|
248 right : parseInt(x[1],10), |
|
249 bottom: parseInt(x[2],10), |
|
250 left : parseInt(x[3],10) |
|
251 }; |
|
252 }, |
|
253 /** |
|
254 * @method getDrag |
|
255 * @description Get a valid Drag instance back from a Node or a selector string, false otherwise |
|
256 * @param {String/Object} node The Node instance or Selector string to check for a valid Drag Object |
|
257 * @return {Object} |
|
258 */ |
|
259 getDrag: function(node) { |
|
260 var drag = false, |
|
261 n = Y.get(node); |
|
262 if (n instanceof Y.Node) { |
|
263 Y.each(this._drags, function(v, k) { |
|
264 if (n.compareTo(v.get('node'))) { |
|
265 drag = v; |
|
266 } |
|
267 }); |
|
268 } |
|
269 return drag; |
|
270 } |
|
271 }); |
|
272 |
|
273 Y.namespace('DD'); |
|
274 Y.DD.DDM = new DDMBase(); |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 }, '3.0.0b1' ,{requires:['node', 'base'], skinnable:false}); |