NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / dd-plugin / dd-plugin-debug.js
blob9ea85280d2ebba401578151208948862a7999741
1 /*
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/
6 */
8 YUI.add('dd-plugin', function (Y, NAME) {
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         },
35         EV_START = 'drag:start',
36         EV_DRAG = 'drag:drag',
37         EV_DRAG_END = 'drag:end';
39         /**
40         * dd-plugin
41         * @property NAME
42         * @type {String}
43         */
44         Drag.NAME = "dd-plugin";
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";
53         Y.extend(Drag, Y.DD.Drag, {
55                 _widgetHandles: null,
57                 /**
58                 * refers to a Y.Widget if its the host, otherwise = false.
59                 *
60                 * @attribute _widget
61                 * @private
62                 */
63                 _widget: undefined,
66                 /**
67                 * refers to the [x,y] coordinate where the drag was stopped last
68                 *
69                 * @attribute _stoppedPosition
70                 * @private
71                 */
72                 _stoppedPosition: undefined,
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');
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));
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) {
151                         this._widgetHandles = [];
153                         this._widget = config.widget;
155                         this.on(EV_START, this._checkEvents); //Always run, don't check
157                         this._attachWidgetListeners();
159                 },
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) {
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);
180                          }
182                          //if only x is 0, set the Y
183                          else if (movedXY[0] === 0) {
184                                  this._widget.set('y',realXY[1]);
185                          }
187                          //otherwise, y is 0, so set X
188                          else if (movedXY[1] === 0){
189                                  this._widget.set('x', realXY[0]);
190                          }
191                 },
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         });
205         Y.namespace('Plugin');
206         Y.Plugin.Drag = Drag;
212 }, '3.13.0', {"optional": ["dd-constrain", "dd-proxy"], "requires": ["dd-drag"]});