]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/org_utils.js
trying to move towards lassos
[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         var o = (typeof org_id == 'object') ? org_id : orgArraySearcher[org_id];
48         if (!o) o = new aou();
49         return o;
50 }
51
52 function findOrgLasso(lasso_id) {
53         if (typeof lasso_id == 'object') return lasso_id;
54     for (var i in _lasso) {
55         if (_lasso[i].id() == lasso_id) return _lasso[i];
56     }
57     return null;
58 }
59
60 var orgArraySearcherSN = {};
61 function findOrgUnitSN(shortname) {
62         if (typeof shortname == 'object') return shortname;
63         if( orgArraySearcherSN[shortname] ) return orgArraySearcherSN[shortname];
64         _debug("fetching org by shortname "+shortname);
65         var req = new Request(FETCH_ORG_BY_SHORTNAME, shortname);
66         req.request.alertEvent = false;
67         req.send(true);
68         return req.result();
69 }
70
71
72 /* builds a trail from the top of the org tree to the node provide.
73         basically fleshes out 'my orgs' 
74         Returns an array of [org0, org1, ..., myorg] */
75 function orgNodeTrail(node) {
76         var na = new Array();
77         while( node ) {
78                 na.push(node);
79                 node = findOrgUnit(node.parent_ou());
80         }
81         return na.reverse();
82 }
83
84 function findSiblingOrgs(node) { return findOrgUnit(node.parent_ou()).children(); }
85
86 /* true if 'org' is 'me' or a child of mine */
87 function orgIsMine(me, org) {
88         if(!me || !org) return false;
89         if(me.id() == org.id()) return true;
90         for( var i in me.children() ) {
91                 if(orgIsMine(me.children()[i], org))
92                         return true;
93         }
94         return false;
95 }
96
97
98
99 var orgArraySearcher = {};
100 var globalOrgTree;
101 for (var i in _l) {
102         var x = new aou();
103         x.id(_l[i][0]);
104         x.ou_type(_l[i][1]);
105         x.parent_ou(_l[i][2]);
106         x.name(_l[i][3]);
107     x.opac_visible(_l[i][4]);
108         orgArraySearcher[x.id()] = x;
109 }
110 for (var i in orgArraySearcher) {
111         var x = orgArraySearcher[i];
112         if (x.parent_ou() == null || x.parent_ou() == '') {
113                 globalOrgTree = x;
114                 continue;
115         } 
116
117         var parent = findOrgUnit(x.parent_ou());
118         if (!parent.children()) parent.children(new Array());
119         parent.children().push(x);
120 }
121
122 function _tree_killer () {
123         for (var i in orgArraySearcher) {
124                 x=orgArraySearcher[i];
125                 x.children(null);
126                 x.parent_ou(null);
127                 orgArraySearcher[i]=null;
128         }
129         globalOrgTree = null;
130         orgArraySearcher = null;
131         globalOrgTypes = null;
132 }
133
134
135