|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('dd-plugin', function (Y, NAME) { |
|
9 |
|
10 |
|
11 |
|
12 /** |
|
13 * Simple Drag plugin that can be attached to a Node or Widget via the plug method. |
|
14 * @module dd |
|
15 * @submodule dd-plugin |
|
16 */ |
|
17 /** |
|
18 * Simple Drag plugin that can be attached to a Node or Widget via the plug method. |
|
19 * @class Drag |
|
20 * @extends DD.Drag |
|
21 * @constructor |
|
22 * @namespace Plugin |
|
23 */ |
|
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; |
|
28 } else { |
|
29 config.node = config.host; |
|
30 config.widget = false; |
|
31 } |
|
32 Drag.superclass.constructor.call(this, config); |
|
33 }, |
|
34 |
|
35 EV_START = 'drag:start', |
|
36 EV_DRAG = 'drag:drag', |
|
37 EV_DRAG_END = 'drag:end'; |
|
38 |
|
39 /** |
|
40 * dd-plugin |
|
41 * @property NAME |
|
42 * @type {String} |
|
43 */ |
|
44 Drag.NAME = "dd-plugin"; |
|
45 |
|
46 /** |
|
47 * The Drag instance will be placed on the Node instance under the dd namespace. It can be accessed via Node.dd; |
|
48 * @property NS |
|
49 * @type {String} |
|
50 */ |
|
51 Drag.NS = "dd"; |
|
52 |
|
53 Y.extend(Drag, Y.DD.Drag, { |
|
54 |
|
55 _widgetHandles: null, |
|
56 |
|
57 /** |
|
58 * refers to a Y.Widget if its the host, otherwise = false. |
|
59 * |
|
60 * @attribute _widget |
|
61 * @private |
|
62 */ |
|
63 _widget: undefined, |
|
64 |
|
65 |
|
66 /** |
|
67 * refers to the [x,y] coordinate where the drag was stopped last |
|
68 * |
|
69 * @attribute _stoppedPosition |
|
70 * @private |
|
71 */ |
|
72 _stoppedPosition: undefined, |
|
73 |
|
74 |
|
75 /** |
|
76 * Returns true if widget uses widgetPosition, otherwise returns false |
|
77 * |
|
78 * @method _usesWidgetPosition |
|
79 * @private |
|
80 */ |
|
81 _usesWidgetPosition: function(widget) { |
|
82 var r = false; |
|
83 if (widget) { |
|
84 r = (widget.hasImpl && widget.hasImpl(Y.WidgetPosition)) ? true : false; |
|
85 } |
|
86 return r; |
|
87 }, |
|
88 /** |
|
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 |
|
93 * @private |
|
94 */ |
|
95 _checkEvents: function() { |
|
96 Y.log('Checking for widget events', 'info', 'dd-plugin'); |
|
97 if (this._widget) { |
|
98 //It's a widget |
|
99 if (this.proxy) { |
|
100 //It's a proxy |
|
101 if (this._widgetHandles.length > 0) { |
|
102 Y.log('Proxy is plugged, remove events', 'info', 'dd-plugin'); |
|
103 //Remove Listeners |
|
104 this._removeWidgetListeners(); |
|
105 } |
|
106 } else { |
|
107 if (this._widgetHandles.length === 0) { |
|
108 Y.log('Proxy is not plugged, attach events', 'info', 'dd-plugin'); |
|
109 this._attachWidgetListeners(); |
|
110 } |
|
111 } |
|
112 } |
|
113 }, |
|
114 /** |
|
115 * Remove the attached widget listeners |
|
116 * @method _removeWidgetListeners |
|
117 * @private |
|
118 */ |
|
119 _removeWidgetListeners: function() { |
|
120 Y.log('Detaching widget events', 'info', 'dd-plugin'); |
|
121 Y.Array.each(this._widgetHandles, function(handle) { |
|
122 handle.detach(); |
|
123 }); |
|
124 this._widgetHandles = []; |
|
125 }, |
|
126 /** |
|
127 * If this is a Widget, then attach the positioning listeners |
|
128 * @method _attachWidgetListeners |
|
129 * @private |
|
130 */ |
|
131 _attachWidgetListeners: function() { |
|
132 //if this thing is a widget, and it uses widgetposition... |
|
133 if (this._usesWidgetPosition(this._widget)) { |
|
134 Y.log('Attaching widget events', 'info', 'dd-plugin'); |
|
135 |
|
136 //set the x,y on the widget's ATTRS |
|
137 this._widgetHandles.push(this.on(EV_DRAG, this._setWidgetCoords)); |
|
138 |
|
139 //store the new position that the widget ends up on |
|
140 this._widgetHandles.push(this.on(EV_DRAG_END, this._updateStopPosition)); |
|
141 } |
|
142 }, |
|
143 /** |
|
144 * Sets up event listeners on drag events if interacting with a widget |
|
145 * |
|
146 * @method initializer |
|
147 * @protected |
|
148 */ |
|
149 initializer: function(config) { |
|
150 |
|
151 this._widgetHandles = []; |
|
152 |
|
153 this._widget = config.widget; |
|
154 |
|
155 this.on(EV_START, this._checkEvents); //Always run, don't check |
|
156 |
|
157 this._attachWidgetListeners(); |
|
158 |
|
159 }, |
|
160 |
|
161 /** |
|
162 * Updates x,y or xy attributes on widget based on where the widget is dragged |
|
163 * |
|
164 * @method initializer |
|
165 * @param {EventFacade} e Event Facade |
|
166 * @private |
|
167 */ |
|
168 _setWidgetCoords: function(e) { |
|
169 |
|
170 //get the last position where the widget was, or get the starting point |
|
171 var nodeXY = this._stoppedPosition || e.target.nodeXY, |
|
172 realXY = e.target.realXY, |
|
173 |
|
174 //amount moved = [(x2 - x1) , (y2 - y1)] |
|
175 movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[1]]; |
|
176 |
|
177 //if both have changed.. |
|
178 if (movedXY[0] !== 0 && movedXY[1] !== 0) { |
|
179 this._widget.set('xy', realXY); |
|
180 } |
|
181 |
|
182 //if only x is 0, set the Y |
|
183 else if (movedXY[0] === 0) { |
|
184 this._widget.set('y',realXY[1]); |
|
185 } |
|
186 |
|
187 //otherwise, y is 0, so set X |
|
188 else if (movedXY[1] === 0){ |
|
189 this._widget.set('x', realXY[0]); |
|
190 } |
|
191 }, |
|
192 |
|
193 /** |
|
194 * Updates the last position where the widget was stopped. |
|
195 * |
|
196 * @method _updateStopPosition |
|
197 * @param {EventFacade} e Event Facade |
|
198 * @private |
|
199 */ |
|
200 _updateStopPosition: function(e) { |
|
201 this._stoppedPosition = e.target.realXY; |
|
202 } |
|
203 }); |
|
204 |
|
205 Y.namespace('Plugin'); |
|
206 Y.Plugin.Drag = Drag; |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 }, '3.10.3', {"optional": ["dd-constrain", "dd-proxy"], "requires": ["dd-drag"]}); |