]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/env.js
LP#1526185 Disable second toast on permfail
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / env.js
1 /**
2  * Core Service - egEnv
3  *
4  * Manages startup data loading and data caching.  
5  * All registered loaders run * simultaneously.  When all promises 
6  * are resolved, the promise * returned by egEnv.load() is resolved.
7  *
8  * There are two main uses cases for egEnv:
9  *
10  * 1. When loading a variety of objects on page load, having them
11  * loaded with egEnv ensures that the load will happen in parallel
12  * and that it will complete before egStartup completes, which is 
13  * generally before page controllers run.
14  *
15  * 2. When loading generic IDL data across different services,
16  * having them all stash the data in egEnv means they each have
17  * an agreed-upon cache mechanism.
18  *
19  * It's also a good place to stash other environmental tidbits...
20  *
21  * Generic and class-based loaders are supported.  
22  *
23  * To load a registred class, push the class hint onto 
24  * egEnv.loadClasses.  
25  *
26  * // will cause all 'pgt' objects to be fetched
27  * egEnv.loadClasses.push('pgt');
28  *
29  * To register a new class loader,attach a loader function to 
30  * egEnv.classLoaders, keyed on the class hint, which returns a promise.
31  *
32  * egEnv.classLoaders.ccs = function() { 
33  *    // loads copy status objects, returns promise
34  * };
35  *
36  * Generic loaders go onto the egEnv.loaders array.  Each should
37  * return a promise.
38  *
39  * egEnv.loaders.push(function() {
40  *    return egNet.request(...)
41  *    .then(function(stuff) { console.log('stuff!') 
42  * });
43  */
44
45 angular.module('egCoreMod')
46
47 // env fetcher
48 .factory('egEnv', 
49        ['$q','$window','egAuth','egPCRUD','egIDL',
50 function($q,  $window , egAuth,  egPCRUD,  egIDL) { 
51
52     var service = {
53         // collection of custom loader functions
54         loaders : []
55     };
56
57
58     // <base href="<basePath>"/> from the current index page
59     // Currently defaults to /eg/staff for all pages.
60     // Use $location.path() to jump around within an app.
61     // Use egEnv.basePath to create URLs to new apps.
62     // NOTE: the dynamic version below derived from the DOM does not
63     // work w/ unit tests.  Use hard-coded value instead for now.
64     service.basePath = '/eg/staff/';
65         //$window.document.getElementsByTagName('base')[0].getAttribute('href');
66
67     /* returns a promise, loads all of the specified classes */
68     service.load = function() {
69         // always assume the user is logged in
70         if (!egAuth.user()) return $q.when();
71
72         var allPromises = [];
73         var classes = this.loadClasses;
74         console.debug('egEnv loading classes => ' + classes);
75
76         angular.forEach(classes, function(cls) {
77             allPromises.push(service.classLoaders[cls]());
78         });
79         angular.forEach(this.loaders, function(loader) {
80             allPromises.push(loader());
81         });
82
83         return $q.all(allPromises).then(
84             function() { console.debug('egEnv load complete') });
85     };
86
87     /** given a tree-shaped collection, captures the tree and
88      *  flattens the tree for absorption.
89      */
90     service.absorbTree = function(tree, class_) {
91         var list = [];
92         function squash(node) {
93             list.push(node);
94             angular.forEach(node.children(), squash);
95         }
96         squash(tree);
97         var blob = service.absorbList(list, class_);
98         blob.tree = tree;
99     };
100
101     /** caches the object list both as the list and an id => object map */
102     service.absorbList = function(list, class_) {
103         var blob;
104         var pkey = egIDL.classes[class_].pkey;
105
106         if (service[class_]) {
107             // appending data to an existing class.  Useful for receiving 
108             // class elements as-needed.  Avoid adding items which are 
109             // already tracked in the list.
110             blob = service[class_];
111             angular.forEach(list, function(item) {
112                 if (!service[class_].map[item[pkey]()]) 
113                     blob.list.push(item);
114             });
115         } else {
116             blob = {list : list, map : {}};
117         }
118
119         angular.forEach(list, function(item) {blob.map[item[pkey]()] = item});
120         service[class_] = blob;
121         return blob;
122     };
123
124     /* 
125      * list of classes to load on every page, regardless of whether
126      * a page-specific list is provided.
127      */
128     service.loadClasses = ['aou'];
129
130     /*
131      * Default class loaders.  Only add classes directly to this file
132      * that are loaded practically always.  All other app-specific
133      * classes should be registerd from within the app.
134      */
135     service.classLoaders = {
136         aou : function() {
137
138             // EXPERIMENT: cache the org tree in session storage.
139             // This means that if the org tree changes, users will have to
140             // open the client in a new browser tab to clear the cached tree.
141             var treeJSON = $window.sessionStorage.getItem('eg.env.aou.tree');
142             if (treeJSON) {
143                 console.debug('serving org tree from cache');
144                 var tree = JSON2js(treeJSON);
145                 service.absorbTree(tree, 'aou')
146                 return $q.when(tree);
147             }
148
149             // sort orgs at each level by shortname
150             function sort_aou(node) {
151                 node.children(node.children().sort(function(a, b) {
152                     return a.shortname() < b.shortname() ? -1 : 1;
153                 }));
154                 angular.forEach(node.children(), sort_aou);
155             }
156
157             return egPCRUD.search('aou', {parent_ou : null}, 
158                 {flesh : -1, flesh_fields : {aou : ['children', 'ou_type']}}
159             ).then(
160                 function(tree) {
161                     sort_aou(tree);
162                     $window.sessionStorage.setItem(
163                         'eg.env.aou.tree', js2JSON(tree));
164                     service.absorbTree(tree, 'aou')
165                 }
166             );
167         },
168     };
169
170     return service;
171 }]);
172
173
174