Added phing based release script
[ajatus.git] / js / ajatus.events.js
blob651eacb4919ff0a8d8e722f3447c9a66b5aa0457
1 /*
2  * This file is part of
3  *
4  * Ajatus - Distributed CRM
5  * @requires jQuery v1.2.1
6  * 
7  * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8  * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9  * Website: http://ajatus.info
10  * Licensed under the GPL license
11  * http://www.gnu.org/licenses/gpl.html
12  * 
13  */
15 (function($){
16     $.ajatus = $.ajatus || {};
17     $.ajatus.locker = $.ajatus.locker || {};
18     $.ajatus.events = $.ajatus.events || {};
20     $.ajatus.events.locks = {
21         list: []
22     };
23     $.extend($.ajatus.events.locks, {
24         add: function(lock)
25         {
26             var new_count = $.ajatus.events.locks.list.push(lock);            
27             return new_count-1;
28         },
29         remove: function(index)
30         {            
31             $.ajatus.events.locks.list = $.grep( $.ajatus.events.locks.list, function(n,i){
32                 return i == index;
33             });
34         },
35         update: function(index, lock)
36         {
37             $.ajatus.events.locks.list[index] = lock;
38         }
39     });
40     
41     $.ajatus.events.lock = function(options)
42     {
43         this._manual = true;
44         this._watcher = null;
45         this.options = $.extend({
46             msg: '<h1><img src="'+$.ajatus.preferences.client.theme_url+'images/loading-small.gif" /> ' + $.ajatus.i10n.get('Loading Ajatus') + '...</h1>',
47             dialog: false,
48             watch: false,
49             on_release: false,
50             disable_application: true
51         }, options);
53         this._id = $.ajatus.events.locks.add(this);
54         
55         if (   !this.options.dialog
56             && this.options.disable_application)
57         {
58             $.blockUI(this.options.msg);
59         }
60         
61         if (this.options.watch)
62         {
63             var watcher_opts = {
64                 after: true,
65                 auto_start: false
66             };
67             $.each(this.options.watch, function(i,n){
68                 if (i != 'after')
69                 {
70                     watcher_opts[i] = n;                    
71                 }
72             });
73             this._watcher = new $.ajatus.events.watcher(watcher_opts);
74             
75             var self = this;
76             this._watcher.element.bind('on_stop', function(watcher_id){
77                 self.release();
78             });
79             
80             this._watcher.start();
81         }
83         $.ajatus.events.locks.update(this);        
84         return this;
85     };
86     $.extend($.ajatus.events.lock.prototype, {
87         release: function() {
88             if (   !this.options.dialog
89                 && this.options.disable_application)
90             {
91                 $.unblockUI();
92             }
93             
94             if (   this.options.on_release
95                 && typeof(this.options.on_release) == 'function')
96             {
97                 this.options.on_release(this);
98             }
99         },
100         update: function(lock_cnt) {
101             if (lock_cnt == 0) {
102                 this._release_lock();
103             }
104         }
105     });
106     
107     $.ajatus.events.watchers = {
108         list: []
109     };
110     $.extend($.ajatus.events.watchers, {
111         add: function(watcher) {
112             var new_count = $.ajatus.events.watchers.list.push(watcher);
113             return new_count-1;
114         },
115         remove: function(index) {
116             $.ajatus.events.watchers.list = $.grep( $.ajatus.events.watchers.list, function(n,i){
117                 return i == index;
118             });
119         }
120     });
121     
122     $.ajatus.events.watcher = function(options) {
123         this.options = $.extend({
124             validate: null,
125             after: false,
126             interval: 1000,
127             safety_runs: 3,
128             auto_start: true,
129             max_runs: 100,
130             timed: 0
131         }, options);
133         if (   typeof(this.options.validate) != 'function'
134             && this.options.timed <= 0)
135         {
136             return false;
137         }
139         this.elapsed = 0;
140         this._timeout_id = -1;
141         this._done_safety_runs = 0;
142         this._done_runs = 0;
143         
144         this._id = $.ajatus.events.watchers.add(this);
145         this.element = $('<div id="ajatus_events_watcher_'+this._id+'" />').appendTo($('body'));
147         if (this.options.auto_start) {
148             this.start();
149         }
150         
151         return this;
152     };
153     $.extend($.ajatus.events.watcher.prototype, {
154         start: function() {
155             if (this.options.interval < 200) {
156                 this.options.interval = 200;  // Safari needs at least 200 ms
157             }
158             // console.log("Watcher "+this._id+" started!");
159             this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
160         },
161         _check: function() {
162             // console.log("Watcher "+this._id+" is checking status");
163             this._done_runs++;
164             
165             if (this.options.timed > 0) {
166                 if (   this.elapsed >= this.options.timed
167                     || (   this.options.max_runs
168                         && this._done_runs >= (this.options.max_runs-this.options.safety_runs)))
169                 {
170                     this._stop();
171                 } else {
172                     this._done_safety_runs = 0;
173                     this.elapsed += this.options.interval;
174                     this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
175                 }
176             } else {
177                 if (   this.options.validate()
178                     || (   this.options.max_runs
179                         && this._done_runs >= (this.options.max_runs-this.options.safety_runs)))
180                 {
181                     //console.log("Watcher "+this._id+" validated!");
183                     if (this._done_safety_runs < this.options.safety_runs) {
184                         this._done_safety_runs++;
185                         //console.log("Watcher "+this._id+" doing safety run: "+this._done_safety_runs+" of "+this.options.safety_runs);
186                         this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
187                     } else {
188                         //console.log("Watcher "+this._id+" has done all safety runs!");                    
189                         this._stop();
190                     }
191                 } else {
192                     this._done_safety_runs = 0;
193                     this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
194                 }                
195             }
196             
197         },
198         _stop: function() {
199             // console.log("Watcher "+this._id+" ended!");
200             clearTimeout(this._timeout_id);
201             
202             this.element.trigger('on_stop',[this._id]);
203         }
204     });
205     
206     $.ajatus.events.lock_pool = {
207         count: 0,
208         increase: function() {
209             $.ajatus.events.lock_pool.count = $.ajatus.events.named_lock_pool.increase('global');
210             // console.log('lock_pool increase to '+$.ajatus.events.lock_pool.count);
211         },
212         decrease: function() {
213             $.ajatus.events.lock_pool.count = $.ajatus.events.named_lock_pool.decrease('global');            
214             // console.log('lock_pool decrease to '+$.ajatus.events.lock_pool.count);
215         },
216         clear: function() {
217             $.ajatus.events.named_lock_pool.clear('global');
218             $.ajatus.events.lock_pool.count = 0;
219         }
220     };
221     
222     $.ajatus.events.named_lock_pool = {
223         counts: {},
224         count: function(name) {
225             if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
226                 return 0;
227             } else {
228                 return $.ajatus.events.named_lock_pool.counts[name];
229             }            
230         },
231         increase: function(name) {
232             if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
233                 $.ajatus.events.named_lock_pool.counts[name] = 0;
234             }
235             $.ajatus.events.named_lock_pool.counts[name]++;
237             //console.log('named_lock_pool increase '+name+' to '+$.ajatus.events.named_lock_pool.counts[name]);
238             return $.ajatus.events.named_lock_pool.counts[name];
239         },
240         decrease: function(name) {
241             if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
242                 $.ajatus.events.named_lock_pool.counts[name] = 0;
243             } else {
244                 $.ajatus.events.named_lock_pool.counts[name] = $.ajatus.events.named_lock_pool.counts[name]-1;
245             }
246             // console.log('named_lock_pool decrease '+name+' to '+$.ajatus.events.named_lock_pool.counts[name]);
247             return $.ajatus.events.named_lock_pool.counts[name];
248         },
249         clear: function(name) {
250             $.ajatus.events.named_lock_pool.counts[name] = 0;
251         }
252     };
254 })(jQuery);