]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
breaking out org stuff
[working/Evergreen.git] / Open-ILS / web / opac / common / js / org_utils.js
1 /* ------------------------------------------------------------------------------------------------------ */
2 /* org tree utilities */
3 /* ------------------------------------------------------------------------------------------------------ */
4
5 /* takes an org unit or id and return the numeric depth */
6 function findOrgDepth(org_id_or_node) {
7         return findOrgType(findOrgUnit(org_id_or_node).ou_type()).depth();
8 }
9
10 /* takes the org type id from orgunit.ou_type() field and returns
11         the org type object */
12 function findOrgType(type_id) {
13         if(typeof type_id == 'object') return type_id;
14         for(var type in globalOrgTypes) {
15                 var t =globalOrgTypes[type]; 
16                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
17                         return t;
18         }
19         return null;
20 }
21
22
23 /* returns an org unit by id.  if an object is passed in as the id,
24         then the object is assumed to be an org unit and is returned */
25 function findOrgUnit(org_id) {
26         return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
27 }
28
29
30 /* builds a trail from the top of the org tree to the node provide.
31         basically fleshes out 'my orgs' 
32         Returns an array of [org0, org1, ..., myorg] */
33 function orgNodeTrail(node) {
34         var na = new Array();
35         while( node ) {
36                 na.push(node);
37                 node = findOrgUnit(node.parent_ou());
38         }
39         return na.reverse();
40 }
41
42
43
44 var orgArraySearcher = {};
45 var globalOrgTree;
46 for (var i in _l) {
47         var x = new aou();
48         x.id(_l[i][0]);
49         x.ou_type(_l[i][1]);
50         x.parent_ou(_l[i][2]);
51         x.name(_l[i][3]);
52         orgArraySearcher[x.id()] = x;
53 }
54 for (var i in orgArraySearcher) {
55         var x = orgArraySearcher[i];
56         if (x.parent_ou() == null || x.parent_ou() == '') {
57                 globalOrgTree = x;
58                 continue;
59         } 
60
61         var parent = findOrgUnit(x.parent_ou());
62         if (!parent.children()) parent.children(new Array());
63         parent.children().push(x);
64 }
65
66 function _tree_killer () {
67         for (var i in orgArraySearcher) {
68                 x=orgArraySearcher[i];
69                 x.children(null);
70                 x.parent_ou(null);
71                 orgArraySearcher[i]=null;
72         }
73         globalOrgTree = null;
74         orgArraySearcher = null;
75         globalOrgTypes = null;
76 }