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