~launchpad-pqm/lazr-js/toolchain

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/dd/dd-delegate.js

  • Committer: Sidnei da Silva
  • Date: 2009-11-16 00:51:29 UTC
  • mto: This revision was merged to the branch mainline in revision 154.
  • Revision ID: sidnei.da.silva@canonical.com-20091116005129-8ibwjlboa38glaw5
- Improved generation of skin modules and revamped combo service to make it more twisty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
 
Code licensed under the BSD License:
4
 
http://developer.yahoo.com/yui/license.html
5
 
version: 3.2.0
6
 
build: 2676
7
 
*/
8
 
YUI.add('dd-delegate', function(Y) {
9
 
 
10
 
 
11
 
    /**
12
 
     * Provides the ability to drag multiple nodes under a container element using only one Y.DD.Drag instance as a delegate.
13
 
     * @module dd
14
 
     * @submodule dd-delegate
15
 
     */     
16
 
    /**
17
 
     * Provides the ability to drag multiple nodes under a container element using only one Y.DD.Drag instance as a delegate.
18
 
     * @class Delegate
19
 
     * @extends Base
20
 
     * @constructor
21
 
     * @namespace DD
22
 
     */
23
 
 
24
 
 
25
 
    var Delegate = function(o) {
26
 
        Delegate.superclass.constructor.apply(this, arguments);
27
 
    },
28
 
    CONT = 'container',
29
 
    NODES = 'nodes',
30
 
    _tmpNode = Y.Node.create('<div>Temp Node</div>');
31
 
 
32
 
 
33
 
    Y.extend(Delegate, Y.Base, {
34
 
        /**
35
 
        * @private
36
 
        * @property _bubbleTargets
37
 
        * @description The default bubbleTarget for this object. Default: Y.DD.DDM
38
 
        */
39
 
        _bubbleTargets: Y.DD.DDM,
40
 
        /**
41
 
        * @property dd
42
 
        * @description A reference to the temporary dd instance used under the hood.
43
 
        */    
44
 
        dd: null,
45
 
        /**
46
 
        * @property _shimState
47
 
        * @private
48
 
        * @description The state of the Y.DD.DDM._noShim property to it can be reset.
49
 
        */    
50
 
        _shimState: null,
51
 
        /**
52
 
        * @private
53
 
        * @property _handles
54
 
        * @description Array of event handles to be destroyed
55
 
        */
56
 
        _handles: null,
57
 
        /**
58
 
        * @private
59
 
        * @method _onNodeChange
60
 
        * @description Listens to the nodeChange event and sets the dragNode on the temp dd instance.
61
 
        * @param {Event} e The Event.
62
 
        */
63
 
        _onNodeChange: function(e) {
64
 
            this.set('dragNode', e.newVal);
65
 
        },
66
 
        /**
67
 
        * @private
68
 
        * @method _afterDragEnd
69
 
        * @description Listens for the drag:end event and updates the temp dd instance.
70
 
        * @param {Event} e The Event.
71
 
        */
72
 
        _afterDragEnd: function(e) {
73
 
            Y.DD.DDM._noShim = this._shimState;
74
 
 
75
 
            this.set('lastNode', this.dd.get('node'));
76
 
            this.get('lastNode').removeClass(Y.DD.DDM.CSS_PREFIX + '-dragging');
77
 
            this.dd._unprep();
78
 
            this.dd.set('node', _tmpNode);
79
 
        },
80
 
        /**
81
 
        * @private
82
 
        * @method _delMouseDown
83
 
        * @description The callback for the Y.DD.Delegate instance used
84
 
        * @param {Event} e The MouseDown Event.
85
 
        */
86
 
        _delMouseDown: function(e) {
87
 
            var tar = e.currentTarget,
88
 
                dd = this.dd;
89
 
            
90
 
            if (tar.test(this.get(NODES)) && !tar.test(this.get('invalid'))) {
91
 
                this._shimState = Y.DD.DDM._noShim;
92
 
                Y.DD.DDM._noShim = true;
93
 
                this.set('currentNode', tar);
94
 
                dd.set('node', tar);
95
 
                if (dd.proxy) {
96
 
                    dd.set('dragNode', Y.DD.DDM._proxy);
97
 
                } else {
98
 
                    dd.set('dragNode', tar);
99
 
                }
100
 
                dd._prep();
101
 
                
102
 
                dd.fire('drag:mouseDown', { ev: e });
103
 
            }
104
 
        },
105
 
        /**
106
 
        * @private
107
 
        * @method _onMouseEnter
108
 
        * @description Sets the target shim state
109
 
        * @param {Event} e The MouseEnter Event
110
 
        */
111
 
        _onMouseEnter: function(e) {
112
 
            this._shimState = Y.DD.DDM._noShim;
113
 
            Y.DD.DDM._noShim = true;
114
 
        },
115
 
        /**
116
 
        * @private
117
 
        * @method _onMouseLeave
118
 
        * @description Resets the target shim state
119
 
        * @param {Event} e The MouseLeave Event
120
 
        */
121
 
        _onMouseLeave: function(e) {
122
 
            Y.DD.DDM._noShim = this._shimState;
123
 
        },
124
 
        initializer: function(cfg) {
125
 
            this._handles = [];
126
 
            //Create a tmp DD instance under the hood.
127
 
            var conf = Y.clone(this.get('dragConfig') || {}),
128
 
                cont = this.get(CONT);
129
 
 
130
 
            conf.node = _tmpNode.cloneNode(true);
131
 
            conf.bubbleTargets = this;
132
 
 
133
 
            if (this.get('handles')) {
134
 
                conf.handles = this.get('handles');
135
 
            }
136
 
 
137
 
            this.dd = new Y.DD.Drag(conf);
138
 
 
139
 
            //On end drag, detach the listeners
140
 
            this.dd.after('drag:end', Y.bind(this._afterDragEnd, this));
141
 
            this.dd.on('dragNodeChange', Y.bind(this._onNodeChange, this));
142
 
 
143
 
            //Attach the delegate to the container
144
 
            this._handles.push(Y.delegate(Y.DD.Drag.START_EVENT, Y.bind(this._delMouseDown, this), cont, this.get(NODES)));
145
 
 
146
 
            this._handles.push(Y.on('mouseenter', Y.bind(this._onMouseEnter, this), cont));
147
 
 
148
 
            this._handles.push(Y.on('mouseleave', Y.bind(this._onMouseLeave, this), cont));
149
 
 
150
 
            Y.later(50, this, this.syncTargets);
151
 
            Y.DD.DDM.regDelegate(this);
152
 
        },
153
 
        /**
154
 
        * @method syncTargets
155
 
        * @description Applies the Y.Plugin.Drop to all nodes matching the cont + nodes selector query.
156
 
        * @return {Self}
157
 
        * @chainable
158
 
        */        
159
 
        syncTargets: function() {
160
 
            if (!Y.Plugin.Drop || this.get('destroyed')) {
161
 
                return;
162
 
            }
163
 
            var items, groups, config;
164
 
 
165
 
            if (this.get('target')) {
166
 
                items = Y.one(this.get(CONT)).all(this.get(NODES));
167
 
                groups = this.dd.get('groups');
168
 
                config = this.get('dragConfig');
169
 
                
170
 
                if (config && 'groups' in config) {
171
 
                    groups = config.groups;
172
 
                }
173
 
 
174
 
                items.each(function(i) {
175
 
                    this.createDrop(i, groups);
176
 
                }, this);
177
 
            }
178
 
            return this;
179
 
        },
180
 
        /**
181
 
        * @method createDrop
182
 
        * @description Apply the Drop plugin to this node
183
 
        * @param {Node} node The Node to apply the plugin to
184
 
        * @param {Array} groups The default groups to assign this target to.
185
 
        * @return Node
186
 
        */
187
 
        createDrop: function(node, groups) {
188
 
            var config = {
189
 
                useShim: false,
190
 
                bubbleTargets: this
191
 
            };
192
 
 
193
 
            if (!node.drop) {
194
 
                node.plug(Y.Plugin.Drop, config);
195
 
            }
196
 
            node.drop.set('groups', groups);
197
 
            return node;
198
 
        },
199
 
        destructor: function() {
200
 
            if (this.dd) {
201
 
                this.dd.destroy();
202
 
            }
203
 
            if (Y.Plugin.Drop) {
204
 
                var targets = Y.one(this.get(CONT)).all(this.get(NODES));
205
 
                targets.unplug(Y.Plugin.Drop);
206
 
            }
207
 
            Y.each(this._handles, function(v) {
208
 
                v.detach();
209
 
            });
210
 
        }
211
 
    }, {
212
 
        NAME: 'delegate',
213
 
        ATTRS: {
214
 
            /**
215
 
            * @attribute container
216
 
            * @description A selector query to get the container to listen for mousedown events on. All "nodes" should be a child of this container.
217
 
            * @type String
218
 
            */    
219
 
            container: {
220
 
                value: 'body'
221
 
            },
222
 
            /**
223
 
            * @attribute nodes
224
 
            * @description A selector query to get the children of the "container" to make draggable elements from.
225
 
            * @type String
226
 
            */        
227
 
            nodes: {
228
 
                value: '.dd-draggable'
229
 
            },
230
 
            /**
231
 
            * @attribute invalid
232
 
            * @description A selector query to test a node to see if it's an invalid item.
233
 
            * @type String
234
 
            */        
235
 
            invalid: {
236
 
                value: 'input, select, button, a, textarea'
237
 
            },
238
 
            /**
239
 
            * @attribute lastNode
240
 
            * @description Y.Node instance of the last item dragged.
241
 
            * @type Node
242
 
            */        
243
 
            lastNode: {
244
 
                value: _tmpNode
245
 
            },
246
 
            /**
247
 
            * @attribute currentNode
248
 
            * @description Y.Node instance of the dd node.
249
 
            * @type Node
250
 
            */        
251
 
            currentNode: {
252
 
                value: _tmpNode
253
 
            },
254
 
            /**
255
 
            * @attribute dragNode
256
 
            * @description Y.Node instance of the dd dragNode.
257
 
            * @type Node
258
 
            */        
259
 
            dragNode: {
260
 
                value: _tmpNode
261
 
            },
262
 
            /**
263
 
            * @attribute over
264
 
            * @description Is the mouse currently over the container
265
 
            * @type Boolean
266
 
            */        
267
 
            over: {
268
 
                value: false
269
 
            },
270
 
            /**
271
 
            * @attribute target
272
 
            * @description Should the items also be a drop target.
273
 
            * @type Boolean
274
 
            */        
275
 
            target: {
276
 
                value: false
277
 
            },
278
 
            /**
279
 
            * @attribute dragConfig
280
 
            * @description The default config to be used when creating the DD instance.
281
 
            * @type Object
282
 
            */        
283
 
            dragConfig: {
284
 
                value: null
285
 
            },
286
 
            /**
287
 
            * @attribute handles
288
 
            * @description The handles config option added to the temp DD instance.
289
 
            * @type Array
290
 
            */        
291
 
            handles: {
292
 
                value: null
293
 
            }
294
 
        }
295
 
    });
296
 
 
297
 
    Y.mix(Y.DD.DDM, {
298
 
        /**
299
 
        * @private
300
 
        * @for DDM
301
 
        * @property _delegates
302
 
        * @description Holder for all Y.DD.Delegate instances
303
 
        * @type Array
304
 
        */
305
 
        _delegates: [],
306
 
        /**
307
 
        * @for DDM
308
 
        * @method regDelegate
309
 
        * @description Register a Delegate with the DDM
310
 
        */
311
 
        regDelegate: function(del) {
312
 
            this._delegates.push(del);
313
 
        },
314
 
        /**
315
 
        * @for DDM
316
 
        * @method getDelegate
317
 
        * @description Get a delegate instance from a container node
318
 
        * @returns Y.DD.Delegate
319
 
        */
320
 
        getDelegate: function(node) {
321
 
            var del = null;
322
 
            node = Y.one(node);
323
 
            Y.each(this._delegates, function(v) {
324
 
                if (node.test(v.get(CONT))) {
325
 
                    del = v;
326
 
                }
327
 
            }, this);
328
 
            return del;
329
 
        }
330
 
    });
331
 
 
332
 
    Y.namespace('DD');    
333
 
    Y.DD.Delegate = Delegate;
334
 
 
335
 
 
336
 
 
337
 
}, '3.2.0' ,{requires:['dd-drag', 'event-mouseenter'], skinnable:false, optional:['dd-drop-plugin']});