]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
fixing typos.., small tweaks, re-enabled the timeout alert
[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         var org = findOrgUnit(org_id_or_node);
8         if(!org) return -1;
9         var type = findOrgType(org.ou_type());
10         if(type) return type.depth();
11         return -1;
12 }
13
14 function findOrgTypeFromDepth(depth) {
15         if( depth == null ) return null;
16         for( var type in globalOrgTypes ) {
17                 var t = globalOrgTypes[type];
18                 if( t.depth() == depth ) return t;
19         }
20 }
21
22 /* takes the org type id from orgunit.ou_type() field and returns
23         the org type object */
24 function findOrgType(type_id) {
25         if(typeof type_id == 'object') return type_id;
26         for(var type in globalOrgTypes) {
27                 var t =globalOrgTypes[type]; 
28                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
29                         return t;
30         }
31         return null;
32 }
33
34
35 /* returns an org unit by id.  if an object is passed in as the id,
36         then the object is assumed to be an org unit and is returned */
37 function findOrgUnit(org_id) {
38         return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
39 }
40
41
42 /* builds a trail from the top of the org tree to the node provide.
43         basically fleshes out 'my orgs' 
44         Returns an array of [org0, org1, ..., myorg] */
45 function orgNodeTrail(node) {
46         var na = new Array();
47         while( node ) {
48                 na.push(node);
49                 node = findOrgUnit(node.parent_ou());
50         }
51         return na.reverse();
52 }
53
54 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
55
56 /* true if 'org' is 'me' or a child of mine */
57 function orgIsMine(me, org) {
58         if(me.id() == org.id()) return true;
59         for( var i in me.children() ) {
60                 if(orgIsMine(me.children()[i], org))
61                         return true;
62         }
63         return false;
64 }
65
66
67
68 var orgArraySearcher = {};
69 var globalOrgTree;
70 for (var i in _l) {
71         var x = new aou();
72         x.id(_l[i][0]);
73         x.ou_type(_l[i][1]);
74         x.parent_ou(_l[i][2]);
75         x.name(_l[i][3]);
76         orgArraySearcher[x.id()] = x;
77 }
78 for (var i in orgArraySearcher) {
79         var x = orgArraySearcher[i];
80         if (x.parent_ou() == null || x.parent_ou() == '') {
81                 globalOrgTree = x;
82                 continue;
83         } 
84
85         var parent = findOrgUnit(x.parent_ou());
86         if (!parent.children()) parent.children(new Array());
87         parent.children().push(x);
88 }
89
90 function _tree_killer () {
91         for (var i in orgArraySearcher) {
92                 x=orgArraySearcher[i];
93                 x.children(null);
94                 x.parent_ou(null);
95                 orgArraySearcher[i]=null;
96         }
97         globalOrgTree = null;
98         orgArraySearcher = null;
99         globalOrgTypes = null;
100 }
101
102
103