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 // tests that a node can have users
45 service.CanHaveUsers = function(node_or_id) {
49 .can_have_users() == 't';
52 // tests that a node can have volumes
53 service.CanHaveVolumes = function(node_or_id) {
57 .can_have_vols() == 't';
60 // list of org_unit objects or IDs for me + descendants
61 service.descendants = function(node_or_id, as_id) {
62 var node = service.get(node_or_id);
67 angular.forEach(n.children(), descend);
71 return nodes.map(function(n){return n.id()});
75 // list of org_unit objects or IDs for ancestors + me + descendants
76 service.fullPath = function(node_or_id, as_id) {
77 var list = service.ancestors(node_or_id).concat(
78 service.descendants(node_or_id).slice(1));
80 return list.map(function(n){return n.id()});
84 // returns a promise, resolved with a hash of setting name =>
85 // setting value for the selected org unit. Org unit defaults to
86 // auth workstation org unit.
87 service.settings = function(names, ou_id) {
88 var deferred = $q.defer();
89 ou_id = ou_id || egAuth.user().ws_ou();
90 var here = (ou_id == egAuth.user().ws_ou());
93 if (!angular.isArray(names)) names = [names];
96 // only cache org settings retrieved for the current
97 // workstation org unit.
99 angular.forEach(names, function(name) {
100 if (!angular.isDefined(service.cachedSettings[name]))
104 // only retrieve uncached values
106 if (names.length == 0)
107 return $q.when(service.cachedSettings);
112 'open-ils.actor.ou_setting.ancestor_default.batch',
113 ou_id, names, egAuth.token()
114 ).then(function(blob) {
116 angular.forEach(blob, function(val, key) {
117 // val is either null or a structure containing the value
118 settings[key] = val ? val.value : null;
119 if (here) service.cachedSettings[key] = settings[key];
122 // resolve with cached settings if 'here', since 'settings'
123 // will only contain settings we had to retrieve
124 deferred.resolve(here ? service.cachedSettings : settings);
126 return deferred.promise;