1 dump('entering util/fm_utils.js\n');
3 if (typeof util == 'undefined') var util = {};
6 util.fm_utils.EXPORT_OK = [ 'flatten_ou_branch', 'find_ou', 'compare_aou_a_is_b_or_ancestor', 'sort_func_aou_by_depth_and_then_string', 'find_common_aou_ancestor', 'find_common_aou_ancestors' ];
7 util.fm_utils.EXPORT_TAGS = { ':all' : util.fm_utils.EXPORT_OK };
9 util.fm_utils.flatten_ou_branch = function(branch) {
10 var my_array = new Array();
11 my_array.push( branch );
12 if (typeof branch.children == 'function') for (var i in branch.children() ) {
13 var child = branch.children()[i];
15 var temp_array = util.fm_utils.flatten_ou_branch(child);
16 for (var j in temp_array) {
17 my_array.push( temp_array[j] );
24 util.fm_utils.find_ou = function(tree,id) {
25 if (typeof(id)=='object') { id = id.id(); }
29 for (var i in tree.children()) {
30 var child = tree.children()[i];
31 ou = util.fm_utils.find_ou( child, id );
32 if (ou) { return ou; }
37 util.fm_utils.compare_aou_a_is_b_or_ancestor = function(a,b) {
38 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
39 if (typeof a != 'object') a = data.hash.aou[ a ];
40 if (typeof b != 'object') b = data.hash.aou[ b ];
42 while ( node != null ) {
43 if (a.id() == node.id()) return true;
44 node = typeof node.parent_ou() == 'object' ? node.parent_ou() : data.hash.aou[ node.parent_ou() ];
49 util.fm_utils.sort_func_aou_by_depth_and_then_string = function(a,b) {
51 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
52 var a_aou = a[0]; var b_aou = b[0];
53 var a_string = a[1]; var b_string = b[1];
54 if (typeof a_aou != 'object') a_aou = data.hash.aou[ a_aou ];
55 if (typeof b_aou != 'object') b_aou = data.hash.aou[ b_aou ];
56 var A = data.hash.aout[ a_aou.ou_type() ].depth();
57 var B = data.hash.aout[ b_aou.ou_type() ].depth();
60 if (a_string < b_string ) return -1;
61 if (a_string > b_string ) return 1;
64 alert('error in util.fm_utils.sort_func_aou_by_depth_and_string: ' + E);
69 util.fm_utils.find_common_aou_ancestor = function(orgs) {
71 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
74 for (var i = 0; i < orgs.length; i++) {
80 if (typeof node != 'object') node = data.hash.aou[ node ];
83 if ( candidates[node.id()] ) {
85 candidates[node.id()]++;
89 candidates[node.id()] = 1;
92 if (candidates[node.id()] == orgs.length) return node;
94 node = node.parent_ou();
102 alert('error in util.fm_utils.find_common_aou_ancestor: ' + E);
107 util.fm_utils.find_common_aou_ancestors = function(orgs) {
109 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
111 var candidates = {}; var winners = [];
112 for (var i = 0; i < orgs.length; i++) {
118 if (typeof node != 'object') node = data.hash.aou[ node ];
121 if ( candidates[node.id()] ) {
123 candidates[node.id()]++;
127 candidates[node.id()] = 1;
130 node = node.parent_ou();
135 for (var i in candidates) {
137 if (candidates[i] == orgs.length) winners.push( i );
143 alert('error in util.fm_utils.find_common_aou_ancestors: ' + E);