]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
LP1615805 No inputs after submit in patron search (AngularJS)
[working/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 = 0; type < globalOrgTypes.length; type++ ) {
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 = 0; type < globalOrgTypes.length; type++) {
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 = 0; i < _lasso.length; i++) {
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, or optionally, a child of an ancestor org within the specified depth */
96 function orgIsMine(me, org, depth) {
97         if(!me || !org) {
98                 return false;
99         }
100         if(me.id() == org.id()) {
101                 return true;
102         }
103         if (depth !== undefined) {
104                 while (depth < findOrgDepth(me)) {
105                         me = findOrgUnit( me.parent_ou() );
106                 }
107                 if(me.id() == org.id()) {
108                         return true;
109                 }
110         }
111         var kids = me.children();
112         for( var i = 0; kids && i < kids.length; i++ ) {
113                 if(orgIsMine(kids[i], org /* intentional lack of 3rd arg */)) {
114                         return true;
115                 }
116
117         }
118         return false;
119 }
120
121 function orgIsMineFromSet(meList, org) {
122     org = findOrgUnit(org);
123     for(var i = 0; i < meList.length; i++) {
124         if(orgIsMine(findOrgUnit(meList[i]), org))
125             return true;
126     }
127     return false;
128 }
129
130 var orgArraySearcher = {};
131 var orgArraySearcherOrder = [];
132 var globalOrgTree;
133 for (var i = 0; i < _l.length; i++) {
134         var x = new aou();
135         x.id(_l[i][0]);
136         x.ou_type(_l[i][1]);
137         x.parent_ou(_l[i][2]);
138         x.name(_l[i][3]);
139         x.opac_visible(_l[i][4]);
140         x.shortname(_l[i][5]);
141         orgArraySearcher[x.id()] = x;
142         orgArraySearcherOrder.push(x.id());
143 }
144 for (var i = 0; i < orgArraySearcherOrder.length; i++) {
145         var x = orgArraySearcher[orgArraySearcherOrder[i]];
146         if (x.parent_ou() == null || x.parent_ou() == '') {
147                 globalOrgTree = x;
148                 continue;
149         } 
150
151         var par = findOrgUnit(x.parent_ou());
152         if (!par.children()) par.children(new Array());
153         par.children().push(x);
154 }
155
156 function _tree_killer () {
157         for (var i in orgArraySearcher) {
158                 x=orgArraySearcher[i];
159                 x.children(null);
160                 x.parent_ou(null);
161                 orgArraySearcher[i]=null;
162         }
163         globalOrgTree = null;
164         orgArraySearcher = null;
165         globalOrgTypes = null;
166 }
167
168
169