]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/fm_utils.js
LP#882586: In Serial Control View, show copy templates owned by ancestors
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / fm_utils.js
1 dump('entering util/fm_utils.js\n');
2
3 if (typeof util == 'undefined') var util = {};
4 util.fm_utils = {};
5
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',  'aou_get_ancestor_list_by_id' ];
7 util.fm_utils.EXPORT_TAGS    = { ':all' : util.fm_utils.EXPORT_OK };
8
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];
14         if (child != null) {
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] );
18             }
19         }
20     }
21     return my_array;
22 }
23
24 util.fm_utils.find_ou = function(tree,id) {
25     if (typeof(id)=='object') { id = id.id(); }
26     if (tree.id()==id) {
27         return tree;
28     }
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; }
33     }
34     return null;
35 }
36
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 ];
41     var node = 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() ];
45     }
46     return false;
47 }
48
49 util.fm_utils.sort_func_aou_by_depth_and_then_string = function(a,b) {
50     try {
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();
58         if (A < B) return 1;
59         if (A > B) return -1;
60         if (a_string < b_string ) return -1;
61         if (a_string > b_string ) return 1;
62         return 0;
63     } catch(E) {
64         alert('error in util.fm_utils.sort_func_aou_by_depth_and_string: ' + E);
65         return 0;
66     }
67 }
68
69 util.fm_utils.find_common_aou_ancestor = function(orgs) {
70     try {
71         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
72
73         var candidates = {};
74         for (var i = 0; i < orgs.length; i++) {
75
76             var node = orgs[i]; 
77
78             while (node) {
79
80                 if (typeof node != 'object') node = data.hash.aou[ node ];
81                 if (!node) continue;
82
83                 if ( candidates[node.id()] ) {
84
85                     candidates[node.id()]++;
86                     
87                 } else {
88
89                     candidates[node.id()] = 1;
90                 }
91
92                 if (candidates[node.id()] == orgs.length) return node;
93
94                 node = node.parent_ou();
95             }
96
97         }
98
99         return null;
100
101     } catch(E) {
102         alert('error in util.fm_utils.find_common_aou_ancestor: ' + E);
103         return null;
104     }
105 }
106
107 util.fm_utils.find_common_aou_ancestors = function(orgs) {
108     try {
109         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
110
111         var candidates = {}; var winners = [];
112         for (var i = 0; i < orgs.length; i++) {
113
114             var node = orgs[i]; 
115
116             while (node) {
117
118                 if (typeof node != 'object') node = data.hash.aou[ node ];
119                 if (!node) continue;
120
121                 if ( candidates[node.id()] ) {
122
123                     candidates[node.id()]++;
124                     
125                 } else {
126
127                     candidates[node.id()] = 1;
128                 }
129
130                 node = node.parent_ou();
131             }
132
133         }
134
135         for (var i in candidates) {
136
137             if (candidates[i] == orgs.length) winners.push( i );
138         }
139
140         return winners;
141
142     } catch(E) {
143         alert('error in util.fm_utils.find_common_aou_ancestors: ' + E);
144         return [];
145     }
146 }
147
148 /* There wasn't already something like this in staff client JS? Really? */
149 util.fm_utils.aou_get_ancestor_list_by_id = function(aou_id) {
150     JSAN.use('OpenILS.data');
151     var data = new OpenILS.data();
152     data.stash_retrieve();
153
154     var list = [];
155
156     var aou = data.hash.aou[aou_id];
157     do {
158         list.push(aou.id());
159         aou = data.hash.aou[aou.parent_ou()];
160     } while (aou);
161
162     return list;
163 }