| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | /********************************************************************** | 
					
						
							| 
									
										
										
										
											2013-01-24 03:12:01 +04:00
										 |  |  | * JavaScript Lib | 
					
						
							|  |  |  | * at this point this is just a place I put most of the generic stuff I  | 
					
						
							|  |  |  | * use. | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * P.S. the name "jli" just stands for Java script LIb, like how it  | 
					
						
							|  |  |  | * looks... | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | **********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-26 22:48:59 +04:00
										 |  |  | //var DEBUG = DEBUG != null ? DEBUG : true
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-09 07:17:40 +04:00
										 |  |  | // this will create a function that will cycle through a class_list on elem 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | // calling the optional callbacks before and/or after.
 | 
					
						
							| 
									
										
										
										
											2013-02-09 07:17:40 +04:00
										 |  |  | // if class_list is given as a string, then this will create a toggler that 
 | 
					
						
							|  |  |  | // will turn the given class on the element on and off.
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | //
 | 
					
						
							|  |  |  | // elem is a jquery compatible object; default use-case: a css selector.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-10 03:05:15 +04:00
										 |  |  | // if class_list is a string, the resulting function understands the 
 | 
					
						
							| 
									
										
										
										
											2013-02-09 07:17:40 +04:00
										 |  |  | // folowing arguments:
 | 
					
						
							|  |  |  | // 	- <index>		: 0 for 'off' and 1 for 'on' (see below)
 | 
					
						
							| 
									
										
										
										
											2013-02-09 16:26:17 +04:00
										 |  |  | // 	- 'on'			: switch mode on -- add class
 | 
					
						
							|  |  |  | // 	- 'off'			: switch mode off -- remove class
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | // 	- '?'			: return current state ('on'|'off')
 | 
					
						
							|  |  |  | // 	- no arguments	: toggle the state
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-10 03:05:15 +04:00
										 |  |  | // otherwise, if class_list is a list of strings:
 | 
					
						
							| 
									
										
										
										
											2013-02-09 07:17:40 +04:00
										 |  |  | //  - <index>		: explicitly set the state to index in class_list
 | 
					
						
							|  |  |  | //  - <class-name>	: explicitly set a class from the list
 | 
					
						
							|  |  |  | // 	- '?'			: return current state ('on'|'off')
 | 
					
						
							|  |  |  | // 	- no arguments	: set next state in cycle
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-09 16:26:17 +04:00
										 |  |  | // NOTE: a special class name 'none' means no class is set, if it is present 
 | 
					
						
							|  |  |  | // 		in the class_list then that state will be with all other state 
 | 
					
						
							|  |  |  | // 		classes removed.
 | 
					
						
							| 
									
										
										
										
											2013-02-09 07:17:40 +04:00
										 |  |  | // NOTE: <class-name> must be an exact match to a string given in class_list
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | // NOTE: of only one callback is given then it will be called after the 
 | 
					
						
							| 
									
										
										
										
											2013-02-09 16:26:17 +04:00
										 |  |  | // 		class change...
 | 
					
						
							|  |  |  | // 		a way around this is to pass an empty function as callback_b
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | function createCSSClassToggler(elem, class_list, callback_a, callback_b){ | 
					
						
							|  |  |  | 	var bool_action = false | 
					
						
							|  |  |  | 	if(typeof(class_list) == typeof('')){ | 
					
						
							|  |  |  | 		class_list = ['none', class_list] | 
					
						
							|  |  |  | 		bool_action = true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 	if(callback_b == null){ | 
					
						
							|  |  |  | 		var callback_pre = null | 
					
						
							|  |  |  | 		var callback_post = callback_a | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		var callback_pre = callback_a | 
					
						
							|  |  |  | 		var callback_post = callback_b | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// XXX make this generic...
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 	var func = function(action){ | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 		elem = $(elem) | 
					
						
							|  |  |  | 		// option number...
 | 
					
						
							|  |  |  | 		if(typeof(action) == typeof(1)){ | 
					
						
							|  |  |  | 			// range check...
 | 
					
						
							|  |  |  | 			if(action < 0 || action >= class_list.length){ | 
					
						
							|  |  |  | 				return null | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if(bool_action){ | 
					
						
							|  |  |  | 				action = action == 0 ? 'off' : 'on' | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				action = class_list[action] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// we need to get the current state...
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 		if(action == null || action == '?'){ | 
					
						
							|  |  |  | 			// get current state...
 | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 			var cur = 'none' | 
					
						
							|  |  |  | 			for(var i=0; i < class_list.length; i++){ | 
					
						
							|  |  |  | 				if(elem.hasClass(class_list[i])){ | 
					
						
							|  |  |  | 					cur = class_list[i] | 
					
						
							|  |  |  | 					break | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}  | 
					
						
							|  |  |  | 			// just asking for info...
 | 
					
						
							|  |  |  | 			if(action == '?'){ | 
					
						
							|  |  |  | 				return bool_action ? (cur == 'none' ? 'off' : 'on') : cur | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// invalid action...
 | 
					
						
							|  |  |  | 		} else if((bool_action && ['on', 'off'].indexOf(action) == -1) | 
					
						
							|  |  |  | 				|| (!bool_action && class_list.indexOf(action) == -1)){ | 
					
						
							|  |  |  | 			return null | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var cls = bool_action ? class_list[1] : action | 
					
						
							|  |  |  | 		// get the right class...
 | 
					
						
							|  |  |  | 		if(action == null){ | 
					
						
							|  |  |  | 			var i = class_list.indexOf(cur)+1 | 
					
						
							|  |  |  | 			i = i == -1 ? 0 : i | 
					
						
							|  |  |  | 			i = i == class_list.length ? 0 : i | 
					
						
							|  |  |  | 			cls = class_list[i] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(bool_action){ | 
					
						
							|  |  |  | 				action = cls == 'none' ? 'off' : 'on' | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				action = cls | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// pre callback...
 | 
					
						
							|  |  |  | 		if(callback_a != null){ | 
					
						
							|  |  |  | 			callback_a(action) | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 		// update the element...
 | 
					
						
							|  |  |  | 		elem.removeClass(class_list.join(' ')) | 
					
						
							|  |  |  | 		if(cls != 'none' && action != 'off'){ | 
					
						
							|  |  |  | 			elem.addClass(cls) | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 		// post callback...
 | 
					
						
							|  |  |  | 		if(callback_b != null){ | 
					
						
							|  |  |  | 			callback_b(action) | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return action | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	func.class_list = class_list | 
					
						
							|  |  |  | 	if(bool_action){ | 
					
						
							|  |  |  | 		func.doc = 'With no arguments this will toggle between "on" and '+ | 
					
						
							|  |  |  | 			'"off".\n'+ | 
					
						
							|  |  |  | 			'If either "on" or "off" are given then this will switch '+ | 
					
						
							|  |  |  | 			'to that mode.\n'+ | 
					
						
							|  |  |  | 			'If "?" is given, this will return either "on" or "off" '+ | 
					
						
							|  |  |  | 			'depending on the current state.' | 
					
						
							|  |  |  | 	}else{ | 
					
						
							|  |  |  | 		func.doc = 'With no arguments this will toggle between '+ | 
					
						
							|  |  |  | 			class_list +' in cycle.\n' +  | 
					
						
							|  |  |  | 			'if any of the state names or its number is given then that '+ | 
					
						
							|  |  |  | 			'state is switched on.'+ | 
					
						
							|  |  |  | 			'If "?" is given, this will return the current state.' | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 	return func | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-09 01:47:04 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | // show a jQuary opject in viewer overlay...
 | 
					
						
							|  |  |  | // XXX need to set .scrollTop(0) when showing different UI... 
 | 
					
						
							|  |  |  | // 		...and not set it when the UI is the same
 | 
					
						
							|  |  |  | // XXX this must create it's own overlay...
 | 
					
						
							|  |  |  | function showInOverlay(obj){ | 
					
						
							|  |  |  | 	obj.click(function(){ return false }) | 
					
						
							|  |  |  | 	// XXX 
 | 
					
						
							|  |  |  | 	$('.viewer').addClass('overlay-mode') | 
					
						
							|  |  |  | 	// clean things up...
 | 
					
						
							|  |  |  | 	$('.overlay .content').children().remove() | 
					
						
							|  |  |  | 	// put it in the overlay...
 | 
					
						
							|  |  |  | 	$('.overlay .content').append(obj) | 
					
						
							|  |  |  | 	// prepare the overlay...
 | 
					
						
							|  |  |  | 	$('.overlay') | 
					
						
							|  |  |  | 		.one('click', function(){ | 
					
						
							|  |  |  | 			$('.overlay') | 
					
						
							|  |  |  | 				.fadeOut(function(){ | 
					
						
							|  |  |  | 					$('.overlay .content') | 
					
						
							|  |  |  | 						.children() | 
					
						
							|  |  |  | 							.remove() | 
					
						
							|  |  |  | 					$('.overlay-mode').removeClass('overlay-mode') | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.fadeIn() | 
					
						
							|  |  |  | 	return obj | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function overlayMessage(text){ | 
					
						
							|  |  |  | 	return showInOverlay($('<div class="overlay-message">' +text+ '</div>')) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function unanimated(obj, func, time){ | 
					
						
							|  |  |  | 	return function(){ | 
					
						
							|  |  |  | 		if(time == null){ | 
					
						
							|  |  |  | 			time = 5 | 
					
						
							|  |  |  | 		}	 | 
					
						
							| 
									
										
										
										
											2013-02-13 17:27:04 +04:00
										 |  |  | 		obj = $(obj) | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 		obj.addClass('unanimated') | 
					
						
							|  |  |  | 		var res = func.apply(func, arguments) | 
					
						
							|  |  |  | 		setTimeout(function(){obj.removeClass('unanimated')}, time) | 
					
						
							|  |  |  | 		return res | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-26 21:55:38 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 19:56:06 +04:00
										 |  |  | var USE_3D_TRANSFORM = true | 
					
						
							| 
									
										
										
										
											2013-02-22 18:44:11 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | // NOTE: at this point this works only on the X axis...
 | 
					
						
							|  |  |  | function setElementTransform(elem, offset, scale){ | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 	elem = $(elem) | 
					
						
							| 
									
										
										
										
											2013-02-22 18:44:11 +04:00
										 |  |  | 	var t3d = USE_3D_TRANSFORM ? 'translateZ(0px)' : '' | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 	if(offset == null){ | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 		offset = getElementShift(elem) | 
					
						
							|  |  |  | 	// number -- only the x coord...
 | 
					
						
							|  |  |  | 	} else if(typeof(offset) == typeof(1)){ | 
					
						
							|  |  |  | 		offset = { | 
					
						
							|  |  |  | 			left: offset, | 
					
						
							|  |  |  | 			top: 0 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	// array...
 | 
					
						
							|  |  |  | 	} else if(offset.indexOf){ | 
					
						
							|  |  |  | 		offset = { | 
					
						
							|  |  |  | 			left: offset[0] ? offset[0] : 0, | 
					
						
							|  |  |  | 			top: offset[1] ? offset[1] : 0 | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if(scale == null){ | 
					
						
							|  |  |  | 		var scale = getElementScale(elem) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(USE_TRANSFORM){ | 
					
						
							| 
									
										
										
										
											2013-02-22 18:44:11 +04:00
										 |  |  | 		var transform = 'translate('+ offset.left +'px, '+ offset.top +'px) scale('+ scale +') ' + t3d | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 		elem.css({ | 
					
						
							|  |  |  | 			'-ms-transform' : transform,  | 
					
						
							|  |  |  | 			'-webkit-transform' : transform,  | 
					
						
							|  |  |  | 			'-moz-transform' : transform,  | 
					
						
							|  |  |  | 			'-o-transform' : transform,  | 
					
						
							|  |  |  | 			'transform' : transform,  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// XXX can we avoid this here?? 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 			left: 0, | 
					
						
							|  |  |  | 			// XXX is this correct???
 | 
					
						
							|  |  |  | 			top: '' | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-02-22 18:44:11 +04:00
										 |  |  | 		var transform = 'translate(0px, 0px) scale('+ scale +') ' + t3d | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 		elem.css({ | 
					
						
							|  |  |  | 			// NOTE: this will be wrong during a transition, that's why we 
 | 
					
						
							|  |  |  | 			// 		can pass the pre-calculated offset as an argument...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 			left: offset.left, | 
					
						
							|  |  |  | 			top: offset.top, | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// XXX can we avoid this here?? 
 | 
					
						
							|  |  |  | 			'-ms-transform' : transform,  | 
					
						
							|  |  |  | 			'-webkit-transform' : transform,  | 
					
						
							|  |  |  | 			'-moz-transform' : transform,  | 
					
						
							|  |  |  | 			'-o-transform' : transform,  | 
					
						
							|  |  |  | 			'transform' : transform,  | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return elem | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | // Return a scale value for the given element(s).
 | 
					
						
							|  |  |  | // NOTE: this will only return a single scale value...
 | 
					
						
							|  |  |  | function getElementScale(elem){ | 
					
						
							| 
									
										
										
										
											2013-02-14 18:25:00 +04:00
										 |  |  | 	elem = $(elem) | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 	//var transform = elem.css('transform')
 | 
					
						
							|  |  |  | 	var vendors = ['o', 'moz', 'ms', 'webkit'] | 
					
						
							|  |  |  | 	var transform = elem.css('transform') | 
					
						
							|  |  |  | 	var res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// go through vendor prefixes... (hate this!)
 | 
					
						
							|  |  |  | 	if(!transform || transform == 'none'){ | 
					
						
							|  |  |  | 		for(var i in vendors){ | 
					
						
							|  |  |  | 			transform = elem.css('-' + vendors[i] + '-transform') | 
					
						
							|  |  |  | 			if(transform && transform != 'none'){ | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// no transform is set...
 | 
					
						
							|  |  |  | 	if(!transform || transform == 'none'){ | 
					
						
							|  |  |  | 		return 1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// get the scale value -- first argument of scale/matrix...
 | 
					
						
							|  |  |  | 	return parseFloat((/(scale|matrix)\(([^,]*),.*\)/).exec(transform)[2]) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | // XXX account for other transitions...
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | function setElementScale(elem, scale){ | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 	return setElementTransform(elem, null, scale) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getElementShift(elem){ | 
					
						
							|  |  |  | 	elem = $(elem) | 
					
						
							|  |  |  | 	// using transform...
 | 
					
						
							|  |  |  | 	if(USE_TRANSFORM){ | 
					
						
							|  |  |  | 		var vendors = ['o', 'moz', 'ms', 'webkit'] | 
					
						
							|  |  |  | 		var transform = elem.css('transform') | 
					
						
							|  |  |  | 		var res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// go through vendor prefixes... (hate this!)
 | 
					
						
							|  |  |  | 		if(!transform || transform == 'none'){ | 
					
						
							|  |  |  | 			for(var i in vendors){ | 
					
						
							|  |  |  | 				transform = elem.css('-' + vendors[i] + '-transform') | 
					
						
							|  |  |  | 				if(transform && transform != 'none'){ | 
					
						
							|  |  |  | 					break | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// no transform is set...
 | 
					
						
							|  |  |  | 		if(!transform || transform == 'none'){ | 
					
						
							|  |  |  | 			return {left: 0, top: 0} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 		res = /(translate\(|matrix\([^,]*,[^,]*,[^,]*,[^,]*,)([^,]*),([^\)]*)\)/.exec(transform) | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 		return { | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 			left: parseFloat(res[2]), | 
					
						
							|  |  |  | 			top: parseFloat(res[3]) | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	// using left...
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return { | 
					
						
							|  |  |  | 			left: elem.position().left, | 
					
						
							| 
									
										
										
										
											2013-02-20 04:37:47 +04:00
										 |  |  | 			top: elem.position().top | 
					
						
							| 
									
										
										
										
											2013-02-19 19:34:03 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 18:04:56 +04:00
										 |  |  | function setTransitionEasing(elem, ease){ | 
					
						
							|  |  |  | 	if(typeof(ms) == typeof(0)){ | 
					
						
							|  |  |  | 		ms = ms + 'ms' | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return elem.css({ | 
					
						
							|  |  |  | 		'transition-timing-function': ease,  | 
					
						
							|  |  |  | 		'-moz-transition-timing-function': ease, | 
					
						
							|  |  |  | 		'-o-transition-timing-function': ease, | 
					
						
							|  |  |  | 		'-ms-transition-timing-function': ease, | 
					
						
							|  |  |  | 		'-webkit-transition-timing-function': ease | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-07 03:24:57 +04:00
										 |  |  | function setTransitionDuration(elem, ms){ | 
					
						
							|  |  |  | 	if(typeof(ms) == typeof(0)){ | 
					
						
							|  |  |  | 		ms = ms + 'ms' | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return elem.css({ | 
					
						
							|  |  |  | 		'transition-duration': ms,  | 
					
						
							|  |  |  | 		'-moz-transition-duration': ms, | 
					
						
							|  |  |  | 		'-o-transition-duration': ms, | 
					
						
							|  |  |  | 		'-ms-transition-duration': ms, | 
					
						
							|  |  |  | 		'-webkit-transition-duration': ms | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /************************************************* keyboard handler **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NOTE: don't understand why am I the one who has to write this...
 | 
					
						
							|  |  |  | var SPECIAL_KEYS = { | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 	// Special Keys...
 | 
					
						
							|  |  |  | 	9:		'Tab',		33:		'PgUp',		45:		'Ins',		 | 
					
						
							|  |  |  | 	13:		'Enter',	34:		'PgDown',	46:		'Del',		 | 
					
						
							|  |  |  | 	16:		'Shift',	35:		'End',		80:		'Backspace', | 
					
						
							|  |  |  | 	17:		'Ctrl',		36:		'Home',		91:		'Win',		 | 
					
						
							|  |  |  | 	18:		'Alt',		37:		'Right',	93:		'Menu',		 | 
					
						
							|  |  |  | 	20:		'Caps Lock',38:		'Up',	  | 
					
						
							|  |  |  | 	27:		'Esc',		39:		'Left',   | 
					
						
							|  |  |  | 	32:		'Space',	40:		'Down',   | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Function Keys...
 | 
					
						
							|  |  |  | 	112:	'F1',		116:	'F5',		120:	'F9',  | 
					
						
							|  |  |  | 	113:	'F2',		117:	'F6',		121:	'F10', | 
					
						
							|  |  |  | 	114:	'F3',		118:	'F7',		122:	'F11', | 
					
						
							|  |  |  | 	115:	'F4',		119:	'F8',		123:	'F12', | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // XXX some keys look really wrong...
 | 
					
						
							|  |  |  | function toKeyName(code){ | 
					
						
							|  |  |  | 	// check for special keys...
 | 
					
						
							|  |  |  | 	var k = SPECIAL_KEYS[code] | 
					
						
							|  |  |  | 	if(k != null){ | 
					
						
							|  |  |  | 		return k | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// chars...
 | 
					
						
							|  |  |  | 	k = String.fromCharCode(code) | 
					
						
							|  |  |  | 	if(k != ''){ | 
					
						
							|  |  |  | 		return k.toLowerCase() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return null | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // if set to false the event handlers will always return false...
 | 
					
						
							| 
									
										
										
										
											2013-01-27 23:56:30 +04:00
										 |  |  | var KEYBOARD_HANDLER_PROPAGATE = true | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Basic key format: | 
					
						
							| 
									
										
										
										
											2013-01-29 22:25:51 +04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  |  * 		<key-code> : <callback>, | 
					
						
							| 
									
										
										
										
											2013-01-29 22:25:51 +04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  |  * 		<key-code> : { | 
					
						
							|  |  |  |  * 			'default': <callback>, | 
					
						
							|  |  |  |  *			// a modifier can be any single modifier, like shift or a 
 | 
					
						
							|  |  |  |  *			// combination of modifers like 'ctrl+shift', given in order 
 | 
					
						
							|  |  |  |  *			// of priority.
 | 
					
						
							|  |  |  |  *			// supported modifiers are (in order of priority):
 | 
					
						
							|  |  |  |  *			//	- ctrl
 | 
					
						
							|  |  |  |  *			//	- alt
 | 
					
						
							|  |  |  |  *			//	- shift
 | 
					
						
							|  |  |  |  * 			<modifer>: [...] | 
					
						
							|  |  |  |  * 		}, | 
					
						
							| 
									
										
										
										
											2013-01-29 22:25:51 +04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  |  * 		<key-code> : [ | 
					
						
							|  |  |  |  *			// this can be any type of handler except for an alias...
 | 
					
						
							|  |  |  |  * 			<handler>,  | 
					
						
							|  |  |  |  * 			<doc> | 
					
						
							|  |  |  |  * 		], | 
					
						
							| 
									
										
										
										
											2013-01-29 22:25:51 +04:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  |  *		// alias...
 | 
					
						
							|  |  |  |  * 		<key-code-a> : <key-code-b>, | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * XXX might need to add meta information to generate sensible help... | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function makeKeyboardHandler(keybindings, unhandled){ | 
					
						
							|  |  |  | 	if(unhandled == null){ | 
					
						
							| 
									
										
										
										
											2013-01-31 02:46:02 +04:00
										 |  |  | 		//unhandled = function(){return false}
 | 
					
						
							|  |  |  | 		unhandled = function(){return KEYBOARD_HANDLER_PROPAGATE} | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return function(evt){ | 
					
						
							|  |  |  | 		var did_handling = false | 
					
						
							|  |  |  | 		var res = null | 
					
						
							|  |  |  | 		for(var mode in keybindings){ | 
					
						
							|  |  |  | 			if($(mode).length > 0){ | 
					
						
							|  |  |  | 				var bindings = keybindings[mode] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var key = evt.keyCode | 
					
						
							| 
									
										
										
										
											2013-01-31 02:46:02 +04:00
										 |  |  | 				if(bindings.ignore == '*'  | 
					
						
							|  |  |  | 						|| bindings.ignore != null && bindings.ignore.indexOf(key) != -1){ | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  | 					// return true
 | 
					
						
							| 
									
										
										
										
											2013-01-29 21:41:26 +04:00
										 |  |  | 					res = res == null ? true : res | 
					
						
							| 
									
										
										
										
											2013-01-27 22:41:06 +04:00
										 |  |  | 					did_handling = true | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// XXX ugly...
 | 
					
						
							|  |  |  | 				var modifers = evt.ctrlKey ? 'ctrl' : '' | 
					
						
							|  |  |  | 				modifers += evt.altKey ? (modifers != '' ? '+alt' : 'alt') : '' | 
					
						
							|  |  |  | 				modifers += evt.shiftKey ? (modifers != '' ? '+shift' : 'shift') : '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var handler = bindings[key] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// alias...
 | 
					
						
							|  |  |  | 				while (typeof(handler) == typeof(123)) { | 
					
						
							|  |  |  | 					handler = bindings[handler] | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// no handler...
 | 
					
						
							|  |  |  | 				if(handler == null){ | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// Array, lisp style with docs...
 | 
					
						
							|  |  |  | 				// XXX for some odd reason in chrome typeof([]) == typeof({})!!!
 | 
					
						
							|  |  |  | 				if(typeof(handler) == typeof([]) && handler.constructor.name == 'Array'){ | 
					
						
							|  |  |  | 					// we do not care about docs here, so just get the handler...
 | 
					
						
							|  |  |  | 					handler = handler[0] | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				// complex handler...
 | 
					
						
							|  |  |  | 				if(typeof(handler) == typeof({})){ | 
					
						
							|  |  |  | 					var callback = handler[modifers] | 
					
						
							|  |  |  | 					if(callback == null){ | 
					
						
							|  |  |  | 						callback = handler['default'] | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if(callback != null){ | 
					
						
							|  |  |  | 						res = callback() | 
					
						
							|  |  |  | 						did_handling = true | 
					
						
							|  |  |  | 						continue | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					// simple callback...
 | 
					
						
							|  |  |  | 					res = handler()  | 
					
						
							|  |  |  | 					did_handling = true | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(!did_handling){ | 
					
						
							|  |  |  | 			// key is unhandled by any modes...
 | 
					
						
							|  |  |  | 			return unhandled(key) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// XXX should we handle multiple hits???
 | 
					
						
							|  |  |  | 			return KEYBOARD_HANDLER_PROPAGATE&&res?true:false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | // click threshold in pixels, if the distance between start and end is 
 | 
					
						
							|  |  |  | // less than this, the whole event is considered a click and not a 
 | 
					
						
							|  |  |  | // drag/swipe...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | var CLICK_THRESHOLD = 10 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | // if the amount of time to wait beween start and end is greater than this
 | 
					
						
							|  |  |  | // the event is considered a long click.
 | 
					
						
							|  |  |  | // NOTE: this will not auto-fire the event, the user MUST release first.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | var LONG_CLICK_THRESHOLD = 400 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // the maximum amount of time between clicks to count them together.
 | 
					
						
							|  |  |  | // NOTE: if multi-clicks are disabled this has no effect.
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | // NOTE: this is reset by the timeout explicitly set in the handler...
 | 
					
						
							|  |  |  | // NOTE: this is the timeout between two consecutive clicks and not the total.
 | 
					
						
							|  |  |  | // NOTE: if multiple clicks are enabled this will introduce a lag after
 | 
					
						
							|  |  |  | // 		each click (while we wait for the next), so keep this as small 
 | 
					
						
							|  |  |  | // 		as possible.
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | var MULTI_CLICK_TIMEOUT = 200 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | // the amount of time between finger releases.
 | 
					
						
							|  |  |  | // NOTE: when this is passed all the fingers released before are ignored.
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | var MULTITOUCH_RELEASE_THRESHOLD = 100 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | // XXX add a resonable cancel scheme...
 | 
					
						
							|  |  |  | // 		... something similar to touch threshold but bigger...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:07:51 +04:00
										 |  |  | // XXX handle multiple touches...
 | 
					
						
							|  |  |  | // 		- timeout on lift to count fingers...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | // XXX setup basic styles for the contained element...
 | 
					
						
							|  |  |  | // XXX revise...
 | 
					
						
							|  |  |  | // XXX test on other devices...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:45:06 +04:00
										 |  |  | // XXX BUG: on landing a second finger while scrolling the things goes 
 | 
					
						
							|  |  |  | // 		haywhire...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | // 		...check if this is gone...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | // XXX add something like a scrollTo that would understand elements as 
 | 
					
						
							|  |  |  | // 		well as explicit positions.
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | // XXX split this into a seporate lib...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | function makeScrollHandler(root, config){ | 
					
						
							|  |  |  | 	root = $(root) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// local data...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 	var ignoring = false | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 	var cancelThreshold | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	var scrolled | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 	// XXX this and scroller.state are redundent...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	var scrolling = false | 
					
						
							|  |  |  | 	var touch = false | 
					
						
							|  |  |  | 	var touches = 0 | 
					
						
							|  |  |  | 	var start_x | 
					
						
							|  |  |  | 	var start_y | 
					
						
							| 
									
										
										
										
											2013-02-20 21:35:35 +04:00
										 |  |  | 	var start_t | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	var prev_x | 
					
						
							|  |  |  | 	var prev_y | 
					
						
							|  |  |  | 	var prev_t | 
					
						
							|  |  |  | 	var bounds | 
					
						
							|  |  |  | 	var shift | 
					
						
							|  |  |  | 	var scale | 
					
						
							|  |  |  | 	var x | 
					
						
							|  |  |  | 	var y | 
					
						
							|  |  |  | 	var t | 
					
						
							|  |  |  | 	var dx | 
					
						
							|  |  |  | 	var dy | 
					
						
							|  |  |  | 	var dt | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 	var max_dx = 0 | 
					
						
							|  |  |  | 	var max_dy = 0 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	function startMoveHandler(evt, callback){ | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		var options = scroller.options | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 		// ignore...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		if(options.ignoreElements  | 
					
						
							|  |  |  | 				&& $(evt.target).closest(options.ignoreElements).length > 0){ | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 			ignoring = true | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ignoring = false | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 05:45:06 +04:00
										 |  |  | 		if(event.touches != null){ | 
					
						
							|  |  |  | 			touch = true | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		cancelThreshold = options.scrollCancelThreshold | 
					
						
							| 
									
										
										
										
											2013-02-20 05:45:06 +04:00
										 |  |  | 		touches = touch ? event.touches.length : 1 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:07:51 +04:00
										 |  |  | 		// if we are already touching then just skip on this...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:45:06 +04:00
										 |  |  | 		// XXX test this...
 | 
					
						
							|  |  |  | 		if(touches > 1){ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:07:51 +04:00
										 |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		prev_t = event.timeStamp || Date.now(); | 
					
						
							| 
									
										
										
										
											2013-02-20 21:35:35 +04:00
										 |  |  | 		start_t = prev_t | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		if(options.autoCancelEvents){ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			bounds = { | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 				left: options.eventBounds, | 
					
						
							|  |  |  | 				right: root.width() - options.eventBounds, | 
					
						
							|  |  |  | 				top: options.eventBounds, | 
					
						
							|  |  |  | 				bottom: root.height() - options.eventBounds  | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 		//togglePageDragging('on')
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		scrolled = $(root.children()[0]) | 
					
						
							|  |  |  | 		setTransitionDuration(scrolled, 0) | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 		// XXX these two are redundant...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		scrolling = true | 
					
						
							|  |  |  | 		scroller.state = 'scrolling' | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		options.enabelStartEvent && root.trigger('userScrollStart') | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		shift = getElementShift(scrolled) | 
					
						
							|  |  |  | 		scale = getElementScale(scrolled) | 
					
						
							|  |  |  | 		// get the user coords...
 | 
					
						
							|  |  |  | 		prev_x = touch ? event.touches[0].pageX : evt.clientX | 
					
						
							|  |  |  | 		start_x = prev_x | 
					
						
							|  |  |  | 		prev_y = touch ? event.touches[0].pageY : evt.clientY | 
					
						
							|  |  |  | 		start_y = prev_y | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	// XXX try and make this adaptive to stay ahead of the lags...
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 	// NOTE: this does not support limiting the scroll, might be done in
 | 
					
						
							|  |  |  | 	// 		the future though.
 | 
					
						
							|  |  |  | 	// 		The way to go about this is to track scrolled size in the 
 | 
					
						
							|  |  |  | 	// 		callback...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	function moveHandler(evt){ | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 		if(ignoring){ | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		var options = scroller.options | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		evt.preventDefault() | 
					
						
							|  |  |  | 		t = event.timeStamp || Date.now(); | 
					
						
							|  |  |  | 		// get the user coords...
 | 
					
						
							|  |  |  | 		x = touch ? event.touches[0].pageX : evt.clientX | 
					
						
							|  |  |  | 		y = touch ? event.touches[0].pageY : evt.clientY | 
					
						
							| 
									
										
										
										
											2013-02-20 05:07:51 +04:00
										 |  |  | 		touches = touch ? event.touches.length : 1 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// XXX needs testing...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		// XXX do we need to account for scrollDisabled here???
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		// check scroll bounds...
 | 
					
						
							|  |  |  | 		if(bounds != null){ | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 			if(options.hScroll && (x <= bounds.left || x >= bounds.right) | 
					
						
							|  |  |  | 					|| options.vScroll && (y <= bounds.top || y >= bounds.bottom)){ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 				// XXX cancel the touch event and trigger the end handler...
 | 
					
						
							|  |  |  | 				return endMoveHandler(evt) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// do the actual scroll...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		if(!options.scrollDisabled && scrolling){ | 
					
						
							|  |  |  | 			if(options.hScroll){ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 				shift.left += x - prev_x | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 			if(options.vScroll){ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 				shift.top += y - prev_y | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			setElementTransform(scrolled, shift, scale) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// XXX these should be done every time the event is caught or 
 | 
					
						
							|  |  |  | 			// 		just while scrolling?
 | 
					
						
							|  |  |  | 			dx = x - prev_x | 
					
						
							|  |  |  | 			dy = y - prev_y | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			max_dx += Math.abs(dx) | 
					
						
							|  |  |  | 			max_dy += Math.abs(dy) | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			dt = t - prev_t | 
					
						
							|  |  |  | 			prev_x = x | 
					
						
							|  |  |  | 			prev_y = y | 
					
						
							|  |  |  | 			prev_t = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 			options.enableUserScrollEvent && root.trigger('userScroll') | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	function endMoveHandler(evt){ | 
					
						
							| 
									
										
										
										
											2013-02-20 21:35:35 +04:00
										 |  |  | 		t = event.timeStamp || Date.now(); | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 		touches = touch ? event.touches.length : 0 | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 		if(ignoring){ | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 			if(touches == 0){ | 
					
						
							|  |  |  | 				ignoring = false | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		var options = scroller.options | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		// XXX get real transition duration...
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 		scroller.resetTransitions() | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		x = touch ? event.changedTouches[0].pageX : evt.clientX | 
					
						
							|  |  |  | 		y = touch ? event.changedTouches[0].pageY : evt.clientY | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 		// check if we are canceling...
 | 
					
						
							|  |  |  | 		if(cancelThreshold  | 
					
						
							|  |  |  | 				&& Math.abs(start_x-x) < cancelThreshold  | 
					
						
							|  |  |  | 				&& Math.abs(start_y-y) < cancelThreshold | 
					
						
							|  |  |  | 				&& (max_dx > cancelThreshold  | 
					
						
							|  |  |  | 					|| max_dy > cancelThreshold)){ | 
					
						
							|  |  |  | 			scroller.state = 'canceling' | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		var data = { | 
					
						
							|  |  |  | 			orig_event: evt,  | 
					
						
							|  |  |  | 			scroller: scroller,  | 
					
						
							|  |  |  | 			speed: { | 
					
						
							|  |  |  | 				x: dx/dt, | 
					
						
							|  |  |  | 				y: dy/dt | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			distance: { | 
					
						
							|  |  |  | 				x: start_x-x, | 
					
						
							|  |  |  | 				y: start_y-y | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			duration: t-start_t,  | 
					
						
							|  |  |  | 			// current touches...
 | 
					
						
							|  |  |  | 			touches: touches, | 
					
						
							|  |  |  | 			clicks: null, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		// XXX stop only if no fingers are touching or let the callback decide...
 | 
					
						
							|  |  |  | 		//togglePageDragging('off')
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		// XXX update this with the new data model!!! (see below)
 | 
					
						
							|  |  |  | 		options.enableEndEvent && root.trigger('userScrollEnd', data) | 
					
						
							|  |  |  | 		if(options.callback  | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 				// XXX revise this....
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 				&& options.callback(data) === false  | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 				|| touches == 0){ | 
					
						
							|  |  |  | 			// cleanup and stop...
 | 
					
						
							|  |  |  | 			touch = false | 
					
						
							|  |  |  | 			scrolling = false | 
					
						
							|  |  |  | 			scroller.state = 'waiting' | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 			scrolled = null  | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			bounds = null | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			max_dx = 0 | 
					
						
							|  |  |  | 			max_dy = 0 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 07:19:51 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 	var scroller = { | 
					
						
							|  |  |  | 		options: { | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			// if one of these is false, it will restrict scrolling in 
 | 
					
						
							|  |  |  | 			// that direction. hScroll for horizontal and vScroll for 
 | 
					
						
							|  |  |  | 			// vertical.
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 			// NOTE: to disable scroll completely use scrollDisabled, see
 | 
					
						
							|  |  |  | 			// 		below for details.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			hScroll: true, | 
					
						
							|  |  |  | 			vScroll: true, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 			// this will disable scroll.
 | 
					
						
							|  |  |  | 			// NOTE: this is the same as setting both vScroll and hScroll 
 | 
					
						
							|  |  |  | 			// 		to false, but can be set and reset without affecting 
 | 
					
						
							|  |  |  | 			// 		the actual settings individually...
 | 
					
						
							|  |  |  | 			// NOTE: this takes priority over hScroll/vScroll.
 | 
					
						
							|  |  |  | 			scrollDisabled: false, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 			// sets the default transition settings while not scrolling...
 | 
					
						
							|  |  |  | 			transitionDuration: 200, | 
					
						
							|  |  |  | 			transitionEasing: 'ease', | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			// items to be ignored by the scroller...
 | 
					
						
							|  |  |  | 			// this is a jQuery compatible selector.
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 			ignoreElements: '.noScroll', | 
					
						
							|  |  |  | 			// this is the side of the rectangle in px, if the user moves
 | 
					
						
							|  |  |  | 			// out of it, and then returns back, the action will get cancelled.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			// i.e. the callback will get called with the "cancelling" state.
 | 
					
						
							|  |  |  | 			scrollCancelThreshold: 100, | 
					
						
							| 
									
										
										
										
											2013-02-20 06:33:23 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 			// these control weather in-scroll events will get triggered.
 | 
					
						
							|  |  |  | 			// NOTE: these may impact performance, especially the scroll 
 | 
					
						
							|  |  |  | 			// 		event, thus they need to enabled explicitly.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			enabelStartEvent: false, | 
					
						
							|  |  |  | 			enableUserScrollEvent: false, | 
					
						
							|  |  |  | 			enableEndEvent: false, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			// XXX padding within the target element moving out of which 
 | 
					
						
							|  |  |  | 			// 		will cancell the action...
 | 
					
						
							|  |  |  | 			// XXX needs testing...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			autoCancelEvents: false, | 
					
						
							|  |  |  | 			eventBounds: 5, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 			// callback to be called when the user lifts a finger/mouse.
 | 
					
						
							|  |  |  | 			// NOTE: this may happen before the scroll is done, for instance
 | 
					
						
							|  |  |  | 			// 		when one of several fingers participating in the action
 | 
					
						
							|  |  |  | 			// 		gets lifted.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			// NOTE: if this returns false explicitly, this will stop scrolling.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 			callback: postScrollCallback, | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 			// These are used by the default callback...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 			// if true then doubleClick and multiClick events will get 
 | 
					
						
							|  |  |  | 			// triggered.
 | 
					
						
							|  |  |  | 			// NOTE: this will introduce a lag needed to wait for next 
 | 
					
						
							|  |  |  | 			// 		clicks in a group.
 | 
					
						
							|  |  |  | 			// NOTE: when this is false, shortClick is triggered for every 
 | 
					
						
							|  |  |  | 			// 		single click separately.
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 			enableMultiClicks: false, | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 			// NOTE: if these are null, respective values from the env will
 | 
					
						
							|  |  |  | 			// 		be used.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 			clickThreshold: null, | 
					
						
							|  |  |  | 			longClickThreshold: null, | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 			multiClickTimeout: null, | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | 			multitouchTimeout: null, | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		// NOTE: this is updated live but not used by the system in any way...
 | 
					
						
							|  |  |  | 		state: 'stopped', | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 		root: root, | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		start: function(){ | 
					
						
							|  |  |  | 			this.state = 'waiting' | 
					
						
							|  |  |  | 			// XXX STUB: this makes starting the scroll a bit sluggish, 
 | 
					
						
							|  |  |  | 			//		find a faster way...
 | 
					
						
							|  |  |  | 			//togglePageDragging('on')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// NOTE: if we bind both touch and mouse events, on touch devices they 
 | 
					
						
							|  |  |  | 			//		might start interfering with each other...
 | 
					
						
							|  |  |  | 			if('ontouchmove' in window){ | 
					
						
							|  |  |  | 				root | 
					
						
							|  |  |  | 					.on('touchstart', startMoveHandler) | 
					
						
							|  |  |  | 					.on('touchmove', moveHandler)  | 
					
						
							|  |  |  | 					.on('touchend', endMoveHandler) | 
					
						
							|  |  |  | 					.on('touchcancel', endMoveHandler) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				root | 
					
						
							|  |  |  | 					.on('mousedown', startMoveHandler)  | 
					
						
							|  |  |  | 					.on('mousemove', moveHandler)  | 
					
						
							|  |  |  | 					.on('mouseup', endMoveHandler)  | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return this | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		stop: function(){ | 
					
						
							|  |  |  | 			if('ontouchmove' in window){ | 
					
						
							|  |  |  | 				root | 
					
						
							|  |  |  | 					.off('touchstart', startMoveHandler) | 
					
						
							|  |  |  | 					.off('touchmove', moveHandler)  | 
					
						
							|  |  |  | 					.off('touchend', endMoveHandler) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				root | 
					
						
							|  |  |  | 					.off('mousedown', startMoveHandler)  | 
					
						
							|  |  |  | 					.off('mousemove', moveHandler)  | 
					
						
							|  |  |  | 					.off('mouseup', endMoveHandler)  | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-02-20 07:15:17 +04:00
										 |  |  | 			this.state = 'stopped' | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 			return this | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		resetTransitions: function(){ | 
					
						
							|  |  |  | 			var scrolled = this.root.children().first() | 
					
						
							|  |  |  | 			setTransitionDuration(scrolled, this.options.transitionDuration) | 
					
						
							|  |  |  | 			setTransitionEasing(scrolled, this.options.transitionEasing) | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// merge the config with the defaults...
 | 
					
						
							|  |  |  | 	if(config != null){ | 
					
						
							|  |  |  | 		$.extend(scroller.options, config) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return scroller | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | // default callback...
 | 
					
						
							|  |  |  | // This will provide support for the folowing events on the scroll root
 | 
					
						
							|  |  |  | // element:
 | 
					
						
							|  |  |  | // 		- scrollCancelled
 | 
					
						
							| 
									
										
										
										
											2013-02-22 16:54:29 +04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | // 		- shortClick
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | // 		- doubleClick
 | 
					
						
							|  |  |  | // 		- multiClick
 | 
					
						
							|  |  |  | // 			this will store the number of clicks in data.clicks
 | 
					
						
							|  |  |  | // 		- longClick
 | 
					
						
							| 
									
										
										
										
											2013-02-22 16:54:29 +04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | // 		- swipeLeft
 | 
					
						
							|  |  |  | // 		- swipeRight
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | // 		- swipeUp
 | 
					
						
							|  |  |  | // 		- swipeDown
 | 
					
						
							| 
									
										
										
										
											2013-02-22 16:54:29 +04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | // 		- screenReleased
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | // NOTE: data.touches passed to the event is the number of touches 
 | 
					
						
							|  |  |  | // 		released within the multitouchTimeout.
 | 
					
						
							|  |  |  | // 		this differs from what postScrollCallback actually gets in the 
 | 
					
						
							|  |  |  | // 		same field when it recieves the object.
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | // XXX add generic snap
 | 
					
						
							|  |  |  | // XXX add generic innertial scroll
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | // XXX test multiple touches...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | function postScrollCallback(data){ | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 	var scroller = data.scroller | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	var options = scroller.options | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 	var root = scroller.root | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	var clickThreshold = options.clickThreshold || CLICK_THRESHOLD | 
					
						
							|  |  |  | 	var longClickThreshold = options.longClickThreshold || LONG_CLICK_THRESHOLD | 
					
						
							|  |  |  | 	var multitouchTimeout = options.multitouchTimeout || MULTITOUCH_RELEASE_THRESHOLD | 
					
						
							|  |  |  | 	var enableMultiClicks = options.enableMultiClicks | 
					
						
							|  |  |  | 	var multiClickTimeout = options.multiClickTimeout || MULTI_CLICK_TIMEOUT | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | 	var now = Date.now(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 	// cancel event...
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 	if(scroller.state == 'canceling'){ | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 		return root.trigger('scrollCancelled', data) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	// handle multiple touches...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 	if(data.touches > 0){ | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | 		var then = scroller._last_touch_release | 
					
						
							|  |  |  | 		if(then == null || now - then < multitouchTimeout){ | 
					
						
							|  |  |  | 			if(scroller._touches == null){ | 
					
						
							|  |  |  | 				scroller._touches = 1 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				scroller._touches += 1 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			scroller._touches = null | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// wait for the next touch release...
 | 
					
						
							|  |  |  | 		scroller._last_touch_release = now | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | 	// calculate how many touches did participate... 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 		data.touches = scroller._touches ? scroller._touches + 1 : 1 | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | 		scroller._last_touch_release = null | 
					
						
							|  |  |  | 		scroller._touches = null | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	// clicks, double-clicks, multi-clicks and long-clicks...
 | 
					
						
							|  |  |  | 	if(Math.max( | 
					
						
							|  |  |  | 			Math.abs(data.distance.x),  | 
					
						
							|  |  |  | 			Math.abs(data.distance.y)) < clickThreshold){ | 
					
						
							| 
									
										
										
										
											2013-02-20 23:29:22 +04:00
										 |  |  | 		if(data.duration > longClickThreshold){ | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 			return root.trigger('longClick', data)  | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 		if(!enableMultiClicks){ | 
					
						
							|  |  |  | 			return root.trigger('shortClick', data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2013-02-21 23:28:15 +04:00
										 |  |  | 			// count the clicks so far...
 | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 			if(scroller._clicks == null){ | 
					
						
							|  |  |  | 				scroller._clicks = 1 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				scroller._clicks += 1 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// kill any previous waits...
 | 
					
						
							|  |  |  | 			if(scroller._click_timeout_id != null){ | 
					
						
							|  |  |  | 				clearTimeout(scroller._click_timeout_id) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// wait for the next click...
 | 
					
						
							|  |  |  | 			scroller._click_timeout_id = setTimeout(function(){ | 
					
						
							|  |  |  | 				var clicks = scroller._clicks | 
					
						
							|  |  |  | 				data.clicks = clicks | 
					
						
							|  |  |  | 				if(clicks == 1){ | 
					
						
							|  |  |  | 					root.trigger('shortClick', data) | 
					
						
							|  |  |  | 				} else if(clicks == 2){ | 
					
						
							|  |  |  | 					root.trigger('doubleClick', data) | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					root.trigger('multiClick', data) | 
					
						
							|  |  |  | 				}					 | 
					
						
							|  |  |  | 				scroller._clicks = null | 
					
						
							|  |  |  | 				scroller._click_timeout_id = null | 
					
						
							|  |  |  | 			}, multiClickTimeout) | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			return | 
					
						
							| 
									
										
										
										
											2013-02-21 22:36:16 +04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	// swipes...
 | 
					
						
							| 
									
										
										
										
											2013-02-22 02:34:11 +04:00
										 |  |  | 	// XXX might be a good idea to chain these with swipe and screenReleased
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	if(Math.abs(data.distance.x) > Math.abs(data.distance.y)){ | 
					
						
							|  |  |  | 		if(data.distance.x <= -clickThreshold && root.data('events').swipeLeft){ | 
					
						
							|  |  |  | 			return root.trigger('swipeLeft', data) | 
					
						
							|  |  |  | 		} else if(data.distance.x >= clickThreshold && root.data('events').swipeRight){ | 
					
						
							|  |  |  | 			return root.trigger('swipeRight', data) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if(data.distance.y <= -clickThreshold && root.data('events').swipeUp){ | 
					
						
							|  |  |  | 			return root.trigger('swipeUp', data) | 
					
						
							|  |  |  | 		} else if(data.distance.y >= clickThreshold && root.data('events').swipeDown){ | 
					
						
							|  |  |  | 			return root.trigger('swipeDown', data) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-22 00:57:00 +04:00
										 |  |  | 	// this is triggered of no swipes were handled...
 | 
					
						
							| 
									
										
										
										
											2013-02-20 23:08:07 +04:00
										 |  |  | 	return root.trigger('screenReleased', data) | 
					
						
							|  |  |  | }  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:02:09 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 03:12:01 +04:00
										 |  |  | /************************************************ jQuery extensions **/ | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 03:12:01 +04:00
										 |  |  | jQuery.fn.reverseChildren = function(){ | 
					
						
							|  |  |  | 	return $(this).each(function(_, e){ | 
					
						
							|  |  |  | 		return $(e).append($(e).children().detach().get().reverse()) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | jQuery.fn.sortChildren = function(func){ | 
					
						
							|  |  |  | 	return $(this).each(function(_, e){ | 
					
						
							|  |  |  | 		return $(e).append($(e).children().detach().get().sort(func)) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 02:58:13 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 03:12:01 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-14 19:06:18 +04:00
										 |  |  | /********************************************************** logger ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function Logger(){ | 
					
						
							|  |  |  | 	_log = null | 
					
						
							|  |  |  | 	return { | 
					
						
							|  |  |  | 		setup: function(){ | 
					
						
							|  |  |  | 			if(_log == null){ | 
					
						
							|  |  |  | 				_log = $('<div id="log"></div>') | 
					
						
							|  |  |  | 					.css({ | 
					
						
							|  |  |  | 						position: 'fixed', | 
					
						
							|  |  |  | 						background: 'silver', | 
					
						
							|  |  |  | 						opacity: 0.5, | 
					
						
							|  |  |  | 						width: 200, | 
					
						
							|  |  |  | 						height: '80%', | 
					
						
							|  |  |  | 						top: 10, | 
					
						
							|  |  |  | 						left: 10, | 
					
						
							|  |  |  | 						'z-index': 90000, | 
					
						
							|  |  |  | 						overflow: 'hidden', | 
					
						
							|  |  |  | 						padding: 10, | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 					.text('log') | 
					
						
							|  |  |  | 					.appendTo($('body')) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				_log.appendTo($('body')) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return this | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		remove: function(){ | 
					
						
							|  |  |  | 			_log.detach() | 
					
						
							|  |  |  | 			return this | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		log: function(text){ | 
					
						
							|  |  |  | 			_log.html(_log.html() + '<br>' + text + '') | 
					
						
							|  |  |  | 			_log.scrollTop(_log.prop('scrollHeight')) | 
					
						
							|  |  |  | 			return this | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		clear: function(){ | 
					
						
							|  |  |  | 			_log.html('') | 
					
						
							|  |  |  | 			return this | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		get: function(){ | 
					
						
							|  |  |  | 			return _log | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		set: function(elem){ | 
					
						
							|  |  |  | 			_log = elem | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}.setup() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-24 03:12:01 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                                                */ |