4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
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
16 $.ajatus = $.ajatus || {};
17 $.ajatus.locker = $.ajatus.locker || {};
18 $.ajatus.events = $.ajatus.events || {};
20 $.ajatus.events.locks = {
23 $.extend($.ajatus.events.locks, {
26 var new_count = $.ajatus.events.locks.list.push(lock);
29 remove: function(index)
31 $.ajatus.events.locks.list = $.grep( $.ajatus.events.locks.list, function(n,i){
35 update: function(index, lock)
37 $.ajatus.events.locks.list[index] = lock;
41 $.ajatus.events.lock = function(options)
45 this.options = $.extend({
46 msg: '<h1><img src="'+$.ajatus.preferences.client.theme_url+'images/loading-small.gif" /> ' + $.ajatus.i10n.get('Loading Ajatus') + '...</h1>',
50 disable_application: true
53 this._id = $.ajatus.events.locks.add(this);
55 if ( !this.options.dialog
56 && this.options.disable_application)
58 $.blockUI(this.options.msg);
61 if (this.options.watch)
67 $.each(this.options.watch, function(i,n){
73 this._watcher = new $.ajatus.events.watcher(watcher_opts);
76 this._watcher.element.bind('on_stop', function(watcher_id){
80 this._watcher.start();
83 $.ajatus.events.locks.update(this);
86 $.extend($.ajatus.events.lock.prototype, {
88 if ( !this.options.dialog
89 && this.options.disable_application)
94 if ( this.options.on_release
95 && typeof(this.options.on_release) == 'function')
97 this.options.on_release(this);
100 update: function(lock_cnt) {
102 this._release_lock();
107 $.ajatus.events.watchers = {
110 $.extend($.ajatus.events.watchers, {
111 add: function(watcher) {
112 var new_count = $.ajatus.events.watchers.list.push(watcher);
115 remove: function(index) {
116 if (typeof $.ajatus.events.watchers.list[index] != 'undefined') {
117 delete($.ajatus.events.watchers.list[index]);
119 // $.ajatus.events.watchers.list = $.grep( $.ajatus.events.watchers.list, function(n,i){
120 // return i == index;
125 $.ajatus.events.watcher = function(options) {
126 this.options = $.extend({
136 if ( typeof(this.options.validate) != 'function'
137 && this.options.timed <= 0)
142 if (this.options.timed > 0) {
143 this.options.max_runs = false;
147 this._timeout_id = -1;
148 this._done_safety_runs = 0;
151 this._id = $.ajatus.events.watchers.add(this);
152 this.element = $('<div id="ajatus_events_watcher_'+this._id+'" />').appendTo($('body'));
154 if (this.options.auto_start) {
160 $.extend($.ajatus.events.watcher.prototype, {
162 if (this.options.interval < 200) {
163 this.options.interval = 200; // Safari needs at least 200 ms
165 // console.log("Watcher "+this._id+" started!");
166 this._timeout_id = setTimeout("if (typeof $.ajatus.events.watchers.list["+this._id+"] != 'undefined') {$.ajatus.events.watchers.list["+this._id+"]._check();}", this.options.interval);
169 // console.log("Watcher "+this._id+" is checking status");
170 if (typeof $.ajatus.events.watchers.list[this._id] == 'undefined') {
176 if (this.options.timed > 0) {
177 if ( this.elapsed >= this.options.timed
178 || ( this.options.max_runs
179 && this._done_runs >= (this.options.max_runs-this.options.safety_runs)))
183 this._done_safety_runs = 0;
184 this.elapsed += this.options.interval;
185 this._timeout_id = setTimeout("if (typeof $.ajatus.events.watchers.list["+this._id+"] != 'undefined') {$.ajatus.events.watchers.list["+this._id+"]._check();}", this.options.interval);
188 if ( this.options.validate()
189 || ( this.options.max_runs
190 && this._done_runs >= (this.options.max_runs-this.options.safety_runs)))
192 // console.log("Watcher "+this._id+" validated!");
194 if (this._done_safety_runs < this.options.safety_runs) {
195 this._done_safety_runs++;
196 //console.log("Watcher "+this._id+" doing safety run: "+this._done_safety_runs+" of "+this.options.safety_runs);
197 this._timeout_id = setTimeout("if (typeof $.ajatus.events.watchers.list["+this._id+"] != 'undefined') {$.ajatus.events.watchers.list["+this._id+"]._check();}", this.options.interval);
199 //console.log("Watcher "+this._id+" has done all safety runs!");
203 this._done_safety_runs = 0;
204 this._timeout_id = setTimeout("if (typeof $.ajatus.events.watchers.list["+this._id+"] != 'undefined') {$.ajatus.events.watchers.list["+this._id+"]._check();}", this.options.interval);
208 done_runs: this._done_runs,
209 elapsed: this.elapsed,
211 if (this.options.timed > 0) {
212 tick_data['elapsed'] = this.elapsed;
213 tick_data['timed'] = this.options.timed;
214 tick_data['left_ms'] = this.options.timed - this.elapsed;
215 tick_data['left_prc'] = (this.elapsed/this.options.timed)*100;
217 this.element.trigger('on_tick',[this._id, tick_data]);
220 // console.log("Watcher "+this._id+" ended!");
223 this.element.trigger('on_stop',[this._id]);
225 _cleanup: function() {
226 // console.log("CleanUp "+this._id);
227 clearTimeout(this._timeout_id);
228 //$.ajatus.events.watchers.remove(this._id);
232 $.ajatus.events.lock_pool = {
234 increase: function() {
235 $.ajatus.events.lock_pool.count = $.ajatus.events.named_lock_pool.increase('global');
236 // console.log('lock_pool increase to '+$.ajatus.events.lock_pool.count);
238 decrease: function() {
239 $.ajatus.events.lock_pool.count = $.ajatus.events.named_lock_pool.decrease('global');
240 // console.log('lock_pool decrease to '+$.ajatus.events.lock_pool.count);
243 $.ajatus.events.named_lock_pool.clear('global');
244 $.ajatus.events.lock_pool.count = 0;
248 $.ajatus.events.named_lock_pool = {
250 count: function(name) {
251 if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
254 return $.ajatus.events.named_lock_pool.counts[name];
257 increase: function(name) {
258 if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
259 $.ajatus.events.named_lock_pool.counts[name] = 0;
261 $.ajatus.events.named_lock_pool.counts[name]++;
263 //console.log('named_lock_pool increase '+name+' to '+$.ajatus.events.named_lock_pool.counts[name]);
264 return $.ajatus.events.named_lock_pool.counts[name];
266 decrease: function(name) {
267 if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
268 $.ajatus.events.named_lock_pool.counts[name] = 0;
270 $.ajatus.events.named_lock_pool.counts[name] = $.ajatus.events.named_lock_pool.counts[name]-1;
272 // console.log('named_lock_pool decrease '+name+' to '+$.ajatus.events.named_lock_pool.counts[name]);
273 return $.ajatus.events.named_lock_pool.counts[name];
275 clear: function(name) {
276 $.ajatus.events.named_lock_pool.counts[name] = 0;