]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/org.js
LP#1350042 Browser client templates/scripts (phase 1)
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / org.js
1 /**
2  * Core Service - egOrg
3  *
4  * TODO: more docs
5  */
6 angular.module('egCoreMod')
7
8 .factory('egOrg', 
9        ['$q','egEnv','egAuth','egNet',
10 function($q,  egEnv,  egAuth,  egNet) { 
11
12     var service = {};
13
14     // org unit settings cache.
15     // This allows the caller to avoid local caches
16     service.cachedSettings = {};
17
18     service.get = function(node_or_id) {
19         if (typeof node_or_id == 'object')
20             return node_or_id;
21         return egEnv.aou.map[node_or_id];
22     };
23
24     service.list = function() {
25         return egEnv.aou.list;
26     };
27
28     service.tree = function() {
29         return egEnv.aou.tree;
30     }
31
32     // list of org_unit objects or IDs for ancestors + me
33     service.ancestors = function(node_or_id, as_id) {
34         var node = service.get(node_or_id);
35         if (!node) return [];
36         var nodes = [node];
37         while( (node = service.get(node.parent_ou())))
38             nodes.push(node);
39         if (as_id) 
40             return nodes.map(function(n){return n.id()});
41         return nodes;
42     };
43
44     // list of org_unit objects  or IDs for me + descendants
45     service.descendants = function(node_or_id, as_id) {
46         var node = service.get(node_or_id);
47         if (!node) return [];
48         var nodes = [];
49         function descend(n) {
50             nodes.push(n);
51             angular.forEach(n.children(), descend);
52         }
53         descend(node);
54         if (as_id) 
55             return nodes.map(function(n){return n.id()});
56         return nodes;
57     }
58
59     // list of org_unit objects or IDs for ancestors + me + descendants
60     service.fullPath = function(node_or_id, as_id) {
61         var list = service.ancestors(node_or_id).concat(
62           service.descendants(node_or_id).slice(1));
63         if (as_id) 
64             return list.map(function(n){return n.id()});
65         return list;
66     }
67
68     // returns a promise, resolved with a hash of setting name =>
69     // setting value for the selected org unit.  Org unit defaults to 
70     // auth workstation org unit.
71     service.settings = function(names, ou_id) {
72         var deferred = $q.defer();
73         ou_id = ou_id || egAuth.user().ws_ou();
74         var here = (ou_id == egAuth.user().ws_ou());
75
76         // allow non-array
77         if (!angular.isArray(names)) names = [names];
78         
79         if (here) { 
80             // only cache org settings retrieved for the current 
81             // workstation org unit.
82             var newNames = [];
83             angular.forEach(names, function(name) {
84                 if (!angular.isDefined(service.cachedSettings[name]))
85                     newNames.push(name)
86             });
87
88             // only retrieve uncached values
89             names = newNames;
90             if (names.length == 0)
91                 return $q.when(service.cachedSettings);
92         }
93
94         egNet.request(
95             'open-ils.actor',
96             'open-ils.actor.ou_setting.ancestor_default.batch',
97             ou_id, names, egAuth.token()
98         ).then(function(blob) {
99             var settings = {};
100             angular.forEach(blob, function(val, key) {
101                 // val is either null or a structure containing the value
102                 settings[key] = val ? val.value : null;
103                 if (here) service.cachedSettings[key] = settings[key];
104             });
105
106             // resolve with cached settings if 'here', since 'settings'
107             // will only contain settings we had to retrieve
108             deferred.resolve(here ? service.cachedSettings : settings);
109         });
110         return deferred.promise;
111     }
112
113     return service;
114 }]);
115