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