Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
                    Y.log('Checking for widget events', 'info', 'dd-plugin');
90
                    if (this._widget) {
91
                        //It's a widget
92
                        if (this.proxy) {
93
                            //It's a proxy
94
                            if (this._widgetHandles.length > 0) {
95
                                Y.log('Proxy is plugged, remove events', 'info', 'dd-plugin');
96
                                //Remove Listeners
97
                                this._removeWidgetListeners();
98
                            }
99
                        } else {
100
                            if (this._widgetHandles.length === 0) {
101
                                Y.log('Proxy is not plugged, attach events', 'info', 'dd-plugin');
102
                                this._attachWidgetListeners();
103
                            }
104
                        }
105
                    }
106
                },
107
                /**
108
                * Remove the attached widget listeners
109
                * @method _removeWidgetListeners
110
                * @private
111
                */
112
                _removeWidgetListeners: function() {
113
                    Y.log('Detaching widget events', 'info', 'dd-plugin');
114
                    Y.Array.each(this._widgetHandles, function(handle) {
115
                        handle.detach();
116
                    });
117
                    this._widgetHandles = [];
118
                },
119
                /**
120
                * If this is a Widget, then attach the positioning listeners
121
                * @method _attachWidgetListeners
122
                * @private
123
                */
124
                _attachWidgetListeners: function() {
125
                        //if this thing is a widget, and it uses widgetposition...
126
                        if (this._usesWidgetPosition(this._widget)) {
127
                            Y.log('Attaching widget events', 'info', 'dd-plugin');
128
 
129
                               //set the x,y on the widget's ATTRS
130
                               this._widgetHandles.push(this.on(EV_DRAG, this._setWidgetCoords));
131
 
132
                               //store the new position that the widget ends up on
133
                               this._widgetHandles.push(this.on(EV_DRAG_END, this._updateStopPosition));
134
                        }
135
                },
136
                /**
137
                * Sets up event listeners on drag events if interacting with a widget
138
                *
139
                * @method initializer
140
                * @protected
141
                */
142
                initializer: function(config) {
143
 
144
                        this._widgetHandles = [];
145
 
146
                        this._widget = config.widget;
147
 
148
                        this.on(EV_START, this._checkEvents); //Always run, don't check
149
 
150
                        this._attachWidgetListeners();
151
 
152
                },
153
 
154
                /**
155
                * Updates x,y or xy attributes on widget based on where the widget is dragged
156
                *
157
                * @method initializer
158
                * @param {EventFacade} e Event Facade
159
                * @private
160
                */
161
                _setWidgetCoords: function(e) {
162
 
163
                        //get the last position where the widget was, or get the starting point
164
                        var nodeXY = this._stoppedPosition || e.target.nodeXY,
165
                         realXY = e.target.realXY,
166
 
167
                         //amount moved = [(x2 - x1) , (y2 - y1)]
168
                         movedXY = [realXY[0] - nodeXY[0], realXY[1] - nodeXY[1]];
169
 
170
                         //if both have changed..
171
                         if (movedXY[0] !== 0 && movedXY[1] !== 0) {
172
                                 this._widget.set('xy', realXY);
173
                         }
174
 
175
                         //if only x is 0, set the Y
176
                         else if (movedXY[0] === 0) {
177
                                 this._widget.set('y',realXY[1]);
178
                         }
179
 
180
                         //otherwise, y is 0, so set X
181
                         else if (movedXY[1] === 0){
182
                                 this._widget.set('x', realXY[0]);
183
                         }
184
                },
185
 
186
                /**
187
                * Updates the last position where the widget was stopped.
188
                *
189
                * @method _updateStopPosition
190
                * @param {EventFacade} e Event Facade
191
                * @private
192
                */
193
                _updateStopPosition: function(e) {
194
                        this._stoppedPosition = e.target.realXY;
195
                }
196
        });
197
 
198
        Y.namespace('Plugin');
199
        Y.Plugin.Drag = Drag;
200
 
201
 
202
 
203
 
204
 
205
}, '3.18.1', {"optional": ["dd-constrain", "dd-proxy"], "requires": ["dd-drag"]});