]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
fixed session cookie to work with IE
[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 var orgArraySearcherSN = {};
42 function findOrgUnitSN(shortname) {
43         if (typeof shortname == 'object') return shortname;
44         if( orgArraySearcherSN[shortname] ) return orgArraySearcherSN[shortname];
45         _debug("fetching org by shortname "+shortname);
46         var req = new Request(FETCH_ORG_BY_SHORTNAME, shortname);
47         req.request.alertEvent = false;
48         req.send(true);
49         return req.result();
50 }
51
52
53 /* builds a trail from the top of the org tree to the node provide.
54         basically fleshes out 'my orgs' 
55         Returns an array of [org0, org1, ..., myorg] */
56 function orgNodeTrail(node) {
57         var na = new Array();
58         while( node ) {
59                 na.push(node);
60                 node = findOrgUnit(node.parent_ou());
61         }
62         return na.reverse();
63 }
64
65 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
66
67 /* true if 'org' is 'me' or a child of mine */
68 function orgIsMine(me, org) {
69         if(!me || !org) return false;
70         if(me.id() == org.id()) return true;
71         for( var i in me.children() ) {
72                 if(orgIsMine(me.children()[i], org))
73                         return true;
74         }
75         return false;
76 }
77
78
79
80 var orgArraySearcher = {};
81 var globalOrgTree;
82 for (var i in _l) {
83         var x = new aou();
84         x.id(_l[i][0]);
85         x.ou_type(_l[i][1]);
86         x.parent_ou(_l[i][2]);
87         x.name(_l[i][3]);
88         orgArraySearcher[x.id()] = x;
89 }
90 for (var i in orgArraySearcher) {
91         var x = orgArraySearcher[i];
92         if (x.parent_ou() == null || x.parent_ou() == '') {
93                 globalOrgTree = x;
94                 continue;
95         } 
96
97         var parent = findOrgUnit(x.parent_ou());
98         if (!parent.children()) parent.children(new Array());
99         parent.children().push(x);
100 }
101
102 function _tree_killer () {
103         for (var i in orgArraySearcher) {
104                 x=orgArraySearcher[i];
105                 x.children(null);
106                 x.parent_ou(null);
107                 orgArraySearcher[i]=null;
108         }
109         globalOrgTree = null;
110         orgArraySearcher = null;
111         globalOrgTypes = null;
112 }
113
114
115