]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
using batch version of perm org fetcher. added onload callback option
[Evergreen.git] / Open-ILS / web / opac / common / js / org_utils.js
1 /* ------------------------------------------------------------------------------------------------------ */
2 /* org tree utilities */
3 /* ------------------------------------------------------------------------------------------------------ */
4
5 function fetchOrgSettingDefault(orgId, name) {
6     var req = new Request(FETCH_ORG_SETTING, orgId, name);
7     req.send(true);
8     var res = req.result();
9     return (res) ? res.value : null;
10 }
11
12
13
14 /* takes an org unit or id and return the numeric depth */
15 function findOrgDepth(org_id_or_node) {
16         var org = findOrgUnit(org_id_or_node);
17         if(!org) return -1;
18         var type = findOrgType(org.ou_type());
19         if(type) return type.depth();
20         return -1;
21 }
22
23 function findOrgTypeFromDepth(depth) {
24         if( depth == null ) return null;
25         for( var type in globalOrgTypes ) {
26                 var t = globalOrgTypes[type];
27                 if( t.depth() == depth ) return t;
28         }
29 }
30
31 /* takes the org type id from orgunit.ou_type() field and returns
32         the org type object */
33 function findOrgType(type_id) {
34         if(typeof type_id == 'object') return type_id;
35         for(var type in globalOrgTypes) {
36                 var t =globalOrgTypes[type]; 
37                 if( t.id() == type_id || t.id() == parseInt(type_id) ) 
38                         return t;
39         }
40         return null;
41 }
42
43
44 /* returns an org unit by id.  if an object is passed in as the id,
45         then the object is assumed to be an org unit and is returned */
46 function findOrgUnit(org_id) {
47         return (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
48 }
49
50 function findOrgLasso(lasso_id) {
51         if (typeof lasso_id == 'object') return lasso_id;
52     for (var i in _lasso) {
53         if (_lasso[i].id() == lasso_id) return _lasso[i];
54     }
55     return null;
56 }
57
58 var orgArraySearcherSN = {};
59 function findOrgUnitSN(shortname) {
60         if (typeof shortname == 'object') return shortname;
61         if( orgArraySearcherSN[shortname] ) return orgArraySearcherSN[shortname];
62         _debug("fetching org by shortname "+shortname);
63         var req = new Request(FETCH_ORG_BY_SHORTNAME, shortname);
64         req.request.alertEvent = false;
65         req.send(true);
66         return req.result();
67 }
68
69
70 /* builds a trail from the top of the org tree to the node provide.
71         basically fleshes out 'my orgs' 
72         Returns an array of [org0, org1, ..., myorg] */
73 function orgNodeTrail(node) {
74         var na = new Array();
75         while( node ) {
76                 na.push(node);
77                 node = findOrgUnit(node.parent_ou());
78         }
79         return na.reverse();
80 }
81
82 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
83
84 /* true if 'org' is 'me' or a child of mine */
85 function orgIsMine(me, org) {
86         if(!me || !org) return false;
87         if(me.id() == org.id()) return true;
88         for( var i in me.children() ) {
89                 if(orgIsMine(me.children()[i], org))
90                         return true;
91         }
92         return false;
93 }
94
95 function orgIsMineFromSet(meList, org) {
96     org = findOrgUnit(org);
97     for(var i = 0; i < meList.length; i++) {
98         if(orgIsMine(findOrgUnit(meList[i]), org))
99             return true;
100     }
101     return false;
102 }
103
104 var orgArraySearcher = {};
105 var globalOrgTree;
106 for (var i in _l) {
107         var x = new aou();
108         x.id(_l[i][0]);
109         x.ou_type(_l[i][1]);
110         x.parent_ou(_l[i][2]);
111         x.name(_l[i][3]);
112     x.opac_visible(_l[i][4]);
113         orgArraySearcher[x.id()] = x;
114 }
115 for (var i in orgArraySearcher) {
116         var x = orgArraySearcher[i];
117         if (x.parent_ou() == null || x.parent_ou() == '') {
118                 globalOrgTree = x;
119                 continue;
120         } 
121
122         var par = findOrgUnit(x.parent_ou());
123         if (!par.children()) par.children(new Array());
124         par.children().push(x);
125 }
126
127 function _tree_killer () {
128         for (var i in orgArraySearcher) {
129                 x=orgArraySearcher[i];
130                 x.children(null);
131                 x.parent_ou(null);
132                 orgArraySearcher[i]=null;
133         }
134         globalOrgTree = null;
135         orgArraySearcher = null;
136         globalOrgTypes = null;
137 }
138
139
140