2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('dd-ddm-base', function(Y) {
12 * Provides the base Drag Drop Manger required for making a Node draggable.
14 * @submodule dd-ddm-base
17 * Provides the base Drag Drop Manger required for making a Node draggable.
24 var DDMBase = function() {
25 DDMBase.superclass.constructor.apply(this, arguments);
32 * @attribute dragCursor
33 * @description The cursor to apply when dragging, if shimmed the shim will get the cursor.
40 * @attribute clickPixelThresh
41 * @description The number of pixels to move to start a drag operation, default is 3.
48 * @attribute clickTimeThresh
49 * @description The number of milliseconds a mousedown has to pass to start a drag operation, default is 1000.
56 * @attribute throttleTime
57 * @description The number of milliseconds to throttle the mousemove event. Default: 150
66 * @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.
71 setter: function(mode) {
72 this._setDragMode(mode);
79 Y.extend(DDMBase, Y.Base, {
80 _createPG: function() {},
83 * @description flag set when we activate our first drag, so DDM can start listening for events.
89 * @method _setDragMode
90 * @description Handler for dragMode attribute setter.
91 * @param String/Number The Number value or the String for the DragMode to default all future drag instances to.
92 * @return Number The Mode to be set
94 _setDragMode: function(mode) {
96 mode = Y.DD.DDM.get('dragMode');
112 * @property CSS_PREFIX
113 * @description The PREFIX to attach to all DD CSS class names
116 CSS_PREFIX: Y.ClassNameManager.getClassName('dd'),
117 _activateTargets: function() {},
121 * @description Holder for all registered drag elements.
126 * @property activeDrag
127 * @description A reference to the currently active draggable object.
134 * @description Adds a reference to the drag object to the DDM._drags array, called in the constructor of Drag.
135 * @param {Drag} d The Drag object
137 _regDrag: function(d) {
138 if (this.getDrag(d.get('node'))) {
143 this._setupListeners();
151 * @description Remove this drag object from the DDM._drags array.
152 * @param {Drag} d The drag object.
154 _unregDrag: function(d) {
156 Y.each(this._drags, function(n, i) {
165 * @method _setupListeners
166 * @description Add the document listeners.
168 _setupListeners: function() {
172 var doc = Y.one(Y.config.doc);
173 doc.on('mousemove', Y.throttle(Y.bind(this._move, this), this.get('throttleTime')));
174 doc.on('mouseup', Y.bind(this._end, this));
179 * @description Internal method used by Drag to signal the start of a drag operation
182 this.fire('ddm:start');
188 * @description Factory method to be overwritten by other DDM's
189 * @param {Number} x The x position of the drag element
190 * @param {Number} y The y position of the drag element
191 * @param {Number} w The width of the drag element
192 * @param {Number} h The height of the drag element
194 _startDrag: function() {},
198 * @description Factory method to be overwritten by other DDM's
200 _endDrag: function() {},
201 _dropMove: function() {},
205 * @description Internal method used by Drag to signal the end of a drag operation
208 if (this.activeDrag) {
210 this.fire('ddm:end');
211 this.activeDrag.end.call(this.activeDrag);
212 this.activeDrag = null;
217 * @description Method will forcefully stop a drag operation. For example calling this from inside an ESC keypress handler will stop this drag.
221 stopDrag: function() {
222 if (this.activeDrag) {
230 * @description Internal listener for the mousemove DOM event to pass to the Drag's move method.
231 * @param {Event.Facade} ev The Dom mousemove Event
233 _move: function(ev) {
234 if (this.activeDrag) {
235 this.activeDrag._move.call(this.activeDrag, ev);
240 * //TODO Private, rename??...
242 * @method cssSizestoObject
243 * @description Helper method to use to set the gutter from the attribute setter.
244 * @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)
245 * @return {Object} The gutter Object Literal.
247 cssSizestoObject: function(gutter) {
248 var x = gutter.split(' ');
251 case 1: x[1] = x[2] = x[3] = x[0]; break;
252 case 2: x[2] = x[0]; x[3] = x[1]; break;
253 case 3: x[3] = x[1]; break;
257 top : parseInt(x[0],10),
258 right : parseInt(x[1],10),
259 bottom: parseInt(x[2],10),
260 left : parseInt(x[3],10)
265 * @description Get a valid Drag instance back from a Node or a selector string, false otherwise
266 * @param {String/Object} node The Node instance or Selector string to check for a valid Drag Object
269 getDrag: function(node) {
272 if (n instanceof Y.Node) {
273 Y.each(this._drags, function(v, k) {
274 if (n.compareTo(v.get('node'))) {
282 * @method swapPosition
283 * @description Swap the position of 2 nodes based on their CSS positioning.
284 * @param {Node} n1 The first node to swap
285 * @param {Node} n2 The first node to swap
288 swapPosition: function(n1, n2) {
289 n1 = Y.DD.DDM.getNode(n1);
290 n2 = Y.DD.DDM.getNode(n2);
291 var xy1 = n1.getXY(),
300 * @description Return a node instance from the given node, selector string or Y.Base extended object.
301 * @param {Node/Object/String} n The node to resolve.
304 getNode: function(n) {
306 if (Y.Widget && (n instanceof Y.Widget)) {
307 n = n.get('boundingBox');
318 * @description Swap the position of 2 nodes based on their DOM location.
319 * @param {Node} n1 The first node to swap
320 * @param {Node} n2 The first node to swap
323 swapNode: function(n1, n2) {
324 n1 = Y.DD.DDM.getNode(n1);
325 n2 = Y.DD.DDM.getNode(n2);
326 var p = n2.get('parentNode'),
327 s = n2.get('nextSibling');
330 p.insertBefore(n1, n2);
331 } else if (n2 == n1.get('nextSibling')) {
332 p.insertBefore(n2, n1);
334 n1.get('parentNode').replaceChild(n2, n1);
335 p.insertBefore(n1, s);
342 Y.DD.DDM = new DDMBase();
346 * @description Fires from the DDM before all drag events fire.
347 * @type {Event.Custom}
351 * @description Fires from the DDM after the DDM finishes, before the drag end events.
352 * @type {Event.Custom}
358 }, '3.3.0' ,{requires:['node', 'base', 'yui-throttle', 'classnamemanager'], skinnable:false});