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