summaryrefslogtreecommitdiff
path: root/chimere/static/jme/utils/enterLeave.js
blob: 07d96ccfd5fe416035a27f2682f6f1e27391a452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function($){
	/*
	*  enterLeave
	*  similiar to hover, but more accessible
	*  hover = focusblur
	*/
	var inReg = /focusin|focus$|mouseenter|mouseover/;	
	$.fn.enterLeave = function(enter, out, opts){
		opts = $.extend({}, $.fn.enterLeave.defaults, opts);
		
		var eventTypes 	= 'mouseenter mouseleave focusin focusout',
			selector 	= this.selector,
			context 	= this.context
		;
		
		if(opts.useEventTypes === 'mouse'){
			eventTypes = 'mouseenter mouseleave';
		} else if(opts.useEventTypes === 'focus'){
			eventTypes = 'focusin focusout';
		}
		
		

		this
			.each(function(){
				var inOutData = {inEvents: 0};
				function handler(e){
					var fn,
						params,
						elem = this,
						evt
					;
					if(inReg.test(e.type)){
						fn = enter;
						params =  [1, 'in', true];
						//webkit autoblur prevention 
						if(opts.useWebkitAutoBlur){
							inOutData.autoBlur = true; 
							setTimeout(function(){
								inOutData.autoBlur = false;
							}, 0);
						}
					} else {
						fn = out;
						params = [-1, 'out', false];
						if(inOutData.autoBlur){
							return;
						}
					}
					
					clearTimeout(inOutData.inOutTimer);
					inOutData.inEvents = Math.max(inOutData.inEvents + params[0], 0);
					inOutData.inOutTimer = setTimeout(function(){
						if(params[2] != inOutData.inOutState && 
								(params[2] || !opts.bothOut || !inOutData.inEvents)){
							
							inOutData.inOutState = params[2];
							evt = $.Event(params[1]);
							evt.originalEvent = e;
							fn.call(elem, evt);
						}
					}, /focus/.test(e.type) ? opts.keyDelay : opts.mouseDelay);
				}
				$(this)[opts.bindStyle](eventTypes, handler);
			});
		return this;
	};
	
	$.fn.enterLeave.defaults = {
		mouseDelay: 0,
		bindStyle: 'bind', // bind | live | bubbleLive
		keyDelay: 1,
		bothOut: false,
		useEventTypes: 'both', // both || mouse || focus
		useWebkitAutoBlur: false
	};
	
	$.fn.inOut = $.fn.enterLeave;
})(jQuery);