r39986 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r39985‎ | r39986 | r39987 >
Date:23:42, 25 August 2008
Author:stipe
Status:old
Tags:
Comment:
adding jquery.timers plugin
Modified paths:
  • /trunk/extensions/MetavidWiki/skins/mv_embed/jquery/plugins/jquery.timers.js (added) (history)

Diff [purge]

Index: trunk/extensions/MetavidWiki/skins/mv_embed/jquery/plugins/jquery.timers.js
@@ -0,0 +1,142 @@
 2+jQuery.fn.extend({
 3+ everyTime: function(interval, label, fn, times, belay) {
 4+ return this.each(function() {
 5+ jQuery.timer.add(this, interval, label, fn, times, belay);
 6+ });
 7+ },
 8+ oneTime: function(interval, label, fn) {
 9+ return this.each(function() {
 10+ jQuery.timer.add(this, interval, label, fn, 1);
 11+ });
 12+ },
 13+ stopTime: function(label, fn) {
 14+ return this.each(function() {
 15+ jQuery.timer.remove(this, label, fn);
 16+ });
 17+ }
 18+});
 19+
 20+jQuery.extend({
 21+ timer: {
 22+ guid: 1,
 23+ global: {},
 24+ regex: /^([0-9]+)\s*(.*s)?$/,
 25+ powers: {
 26+ // Yeah this is major overkill...
 27+ 'ms': 1,
 28+ 'cs': 10,
 29+ 'ds': 100,
 30+ 's': 1000,
 31+ 'das': 10000,
 32+ 'hs': 100000,
 33+ 'ks': 1000000
 34+ },
 35+ timeParse: function(value) {
 36+ if (value == undefined || value == null)
 37+ return null;
 38+ var result = this.regex.exec(jQuery.trim(value.toString()));
 39+ if (result[2]) {
 40+ var num = parseInt(result[1], 10);
 41+ var mult = this.powers[result[2]] || 1;
 42+ return num * mult;
 43+ } else {
 44+ return value;
 45+ }
 46+ },
 47+ add: function(element, interval, label, fn, times, belay) {
 48+ var counter = 0;
 49+
 50+ if (jQuery.isFunction(label)) {
 51+ if (!times)
 52+ times = fn;
 53+ fn = label;
 54+ label = interval;
 55+ }
 56+
 57+ interval = jQuery.timer.timeParse(interval);
 58+
 59+ if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
 60+ return;
 61+
 62+ if (times && times.constructor != Number) {
 63+ belay = !!times;
 64+ times = 0;
 65+ }
 66+
 67+ times = times || 0;
 68+ belay = belay || false;
 69+
 70+ if (!element.$timers)
 71+ element.$timers = {};
 72+
 73+ if (!element.$timers[label])
 74+ element.$timers[label] = {};
 75+
 76+ fn.$timerID = fn.$timerID || this.guid++;
 77+
 78+ var handler = function() {
 79+ if (belay && this.inProgress)
 80+ return;
 81+ this.inProgress = true;
 82+ if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
 83+ jQuery.timer.remove(element, label, fn);
 84+ this.inProgress = false;
 85+ };
 86+
 87+ handler.$timerID = fn.$timerID;
 88+
 89+ if (!element.$timers[label][fn.$timerID])
 90+ element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);
 91+
 92+ if ( !this.global[label] )
 93+ this.global[label] = [];
 94+ this.global[label].push( element );
 95+
 96+ },
 97+ remove: function(element, label, fn) {
 98+ var timers = element.$timers, ret;
 99+
 100+ if ( timers ) {
 101+
 102+ if (!label) {
 103+ for ( label in timers )
 104+ this.remove(element, label, fn);
 105+ } else if ( timers[label] ) {
 106+ if ( fn ) {
 107+ if ( fn.$timerID ) {
 108+ window.clearInterval(timers[label][fn.$timerID]);
 109+ delete timers[label][fn.$timerID];
 110+ }
 111+ } else {
 112+ for ( var fn in timers[label] ) {
 113+ window.clearInterval(timers[label][fn]);
 114+ delete timers[label][fn];
 115+ }
 116+ }
 117+
 118+ for ( ret in timers[label] ) break;
 119+ if ( !ret ) {
 120+ ret = null;
 121+ delete timers[label];
 122+ }
 123+ }
 124+
 125+ for ( ret in timers ) break;
 126+ if ( !ret )
 127+ element.$timers = null;
 128+ }
 129+ }
 130+ }
 131+});
 132+
 133+if (jQuery.browser.msie)
 134+ jQuery(window).one("unload", function() {
 135+ var global = jQuery.timer.global;
 136+ for ( var label in global ) {
 137+ var els = global[label], i = els.length;
 138+ while ( --i )
 139+ jQuery.timer.remove(els[i], label);
 140+ }
 141+ });
 142+
 143+
Property changes on: trunk/extensions/MetavidWiki/skins/mv_embed/jquery/plugins/jquery.timers.js
___________________________________________________________________
Added: svn:eol-style
1144 + native

Status & tagging log