converted util.exec from a function library to an object library
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / evergreen / util / exec.js
1 dump('entering util/exec.js\n');
2
3 if (typeof util == 'undefined') var util = {};
4 util.exec = function() {
5         JSAN.use('util.error');
6         this.error = new util.error();
7
8         return this;
9 };
10
11 util.exec.prototype = {
12         // This executes a series of functions, but tries to give other events/functions a chance to
13         // execute between each one.
14         'chain' : function () {
15                 var args = [];
16                 var obj = this;
17                 for (var i = 0; i < arguments.length; i++) {
18                         var arg = arguments[i];
19                         switch(arg.constructor.name) {
20                                 case 'Function' :
21                                         args.push( arg );
22                                 break;
23                                 case 'Array' :
24                                         for (var j = 0; j < arg.length; j++) {
25                                                 if (typeof arg[j] == 'function') args.push( arg[j] );
26                                         }
27                                 break;
28                                 case 'Object' :
29                                         for (var j in arg) {
30                                                 if (typeof arg[j] == 'function') args.push( arg[j] );
31                                         }
32                                 break;
33                         }
34                 }
35                 if (args.length > 0) setTimeout(
36                         function() {
37                                 try {
38                                         args[0]();
39                                         if (args.length > 1 ) obj.chain( args.slice(1) );
40                                 } catch(E) {
41                                         this.error('D_EXEC','util.exec.chain broken: ' + E);
42                                         if (typeof obj.on_error == 'function') {
43                                                 obj.on_error(E);
44                                         }
45                                 }
46                         }, 0
47                 );
48         }
49 }
50
51 dump('exiting util/exec.js\n');