xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / bean / support / syn / drag / drag.js

7.4 KB
1(function(){
2	// document body has to exists for this test
3
4	(function(){
5		if (!document.body) {
6			setTimeout(arguments.callee, 1)
7			return;
8		}
9		var div = document.createElement('div')
10		document.body.appendChild(div);
11		Syn.helpers.extend(div.style, {
12			width: "100px",
13			height: "10000px",
14			backgroundColor: "blue",
15			position: "absolute",
16			top: "10px",
17			left: "0px",
18			zIndex: 19999
19		});
20		document.body.scrollTop = 11;
21		if(!document.elementFromPoint){
22			return;
23		}
24		var el = document.elementFromPoint(3, 1)
25		if (el == div) {
26			Syn.support.elementFromClient = true;
27		}
28		else {
29			Syn.support.elementFromPage = true;
30		}
31		document.body.removeChild(div);
32		document.body.scrollTop = 0;
33	})();
34	
35	
36	//gets an element from a point
37	var elementFromPoint = function(point, element){
38		var clientX = point.clientX, 
39			clientY = point.clientY, 
40			win = Syn.helpers.getWindow(element),
41			el;
42		
43		if (Syn.support.elementFromPage) {
44			var off = Syn.helpers.scrollOffset(win);
45			clientX = clientX + off.left; //convert to pageX
46			clientY = clientY + off.top; //convert to pageY
47		}
48		el = win.document.elementFromPoint ? win.document.elementFromPoint(clientX, clientY) : element;
49		if(el === win.document.documentElement && (point.clientY < 0 || point.clientX < 0 ) ){
50			return element;
51		}else{
52			return el;
53		}
54	}, //creates an event at a certain point
55	createEventAtPoint = function(event, point, element){
56		var el = elementFromPoint(point, element)
57		Syn.trigger(event, point, el || element)
58		return el;
59	}, // creates a mousemove event, but first triggering mouseout / mouseover if appropriate
60	mouseMove = function(point, element, last){
61		var el = elementFromPoint(point, element)
62		if (last != el && el && last) {
63			var options = Syn.helpers.extend({},point);
64			options.relatedTarget = el;
65			Syn.trigger("mouseout", options, last);
66			options.relatedTarget = last;
67			Syn.trigger("mouseover", options, el);
68		}
69		
70		Syn.trigger("mousemove", point, el || element)
71		return el;
72	}, // start and end are in clientX, clientY
73	startMove = function(start, end, duration, element, callback){
74		var startTime = new Date(), 
75			distX = end.clientX - start.clientX, 
76			distY = end.clientY - start.clientY, 
77			win = Syn.helpers.getWindow(element), 
78			current = elementFromPoint(start, element), 
79			cursor = win.document.createElement('div'),
80			calls = 0;
81		move = function(){
82			//get what fraction we are at
83			var now = new Date(), 
84				scrollOffset = Syn.helpers.scrollOffset(win), 
85				fraction = ( calls == 0 ? 0 : now - startTime) / duration, 
86				options = {
87					clientX: distX * fraction + start.clientX,
88					clientY: distY * fraction + start.clientY
89				};
90			calls++;
91			if (fraction < 1) {
92				Syn.helpers.extend(cursor.style, {
93					left: (options.clientX + scrollOffset.left + 2) + "px",
94					top: (options.clientY + scrollOffset.top + 2) + "px"
95				})
96				current = mouseMove(options, element, current)
97				setTimeout(arguments.callee, 15)
98			}
99			else {
100				current = mouseMove(end, element, current);
101				win.document.body.removeChild(cursor)
102				callback();
103			}
104		}
105		Syn.helpers.extend(cursor.style, {
106			height: "5px",
107			width: "5px",
108			backgroundColor: "red",
109			position: "absolute",
110			zIndex: 19999,
111			fontSize: "1px"
112		})
113		win.document.body.appendChild(cursor)
114		move();
115	}, 
116	startDrag = function(start, end, duration, element, callback){
117		createEventAtPoint("mousedown", start, element);
118		startMove(start, end, duration, element, function(){
119			createEventAtPoint("mouseup", end, element);
120			callback();
121		})
122	},
123	center = function(el){
124		var j = Syn.jquery()(el),
125		o = j.offset();
126		return{
127			pageX: o.left + (j.width() / 2),
128			pageY: o.top + (j.height() / 2)
129		}
130	},
131	convertOption = function(option, win, from){
132		var page = /(\d+)[x ](\d+)/,
133			client = /(\d+)X(\d+)/,
134			relative = /([+-]\d+)[xX ]([+-]\d+)/
135		//check relative "+22x-44"
136		if (typeof option == 'string' && relative.test(option) && from) {
137			var cent = center(from),
138				parts = option.match(relative);
139			option = {
140				pageX: cent.pageX + parseInt(parts[1]),
141				pageY: cent.pageY +parseInt(parts[2])
142			}
143		}
144		if (typeof option == 'string' && page.test(option)) {
145			var parts = option.match(page)
146			option = {
147				pageX: parseInt(parts[1]),
148				pageY: parseInt(parts[2])
149			}
150		}
151		if (typeof option == 'string' && client.test(option)) {
152			var parts = option.match(client)
153			option = {
154				clientX: parseInt(parts[1]),
155				clientY: parseInt(parts[2])
156			}
157		}
158		if (typeof option == 'string') {
159			option = Syn.jquery()(option, win.document)[0];
160		}
161		if (option.nodeName) {
162			option = center(option)
163		}
164		if (option.pageX) {
165			var off = Syn.helpers.scrollOffset(win);
166			option = {
167				clientX: option.pageX - off.left,
168				clientY: option.pageY - off.top
169			}
170		}
171		return option;
172	}
173/**
174 * @add Syn prototype
175 */	
176Syn.helpers.extend(Syn.init.prototype,{
177	/**
178	 * @function move
179	 * Moves the cursor from one point to another.  
180	 * <h3>Quick Example</h3>
181	 * The following moves the cursor from (0,0) in
182	 * the window to (100,100) in 1 second.
183	 * @codestart
184	 * Syn.move(
185	 *      {
186	 *        from: {clientX: 0, clientY: 0},
187	 *        to: {clientX: 100, clientY: 100},
188	 *        duration: 1000
189	 *      },
190	 *      document.document)
191	 * @codeend
192	 * <h2>Options</h2>
193	 * There are many ways to configure the endpoints of the move.
194	 * 
195	 * <h3>PageX and PageY</h3>
196	 * If you pass pageX or pageY, these will get converted
197	 * to client coordinates.
198	 * @codestart
199	 * Syn.move(
200	 *      {
201	 *        from: {pageX: 0, pageY: 0},
202	 *        to: {pageX: 100, pageY: 100}
203	 *      },
204	 *      document.document)
205	 * @codeend
206	 * <h3>String Coordinates</h3>
207	 * You can set the pageX and pageY as strings like:
208	 * @codestart
209	 * Syn.move(
210	 *      {
211	 *        from: "0x0",
212	 *        to: "100x100"
213	 *      },
214	 *      document.document)
215	 * @codeend
216	 * <h3>Element Coordinates</h3>
217	 * If jQuery is present, you can pass an element as the from or to option
218	 * and the coordinate will be set as the center of the element.
219	 * @codestart
220	 * Syn.move(
221	 *      {
222	 *        from: $(".recipe")[0],
223	 *        to: $("#trash")[0]
224	 *      },
225	 *      document.document)
226	 * @codeend
227	 * <h3>Query Strings</h3>
228	 * If jQuery is present, you can pass a query string as the from or to option.
229	 * @codestart
230	 * Syn.move(
231	 *      {
232	 *        from: ".recipe",
233	 *        to: "#trash"
234	 *      },
235	 *      document.document)
236	 * @codeend   
237	 * <h3>No From</h3>
238	 * If you don't provide a from, the element argument passed to Syn is used.
239	 * @codestart
240	 * Syn.move(
241	 *      { to: "#trash" },
242	 *      'myrecipe')
243	 * @codeend  
244	 * @param {Object} options
245	 * @param {HTMLElement} from
246	 * @param {Function} callback
247	 */
248	_move: function( options, from, callback ) {
249		//need to convert if elements
250		var win = Syn.helpers.getWindow(from), 
251			fro = convertOption(options.from || from, win), 
252			to = convertOption(options.to || options, win);
253		
254		startMove(fro, to, options.duration || 500, from, callback);
255	},
256	/**
257	 * @function drag
258	 * Creates a mousedown and drags from one point to another.  
259	 * Check out [Syn.prototype.move move] for API details.
260	 * 
261	 * @param {Object} options
262	 * @param {Object} from
263	 * @param {Object} callback
264	 */
265	_drag: function( options, from, callback ) {
266		//need to convert if elements
267		var win = Syn.helpers.getWindow(from), 
268			fro = convertOption(options.from || from, win, from), 
269			to = convertOption(options.to || options, win, from);
270		
271		startDrag(fro, to, options.duration || 500, from, callback);
272	}
273})
274}())