source: extensions/GrumPluginClasses/js/ui.timer.js @ 16733

Last change on this file since 16733 was 16733, checked in by grum, 12 years ago

update GPC framework

File size: 7.3 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.timer.js
4 * file version: 1.0.0
5 * date: 2012-07-14
6 *
7 * A jQuery plugin provided by the piwigo's plugin "GrumPluginClasses"
8 *
9 * -----------------------------------------------------------------------------
10 * Author     : Grum
11 *   email    : grum@piwigo.com
12 *   website  : http://www.grum.fr
13 *
14 *   << May the Little SpaceFrog be with you ! >>
15 * -----------------------------------------------------------------------------
16 *
17 *
18 *
19 *
20 * :: HISTORY ::
21 *
22 * | release | date       |
23 * | 1.0.0   | 2012/07/14 | first release
24 * |         |            |
25 * |         |            |
26 * |         |            |
27 * |         |            |
28 * |         |            |
29 * |         |            |
30 *
31 */
32
33
34
35/**
36 * set a timer
37 *
38 * $.timer('id', {properties})
39 *  => set properties for a timer 'id'
40 *
41 * $.timer('id', 'function' [, param])
42 *  => available functions are:
43 *    - start: start the timer
44 *    - stop: stop the timer
45 *    - reset: reset the timer
46 *    - remove: remove the timer
47 *    - count: if no param, returns the current count value otherwise set the count value to param value
48 *    - frequency: if no param, returns the current frequency value otherwise set the frequency value to param value
49 *
50 */
51$.timer = function (id, value1, value2)
52{
53  var returned=null,
54      properties=
55        {
56          timers:{}
57        },
58
59
60    // set timer properties
61    setTimer = function (id, value)
62      {
63        if(properties.timers[id]==null)
64        {
65          properties.timers[id]={
66            autoStart:false,      // if set to true, timer start automaticaly when created
67            frequency:1000,       // frequency of timer, in milliseconds
68            limit:0,              // limit of timer, 0=no limit; timer stop when limit is reached
69            timer:null,           // function callback
70
71            count:0,              // number of times the timer was triggered
72            started:false,        // true if timer is started, otherwise false
73            timerId:null          // timer uid
74          }
75        }
76
77        if($.isPlainObject(value))
78        {
79          if(value.autoStart &&
80            (value.autoStart==true || value.autoStart==false))
81            properties.timers[id].autoStart=value.autoStart;
82
83          if(value.frequency &&
84            Math.round(value.frequency)>0)
85            properties.timers[id].frequency=Math.round(value.frequency);
86
87          if(value.limit &&
88            Math.round(value.limit)>=0)
89            properties.timers[id].limit=Math.round(value.limit);
90
91          if(value.timer &&
92            $.isFunction(value.timer))
93            properties.timers[id].timer=value.timer;
94        }
95
96        if(properties.timers[id].autoStart && !properties.timers[id].started)
97          startTimer(id);
98      },
99
100    /**
101     * start the timer if not started
102     *
103     * @param String Id: timer Id
104     * @return Boolean: true if started, otherwise false
105     */
106    startTimer = function (id)
107      {
108        if(properties.timers[id]==null || properties.timers[id].started) return(false);
109
110        properties.timers[id].timerId=window.setInterval(
111          callFunction,
112          properties.timers[id].frequency,
113          id
114        );
115        properties.timers[id].started=true;
116        return(true);
117      },
118
119    /**
120     * stop the timer if started
121     *
122     * @param String Id: timer Id
123     * @return Boolean: true if stopped, otherwise false
124     */
125    stopTimer = function (id)
126      {
127        if(properties.timers[id]==null && !properties.timers[id].started) return(false);
128
129        properties.timers[id].started=false;
130        window.clearInterval(properties.timers[id].timerId);
131        return(true);
132      },
133
134    /**
135     * reset the timer:
136     *  - count is set to 0
137     *  - timer is reseted for a new cycle
138     *
139     * @param String Id: timer Id
140     * @return Boolean: true if reseted, otherwise false
141     */
142    resetTimer = function (id)
143      {
144        if(properties.timers[id]==null) return(false);
145
146        stopTimer(id);
147        properties.timers[id].count=0;
148        startTimer(id);
149        return(true);
150      },
151
152    /**
153     * remove a timer
154     * timer if stopped is necessary
155     *
156     * @param String Id: timer Id
157     * @return Boolean: true if removed, otherwise false
158     */
159    removeTimer = function (id)
160      {
161        if(properties.timers[id]==null) return(false);
162
163        stopTimer(id);
164        delete properties.timers[id];
165        return(true);
166      },
167
168    /**
169     * set the limit of execution
170     *
171     * @param String Id: timer Id
172     * @param Integer value: limit to apply
173     * @return Boolean: true if limit is set, otherwise false
174     */
175    setLimit = function (id, value)
176      {
177        if(properties.timers[id]==null || Math.round(value)<0) return(false);
178        properties.timers[id].limit=Math.round(value);
179      },
180
181    /**
182     * set the frequency of execution
183     *
184     * @param String Id: timer Id
185     * @param Integer value: frequency (in milliseconds) to apply
186     * @return Boolean: true if frequency is set, otherwise false
187     */
188    setFrequency = function (id, value)
189      {
190        if(properties.timers[id]==null || Math.round(value)<=0) return(false);
191        properties.timers[id].frequency=Math.round(value);
192        stopTimer(id);
193        startTimer(id);
194        return(true);
195      },
196
197    /**
198     * internal callback
199     * called when a timer is triggered; if a callback function is set for the
200     * timer, execute it
201     *
202     *
203     * @param String Id: timer Id
204     * @return Boolean: true if callback is called, otherwise false
205     */
206    callFunction = function (id)
207      {
208        var noLimit=true,
209            last=false;
210
211        if(properties.timers[id]==null) return(false);
212        properties.timers[id].count++;
213
214        noLimit=(properties.timers[id].limit==0);
215        last=(!noLimit && (properties.timers[id].count==properties.timers[id].limit));
216
217        if(last)
218          stopTimer(id);
219
220        if(properties.timers[id].timer)
221          properties.timers[id].timer(
222            properties.timers[id].count,
223            last
224          );
225
226        return(true);
227      },
228
229    // save current data in global stack
230    setData = function ()
231      {
232        $(window).data('timerProperties', properties.timers);
233      },
234
235    // load current data from global stack
236    getData = function ()
237      {
238        var tmp=$(window).data('timerProperties');
239        if(tmp!=null) properties.timers=tmp;
240      }
241
242  getData();
243
244  switch(value1)
245  {
246    case 'start':
247      returned=startTimer(id);
248      break;
249    case 'stop':
250      returned=stopTimer(id);
251      break;
252    case 'reset':
253      returned=resetTimer(id);
254      break;
255    case 'count':
256      getData();
257      if(!properties.timers[id]) return(-1);
258      returned=properties.timers[id].count;
259      break;
260    case 'limit':
261      getData();
262      if(!properties.timers[id]) return(-1);
263
264      if(value2!=null)
265        setLimit(id, value2);
266      returned=properties.timers[id].limit;
267      break;
268    case 'frequency':
269      getData();
270      if(!properties.timers[id]) return(-1);
271
272      if(value2!=null)
273        setFrequency(id, value2);
274      returned=properties.timers[id].frequency;
275      break;
276    case 'remove':
277      returned=removeTimer(id);
278      break;
279    default:
280      setTimer(id, value1);
281      break;
282  }
283
284  setData();
285  return(returned);
286} // $.timer
Note: See TracBrowser for help on using the repository browser.