xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / bean / support / syn / resources / jquery.event.drop.js

13.1 KB
1(function($){
2   var withinBox = function(x, y, left, top, width, height ){
3        return (y >= top &&
4                y <  top + height &&
5                x >= left &&
6                x <  left + width);
7    } 
8/**
9 * @function within
10 * @parent dom
11 * Returns if the elements are within the position
12 * @param {Object} x
13 * @param {Object} y
14 * @param {Object} cache
15 */
16$.fn.within= function(x, y, cache) {
17    var ret = []
18    this.each(function(){
19        var q = jQuery(this);
20
21        if(this == document.documentElement) return ret.push(this);
22
23        var offset = cache ? jQuery.data(this,"offset", q.offset()) : q.offset();
24
25        var res =  withinBox(x, y, 
26                                      offset.left, offset.top,
27                                      this.offsetWidth, this.offsetHeight );
28
29        if(res) ret.push(this);
30    });
31    
32    return this.pushStack( jQuery.unique( ret ), "within", x+","+y );
33}
34
35
36/**
37 * @function withinBox
38 * returns if elements are within the box
39 * @param {Object} left
40 * @param {Object} top
41 * @param {Object} width
42 * @param {Object} height
43 * @param {Object} cache
44 */
45$.fn.withinBox = function(left, top, width, height, cache){
46  	var ret = []
47    this.each(function(){
48        var q = jQuery(this);
49
50        if(this == document.documentElement) return  this.ret.push(this);
51
52        var offset = cache ? jQuery.data(this,"offset", q.offset()) : q.offset();
53
54        var ew = q.width(), eh = q.height();
55
56		res =  !( (offset.top > top+height) || (offset.top +eh < top) || (offset.left > left+width ) || (offset.left+ew < left));
57
58        if(res)
59            ret.push(this);
60    });
61    return this.pushStack( jQuery.unique( ret ), "withinBox", jQuery.makeArray(arguments).join(",") );
62}
63    
64})(jQuery);
65(function($){
66/**
67 * @function compare
68 * @parent dom
69 * @download http://jmvcsite.heroku.com/pluginify?plugins[]=jquery/dom/compare/compare.js 
70 * Compares the position of two nodes and returns a bitmask detailing how they are positioned 
71 * relative to each other.  You can expect it to return the same results as 
72 * [http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-compareDocumentPosition | compareDocumentPosition].
73 * Parts of this documentation and source come from [http://ejohn.org/blog/comparing-document-position | John Resig].
74 * <h2>Demo</h2>
75 * @demo jquery/dom/compare/compare.html
76 * @test jquery/dom/compare/qunit.html
77 * @plugin dom/compare
78 * @param {HTMLElement} a the first node
79 * @param {HTMLElement} b the second node
80 * @return {Number} A bitmap with the following digit values:
81 * <table class='options'>
82 *     <tr><th>Bits</th><th>Number</th><th>Meaning</th></tr>
83 *     <tr><td>000000</td><td>0</td><td>Elements are identical.</td></tr>
84 *     <tr><td>000001</td><td>1</td><td>The nodes are in different documents (or one is outside of a document).</td></tr>
85 *     <tr><td>000010</td><td>2</td><td>Node B precedes Node A.</td></tr>
86 *     <tr><td>000100</td><td>4</td><td>Node A precedes Node B.</td></tr>
87 *     <tr><td>001000</td><td>8</td><td>Node B contains Node A.</td></tr>
88 *     <tr><td>010000</td><td>16</td><td>Node A contains Node B.</td></tr>
89 * </table>
90 */
91jQuery.fn.compare = function(b){ //usually 
92	//b is usually a relatedTarget, but b/c it is we have to avoid a few FF errors
93	
94	try{ //FF3 freaks out with XUL
95		b = b.jquery ? b[0] : b;
96	}catch(e){
97		return null;
98	}
99	if (window.HTMLElement) { //make sure we aren't coming from XUL element
100		var s = HTMLElement.prototype.toString.call(b)
101		if (s == '[xpconnect wrapped native prototype]' || s == '[object XULElement]') return null;
102	}
103	if(this[0].compareDocumentPosition){
104		return this[0].compareDocumentPosition(b);
105	}
106	if(this[0] == document && b != document) return 8;
107	var number = (this[0] !== b && this[0].contains(b) && 16) + (this[0] != b && b.contains(this[0]) && 8),
108		docEl = document.documentElement;
109	if(this[0].sourceIndex){
110		number += (this[0].sourceIndex < b.sourceIndex && 4)
111		number += (this[0].sourceIndex > b.sourceIndex && 2)
112		number += (this[0].ownerDocument !== b.ownerDocument ||
113			(this[0] != docEl && this[0].sourceIndex <= 0 ) ||
114			(b != docEl && b.sourceIndex <= 0 )) && 1
115	}else{
116		var range = document.createRange(), 
117			sourceRange = document.createRange(),
118			compare;
119		range.selectNode(this[0]);
120		sourceRange.selectNode(b);
121		compare = range.compareBoundaryPoints(Range.START_TO_START, sourceRange);
122		
123	}
124
125	return number;
126}
127
128})(jQuery);
129(function($){
130	var event = $.event, 
131		callHanders = function(){
132			
133		};
134	//somehow need to keep track of elements with selectors on them.  When element is removed, somehow we need to know that
135	//
136	/**
137	 * @add jQuery.event.special
138	 */
139	var eventNames = [
140	/**
141	 * @attribute dropover
142	 * Called when a drag is first moved over this drop element.
143	 * <p>Drop events are covered in more detail in [jQuery.Drop].</p>
144	 */
145	"dropover",
146	/**
147	 * @attribute dropon
148	 * Called when a drag is dropped on a drop element.
149	 * <p>Drop events are covered in more detail in [jQuery.Drop].</p>
150	 */
151	"dropon",
152	/**
153	 * @attribute dropout
154	 * Called when a drag is moved out of this drop.
155	 * <p>Drop events are covered in more detail in [jQuery.Drop].</p>
156	 */
157	"dropout",
158	/**
159	 * @attribute dropinit
160	 * Called when a drag motion starts and the drop elements are initialized.
161	 * <p>Drop events are covered in more detail in [jQuery.Drop].</p>
162	 */
163	"dropinit",
164	/**
165	 * @attribute dropmove
166	 * Called repeatedly when a drag is moved over a drop.
167	 * <p>Drop events are covered in more detail in [jQuery.Drop].</p>
168	 */
169	"dropmove",
170	/**
171	 * @attribute dropend
172	 * Called when the drag is done for this drop.
173	 * <p>Drop events are covered in more detail in [jQuery.Drop].</p>
174	 */
175	"dropend"];
176	
177	
178	
179	/**
180	 * @class jQuery.Drop
181	 * @parent specialevents
182	 * @plugin jquery/event/drop
183	 * @download  http://jmvcsite.heroku.com/pluginify?plugins[]=jquery/event/drop/drop.js
184	 * @test jquery/event/drag/qunit.html
185	 * 
186	 * Provides drop events as a special event to jQuery.  
187	 * By binding to a drop event, the your callback functions will be 
188	 * called during the corresponding phase of drag.
189	 * <h2>Drop Events</h2>
190	 * All drop events are called with the native event, an instance of drop, and the drag.  Here are the available drop 
191	 * events:
192	 * <ul>
193	 * 	<li><code>dropinit</code> - the drag motion is started, drop positions are calculated.</li>
194	 *  <li><code>dropover</code> - a drag moves over a drop element, called once as the drop is dragged over the element.</li>
195	 *  <li><code>dropout</code> - a drag moves out of the drop element.</li>
196	 *  <li><code>dropmove</code> - a drag is moved over a drop element, called repeatedly as the element is moved.</li>
197	 *  <li><code>dropon</code> - a drag is released over a drop element.</li>
198	 *  <li><code>dropend</code> - the drag motion has completed.</li>
199	 * </ul>
200	 * <h2>Examples</h2>
201	 * Here's how to listen for when a drag moves over a drop:
202	 * @codestart
203	 * $('.drop').live("dropover", function(ev, drop, drag){
204	 *   $(this).addClass("drop-over")
205	 * })
206	 * @codeend
207	 * A bit more complex example:
208	 * @demo jquery/event/drop/drop.html 1000
209	 * @constructor
210	 * The constructor is never called directly.
211	 */
212	$.Drop = function(callbacks, element){
213		jQuery.extend(this,callbacks);
214		this.element = element;
215	}
216	$.each(eventNames, function(){
217			event.special[this] = {
218				add: function( handleObj ) {
219					//add this element to the compiles list
220					var el = $(this), current = (el.data("dropEventCount") || 0);
221					el.data("dropEventCount",  current+1   )
222					if(current==0){
223						$.Drop.addElement(this);
224					}
225				},
226				remove: function() {
227					var el = $(this), current = (el.data("dropEventCount") || 0);
228					el.data("dropEventCount",  current-1   )
229					if(current<=1){
230						$.Drop.removeElement(this);
231					}
232				}
233			}
234	})
235	$.extend($.Drop,{
236		lowerName: "drop",
237		_elements: [], //elements that are listening for drops
238		_responders: [], //potential drop points
239		last_active: [],
240		endName: "dropon",
241		addElement: function( el ) {
242			//check other elements
243			for(var i =0; i < this._elements.length ; i++  ){
244				if(el ==this._elements[i]) return;
245			}
246			this._elements.push(el);
247		},
248		removeElement: function( el ) {
249			 for(var i =0; i < this._elements.length ; i++  ){
250				if(el == this._elements[i]){
251					this._elements.splice(i,1)
252					return;
253				}
254			}
255		},
256		/**
257		* @hide
258		* For a list of affected drops, sorts them by which is deepest in the DOM first.
259		*/ 
260		sortByDeepestChild: function( a, b ) {
261			var compare = a.element.compare(b.element);
262			if(compare & 16 || compare & 4) return 1;
263			if(compare & 8 || compare & 2) return -1;
264			return 0;
265		},
266		/**
267		 * @hide
268		 * Tests if a drop is within the point.
269		 */
270		isAffected: function( point, moveable, responder ) {
271			return ((responder.element != moveable.element) && (responder.element.within(point[0], point[1], responder).length == 1));
272		},
273		/**
274		 * @hide
275		 * Calls dropout and sets last active to null
276		 * @param {Object} drop
277		 * @param {Object} drag
278		 * @param {Object} event
279		 */
280		deactivate: function( responder, mover, event ) {
281			mover.out(event, responder)
282			responder.callHandlers(this.lowerName+'out',responder.element[0], event, mover)
283		}, 
284		/**
285		 * @hide
286		 * Calls dropover
287		 * @param {Object} drop
288		 * @param {Object} drag
289		 * @param {Object} event
290		 */
291		activate: function( responder, mover, event ) { //this is where we should call over
292			mover.over(event, responder)
293			//this.last_active = responder;
294			responder.callHandlers(this.lowerName+'over',responder.element[0], event, mover);
295		},
296		move: function( responder, mover, event ) {
297			responder.callHandlers(this.lowerName+'move',responder.element[0], event, mover)
298		},
299		/**
300		 * Gets all elements that are droppable, adds them
301		 */
302		compile: function( event, drag ) {
303			var el, drops, selector, sels;
304			this.last_active = [];
305			for(var i=0; i < this._elements.length; i++){ //for each element
306				el = this._elements[i]
307				var drops = $.event.findBySelector(el, eventNames)
308
309				for(selector in drops){ //find the selectors
310					sels = selector ? jQuery(selector, el) : [el];
311					for(var e= 0; e < sels.length; e++){ //for each found element, create a drop point
312						jQuery.removeData(sels[e],"offset");
313						this.add(sels[e], new this(drops[selector]), event, drag);
314					}
315				}
316			}
317			
318		},
319		add: function( element, callbacks, event, drag ) {
320			element = jQuery(element);
321			var responder = new $.Drop(callbacks, element);
322			responder.callHandlers(this.lowerName+'init', element[0], event, drag)
323			if(!responder._canceled){
324				this._responders.push(responder);
325			}
326		},
327		show: function( point, moveable, event ) {
328			var element = moveable.element;
329			if(!this._responders.length) return;
330			
331			var respondable, 
332				affected = [], 
333				propagate = true, 
334				i,j, la, toBeActivated, aff, 
335				oldLastActive = this.last_active;
336				
337			for(var d =0 ; d < this._responders.length; d++ ){
338				
339				if(this.isAffected(point, moveable, this._responders[d])){
340					affected.push(this._responders[d]);  
341				}
342					 
343			}
344			
345			affected.sort(this.sortByDeepestChild); //we should only trigger on lowest children
346			event.stopRespondPropagate = function(){
347				propagate = false;
348			}
349			//deactivate everything in last_active that isn't active
350			toBeActivated = affected.slice();
351			this.last_active = affected;
352			for (j = 0; j < oldLastActive.length; j++) {
353				la = oldLastActive[j]
354				i = 0;
355				while((aff = toBeActivated[i])){
356					if(la == aff){
357						toBeActivated.splice(i,1);break;
358					}else{
359						i++;
360					}
361				}
362				if(!aff){
363					this.deactivate(la, moveable, event);
364				}
365				if(!propagate) return;
366			}
367			for(var i =0; i < toBeActivated.length; i++){
368				this.activate(toBeActivated[i], moveable, event);
369				if(!propagate) return;
370			}
371			//activate everything in affected that isn't in last_active
372			
373			for (i = 0; i < affected.length; i++) {
374				this.move(affected[i], moveable, event);
375				
376				if(!propagate) return;
377			}
378		},
379		end: function( event, moveable ) {
380			var responder, la;
381			for(var r =0; r<this._responders.length; r++){
382				this._responders[r].callHandlers(this.lowerName+'end', null, event, moveable);
383			}
384			//go through the actives ... if you are over one, call dropped on it
385			for(var i = 0; i < this.last_active.length; i++){
386				la = this.last_active[i]
387				if( this.isAffected(event.vector(), moveable, la)  && la[this.endName]){
388					la.callHandlers(this.endName, null, event, moveable);
389				}
390			}
391			
392			
393			this.clear();
394		},
395		/**
396		 * Called after dragging has stopped.
397		 * @hide
398		 */
399		clear: function() {
400		  
401		  this._responders = [];
402		}
403	})
404	$.Drag.responder = $.Drop;
405	
406	$.extend($.Drop.prototype,{
407		callHandlers: function( method, el, ev, drag ) {
408			var length = this[method] ? this[method].length : 0
409			for(var i =0; i < length; i++){
410				this[method][i].call(el || this.element[0], ev, this, drag)
411			}
412		},
413		/**
414		 * Caches positions of draggable elements.  This should be called in dropinit.  For example:
415		 * @codestart
416		 * dropinit: function( el, ev, drop ) { drop.cache_position() }
417		 * @codeend
418		 */
419		cache: function( value ) {
420			this._cache = value != null ? value : true;
421		},
422		/**
423		 * Prevents this drop from being dropped on.
424		 */
425		cancel: function() {
426			this._canceled = true;
427		}
428	} )
429})(jQuery)