Dutch from Niels Breet and Polish from Piotr Pokora
[ajatus.git] / js / ajatus.events.js
blob417110696871115d573693284cbbf8f630452d1e
1 /*
2 * This file is part of
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
15 (function($){
16 $.ajatus = $.ajatus || {};
17 $.ajatus.locker = $.ajatus.locker || {};
18 $.ajatus.events = $.ajatus.events || {};
20 $.ajatus.events.locks = {
21 list: []
23 $.extend($.ajatus.events.locks, {
24 add: function(lock)
26 var new_count = $.ajatus.events.locks.list.push(lock);
27 return new_count-1;
29 remove: function(index)
31 $.ajatus.events.locks.list = $.grep( $.ajatus.events.locks.list, function(n,i){
32 return i == index;
33 });
35 update: function(index, lock)
37 $.ajatus.events.locks.list[index] = lock;
39 });
41 $.ajatus.events.lock = function(options)
43 this._manual = true;
44 this._watcher = null;
45 this.options = $.extend({
46 msg: '<h1><img src="'+$.ajatus.preferences.client.application_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);
55 if ( !this.options.dialog
56 && this.options.disable_application)
58 $.blockUI(this.options.msg);
61 if (this.options.watch)
63 var watcher_opts = {
64 after: true,
65 auto_start: false
67 $.each(this.options.watch, function(i,n){
68 if (i != 'after')
70 watcher_opts[i] = n;
72 });
73 this._watcher = new $.ajatus.events.watcher(watcher_opts);
75 var self = this;
76 this._watcher.element.bind('on_stop', function(watcher_id){
77 self.release();
78 });
80 this._watcher.start();
83 $.ajatus.events.locks.update(this);
84 return this;
86 $.extend($.ajatus.events.lock.prototype, {
87 release: function() {
88 if ( !this.options.dialog
89 && this.options.disable_application)
91 $.unblockUI();
94 if ( this.options.on_release
95 && typeof(this.options.on_release) == 'function')
97 this.options.on_release(this);
100 update: function(lock_cnt) {
101 if (lock_cnt == 0) {
102 this._release_lock();
107 $.ajatus.events.watchers = {
108 list: []
110 $.extend($.ajatus.events.watchers, {
111 add: function(watcher) {
112 var new_count = $.ajatus.events.watchers.list.push(watcher);
113 return new_count-1;
115 remove: function(index) {
116 $.ajatus.events.watchers.list = $.grep( $.ajatus.events.watchers.list, function(n,i){
117 return i == index;
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)
136 return false;
139 this.elapsed = 0;
140 this._timeout_id = -1;
141 this._done_safety_runs = 0;
142 this._done_runs = 0;
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();
151 return this;
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
158 // console.log("Watcher "+this._id+" started!");
159 this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
161 _check: function() {
162 // console.log("Watcher "+this._id+" is checking status");
163 this._done_runs++;
165 if (this.options.timed > 0) {
166 if ( this.elapsed >= this.options.timed
167 || this._done_runs >= (this.options.max_runs-this.options.safety_runs))
169 this._stop();
170 } else {
171 this._done_safety_runs = 0;
172 this.elapsed += this.options.interval;
173 this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
175 } else {
176 if ( this.options.validate()
177 || this._done_runs >= (this.options.max_runs-this.options.safety_runs))
179 //console.log("Watcher "+this._id+" validated!");
181 if (this._done_safety_runs < this.options.safety_runs) {
182 this._done_safety_runs++;
183 //console.log("Watcher "+this._id+" doing safety run: "+this._done_safety_runs+" of "+this.options.safety_runs);
184 this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
185 } else {
186 //console.log("Watcher "+this._id+" has done all safety runs!");
187 this._stop();
189 } else {
190 this._done_safety_runs = 0;
191 this._timeout_id = setTimeout("$.ajatus.events.watchers.list["+this._id+"]._check();", this.options.interval);
196 _stop: function() {
197 // console.log("Watcher "+this._id+" ended!");
198 clearTimeout(this._timeout_id);
200 this.element.trigger('on_stop',[this._id]);
204 $.ajatus.events.lock_pool = {
205 count: 0,
206 increase: function() {
207 $.ajatus.events.lock_pool.count = $.ajatus.events.named_lock_pool.increase('global');
208 // console.log('lock_pool increase to '+$.ajatus.events.lock_pool.count);
210 decrease: function() {
211 $.ajatus.events.lock_pool.count = $.ajatus.events.named_lock_pool.decrease('global');
212 // console.log('lock_pool decrease to '+$.ajatus.events.lock_pool.count);
214 clear: function() {
215 $.ajatus.events.named_lock_pool.clear('global');
216 $.ajatus.events.lock_pool.count = 0;
220 $.ajatus.events.named_lock_pool = {
221 counts: {},
222 count: function(name) {
223 if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
224 return 0;
225 } else {
226 return $.ajatus.events.named_lock_pool.counts[name];
229 increase: function(name) {
230 if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
231 $.ajatus.events.named_lock_pool.counts[name] = 0;
233 $.ajatus.events.named_lock_pool.counts[name]++;
235 //console.log('named_lock_pool increase '+name+' to '+$.ajatus.events.named_lock_pool.counts[name]);
236 return $.ajatus.events.named_lock_pool.counts[name];
238 decrease: function(name) {
239 if ($.ajatus.events.named_lock_pool.counts[name] == undefined) {
240 $.ajatus.events.named_lock_pool.counts[name] = 0;
241 } else {
242 $.ajatus.events.named_lock_pool.counts[name] = $.ajatus.events.named_lock_pool.counts[name]-1;
244 // console.log('named_lock_pool decrease '+name+' to '+$.ajatus.events.named_lock_pool.counts[name]);
245 return $.ajatus.events.named_lock_pool.counts[name];
247 clear: function(name) {
248 $.ajatus.events.named_lock_pool.counts[name] = 0;
252 })(jQuery);