moving over some more utils. Let me know if something makes more sense to do as...
authorphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 3 Oct 2005 21:25:56 +0000 (21:25 +0000)
committerphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 3 Oct 2005 21:25:56 +0000 (21:25 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1889 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/xul/staff_client/chrome/content/evergreen/util/fm_utils.js [new file with mode: 0644]
Open-ILS/xul/staff_client/chrome/content/evergreen/util/functional.js [new file with mode: 0644]

diff --git a/Open-ILS/xul/staff_client/chrome/content/evergreen/util/fm_utils.js b/Open-ILS/xul/staff_client/chrome/content/evergreen/util/fm_utils.js
new file mode 100644 (file)
index 0000000..ee1f7eb
--- /dev/null
@@ -0,0 +1,36 @@
+dump('entering util/fm_utils.js\n');
+
+if (typeof util == 'undefined') var util = {};
+util.fm_utils = {};
+
+util.fm_utils.EXPORT_OK        = [ 'flatten_ou_branch', 'find_ou' ];
+util.fm_utils.EXPORT_TAGS      = { ':all' : util.fm_utils.EXPORT_OK };
+
+util.fm_utils.flatten_ou_branch = function(branch) {
+       var my_array = new Array();
+       my_array.push( branch );
+       for (var i in branch.children() ) {
+               var child = branch.children()[i];
+               if (child != null) {
+                       var temp_array = util.fm_utils.flatten_ou_branch(child);
+                       for (var j in temp_array) {
+                               my_array.push( temp_array[j] );
+                       }
+               }
+       }
+       return my_array;
+}
+
+util.fm_utils.find_ou = function(tree,id) {
+       if (typeof(id)=='object') { id = id.id(); }
+       if (tree.id()==id) {
+               return tree;
+       }
+       for (var i in tree.children()) {
+               var child = tree.children()[i];
+               ou = util.fm_utils.find_ou( child, id );
+               if (ou) { return ou; }
+       }
+       return null;
+}
+
diff --git a/Open-ILS/xul/staff_client/chrome/content/evergreen/util/functional.js b/Open-ILS/xul/staff_client/chrome/content/evergreen/util/functional.js
new file mode 100644 (file)
index 0000000..84a74ce
--- /dev/null
@@ -0,0 +1,120 @@
+dump('entering util/functional.js\n');
+
+if (typeof util == 'undefined') var util = {};
+util.functional = {};
+
+util.functional.EXPORT_OK      = [ 
+       'filter_list', 'filter_object', 'find_list', 'find_object', 'map_list', 'map_flat_list', 
+       'map_object', 'map_object_to_list', 'convert_object_list_to_hash', 'find_id_object_in_list', 
+       'find_attr_object_in_list' 
+];
+util.functional.EXPORT_TAGS    = { ':all' : util.functional.EXPORT_OK };
+
+util.functional.filter_list = function(list,f) {
+       var new_list = [];
+       for (var i in list) {
+               var t = f( list[i] );
+               if (t) new_list.push( list[i] );
+       }
+       return new_list;
+}
+
+util.functional.filter_object = function(obj,f) {
+       var new_obj = {};
+       for (var i in obj) {
+               var t = f( i, obj[i] );
+               if (t) new_obj[i] = obj[i];
+       }
+       return new_obj;
+}
+
+util.functional.find_list = function(list,f) {
+       for (var i in list) {
+               var t = f( list[i] );
+               if (t) return list[i];
+       }
+       return null;
+}
+
+util.functional.find_object = function(obj,f) {
+       for (var i in obj) {
+               var t = f( i, obj[i] );
+               if (t) return obj[i];
+       }
+       return null;
+}
+
+util.functional.map_list = function(list,f) {
+       var new_list = [];
+       for (var i in list) {
+               new_list.push( f( list[i] ) );
+       }
+       return new_list;
+}
+
+util.functional.map_flat_list = function(list,f) {
+       var new_list = [];
+       for (var i in list) {
+               new_list = new_list.concat( f( list[i] ) );
+       }
+       return new_list;
+}
+
+util.functional.map_object = function(obj,f) {
+       var new_obj = {};
+       for (var i in obj) {
+               new_obj[ f( i, obj[i] )[0] ] = f( i, obj[i] )[1];
+       }
+       return new_obj;
+}
+
+util.functional.map_object_to_list = function(obj,f) {
+       var new_list = [];
+       for (var i in obj) {
+               new_list.push( f( obj, i ) );
+       }
+       return new_list;
+}
+
+util.functional.convert_object_list_to_hash = function(list) {
+       var my_hash = new Object();
+       if (list) {
+               for (var i = 0; i < list.length; i++) {
+                       my_hash[ list[i].id() ] = list[i];
+               }
+       }
+       return my_hash;
+}
+
+util.functional.find_id_object_in_list = function(list,id) {
+       if (list) {
+               for (var i = 0; i < list.length; i++ ) {
+                       try {
+                               if ( list[i].id() == id ) {
+                                       return list[i];
+                               }
+                       } catch(E) {
+                               throw(E);
+                       }
+               }
+       }
+       return null;
+}
+
+util.functional.find_attr_object_in_list = function(list,attr,value) {
+       if (list) {
+               for (var i = 0; i < list.length; i++ ) {
+                       try {
+                               var command = 'list[' + i + '].'+attr+'() == ' + value;
+                               if ( eval(command) ) {
+                                       return list[i];
+                               }
+                       } catch(E) {
+                               throw(E);
+                       }
+               }
+       }
+       return null;
+}
+
+dump('exiting util/functional.js\n');