2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('dd-plugin', function (Y, NAME) {
13 * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
15 * @submodule dd-plugin
18 * Simple Drag plugin that can be attached to a Node or Widget via the plug method.
24 var Drag = function(config) {
25 if (Y.Widget && config.host instanceof Y.Widget) {
26 config.node = config.host.get('boundingBox');
27 config.widget = config.host;
29 config.node = config.host;
30 config.widget = false;
32 Drag.superclass.constructor.call(this, config);
35 EV_START = 'drag:start',
36 EV_DRAG = 'drag:drag',
37 EV_DRAG_END = 'drag:end';
44 Drag.NAME = "dd-plugin";
47 * The Drag instance will be placed on the Node instance under the dd namespace. It can be accessed via Node.dd;
53 Y.extend(Drag, Y.DD.Drag, {
58 * refers to a Y.Widget if its the host, otherwise = false.
67 * refers to the [x,y] coordinate where the drag was stopped last
69 * @attribute _stoppedPosition
72 _stoppedPosition: undefined,
76 * Returns true if widget uses widgetPosition, otherwise returns false
78 * @method _usesWidgetPosition
81 _usesWidgetPosition: function(widget) {
84 r = (widget.hasImpl && widget.hasImpl(Y.WidgetPosition)) ? true : false;
89 * Attached to the `drag:start` event, it checks if this plugin needs
90 * to attach or detach listeners for widgets. If `dd-proxy` is plugged
91 * the default widget positioning should be ignored.
92 * @method _checkEvents
95 _checkEvents: function() {
100 if (this._widgetHandles.length > 0) {
102 this._removeWidgetListeners();
105 if (this._widgetHandles.length === 0) {
106 this._attachWidgetListeners();
112 * Remove the attached widget listeners
113 * @method _removeWidgetListeners
116 _removeWidgetListeners: function() {
117 Y.Array.each(this._widgetHandles, function(handle) {
120 this._widgetHandles = [];
123 * If this is a Widget, then attach the positioning listeners
124 * @method _attachWidgetListeners
127 _attachWidgetListeners: function() {
128 //if this thing is a widget, and it uses widgetposition...
129 if (this._usesWidgetPosition(this._widget)) {
131 //set the x,y on the widget's ATTRS
132 this._widgetHandles.push(this.on(EV_DRAG, this._setWidgetCoords));
134 //store the new position that the widget ends up on
135 this._widgetHandles.push(this.on(EV_DRAG_END, this._updateStopPosition));
139 * Sets up event listeners on drag events if interacting with a widget
141 * @method initializer
144 initializer: function(config) {
146 this._widgetHandles = [];
148 this._widget = config.widget;
150 this.on(EV_START, this._checkEvents); //Always run, don't check
152 this._attachWidgetListeners();
157 * Updates x,y or xy attributes on widget based on where the widget is dragged
159 * @method initializer
160 * @param {EventFacade} e Event Facade
163 _setWidgetCoords: function(e) {
165 //get the last position where the widget was, or get the starting point
166 var nodeXY = this._stoppedPosition || e.target.nodeXY,
167 realXY = e.target.realXY,
169 //amount moved = [(x2 - x1) , (y2 - y1)]
170 movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[1]];
172 //if both have changed..
173 if (movedXY[0] !== 0 && movedXY[1] !== 0) {
174 this._widget.set('xy', realXY);
177 //if only x is 0, set the Y
178 else if (movedXY[0] === 0) {
179 this._widget.set('y',realXY[1]);
182 //otherwise, y is 0, so set X
183 else if (movedXY[1] === 0){
184 this._widget.set('x', realXY[0]);
189 * Updates the last position where the widget was stopped.
191 * @method _updateStopPosition
192 * @param {EventFacade} e Event Facade
195 _updateStopPosition: function(e) {
196 this._stoppedPosition = e.target.realXY;
200 Y.namespace('Plugin');
201 Y.Plugin.Drag = Drag;
207 }, '3.13.0', {"optional": ["dd-constrain", "dd-proxy"], "requires": ["dd-drag"]});