3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('dd-ddm-base', function(Y) {
11 * Provides the base Drag Drop Manger required for making a Node draggable.
13 * @submodule dd-ddm-base
16 * Provides the base Drag Drop Manger required for making a Node draggable.
23 var DDMBase = function() {
24 DDMBase.superclass.constructor.apply(this, arguments);
31 * @attribute dragCursor
32 * @description The cursor to apply when dragging, if shimmed the shim will get the cursor.
39 * @attribute clickPixelThresh
40 * @description The number of pixels to move to start a drag operation, default is 3.
47 * @attribute clickTimeThresh
48 * @description The number of milliseconds a mousedown has to pass to start a drag operation, default is 1000.
55 * @attribute throttleTime
56 * @description The number of milliseconds to throttle the mousemove event. Default: 150
65 * @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.
70 setter: function(mode) {
71 this._setDragMode(mode);
78 Y.extend(DDMBase, Y.Base, {
79 _createPG: function() {},
82 * @description flag set when we activate our first drag, so DDM can start listening for events.
88 * @method _setDragMode
89 * @description Handler for dragMode attribute setter.
90 * @param String/Number The Number value or the String for the DragMode to default all future drag instances to.
91 * @return Number The Mode to be set
93 _setDragMode: function(mode) {
95 mode = Y.DD.DDM.get('dragMode');
111 * @property CSS_PREFIX
112 * @description The PREFIX to attach to all DD CSS class names
115 CSS_PREFIX: Y.ClassNameManager.getClassName('dd'),
116 _activateTargets: function() {},
120 * @description Holder for all registered drag elements.
125 * @property activeDrag
126 * @description A reference to the currently active draggable object.
133 * @description Adds a reference to the drag object to the DDM._drags array, called in the constructor of Drag.
134 * @param {Drag} d The Drag object
136 _regDrag: function(d) {
137 if (this.getDrag(d.get('node'))) {
142 this._setupListeners();
150 * @description Remove this drag object from the DDM._drags array.
151 * @param {Drag} d The drag object.
153 _unregDrag: function(d) {
155 Y.each(this._drags, function(n, i) {
164 * @method _setupListeners
165 * @description Add the document listeners.
167 _setupListeners: function() {
171 var doc = Y.one(Y.config.doc);
172 doc.on('mousemove', Y.throttle(Y.bind(this._move, this), this.get('throttleTime')));
173 doc.on('mouseup', Y.bind(this._end, this));
178 * @description Internal method used by Drag to signal the start of a drag operation
181 this.fire('ddm:start');
187 * @description Factory method to be overwritten by other DDM's
188 * @param {Number} x The x position of the drag element
189 * @param {Number} y The y position of the drag element
190 * @param {Number} w The width of the drag element
191 * @param {Number} h The height of the drag element
193 _startDrag: function() {},
197 * @description Factory method to be overwritten by other DDM's
199 _endDrag: function() {},
200 _dropMove: function() {},
204 * @description Internal method used by Drag to signal the end of a drag operation
207 if (this.activeDrag) {
209 this.fire('ddm:end');
210 this.activeDrag.end.call(this.activeDrag);
211 this.activeDrag = null;
216 * @description Method will forcefully stop a drag operation. For example calling this from inside an ESC keypress handler will stop this drag.
220 stopDrag: function() {
221 if (this.activeDrag) {
229 * @description Internal listener for the mousemove DOM event to pass to the Drag's move method.
230 * @param {Event.Facade} ev The Dom mousemove Event
232 _move: function(ev) {
233 if (this.activeDrag) {
234 this.activeDrag._move.call(this.activeDrag, ev);
239 * //TODO Private, rename??...
241 * @method cssSizestoObject
242 * @description Helper method to use to set the gutter from the attribute setter.
243 * @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)
244 * @return {Object} The gutter Object Literal.
246 cssSizestoObject: function(gutter) {
247 var x = gutter.split(' ');
250 case 1: x[1] = x[2] = x[3] = x[0]; break;
251 case 2: x[2] = x[0]; x[3] = x[1]; break;
252 case 3: x[3] = x[1]; break;
256 top : parseInt(x[0],10),
257 right : parseInt(x[1],10),
258 bottom: parseInt(x[2],10),
259 left : parseInt(x[3],10)
264 * @description Get a valid Drag instance back from a Node or a selector string, false otherwise
265 * @param {String/Object} node The Node instance or Selector string to check for a valid Drag Object
268 getDrag: function(node) {
271 if (n instanceof Y.Node) {
272 Y.each(this._drags, function(v, k) {
273 if (n.compareTo(v.get('node'))) {
281 * @method swapPosition
282 * @description Swap the position of 2 nodes based on their CSS positioning.
283 * @param {Node} n1 The first node to swap
284 * @param {Node} n2 The first node to swap
287 swapPosition: function(n1, n2) {
288 n1 = Y.DD.DDM.getNode(n1);
289 n2 = Y.DD.DDM.getNode(n2);
290 var xy1 = n1.getXY(),
299 * @description Return a node instance from the given node, selector string or Y.Base extended object.
300 * @param {Node/Object/String} n The node to resolve.
303 getNode: function(n) {
304 if (n instanceof Y.Node) {
308 if (Y.Widget && (n instanceof Y.Widget)) {
309 n = n.get('boundingBox');
320 * @description Swap the position of 2 nodes based on their DOM location.
321 * @param {Node} n1 The first node to swap
322 * @param {Node} n2 The first node to swap
325 swapNode: function(n1, n2) {
326 n1 = Y.DD.DDM.getNode(n1);
327 n2 = Y.DD.DDM.getNode(n2);
328 var p = n2.get('parentNode'),
329 s = n2.get('nextSibling');
332 p.insertBefore(n1, n2);
333 } else if (n2 == n1.get('nextSibling')) {
334 p.insertBefore(n2, n1);
336 n1.get('parentNode').replaceChild(n2, n1);
337 p.insertBefore(n1, s);
344 Y.DD.DDM = new DDMBase();
348 * @description Fires from the DDM before all drag events fire.
349 * @type {CustomEvent}
353 * @description Fires from the DDM after the DDM finishes, before the drag end events.
354 * @type {CustomEvent}
360 }, '3.5.1' ,{skinnable:false, requires:['node', 'base', 'yui-throttle', 'classnamemanager']});