~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/static/javascript/yui/build/dd/dd-ddm-drop-debug.js

  • Committer: Martin Albisetti
  • Date: 2008-12-22 15:16:45 UTC
  • mfrom: (255 trunk)
  • mto: (157.1.8 loggerhead)
  • mto: This revision was merged to the branch mainline in revision 423.
  • Revision ID: argentina@gmail.com-20081222151645-vqxhqpr0fx9rc9t1
Merge from trunk revno 256

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2008, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.net/yui/license.txt
 
5
version: 3.0.0pr1
 
6
*/
 
7
YUI.add('dd-ddm-drop', function(Y) {
 
8
 
 
9
    /**
 
10
     * Extends the dd-ddm Class to add support for the placement of Drop Target shims inside the viewport shim. It also handles all Drop Target related events and interactions.
 
11
     * @module dd
 
12
     * @submodule dd-ddm-drop
 
13
     * @for DDM
 
14
     */
 
15
 
 
16
    //TODO CSS class name for the bestMatch..
 
17
    Y.mix(Y.DD.DDM, {
 
18
        /**
 
19
        * @private
 
20
        * @property _noShim
 
21
        * @description This flag turns off the use of the mouseover/mouseout shim. It should not be used unless you know what you are doing.
 
22
        * @type {Boolean}
 
23
        */
 
24
        _noShim: false,
 
25
        /**
 
26
        * @private
 
27
        * @property _activeShims
 
28
        * @description Placeholder for all active shims on the page
 
29
        * @type {Array}
 
30
        */
 
31
        _activeShims: [],
 
32
        /**
 
33
        * @private
 
34
        * @method _hasActiveShim
 
35
        * @description This method checks the _activeShims Object to see if there is a shim active.
 
36
        * @return {Boolean}
 
37
        */
 
38
        _hasActiveShim: function() {
 
39
            if (this._noShim) {
 
40
                return true;
 
41
            }
 
42
            return this._activeShims.length;
 
43
        },
 
44
        /**
 
45
        * @private
 
46
        * @method _addActiveShim 
 
47
        * @description Adds a Drop Target to the list of active shims
 
48
        * @param {Object} d The Drop instance to add to the list.
 
49
        */
 
50
        _addActiveShim: function(d) {
 
51
            this._activeShims[this._activeShims.length] = d;
 
52
        },
 
53
        /**
 
54
        * @private
 
55
        * @method _removeActiveShim 
 
56
        * @description Removes a Drop Target to the list of active shims
 
57
        * @param {Object} d The Drop instance to remove from the list.
 
58
        */
 
59
        _removeActiveShim: function(d) {
 
60
            var s = [];
 
61
            Y.each(this._activeShims, function(v, k) {
 
62
                if (v._yuid !== d._yuid) {
 
63
                    s[s.length] = v;
 
64
                }
 
65
                
 
66
            });
 
67
            this._activeShims = s;
 
68
        },
 
69
        /**
 
70
        * @method syncActiveShims
 
71
        * @description This method will sync the position of the shims on the Drop Targets that are currently active.
 
72
        * @param {Boolean} force Resize/sync all Targets.
 
73
        * @return {Array} drops The list of Drop Targets that was just synced.
 
74
        */
 
75
        syncActiveShims: function(force) {
 
76
            var drops = ((force) ? this.targets : this._lookup());
 
77
            Y.each(drops, function(v, k) {
 
78
                v.sizeShim.call(v);
 
79
            }, this);
 
80
 
 
81
            return drops;
 
82
        },
 
83
        /**
 
84
        * @private
 
85
        * @property mode
 
86
        * @description The mode that the drag operations will run in 0 for Point, 1 for Intersect, 2 for Strict
 
87
        * @type Number
 
88
        */
 
89
        mode: 0,
 
90
        /**
 
91
        * @private
 
92
        * @property POINT
 
93
        * @description In point mode, a Drop is targeted by the cursor being over the Target
 
94
        * @type Number
 
95
        */
 
96
        POINT: 0,
 
97
        /**
 
98
        * @private
 
99
        * @property INTERSECT
 
100
        * @description In intersect mode, a Drop is targeted by "part" of the drag node being over the Target
 
101
        * @type Number
 
102
        */
 
103
        INTERSECT: 1,
 
104
        /**
 
105
        * @private
 
106
        * @property STRICT
 
107
        * @description In strict mode, a Drop is targeted by the "entire" drag node being over the Target
 
108
        * @type Number
 
109
        */
 
110
        STRICT: 2,
 
111
        /**
 
112
        * @property useHash
 
113
        * @description Should we only check targets that are in the viewport on drags (for performance), default: true
 
114
        * @type {Boolean}
 
115
        */
 
116
        useHash: true,
 
117
        /**
 
118
        * @property activeDrop
 
119
        * @description A reference to the active Drop Target
 
120
        * @type {Object}
 
121
        */
 
122
        activeDrop: null,
 
123
        /**
 
124
        * @property validDrops
 
125
        * @description An array of the valid Drop Targets for this interaction.
 
126
        * @type {Array}
 
127
        */
 
128
        //TODO Change array/object literals to be in sync..
 
129
        validDrops: [],
 
130
        /**
 
131
        * @property otherDrops
 
132
        * @description An object literal of Other Drop Targets that we encountered during this interaction (in the case of overlapping Drop Targets)
 
133
        * @type {Object}
 
134
        */
 
135
        otherDrops: {},
 
136
        /**
 
137
        * @property targets
 
138
        * @description All of the Targets
 
139
        * @type {Array}
 
140
        */
 
141
        targets: [],
 
142
        /**
 
143
        * @private 
 
144
        * @method _addValid
 
145
        * @description Add a Drop Target to the list of Valid Targets. This list get's regenerated on each new drag operation.
 
146
        * @param {Object} drop
 
147
        * @return {Self}
 
148
        * @chainable
 
149
        */
 
150
        _addValid: function(drop) {
 
151
            this.validDrops[this.validDrops.length] = drop;
 
152
            return this;
 
153
        },
 
154
        /**
 
155
        * @private 
 
156
        * @method _removeValid
 
157
        * @description Removes a Drop Target from the list of Valid Targets. This list get's regenerated on each new drag operation.
 
158
        * @param {Object} drop
 
159
        * @return {Self}
 
160
        * @chainable
 
161
        */
 
162
        _removeValid: function(drop) {
 
163
            var drops = [];
 
164
            Y.each(this.validDrops, function(v, k) {
 
165
                if (v !== drop) {
 
166
                    drops[drops.length] = v
 
167
                }
 
168
            });
 
169
 
 
170
            this.validDrops = drops;
 
171
            return this;
 
172
        },
 
173
        /**
 
174
        * @method isOverTarget
 
175
        * @description Check to see if the Drag element is over the target, method varies on current mode
 
176
        * @param {Object} drop The drop to check against
 
177
        * @return {Boolean}
 
178
        */
 
179
        isOverTarget: function(drop) {
 
180
            //if (Y.Lang.isObject(this.activeDrag) && drop) { //TODO, check this check..
 
181
            if (this.activeDrag && drop) {
 
182
                var xy = this.activeDrag.mouseXY;
 
183
                if (xy) {
 
184
                    if (this.activeDrag.get('dragMode') == this.STRICT) {
 
185
                        return this.activeDrag.get('dragNode').inRegion(drop.region, true, this.activeDrag.region);
 
186
                    } else {
 
187
                        if (drop && drop.shim) {
 
188
                            return drop.shim.intersect({
 
189
                                top: xy[1],
 
190
                                bottom: xy[1],
 
191
                                left: xy[0], 
 
192
                                right: xy[0]
 
193
                            }, drop.region).inRegion;
 
194
                        } else {
 
195
                            return false;
 
196
                        }
 
197
                    }
 
198
                } else {
 
199
                    return false;
 
200
                }
 
201
            } else {
 
202
                return false;
 
203
            }
 
204
        },
 
205
        /**
 
206
        * @method clearCache
 
207
        * @description Clears the cache data used for this interaction.
 
208
        */
 
209
        clearCache: function() {
 
210
            this.validDrops = [];
 
211
            this.otherDrops = {};
 
212
            this._activeShims = [];
 
213
        },
 
214
        /**
 
215
        * @private
 
216
        * @method _activateTargets
 
217
        * @description Clear the cache and activate the shims of all the targets
 
218
        */
 
219
        _activateTargets: function() {
 
220
            this.clearCache();
 
221
            Y.each(this.targets, function(v, k) {
 
222
                v._activateShim.apply(v, []);
 
223
            }, this);
 
224
            this._handleTargetOver();
 
225
            
 
226
        },
 
227
        /**
 
228
        * @method getBestMatch
 
229
        * @description This method will gather the area for all potential targets and see which has the hightest covered area and return it.
 
230
        * @param {Array} drops An Array of drops to scan for the best match.
 
231
        * @param {Boolean} all If present, it returns an Array. First item is best match, second is an Array of the other items in the original Array.
 
232
        * @return {Object or Array} 
 
233
        */
 
234
        getBestMatch: function(drops, all) {
 
235
            var biggest = null, area = 0;
 
236
 
 
237
            Y.each(drops, function(v, k) {
 
238
                var inter = this.activeDrag.get('dragNode').intersect(v.get('node'));
 
239
                v.region.area = inter.area;
 
240
 
 
241
                if (inter.inRegion) {
 
242
                    if (inter.area > area) {
 
243
                        area = inter.area;
 
244
                        biggest = v;
 
245
                    }
 
246
                }
 
247
            }, this);
 
248
            if (all) {
 
249
                var out = [];
 
250
                //TODO Sort the others in numeric order by area covered..
 
251
                Y.each(drops, function(v, k) {
 
252
                    if (v !== biggest) {
 
253
                        out[out.length] = v;
 
254
                    }
 
255
                }, this);
 
256
                return [biggest, out];
 
257
            } else {
 
258
                return biggest;
 
259
            }
 
260
        },
 
261
        /**
 
262
        * @private
 
263
        * @method _deactivateTargets
 
264
        * @description This method fires the drop:hit, drag:drophit, drag:dropmiss methods and deactivates the shims..
 
265
        */
 
266
        _deactivateTargets: function() {
 
267
            var other = [],
 
268
                activeDrag = this.activeDrag;
 
269
            
 
270
            //TODO why is this check so hard??
 
271
            if (activeDrag && !Y.Lang.isNull(this.activeDrop) && this.otherDrops[this.activeDrop]) {
 
272
                if (!activeDrag.get('dragMode')) {
 
273
                    //TODO otherDrops -- private..
 
274
                    other = this.otherDrops;
 
275
                    delete other[this.activeDrop];
 
276
                } else {
 
277
                    var tmp = this.getBestMatch(this.otherDrops, true);
 
278
                    this.activeDrop = tmp[0];
 
279
                    other = tmp[1];
 
280
                }
 
281
                activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over')
 
282
                this.activeDrop.fire('drop:hit', { drag: activeDrag, drop: this.activeDrop, others: other });
 
283
                activeDrag.fire('drag:drophit', { drag: activeDrag,  drop: this.activeDrop, others: other });
 
284
            } else if (this.activeDrag) {
 
285
                activeDrag.get('node').removeClass(this.CSS_PREFIX + '-drag-over')
 
286
                activeDrag.fire('drag:dropmiss', { pageX: activeDrag.lastXY[0], pageY: activeDrag.lastXY[1] });
 
287
            } else {
 
288
                Y.log('No Active Drag', 'warn', 'dd-ddm');
 
289
            }
 
290
            
 
291
            this.activeDrop = null;
 
292
 
 
293
            Y.each(this.targets, function(v, k) {
 
294
                v._deactivateShim.apply(v, []);
 
295
            }, this);
 
296
        },
 
297
        /**
 
298
        * @private
 
299
        * @method _dropMove
 
300
        * @description This method is called when the move method is called on the Drag Object.
 
301
        */
 
302
        _dropMove: function() {
 
303
            if (this._hasActiveShim()) {
 
304
                //Y.log('We have an active shim, check targets', 'info', 'dd-ddm');
 
305
                this._handleTargetOver();
 
306
            } else {
 
307
                Y.each(this.otherDrops, function(v, k) {
 
308
                    v._handleOut.apply(v, []);
 
309
                });
 
310
            }
 
311
        },
 
312
        /**
 
313
        * @private
 
314
        * @method _lookup
 
315
        * @description Filters the list of Drops down to those in the viewport.
 
316
        * @return {Array} The valid Drop Targets that are in the viewport.
 
317
        */
 
318
        _lookup: function() {
 
319
            if (!this.useHash) {
 
320
                return this.validDrops;
 
321
            }
 
322
            var drops = [];
 
323
            //Only scan drop shims that are in the Viewport
 
324
            Y.each(this.validDrops, function(v, k) {
 
325
                if (v.shim && v.shim.inViewportRegion(false, v.region)) {
 
326
                    drops[drops.length] = v;
 
327
                }
 
328
            });
 
329
            return drops;
 
330
                
 
331
        },
 
332
        /**
 
333
        * @private
 
334
        * @method _handleTargetOver
 
335
        * @description This method execs _handleTargetOver on all valid Drop Targets
 
336
        * @param {Boolean} force Force it to run the first time.
 
337
        */
 
338
        _handleTargetOver: function() {
 
339
            var drops = this._lookup();
 
340
            Y.each(drops, function(v, k) {
 
341
                v._handleTargetOver.call(v);
 
342
            }, this);
 
343
        },
 
344
        /**
 
345
        * @private
 
346
        * @method _regTarget
 
347
        * @description Add the passed in Target to the targets collection
 
348
        * @param {Object} t The Target to add to the targets collection
 
349
        */
 
350
        _regTarget: function(t) {
 
351
            this.targets[this.targets.length] = t;
 
352
        },
 
353
        /**
 
354
        * @private
 
355
        * @method _unregTarget
 
356
        * @description Remove the passed in Target from the targets collection
 
357
        * @param {Object} drop The Target to remove from the targets collection
 
358
        */
 
359
        _unregTarget: function(drop) {
 
360
            var targets = [];
 
361
            Y.each(this.targets, function(v, k) {
 
362
                if (v != drop) {
 
363
                    targets[targets.length] = v;
 
364
                }
 
365
            }, this);
 
366
            this.targets = targets;
 
367
 
 
368
            var vdrops = [];
 
369
            Y.each(this.validDrops, function(v, k) {
 
370
                if (v !== drop) {
 
371
                    vdrops[vdrops.length] = v
 
372
                }
 
373
            });
 
374
 
 
375
            this.validDrops = vdrops;
 
376
        },
 
377
        /**
 
378
        * @method getDrop
 
379
        * @description Get a valid Drop instance back from a Node or a selector string, false otherwise
 
380
        * @param {String/Object} node The Node instance or Selector string to check for a valid Drop Object
 
381
        * @return {Object}
 
382
        */
 
383
        getDrop: function(node) {
 
384
            var drop = false,
 
385
                n = Y.Node.get(node);
 
386
            if (n instanceof Y.Node) {
 
387
                Y.each(this.targets, function(v, k) {
 
388
                    if (n.compareTo(v.get('node'))) {
 
389
                        drop = v;
 
390
                    }
 
391
                });
 
392
            }
 
393
            return drop;
 
394
        }
 
395
    }, true);
 
396
    
 
397
 
 
398
 
 
399
 
 
400
 
 
401
}, '3.0.0pr1' ,{requires:['dd-ddm'], skinnable:false});