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/
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() {
96
Y.log('Checking for widget events', 'info', 'dd-plugin');
101
if (this._widgetHandles.length > 0) {
102
Y.log('Proxy is plugged, remove events', 'info', 'dd-plugin');
104
this._removeWidgetListeners();
107
if (this._widgetHandles.length === 0) {
108
Y.log('Proxy is not plugged, attach events', 'info', 'dd-plugin');
109
this._attachWidgetListeners();
115
* Remove the attached widget listeners
116
* @method _removeWidgetListeners
119
_removeWidgetListeners: function() {
120
Y.log('Detaching widget events', 'info', 'dd-plugin');
121
Y.Array.each(this._widgetHandles, function(handle) {
124
this._widgetHandles = [];
127
* If this is a Widget, then attach the positioning listeners
128
* @method _attachWidgetListeners
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');
136
//set the x,y on the widget's ATTRS
137
this._widgetHandles.push(this.on(EV_DRAG, this._setWidgetCoords));
139
//store the new position that the widget ends up on
140
this._widgetHandles.push(this.on(EV_DRAG_END, this._updateStopPosition));
144
* Sets up event listeners on drag events if interacting with a widget
146
* @method initializer
149
initializer: function(config) {
151
this._widgetHandles = [];
153
this._widget = config.widget;
155
this.on(EV_START, this._checkEvents); //Always run, don't check
157
this._attachWidgetListeners();
162
* Updates x,y or xy attributes on widget based on where the widget is dragged
164
* @method initializer
165
* @param {EventFacade} e Event Facade
168
_setWidgetCoords: function(e) {
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,
174
//amount moved = [(x2 - x1) , (y2 - y1)]
175
movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[1]];
177
//if both have changed..
178
if (movedXY[0] !== 0 && movedXY[1] !== 0) {
179
this._widget.set('xy', realXY);
182
//if only x is 0, set the Y
183
else if (movedXY[0] === 0) {
184
this._widget.set('y',realXY[1]);
187
//otherwise, y is 0, so set X
188
else if (movedXY[1] === 0){
189
this._widget.set('x', realXY[0]);
194
* Updates the last position where the widget was stopped.
196
* @method _updateStopPosition
197
* @param {EventFacade} e Event Facade
200
_updateStopPosition: function(e) {
201
this._stoppedPosition = e.target.realXY;
205
Y.namespace('Plugin');
206
Y.Plugin.Drag = Drag;
212
}, '3.10.3', {"optional": ["dd-constrain", "dd-proxy"], "requires": ["dd-drag"]});