]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/env.js
LP2061136 - Stamping 1405 DB upgrade script
[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','$injector','egAuth','egPCRUD','egIDL',
50 function($q,  $window , $injector , egAuth,  egPCRUD,  egIDL) { 
51
52     var service = {
53         // collection of custom loader functions
54         loaders : [],
55
56         // Add class hints to this list when offline does not need them and
57         // if they cause "Maximum call stack size exceeded" console errors.
58         // If offline does need a list that causes problems, a custom loader
59         // will be necessary.
60         // We'll start with authority-related classes causing problems in the
61         // staff catalog.
62         ignoreOffline : ['at','acs','abaafm','aba','acsbf','acsaf']
63     };
64
65
66     // <base href="<basePath>"/> from the current index page
67     // Currently defaults to /eg/staff for all pages.
68     // Use $location.path() to jump around within an app.
69     // Use egEnv.basePath to create URLs to new apps.
70     // NOTE: the dynamic version below derived from the DOM does not
71     // work w/ unit tests.  Use hard-coded value instead for now.
72     service.basePath = '/eg/staff/';
73         //$window.document.getElementsByTagName('base')[0].getAttribute('href');
74
75     /* returns a promise, loads all of the specified classes */
76     service.load = function() {
77         // always assume the user is logged in
78         if (!egAuth.user()) return $q.when();
79
80         var allPromises = [];
81         var classes = this.loadClasses;
82         console.debug('egEnv loading classes => ' + classes);
83
84         angular.forEach(classes, function(cls) {
85             allPromises.push(service.classLoaders[cls]());
86         });
87         angular.forEach(this.loaders, function(loader) {
88             allPromises.push(loader());
89         });
90
91         return $q.all(allPromises).then(
92             function() { console.debug('egEnv load complete') });
93     };
94
95     /** given a tree-shaped collection, captures the tree and
96      *  flattens the tree for absorption.
97      */
98     service.absorbTree = function(tree, class_, noOffline) {
99         var list = [];
100         function squash(node) {
101             list.push(node);
102             angular.forEach(node.children(), squash);
103         }
104         squash(tree);
105         var blob = service.absorbList(list, class_, noOffline);
106         blob.tree = tree;
107     };
108
109     var egLovefield; // we'll inject it manually
110
111     /** caches the object list both as the list and an id => object map */
112     service.absorbList = function(list, class_, noOffline) {
113         var blob;
114         var pkey = egIDL.classes[class_].pkey;
115
116         if (service[class_]) {
117             // appending data to an existing class.  Useful for receiving 
118             // class elements as-needed.  Avoid adding items which are 
119             // already tracked in the list.
120             blob = service[class_];
121             angular.forEach(list, function(item) {
122                 if (!service[class_].map[item[pkey]()]) 
123                     blob.list.push(item);
124             });
125         } else {
126             blob = {list : list, map : {}};
127         }
128
129         if (!noOffline && service.ignoreOffline.indexOf(class_) < 0) {
130             if (!egLovefield) {
131                 egLovefield = $injector.get('egLovefield');
132             }
133             //console.debug('About to cache a list of ' + class_ + ' objects...');
134             egLovefield.isCacheGood(class_).then(
135                 function(good) {
136                     if (!good) {
137                         egLovefield.setListInOfflineCache(class_, blob.list); 
138                     }
139                 },
140                 function() {} // Not Supported
141             );
142         }
143
144         angular.forEach(list, function(item) {blob.map[item[pkey]()] = item});
145         service[class_] = blob;
146         service[class_].loaded = true;
147         return blob;
148     };
149
150     /* 
151      * list of classes to load on every page, regardless of whether
152      * a page-specific list is provided.
153      */
154     service.loadClasses = ['aou'];
155
156     // sort orgs at each level by shortname
157     service.sort_aou = function(node) {
158         node.children(node.children().sort(function(a, b) {
159             return a.shortname() < b.shortname() ? -1 : 1;
160         }));
161         angular.forEach(node.children(), service.sort_aou);
162     }
163
164     /*
165      * Default class loaders.  Only add classes directly to this file
166      * that are loaded practically always.  All other app-specific
167      * classes should be registerd from within the app.
168      */
169     service.classLoaders = {
170         aou : function() {
171
172             if (!egLovefield) {
173                 egLovefield = $injector.get('egLovefield');
174             }
175
176             return egLovefield.reconstituteTree('aou').then(function(offline) {
177                 if (offline) return $q.when();
178                 if (service.aou && service.aou.loaded) return $q.when();
179     
180                 // EXPERIMENT: cache the org tree in session storage.
181                 // This means that if the org tree changes, users will have to
182                 // open the client in a new browser tab to clear the cached tree.
183                 var treeJSON = $window.sessionStorage.getItem('eg.env.aou.tree');
184                 if (treeJSON) {
185                     console.debug('serving org tree from cache');
186                     var tree = JSON2js(treeJSON);
187                     service.absorbTree(tree, 'aou')
188                     return $q.when(tree);
189                 }
190     
191                 return egPCRUD.search('aou', {parent_ou : null}, 
192                     {flesh : -1, flesh_fields : {aou : ['children', 'ou_type']}}
193                 ).then(
194                     function(tree) {
195                         service.sort_aou(tree);
196                         $window.sessionStorage.setItem(
197                             'eg.env.aou.tree', js2JSON(tree));
198                         service.absorbTree(tree, 'aou');
199                         return $q.when();
200                     }
201                 );
202             },
203             function() {return $q.when()} // Not Supported, exit gracefully
204             );
205         },
206     };
207
208     return service;
209 }]);
210
211
212