]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/exec.js
internal: an alternative to default_focus
[working/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         obj.clear_timer();
26         var intervalId = window.setInterval(
27             function() {
28                 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: ' + location.pathname + ': Running interval with id = ' + intervalId + '\n'); }
29                 var i = obj.chunk_size;
30                 while (funcs.length > 0 && i > 0) {
31                     funcs.shift()(); i--;
32                 }
33             },
34             interval
35         );
36         obj._intervalId  = intervalId;
37         window.addEventListener('unload',obj.clear_timer,false); 
38         return intervalId;
39     },
40     'clear_timer' : function() {
41         var obj = this;
42         if (obj._intervalId) {
43             obj.clear_timer();
44             window.removeEventListener('unload',obj.clear_timer,false);
45         }
46     },
47     // This executes a series of functions, but tries to give other events/functions a chance to
48     // execute between each one.
49     'chain' : function () {
50         var args = [];
51         var obj = this;
52         for (var i = 0; i < arguments.length; i++) {
53             var arg = arguments[i];
54             switch(arg.constructor.name) {
55                 case 'Function' :
56                     args.push( arg );
57                 break;
58                 case 'Array' :
59                     for (var j = 0; j < arg.length; j++) {
60                         if (typeof arg[j] == 'function') args.push( arg[j] );
61                     }
62                 break;
63                 case 'Object' :
64                     for (var j in arg) {
65                         if (typeof arg[j] == 'function') args.push( arg[j] );
66                     }
67                 break;
68             }
69         }
70         if (args.length > 0) setTimeout(
71             function() {
72                 try {
73                     for (var i = 0; (i < args.length && i < obj.chunk_size) ; i++) {
74                         try {
75                             if (typeof args[i] == 'function') {
76                                 dump('EXEC: executing queued function.   intervalId = ' + obj._intervalId + '\n');
77                                 if (obj.debug) {
78                                     dump('EXEC: function = ' + args[i] + '\n');
79                                 }
80                                 args[i]();
81                             } else {
82                                 alert('FIXME -- typeof args['+i+'] == ' + typeof args[i]);
83                             }
84                         } catch(E) {
85                             dump('EXEC: util.exec.chain error: ' + js2JSON(E) + '\n');
86                             var keep_going = false;
87                             if (typeof obj.on_error == 'function') {
88                                 keep_going = obj.on_error(E);
89                             }
90                             if (keep_going) {
91                                 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: chain not broken\n'); }
92                                 try {
93                                     if (args.length > 1 ) obj.chain( args.slice(1) );
94
95                                 } catch(E) {
96                                     if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: another error: ' + js2JSON(E) + '\n'); }
97                                 }
98                             } else {
99                                 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: chain broken\n'); }
100                                 return;
101                             }
102                         }
103                     }
104                     if (args.length > obj.chunk_size ) obj.chain( args.slice(obj.chunk_size) );
105                 } catch(E) {
106                     alert(E);
107                 }
108             }, 0
109         );
110     }
111 }
112
113 dump('exiting util/exec.js\n');