]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
fd7753a32da3c2f97d4bc059d14cf2dd7a66d26b
[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 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
43
44 /* true if 'org' is 'me' or a child of mine */
45 function orgIsMine(me, org) {
46         if(me.id() == org.id()) return true;
47         for( var i in me.children() ) {
48                 if(orgIsMine(me.children()[i], org))
49                         return true;
50         }
51         return false;
52 }
53
54
55
56 var orgArraySearcher = {};
57 var globalOrgTree;
58 for (var i in _l) {
59         var x = new aou();
60         x.id(_l[i][0]);
61         x.ou_type(_l[i][1]);
62         x.parent_ou(_l[i][2]);
63         x.name(_l[i][3]);
64         orgArraySearcher[x.id()] = x;
65 }
66 for (var i in orgArraySearcher) {
67         var x = orgArraySearcher[i];
68         if (x.parent_ou() == null || x.parent_ou() == '') {
69                 globalOrgTree = x;
70                 continue;
71         } 
72
73         var parent = findOrgUnit(x.parent_ou());
74         if (!parent.children()) parent.children(new Array());
75         parent.children().push(x);
76 }
77
78 function _tree_killer () {
79         for (var i in orgArraySearcher) {
80                 x=orgArraySearcher[i];
81                 x.children(null);
82                 x.parent_ou(null);
83                 orgArraySearcher[i]=null;
84         }
85         globalOrgTree = null;
86         orgArraySearcher = null;
87         globalOrgTypes = null;
88 }
89
90
91