]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/exec.js
inisial staff client integration in record details page w/ new staff js file; move...
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / exec.js
1 dump('entering util/exec.js\n');
2
3 if (typeof util == 'undefined') var util = {};
4 util.exec = function(chunk_size) {
5     var obj = this;
6
7     this.chunk_size = chunk_size || 1;
8     obj.clear_timer = function() {
9         try {
10             if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: Clearing interval with id = ' + obj._intervalId + '\n'); }
11             window.clearInterval(obj._intervalId);
12         } catch(E) {
13             alert('Error in clear_timer: ' + E);
14         }
15     }
16
17     return this;
18 };
19
20 util.exec.prototype = {
21     // This will create a timer that polls the specified array and shifts off functions to execute
22     'timer' : function(funcs,interval) {
23         var obj = this;
24
25         if (obj._intervalId) {
26             obj.clear_timer();
27             window.removeEventListener('unload',obj.clear_timer,false); 
28         }
29         var intervalId = window.setInterval(
30             function() {
31                 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: ' + location.pathname + ': Running interval with id = ' + intervalId + '\n'); }
32                 var i = obj.chunk_size;
33                 while (funcs.length > 0 && i > 0) {
34                     funcs.shift()(); i--;
35                 }
36             },
37             interval
38         );
39         obj._intervalId  = intervalId;
40         window.addEventListener('unload',obj.clear_timer,false); 
41         return intervalId;
42     },
43     // This executes a series of functions, but tries to give other events/functions a chance to
44     // execute between each one.
45     'chain' : function () {
46         var args = [];
47         var obj = this;
48         for (var i = 0; i < arguments.length; i++) {
49             var arg = arguments[i];
50             switch(arg.constructor.name) {
51                 case 'Function' :
52                     args.push( arg );
53                 break;
54                 case 'Array' :
55                     for (var j = 0; j < arg.length; j++) {
56                         if (typeof arg[j] == 'function') args.push( arg[j] );
57                     }
58                 break;
59                 case 'Object' :
60                     for (var j in arg) {
61                         if (typeof arg[j] == 'function') args.push( arg[j] );
62                     }
63                 break;
64             }
65         }
66         if (args.length > 0) setTimeout(
67             function() {
68                 try {
69                     for (var i = 0; (i < args.length && i < obj.chunk_size) ; i++) {
70                         try {
71                             if (typeof args[i] == 'function') {
72                                 dump('EXEC: executing queued function.   intervalId = ' + obj._intervalId + '\n');
73                                 if (obj.debug) {
74                                     dump('EXEC: function = ' + args[i] + '\n');
75                                 }
76                                 args[i]();
77                             } else {
78                                 alert('FIXME -- typeof args['+i+'] == ' + typeof args[i]);
79                             }
80                         } catch(E) {
81                             dump('EXEC: util.exec.chain error: ' + js2JSON(E) + '\n');
82                             var keep_going = false;
83                             if (typeof obj.on_error == 'function') {
84                                 keep_going = obj.on_error(E);
85                             }
86                             if (keep_going) {
87                                 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: chain not broken\n'); }
88                                 try {
89                                     if (args.length > 1 ) obj.chain( args.slice(1) );
90
91                                 } catch(E) {
92                                     if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: another error: ' + js2JSON(E) + '\n'); }
93                                 }
94                             } else {
95                                 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: chain broken\n'); }
96                                 return;
97                             }
98                         }
99                     }
100                     if (args.length > obj.chunk_size ) obj.chain( args.slice(obj.chunk_size) );
101                 } catch(E) {
102                     alert(E);
103                 }
104             }, 0
105         );
106     }
107 }
108
109 dump('exiting util/exec.js\n');