6 angular.module('egCoreMod')
9 ['$q','egEnv','egAuth','egNet',
10 function($q, egEnv, egAuth, egNet) {
14 // org unit settings cache.
15 // This allows the caller to avoid local caches
16 service.cachedSettings = {};
18 service.get = function(node_or_id) {
19 if (typeof node_or_id == 'object')
21 return egEnv.aou.map[node_or_id];
24 service.list = function() {
25 return egEnv.aou.list;
28 service.tree = function() {
29 return egEnv.aou.tree;
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);
37 while( (node = service.get(node.parent_ou())))
40 return nodes.map(function(n){return n.id()});
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);
51 angular.forEach(n.children(), descend);
55 return nodes.map(function(n){return n.id()});
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));
64 return list.map(function(n){return n.id()});
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());
77 if (!angular.isArray(names)) names = [names];
80 // only cache org settings retrieved for the current
81 // workstation org unit.
83 angular.forEach(names, function(name) {
84 if (!angular.isDefined(service.cachedSettings[name]))
88 // only retrieve uncached values
90 if (names.length == 0)
91 return $q.when(service.cachedSettings);
96 'open-ils.actor.ou_setting.ancestor_default.batch',
97 ou_id, names, egAuth.token()
98 ).then(function(blob) {
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];
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);
110 return deferred.promise;