]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/lib/js/opac/org_utils.js
adding methods, config vars, login code
[working/Evergreen.git] / Open-ILS / src / javascript / lib / js / opac / org_utils.js
1 /* takes an org unit or id and return the numeric depth */
2 function findOrgDepth(org_id_or_node) {
3
4         if(org_id_or_node == null || globalOrgTypes == null)
5                 return null;
6
7         var org = findOrgUnit(org_id_or_node);
8
9         var t = findOrgType(org.ou_type());
10         if(t != null) return t.depth();
11
12         return null;
13 }
14
15 /* takes the org type id from orgunit.ou_type() field and returns
16         the org type object */
17 function findOrgType(type_id) {
18
19         if(type_id == null || globalOrgTypes == null)
20                 return null;
21
22         if(typeof type_id == 'object')
23                 return type_id;
24
25         for(var type in globalOrgTypes) {
26                 var t =globalOrgTypes[type]; 
27                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
28                         return t;
29         }
30         return null;
31 }
32
33
34 /* locates a specific org unit by id, acts as a cache of orgs*/
35 var orgArraySearcher = null;
36
37 /* flatten the org tree for faster searching */
38 function _flattenOrgs(node) { 
39
40         if(node == null) {
41                 node = globalOrgTree;
42                 orgArraySearcher = new Object();
43         }
44
45         orgArraySearcher[node.id()] = node;
46         for(var idx in node.children()) {
47                 _flattenOrgs(node.children()[idx]);
48         }
49 }
50
51 /* returns an org unit by id.  if an object is passed in as the id,
52         then the object is assumed to be an org unit and is returned */
53 function findOrgUnit(org_id, branch) {
54
55         if(org_id == null) return null;
56         if(typeof org_id == 'object') return org_id;
57
58         /* if we don't have the global org tree, grab the org unit from the server */
59         var tree_exists = false;
60         try{if(globalOrgTree != null) tree_exists = true;}catch(E){}
61
62         if(!tree_exists) {
63                 var org = orgArraySearcher[org_id];
64                 if(org) return org;
65                 var r = new RemoteRequest(
66                         "open-ils.actor",
67                         "open-ils.actor.org_unit.retrieve", null, org_id);
68                 r.send(true);
69                 orgArraySearcher[org_id] = r.getResultObject();
70                 return orgArraySearcher[org_id];
71         }
72
73         if(orgArraySearcher == null)
74                 _flattenOrgs();
75
76         return orgArraySearcher[org_id];
77 }
78
79
80 /* builds a trail from the top of the org tree to the node provide.
81         basically fleshes out 'my orgs' 
82         Returns an array of [org0, org1, ..., myorg]
83  */
84 function orgNodeTrail(node) {
85         var nodeArray = new Array();
86         while( node ) {
87                 nodeArray.push(node);
88                 node = findOrgUnit(node.parent_ou());
89         }
90         nodeArray = nodeArray.reverse();
91         return nodeArray;
92 }
93
94
95 /* returns an array of sibling org units */
96 function findSiblingOrgs(node) {
97         return findOrgUnit(node.parent_ou()).children();
98 }
99