~ubuntu-branches/ubuntu/trusty/moodle/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/dd-ddm-drop/dd-ddm-drop.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2013-07-19 08:52:46 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130719085246-yebwditc2exoap2r
Tags: 2.5.1-1
* New upstream version: 2.5.1.
  - Fixes security issues:
    CVE-2013-2242 CVE-2013-2243 CVE-2013-2244 CVE-2013-2245
    CVE-2013-2246
* Depend on apache2 instead of obsolete apache2-mpm-prefork.
* Use packaged libphp-phpmailer (closes: #429339), adodb,
  HTMLPurifier, PclZip.
* Update debconf translations, thanks Salvatore Merone, Pietro Tollot,
  Joe Hansen, Yuri Kozlov, Holger Wansing, Américo Monteiro,
  Adriano Rafael Gomes, victory, Michał Kułach.
  (closes: #716972, #716986, #717080, #717108, #717278)

Show diffs side-by-side

added added

removed removed

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