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