merged cont.
[opensuse:yast-rest-service.git] / webyast / public / javascripts / jquerytimer.js
1 // Copyright (c) 2010 margus.rebane@gmail.com\r
2 // \r
3 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
4 // of this software and associated documentation files (the "Software"), to deal\r
5 // in the Software without restriction, including without limitation the rights\r
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7 // copies of the Software, and to permit persons to whom the Software is\r
8 // furnished to do so, subject to the following conditions:\r
9 // \r
10 // The above copyright notice and this permission notice shall be included in\r
11 // all copies or substantial portions of the Software.\r
12 // \r
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
19 // THE SOFTWARE.\r
20 \r
21 jQuery.extend({\r
22         fjFunctionQueue: function(funcToQue) {\r
23                 if (funcToQue == null) {\r
24                         if (jQuery.fjFunctionQueue.queue != null && jQuery.fjFunctionQueue.queue.queue.length > 0) {\r
25                                 if (jQuery.fjFunctionQueue.queue.running) {\r
26                                         jQuery.fjTimer({\r
27                                                 interval: jQuery.fjFunctionQueue.queue.properties.interval,\r
28                                                 tick: function(counter, timer) {\r
29                                                         var func = jQuery.fjFunctionQueue.queue.queue.shift();\r
30                                                         try {\r
31                                                                 jQuery.fjFunctionQueue.queue.properties.onTick(jQuery.fjFunctionQueue.queue.index, func);\r
32                                                                 jQuery.fjFunctionQueue.queue.index++;\r
33                                                         } catch (e) {\r
34                                                                 jQuery.fjFunctionQueue();\r
35                                                                 throw e;\r
36                                                         }\r
37                                                         if (jQuery.fjFunctionQueue.queue.queue.length > 0) {\r
38                                                                 jQuery.fjFunctionQueue();\r
39                                                         } else {\r
40                                                                 jQuery.fjFunctionQueue.queue.running = false;\r
41                                                                 jQuery.fjFunctionQueue.queue.index = 0;\r
42                                                                 jQuery.fjFunctionQueue.queue.properties.onComplete();\r
43                                                         }\r
44                                                 }\r
45                                         });\r
46                                 } else {\r
47                                         jQuery.fjFunctionQueue.queue.running = true;\r
48                                         jQuery.fjFunctionQueue();\r
49                                 }\r
50                         }\r
51                 } else {\r
52                         if (jQuery.fjFunctionQueue.queue == null) {\r
53                                 jQuery.fjFunctionQueue.queue = {index: 0, running: false, queue:[], properties: {interval: 1, onComplete: function(){}, onStart: function(){}, autoStart: true, onTick: function(counter, func) {func();}}};\r
54                         }\r
55                         var isEmptyArray = jQuery.fjFunctionQueue.queue.queue.length == 0;\r
56                         if (jQuery.isFunction(funcToQue)) {\r
57                                 jQuery.fjFunctionQueue.queue.queue.push(funcToQue);\r
58                         } else if (jQuery.isArray(funcToQue)) {\r
59                                 for(var i = 0; i < funcToQue.length; i++) {\r
60                                         jQuery.fjFunctionQueue.queue.queue.push(funcToQue[i]);\r
61                                 }\r
62                         } else {\r
63                                 jQuery.fjFunctionQueue.queue.properties = jQuery.extend(jQuery.fjFunctionQueue.queue.properties, funcToQue);\r
64                         }\r
65                         if (isEmptyArray && jQuery.fjFunctionQueue.queue.queue.length > 0 && !jQuery.fjFunctionQueue.queue.running && jQuery.fjFunctionQueue.queue.properties.autoStart) {\r
66                                 jQuery.fjFunctionQueue.queue.running = true;\r
67                                 jQuery.fjFunctionQueue.queue.properties.onStart();\r
68                                 jQuery.fjFunctionQueue.queue.running = false;\r
69                                 jQuery.fjFunctionQueue();\r
70                         }\r
71                 }\r
72         },\r
73         fjTimer : function(properties) {\r
74             properties = jQuery.extend({interval: 10, tick: function(){}, repeat: false, random :false, onComplete: function(){}, step: 1}, properties);\r
75             var counter = 0;\r
76             var timer = new function() {\r
77                 this.timerId = null;\r
78                 this.stop = function() {\r
79                         clearInterval(this.timerId);\r
80                 }\r
81             }\r
82             timer.timerId = setInterval(function() {\r
83                 try {\r
84                         properties.tick(counter, timer);\r
85                         counter+=properties.step;\r
86                 } catch (e) {\r
87                         alert(e);\r
88                 }\r
89                 if (properties.repeat !== true && ((properties.repeat * properties.step) <= counter || properties.repeat === false)) {\r
90                         timer.stop();\r
91                         properties.onComplete();\r
92                 }\r
93             }, properties.interval);\r
94         },\r
95         fjTimerEach: function(properties) {\r
96                 var ___array = properties.array;\r
97                 var ___callback = properties.tick;\r
98                 properties.repeat = ___array.length;\r
99                 if (properties.step != null) {\r
100                         properties.repeat = Math.ceil(___array.length / parseInt(properties.step, 10));\r
101                 }\r
102                 properties.tick = function(counter, timer) {\r
103                         ___callback(counter, ___array[counter]);\r
104                 }\r
105                 jQuery.fjTimer(properties);\r
106         }\r
107 });