source: trunk/template-common/lib/ui/ui.droppable.js @ 3282

Last change on this file since 3282 was 3282, checked in by plg, 15 years ago

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
File size: 8.9 KB
Line 
1/*
2 * jQuery UI Droppable
3 *
4 * Copyright (c) 2008 Paul Bakaus
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
7 *
8 * http://docs.jquery.com/UI/Droppables
9 *
10 * Depends:
11 *      ui.core.js
12 *      ui.draggable.js
13 */
14(function($) {
15
16$.widget("ui.droppable", {
17        init: function() {
18
19                this.element.addClass("ui-droppable");
20                this.isover = 0; this.isout = 1;
21               
22                //Prepare the passed options
23                var o = this.options, accept = o.accept;
24                o = $.extend(o, {
25                        accept: o.accept && o.accept.constructor == Function ? o.accept : function(d) {
26                                return $(d).is(accept);
27                        }
28                });
29               
30                //Store the droppable's proportions
31                this.proportions = { width: this.element[0].offsetWidth, height: this.element[0].offsetHeight };
32               
33                // Add the reference and positions to the manager
34                $.ui.ddmanager.droppables.push(this);
35               
36        },
37        plugins: {},
38        ui: function(c) {
39                return {
40                        draggable: (c.currentItem || c.element),
41                        helper: c.helper,
42                        position: c.position,
43                        absolutePosition: c.positionAbs,
44                        options: this.options,
45                        element: this.element
46                };
47        },
48        destroy: function() {
49                var drop = $.ui.ddmanager.droppables;
50                for ( var i = 0; i < drop.length; i++ )
51                        if ( drop[i] == this )
52                                drop.splice(i, 1);
53               
54                this.element
55                        .removeClass("ui-droppable ui-droppable-disabled")
56                        .removeData("droppable")
57                        .unbind(".droppable");
58        },
59        over: function(e) {
60               
61                var draggable = $.ui.ddmanager.current;
62                if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
63               
64                if (this.options.accept.call(this.element,(draggable.currentItem || draggable.element))) {
65                        $.ui.plugin.call(this, 'over', [e, this.ui(draggable)]);
66                        this.element.triggerHandler("dropover", [e, this.ui(draggable)], this.options.over);
67                }
68               
69        },
70        out: function(e) {
71               
72                var draggable = $.ui.ddmanager.current;
73                if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return; // Bail if draggable and droppable are same element
74               
75                if (this.options.accept.call(this.element,(draggable.currentItem || draggable.element))) {
76                        $.ui.plugin.call(this, 'out', [e, this.ui(draggable)]);
77                        this.element.triggerHandler("dropout", [e, this.ui(draggable)], this.options.out);
78                }
79               
80        },
81        drop: function(e,custom) {
82               
83                var draggable = custom || $.ui.ddmanager.current;
84                if (!draggable || (draggable.currentItem || draggable.element)[0] == this.element[0]) return false; // Bail if draggable and droppable are same element
85               
86                var childrenIntersection = false;
87                this.element.find(".ui-droppable").not(".ui-draggable-dragging").each(function() {
88                        var inst = $.data(this, 'droppable');
89                        if(inst.options.greedy && $.ui.intersect(draggable, $.extend(inst, { offset: inst.element.offset() }), inst.options.tolerance)) {
90                                childrenIntersection = true; return false;
91                        }
92                });
93                if(childrenIntersection) return false;
94               
95                if(this.options.accept.call(this.element,(draggable.currentItem || draggable.element))) {
96                        $.ui.plugin.call(this, 'drop', [e, this.ui(draggable)]);
97                        this.element.triggerHandler("drop", [e, this.ui(draggable)], this.options.drop);
98                        return true;
99                }
100               
101                return false;
102               
103        },
104        activate: function(e) {
105               
106                var draggable = $.ui.ddmanager.current;
107                $.ui.plugin.call(this, 'activate', [e, this.ui(draggable)]);
108                if(draggable) this.element.triggerHandler("dropactivate", [e, this.ui(draggable)], this.options.activate);
109               
110        },
111        deactivate: function(e) {
112               
113                var draggable = $.ui.ddmanager.current;
114                $.ui.plugin.call(this, 'deactivate', [e, this.ui(draggable)]);
115                if(draggable) this.element.triggerHandler("dropdeactivate", [e, this.ui(draggable)], this.options.deactivate);
116               
117        }
118});
119
120$.extend($.ui.droppable, {
121        defaults: {
122                disabled: false,
123                tolerance: 'intersect'
124        }
125});
126
127$.ui.intersect = function(draggable, droppable, toleranceMode) {
128       
129        if (!droppable.offset) return false;
130       
131        var x1 = (draggable.positionAbs || draggable.position.absolute).left, x2 = x1 + draggable.helperProportions.width,
132                y1 = (draggable.positionAbs || draggable.position.absolute).top, y2 = y1 + draggable.helperProportions.height;
133        var l = droppable.offset.left, r = l + droppable.proportions.width,
134                t = droppable.offset.top, b = t + droppable.proportions.height;
135       
136        switch (toleranceMode) {
137                case 'fit':
138                        return (l < x1 && x2 < r
139                                && t < y1 && y2 < b);
140                        break;
141                case 'intersect':
142                        return (l < x1 + (draggable.helperProportions.width / 2) // Right Half
143                                && x2 - (draggable.helperProportions.width / 2) < r // Left Half
144                                && t < y1 + (draggable.helperProportions.height / 2) // Bottom Half
145                                && y2 - (draggable.helperProportions.height / 2) < b ); // Top Half
146                        break;
147                case 'pointer':
148                        return (l < ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left) && ((draggable.positionAbs || draggable.position.absolute).left + (draggable.clickOffset || draggable.offset.click).left) < r
149                                && t < ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top) && ((draggable.positionAbs || draggable.position.absolute).top + (draggable.clickOffset || draggable.offset.click).top) < b);
150                        break;
151                case 'touch':
152                        return (
153                                        (y1 >= t && y1 <= b) || // Top edge touching
154                                        (y2 >= t && y2 <= b) || // Bottom edge touching
155                                        (y1 < t && y2 > b)              // Surrounded vertically
156                                ) && (
157                                        (x1 >= l && x1 <= r) || // Left edge touching
158                                        (x2 >= l && x2 <= r) || // Right edge touching
159                                        (x1 < l && x2 > r)              // Surrounded horizontally
160                                );
161                        break;
162                default:
163                        return false;
164                        break;
165                }
166       
167};
168
169/*
170        This manager tracks offsets of draggables and droppables
171*/
172$.ui.ddmanager = {
173        current: null,
174        droppables: [],
175        prepareOffsets: function(t, e) {
176               
177                var m = $.ui.ddmanager.droppables;
178                var type = e ? e.type : null; // workaround for #2317
179
180                for (var i = 0; i < m.length; i++) {
181                        if(m[i].options.disabled || (t && !m[i].options.accept.call(m[i].element,(t.currentItem || t.element)))) continue;
182                        m[i].visible = m[i].element.css("display") != "none"; if(!m[i].visible) continue; //If the element is not visible, continue
183                        m[i].offset = m[i].element.offset();
184                        m[i].proportions = { width: m[i].element[0].offsetWidth, height: m[i].element[0].offsetHeight };
185                       
186                        if(type == "dragstart" || type == "sortactivate") m[i].activate.call(m[i], e); //Activate the droppable if used directly from draggables
187                }
188               
189        },
190        drop: function(draggable, e) {
191               
192                var dropped = false;
193                $.each($.ui.ddmanager.droppables, function() {
194                       
195                        if(!this.options) return;
196                        if (!this.options.disabled && this.visible && $.ui.intersect(draggable, this, this.options.tolerance))
197                                dropped = this.drop.call(this, e);
198                       
199                        if (!this.options.disabled && this.visible && this.options.accept.call(this.element,(draggable.currentItem || draggable.element))) {
200                                this.isout = 1; this.isover = 0;
201                                this.deactivate.call(this, e);
202                        }
203                       
204                });
205                return dropped;
206               
207        },
208        drag: function(draggable, e) {
209               
210                //If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
211                if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, e);
212               
213                //Run through all droppables and check their positions based on specific tolerance options
214
215                $.each($.ui.ddmanager.droppables, function() {
216                       
217                        if(this.options.disabled || this.greedyChild || !this.visible) return;
218                        var intersects = $.ui.intersect(draggable, this, this.options.tolerance);
219                       
220                        var c = !intersects && this.isover == 1 ? 'isout' : (intersects && this.isover == 0 ? 'isover' : null);
221                        if(!c) return;
222                       
223                        var parentInstance;
224                        if (this.options.greedy) {
225                                var parent = this.element.parents('.ui-droppable:eq(0)');
226                                if (parent.length) {
227                                        parentInstance = $.data(parent[0], 'droppable');
228                                        parentInstance.greedyChild = (c == 'isover' ? 1 : 0);
229                                }
230                        }
231                       
232                        // we just moved into a greedy child
233                        if (parentInstance && c == 'isover') {
234                                parentInstance['isover'] = 0;
235                                parentInstance['isout'] = 1;
236                                parentInstance.out.call(parentInstance, e);
237                        }
238                       
239                        this[c] = 1; this[c == 'isout' ? 'isover' : 'isout'] = 0;
240                        this[c == "isover" ? "over" : "out"].call(this, e);
241                       
242                        // we just moved out of a greedy child
243                        if (parentInstance && c == 'isout') {
244                                parentInstance['isout'] = 0;
245                                parentInstance['isover'] = 1;
246                                parentInstance.over.call(parentInstance, e);
247                        }
248                });
249               
250        }
251};
252
253/*
254 * Droppable Extensions
255 */
256
257$.ui.plugin.add("droppable", "activeClass", {
258        activate: function(e, ui) {
259                $(this).addClass(ui.options.activeClass);
260        },
261        deactivate: function(e, ui) {
262                $(this).removeClass(ui.options.activeClass);
263        },
264        drop: function(e, ui) {
265                $(this).removeClass(ui.options.activeClass);
266        }
267});
268
269$.ui.plugin.add("droppable", "hoverClass", {
270        over: function(e, ui) {
271                $(this).addClass(ui.options.hoverClass);
272        },
273        out: function(e, ui) {
274                $(this).removeClass(ui.options.hoverClass);
275        },
276        drop: function(e, ui) {
277                $(this).removeClass(ui.options.hoverClass);
278        }
279});
280
281})(jQuery);
Note: See TracBrowser for help on using the repository browser.