client/player/test/emission_fichiers/jquery.js
author wakimd
Wed, 22 Dec 2010 12:01:05 +0100
changeset 25 c8dfd7ea87e5
parent 9 22ab430e9b64
permissions -rw-r--r--
Corrections on merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     1
/**
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     2
 * jQuery.timers - Timer abstractions for jQuery
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     3
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     4
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     5
 * Date: 2009/02/08
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     6
 *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     7
 * @author Blair Mitchelmore
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     8
 * @version 1.1.2
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
     9
 *
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    10
 **/
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    11
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    12
jQuery.fn.extend({
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    13
	everyTime: function(interval, label, fn, times, belay) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    14
		return this.each(function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    15
			jQuery.timer.add(this, interval, label, fn, times, belay);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    16
		});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    17
	},
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    18
	oneTime: function(interval, label, fn) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    19
		return this.each(function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    20
			jQuery.timer.add(this, interval, label, fn, 1);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    21
		});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    22
	},
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    23
	stopTime: function(label, fn) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    24
		return this.each(function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    25
			jQuery.timer.remove(this, label, fn);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    26
		});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    27
	}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    28
});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    29
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    30
jQuery.event.special
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    31
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    32
jQuery.extend({
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    33
	timer: {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    34
		global: [],
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    35
		guid: 1,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    36
		dataKey: "jQuery.timer",
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    37
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    38
		powers: {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    39
			// Yeah this is major overkill...
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    40
			'ms': 1,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    41
			'cs': 10,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    42
			'ds': 100,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    43
			's': 1000,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    44
			'das': 10000,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    45
			'hs': 100000,
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    46
			'ks': 1000000
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    47
		},
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    48
		timeParse: function(value) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    49
			if (value == undefined || value == null)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    50
				return null;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    51
			var result = this.regex.exec(jQuery.trim(value.toString()));
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    52
			if (result[2]) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    53
				var num = parseFloat(result[1]);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    54
				var mult = this.powers[result[2]] || 1;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    55
				return num * mult;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    56
			} else {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    57
				return value;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    58
			}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    59
		},
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    60
		add: function(element, interval, label, fn, times, belay) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    61
			var counter = 0;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    62
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    63
			if (jQuery.isFunction(label)) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    64
				if (!times) 
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    65
					times = fn;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    66
				fn = label;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    67
				label = interval;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    68
			}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    69
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    70
			interval = jQuery.timer.timeParse(interval);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    71
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    72
			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    73
				return;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    74
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    75
			if (times && times.constructor != Number) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    76
				belay = !!times;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    77
				times = 0;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    78
			}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    79
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    80
			times = times || 0;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    81
			belay = belay || false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    82
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    83
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    84
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    85
			if (!timers[label])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    86
				timers[label] = {};
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    87
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    88
			fn.timerID = fn.timerID || this.guid++;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    89
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    90
			var handler = function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    91
				if (belay && this.inProgress) 
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    92
					return;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    93
				this.inProgress = true;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    94
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    95
					jQuery.timer.remove(element, label, fn);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    96
				this.inProgress = false;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    97
			};
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    98
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
    99
			handler.timerID = fn.timerID;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   100
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   101
			if (!timers[label][fn.timerID])
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   102
				timers[label][fn.timerID] = window.setInterval(handler,interval);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   103
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   104
			this.global.push( element );
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   105
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   106
		},
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   107
		remove: function(element, label, fn) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   108
			var timers = jQuery.data(element, this.dataKey), ret;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   109
			
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   110
			if ( timers ) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   111
				
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   112
				if (!label) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   113
					for ( label in timers )
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   114
						this.remove(element, label, fn);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   115
				} else if ( timers[label] ) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   116
					if ( fn ) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   117
						if ( fn.timerID ) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   118
							window.clearInterval(timers[label][fn.timerID]);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   119
							delete timers[label][fn.timerID];
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   120
						}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   121
					} else {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   122
						for ( var fn in timers[label] ) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   123
							window.clearInterval(timers[label][fn]);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   124
							delete timers[label][fn];
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   125
						}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   126
					}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   127
					
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   128
					for ( ret in timers[label] ) break;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   129
					if ( !ret ) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   130
						ret = null;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   131
						delete timers[label];
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   132
					}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   133
				}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   134
				
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   135
				for ( ret in timers ) break;
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   136
				if ( !ret ) 
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   137
					jQuery.removeData(element, this.dataKey);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   138
			}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   139
		}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   140
	}
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   141
});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   142
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   143
jQuery(window).bind("unload", function() {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   144
	jQuery.each(jQuery.timer.global, function(index, item) {
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   145
		jQuery.timer.remove(item);
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   146
	});
22ab430e9b64 Corrections on models and general structure
wakimd
parents:
diff changeset
   147
});