]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/fieldmapper/OrgUtils.js
Apply jshint suggestions for cleanup to dojo.fieldmapper
[working/Evergreen.git] / Open-ILS / web / js / dojo / fieldmapper / OrgUtils.js
1 /*
2 # ---------------------------------------------------------------------------
3 # Copyright (C) 2008  Georgia Public Library Service / Equinox Software, Inc
4 # Mike Rylander <miker@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------------------
16 */
17
18 if(!dojo._hasResource["fieldmapper.OrgUtils"]){
19
20         dojo._hasResource["fieldmapper.OrgUtils"] = true;
21         dojo.provide("fieldmapper.OrgUtils");
22         dojo.require("fieldmapper.Fieldmapper");
23         dojo.require("fieldmapper.OrgTree", true);
24         dojo.require("fieldmapper.OrgLasso", true);
25
26         fieldmapper.aou.slim_ok = true;
27         fieldmapper.aou.globalOrgTree = {};
28         fieldmapper.aou.OrgCache = {};
29         fieldmapper.aou.OrgCacheSN = {};
30         fieldmapper.aout.OrgTypeCache = {};
31
32         fieldmapper.aout.LoadOrgTypes = function () {
33                 var i;
34                 for (i in fieldmapper.aout.OrgTypeCache) {
35                         return;
36                 }
37
38                 var types = fieldmapper.standardRequest(['open-ils.actor','open-ils.actor.org_types.retrieve']);
39
40                 for (i = 0; i < types.length; i++) {
41                         fieldmapper.aout.OrgTypeCache[types[i].id()] = {
42                                 loaded : true,
43                                 type : types[i]
44                         };
45                 }
46         };
47
48         fieldmapper.aou.LoadOrg = function (id, slim_ok) {
49                 if (slim_ok === null) slim_ok = fieldmapper.aou.slim_ok;
50                 var slim_o = fieldmapper.aou.OrgCache[id];
51
52                 if (slim_o && (slim_ok || slim_o.loaded))
53                         return fieldmapper.aou.OrgCache[id].org;
54
55                 var o = fieldmapper.standardRequest(['open-ils.actor','open-ils.actor.org_unit.retrieve'],[null,id]);
56         if(!(o && o.id)) {
57             throw new Error("fieldmapper.aou.LoadOrg(): No org unit found with ID " + id);
58         }
59                 o.children = fieldmapper.aou.OrgCache[o.id()].children;
60                 fieldmapper.aou.OrgCache[o.id()] = { loaded : true, org : o };
61                 return o;
62         };
63         fieldmapper.aou.findOrgUnit = fieldmapper.aou.LoadOrg;
64
65         if (window._l) {
66                 for (var i = 0; i < _l.length; i++) {
67                         fieldmapper.aou.OrgCache[_l[i][0]] = {
68                                 loaded: false,
69                                 org : new fieldmapper.aou().fromHash({
70                                         id : _l[i][0],
71                                         ou_type : _l[i][1],
72                                         parent_ou : _l[i][2],
73                                         name : _l[i][3],
74                                         opac_visible : _l[i][4],
75                                         shortname : _l[i][5]
76                                 })
77                         };
78
79                 }
80
81                 for (var i in fieldmapper.aou.OrgCache) {
82                         var x = fieldmapper.aou.OrgCache[i].org;
83                         if (x.parent_ou() === null || x.parent_ou() === '') {
84                                 fieldmapper.aou.globalOrgTree = x;
85                                 continue;
86                         }
87
88                         var par = fieldmapper.aou.findOrgUnit(x.parent_ou(),true);
89                         if (!par.children()) par.children([]);
90                         par.children().push(x);
91                         fieldmapper.aou.OrgCache[x.id()].treePtr = x;
92                 }
93
94                 for (var i = 0; i < globalOrgTypes.length; i++) {
95                         fieldmapper.aout.OrgTypeCache[globalOrgTypes[i].id()] = {
96                                 loaded : true,
97                                 type : globalOrgTypes[i]
98                         };
99                 }
100         }
101
102
103    /* ---------------------------------------------------------------------- */
104
105         fieldmapper.aou.prototype.fetchOrgSettingDefault = function (name) {
106                 return this.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING, [this.id(), name] ); 
107         };
108
109         fieldmapper.aou.prototype.fetchOrgSettingBatch = function (nameList) {
110                 return this.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING_BATCH, [this.id(), nameList] ); 
111         };
112
113         fieldmapper.aou.fetchOrgSettingDefault = function (orgId, name) {
114                 return fieldmapper.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING, [orgId, name] ); 
115         };
116
117         fieldmapper.aou.fetchOrgSettingBatch = function (orgId, nameList) {
118                 return fieldmapper.standardRequest( fieldmapper.OpenSRF.methods.FETCH_ORG_SETTING_BATCH, [orgId, nameList] ); 
119         };
120
121         fieldmapper.aout.findOrgType = function (id) {
122                 fieldmapper.aout.LoadOrgTypes();
123                 return fieldmapper.aout.OrgTypeCache[id].type;
124         };
125
126         fieldmapper.aou.prototype.findOrgDepth = function (id) {
127                 if (!id) id = this.id;
128                 if (!id) return null;
129
130                 var org = fieldmapper.aou.findOrgUnit(id);
131                 return fieldmapper.aout.findOrgType(
132                         fieldmapper.aou.findOrgUnit(id).ou_type()
133                 ).depth();
134         };
135         fieldmapper.aou.findOrgDepth = fieldmapper.aou.prototype.findOrgDepth;
136
137         fieldmapper.aout.findOrgTypeFromDepth = function (depth) {
138                 if( depth === null ) return null;
139                 fieldmapper.aout.LoadOrgTypes();
140                 for( var i in fieldmapper.aout.OrgTypeCache ) {
141                         var t = fieldmapper.aout.OrgTypeCache[i].type;
142                         if( t.depth() == depth ) return t;
143                 }
144                 return null;
145         };
146
147         fieldmapper.aou.findOrgUnitSN = function (sn, slim_ok) {
148                 if (slim_ok === null) slim_ok = fieldmapper.aou.slim_ok;
149                 var org = fieldmapper.aou.OrgCacheSN[sn];
150                 if (!org) {
151                         for (var i in fieldmapper.aou.OrgCache) {
152                                 var o = fieldmapper.aou.OrgCache[i];
153                                 if (o.org.shortname() == sn) {
154                                         fieldmapper.aou.OrgCacheSN[o.org.shortname()] = o;
155                                         org = o;
156                                 }
157                         }
158
159                         if (!slim_ok && !fieldmapper.aou.OrgCache[org.id()].loaded) {
160                                 org = fieldmapper.standardRequest(fieldmapper.OpenSRF.methods.FETCH_ORG_BY_SHORTNAME, sn);
161
162                                 org.children = fieldmapper.aou.OrgCache[org.id()].children;
163                                 fieldmapper.aou.OrgCache[org.id()] = { loaded : true, org : org };
164                                 fieldmapper.aou.OrgCacheSN[org.shortname()] = { loaded : true, org : org };
165                         }
166
167                 }
168
169                 return org;
170         };
171
172         fieldmapper.aou.prototype.orgNodeTrail = function(node, asId) {
173                 if (!node) node = this;
174                 if (!node) return [];
175
176                 var na = [];
177
178                 while( node ) {
179                         na.push(node);
180                         if (node.parent_ou())
181                                 node = fieldmapper.aou.findOrgUnit(node.parent_ou());
182                         else
183                                 node = null;
184                 }
185
186                 na.reverse();
187                 if (asId) return na.map(function(o) { return o.id(); });
188                 else return na;
189         };
190         fieldmapper.aou.orgNodeTrail = fieldmapper.aou.prototype.orgNodeTrail;
191
192         fieldmapper.aou.prototype.orgIsMine = function (me, org) {
193                 if (this._isfieldmapper) {
194                         org = me;
195                         me = this;
196                 }
197
198                 if(!me || !org) return false;
199
200                 if(me.id() == org.id()) return true;
201
202                 for( var i in me.children() ) {
203                         if(me.children()[i].orgIsMine(org)) return true;
204                 }
205                 return false;
206         };
207
208     /** Given an org id, returns an array of org units including
209      * the org for the ID provided and all descendant orgs */
210     fieldmapper.aou.descendantNodeList = function(orgId, asId) {
211         var list = [];
212         function addNode(node) {
213             if(!node) return;
214             if(asId) {
215                 list.push(node.id());
216             } else {
217                 list.push(node);
218             }
219             var children = node.children();
220             if(children) {
221                 for(var i = 0; i < children.length; i++) 
222                     addNode(children[i]);
223             }
224         }
225         addNode(fieldmapper.aou.findOrgUnit(orgId));
226         return list;
227     };
228
229     fieldmapper.aou.fullPath = function(orgId, asId) {
230
231         var orgList = fieldmapper.aou.orgNodeTrail(fieldmapper.aou.findOrgUnit(orgId));
232         orgList.pop(); // prevent dupes
233         orgList = orgList.concat(fieldmapper.aou.descendantNodeList(orgId));
234
235         if(!asId) return orgList;
236         return orgList.map( function(node) { return node.id(); } );
237     };
238
239         dojo.addOnUnload( function () {
240                 for (var i in fieldmapper.aou.OrgCache) {
241                         x=fieldmapper.aou.OrgCache[i].treePtr;
242                         if (!x) continue;
243
244                         x.children(null);
245                         x.parent_ou(null);
246                         fieldmapper.aou.OrgCache[i]=null;
247                 }
248                 fieldmapper.aou.globalOrgTree = null;
249                 fieldmapper.aou.OrgCache = null;
250                 fieldmapper.aou.OrgCacheSN = null;
251                 fieldmapper.aout.OrgTypeCache = null;
252         });
253 }
254
255
256