]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/holds/app.js
LP#1712854: Speed improvements for two hold interfaces
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / circ / holds / app.js
1 angular.module('egHoldsApp', 
2     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
3
4 .config(function($routeProvider, $locationProvider, $compileProvider) {
5     $locationProvider.html5Mode(true);
6     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
7
8     var resolver = {delay : 
9         ['egStartup', function(egStartup) {return egStartup.go()}]}
10
11     $routeProvider.when('/circ/holds/shelf', {
12         templateUrl: './circ/holds/t_shelf',
13         controller: 'HoldsShelfCtrl',
14         resolve : resolver
15     });
16
17     $routeProvider.when('/circ/holds/shelf/:hold_id', {
18         templateUrl: './circ/holds/t_shelf',
19         controller: 'HoldsShelfCtrl',
20         resolve : resolver
21     });
22
23     $routeProvider.when('/circ/holds/pull', {
24         templateUrl: './circ/holds/t_pull',
25         controller: 'HoldsPullListCtrl',
26         resolve : resolver
27     });
28
29     $routeProvider.when('/circ/holds/pull/:hold_id', {
30         templateUrl: './circ/holds/t_pull',
31         controller: 'HoldsPullListCtrl',
32         resolve : resolver
33     });
34
35     $routeProvider.otherwise({redirectTo : '/circ/holds/shelf'});
36 })
37
38
39 .controller('HoldsShelfCtrl',
40        ['$scope','$q','$routeParams','$window','$location','egCore','egHolds','egHoldGridActions','egCirc','egGridDataProvider','egProgressDialog',
41 function($scope , $q , $routeParams , $window , $location , egCore , egHolds , egHoldGridActions , egCirc , egGridDataProvider , egProgressDialog)  {
42     $scope.detail_hold_id = $routeParams.hold_id;
43
44     var hold_ids = [];
45     var holds = [];
46     var clear_mode = false;
47     $scope.gridControls = {};
48     $scope.grid_actions = egHoldGridActions;
49
50     var provider = egGridDataProvider.instance({});
51     $scope.gridDataProvider = provider;
52
53     function refresh_page() {
54         hold_count = 0;
55         holds = [];
56         hold_ids = [];
57         provider.refresh();
58     }
59     // called after any egHoldGridActions action occurs
60     $scope.grid_actions.refresh = refresh_page;
61
62     provider.get = function(offset, count) {
63
64         // see if we have the requested range cached
65         if (holds[offset]) {
66             return provider.arrayNotifier(holds, offset, count);
67         }
68
69         // if in clear mode...
70         if (clear_mode && holds.length) {
71             holds = holds.filter(function(h) { return h.hold.clear_me == 't' });
72         } 
73
74         hold_count = 0;
75         holds = [];
76         var restrictions = {
77                 is_staff_request  : 'true',
78                 capture_time      : { not : null },
79                 cs_id             : 8, // on holds shelf
80                 fulfillment_time  : null,
81                 current_shelf_lib : $scope.pickup_ou.id()
82         };
83
84         var order_by = [{ capture_time : null }];
85         if (provider.sort && provider.sort.length) {
86             order_by = [];
87             angular.forEach(provider.sort, function (c) {
88                 if (!angular.isObject(c)) {
89                     if (c.match(/^hold\./)) {
90                         var i = c.replace('hold.','');
91                         var ob = {};
92                         ob[i] = null;
93                         order_by.push(ob);
94                     }
95                 } else {
96                     var i = Object.keys(c)[0];
97                     var direction = c[i];
98                     if (i.match(/^hold\./)) {
99                         i = i.replace('hold.','');
100                         var ob = {}
101                         ob[i] = {dir:direction};
102                         order_by.push(ob);
103                     }
104                 }
105             });
106         }
107
108         egProgressDialog.open({max : 1, value : 0});
109         var first = true;
110         return egHolds.fetch_wide_holds(
111             restrictions,
112             order_by
113         ).then(function () {
114                 return provider.arrayNotifier(holds, offset, count);
115             },
116             null,
117             function(hold_data) { 
118                 if (first) {
119                     hold_count = hold_data;
120                     first = false;
121                     egProgressDialog.update({max:hold_count});
122                 } else {
123                     egProgressDialog.increment();
124                     var new_item = { id : hold_data.id, hold : hold_data };
125                     new_item.status_string =
126                         egCore.strings['HOLD_STATUS_' + hold_data.hold_status]
127                         || hold_data.hold_status;
128
129                     if (clear_mode) {
130                         if (hold_data.clear_me == 't') holds.push(new_item);
131                     } else {
132                         holds.push(new_item);
133                     }
134                 }
135             }
136         ).finally(egProgressDialog.close);
137     }
138
139     // re-draw the grid when user changes the org selector
140     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
141     $scope.$watch('pickup_ou', function(newVal, oldVal) {
142         if (newVal && newVal != oldVal) 
143             refresh_page();
144     });
145
146     $scope.detail_view = function(action, user_data, items) {
147         if (h = items[0]) {
148             $location.path('/circ/holds/shelf/' + h.hold.id);
149         }
150     }
151
152     $scope.list_view = function(items) {
153         $location.path('/circ/holds/shelf');
154     }
155
156     // when the detail hold is fetched (and updated), update the bib
157     // record summary display record id.
158     $scope.set_hold = function(hold_data) {
159         $scope.detail_hold_record_id = hold_data.hold.record_id;
160     }
161
162     // manage active vs. clearable holds display
163     var clearing = false; // true if actively clearing holds (below)
164     $scope.is_clearing = function() { return clearing };
165     $scope.active_mode = function() {return !clear_mode}
166     $scope.clear_mode = function() {return clear_mode}
167     $scope.show_clearable = function() { clear_mode = true; refresh_page() }
168     $scope.show_active = function() { clear_mode = false; refresh_page() }
169     $scope.disable_clear = function() { return clearing || !clear_mode }
170
171     // udpate the in-grid hold with the clear-shelf cached response info.
172     function handle_clear_cache_resp(resp) {
173         if (!angular.isArray(resp)) resp = [resp];
174         angular.forEach(resp, function(info) {
175             if (info.action) {
176                 var grid_item = holds.filter(function(item) {
177                     return item.hold.id == info.hold_details.id
178                 })[0];
179
180                 // there will be no grid item if the hold is off-page
181                 if (grid_item) {
182                     grid_item.post_clear = 
183                         egCore.strings['CLEAR_SHELF_ACTION_' + info.action];
184                 }
185             }
186         });
187     }
188
189     $scope.clear_holds = function() {
190         clearing = true;
191         $scope.clear_progress = {max : 0, value : 0};
192
193         // we want to see all processed holds, so (effectively) remove
194         // the grid limit.
195         $scope.gridControls.setLimit(1000, true); 
196
197         // initiate clear shelf and grab cache key
198         egCore.net.request(
199             'open-ils.circ',
200             'open-ils.circ.hold.clear_shelf.process',
201             egCore.auth.token(), $scope.pickup_ou.id(),
202             null, 1
203
204         // request responses from the clear shelf cache
205         ).then(
206             
207             // clear shelf done; fetch the cached results.
208             function(resp) {
209                 clearing = false;
210                 egCore.net.request(
211                     'open-ils.circ',
212                     'open-ils.circ.hold.clear_shelf.get_cache',
213                     egCore.auth.token(), resp.cache_key, 1
214                 ).then(null, null, handle_clear_cache_resp);
215             }, 
216
217             null,
218
219             // handle streamed clear_shelf progress updates
220             function(resp) {
221                 if (resp.maximum) 
222                     $scope.clear_progress.max = resp.maximum;
223                 if (resp.progress)
224                     $scope.clear_progress.value = resp.progress;
225             }
226
227         );
228     }
229
230     function map_prefix_to_subhash (h,pf) {
231         var newhash = {};
232         angular.forEach(Object.keys(h), function (k) {
233             if (k.startsWith(pf)) {
234                 var nk = k.substr(pf.length);
235                 newhash[nk] = h[k];
236             }
237         });
238         return newhash;
239     }
240
241     $scope.print_shelf_list = function() {
242         var print_holds = [];
243         angular.forEach(holds, function(hold_data) {
244             var phold = {};
245             print_holds.push(phold);
246
247             phold.status_string = hold_data.status_string;
248
249             phold.patron_first = hold_data.hold.usr_first_given_name;
250             phold.patron_last = hold_data.hold.usr_family_name;
251             phold.patron_alias = hold_data.hold.usr_alias;
252             phold.patron_barcode = hold_data.hold.ucard_barcode;
253
254             phold.title = hold_data.hold.title;
255             phold.author = hold_data.hold.author;
256
257             phold.hold = hold_data.hold;
258             phold.copy = map_prefix_to_subhash(hold_data.hold, 'cp_');
259             phold.volume = map_prefix_to_subhash(hold_data.hold, 'cn_');
260             phold.part = map_prefix_to_subhash(hold_data.hold, 'p_');
261         });
262
263         console.log(print_holds);
264
265         return egCore.print.print({
266             context : 'default', 
267             template : 'hold_shelf_list', 
268             scope : {holds : print_holds}
269         });
270     }
271
272     refresh_page();
273
274 }])
275
276 .controller('HoldsPullListCtrl',
277        ['$scope','$q','$routeParams','$window','$location','egCore',
278         'egHolds','egCirc','egHoldGridActions','egProgressDialog',
279 function($scope , $q , $routeParams , $window , $location , egCore , 
280          egHolds , egCirc , egHoldGridActions , egProgressDialog) {
281
282     $scope.detail_hold_id = $routeParams.hold_id;
283
284     var cached_details = {};
285     var details_needed = {};
286
287     $scope.gridControls = {
288         setQuery : function() {
289             return {'copy_circ_lib_id' : egCore.auth.user().ws_ou()}
290         },
291         setSort : function() {
292             return ['copy_location_order_position','call_number_sort_key']
293         },
294         collectStarted : function(offset) {
295             // Launch an indeterminate -> semi-determinate progress
296             // modal.  Using a determinate modal that starts counting
297             // on the post-grid holds data retrieval results in a modal
298             // that's stuck at 0% for most of its life, which is aggravating.
299             egProgressDialog.open();
300         },
301         itemRetrieved : function(item) {
302             egProgressDialog.increment();
303             if (!cached_details[item.id]) {
304                 details_needed[item.id] = item;
305             }
306         },
307         allItemsRetrieved : function() {
308             flesh_holds().finally(egProgressDialog.close);
309         }
310     }
311
312
313     // Fetches hold detail data for each hold in the grid and links
314     // the detail data to the related grid item so egHoldGridActions 
315     // and friends have access to holds data they understand.
316     // Only fetch not-yet-cached data.
317     function flesh_holds() {
318         egProgressDialog.increment();
319
320         // Start by fleshing hold details from our cached data.
321         var items = $scope.gridControls.allItems();
322         angular.forEach(items, function(item) {
323             if (!cached_details[item.id]) return $q.when();
324             angular.forEach(cached_details[item.id], 
325                 function(val, key) { item[key] = val })
326         });
327
328         // Exit if all needed details were already cached
329         if (Object.keys(details_needed).length == 0) return $q.when();
330
331         return egCore.net.request(
332             'open-ils.circ',
333             'open-ils.circ.hold.details.batch.retrieve.authoritative',
334             egCore.auth.token(), Object.keys(details_needed), {
335                 include_usr : true
336             }
337
338         ).then(null, null, function(hold_info) {
339             egProgressDialog.increment();
340
341             // check if this is a staff-created hold
342             // i.e., requestor is not the same as the user
343             hold_info['_is_staff_hold'] = hold_info.hold.requestor() != hold_info.hold.usr().id();
344
345             var hold_id = hold_info.hold.id();
346             cached_details[hold_id] = hold_info;
347             var item = details_needed[hold_id];
348             delete details_needed[hold_id];
349
350             // flesh the grid item from the blob of hold data.
351             angular.forEach(hold_info, 
352                 function(val, key) { item[key] = val });
353
354         });
355     }
356
357     $scope.grid_actions = egHoldGridActions;
358     $scope.grid_actions.refresh = function() {
359         cached_details = {}; // un-cache details after edit actions.
360         $scope.gridControls.refresh();
361     }
362
363     $scope.detail_view = function(action, user_data, items) {
364         if (h = items[0]) {
365             $location.path('/circ/holds/pull/' + h.hold.id());
366         }
367     }
368
369     $scope.list_view = function(items) {
370         $location.path('/circ/holds/pull');
371     }
372
373     // when the detail hold is fetched (and updated), update the bib
374     // record summary display record id.
375     $scope.set_hold = function(hold_data) {
376         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
377     }
378
379     // By default, this action is hidded from the UI, but leaving it
380     // here in case it's needed in the future
381     $scope.print_list_alt = function() {
382         var url = '/opac/extras/circ/alt_holds_print.html';
383         var win = $window.open(url, '_blank');
384         win.ses = function() {return egCore.auth.token()};
385         win.open();
386         win.focus();
387     }
388
389     $scope.print_full_list = function() {
390         var print_holds = [];
391         egProgressDialog.open({value : 0});
392
393         // collect the full list of holds
394         egCore.net.request(
395             'open-ils.circ',
396             'open-ils.circ.hold_pull_list.fleshed.stream',
397             egCore.auth.token(), 10000, 0
398         ).then(
399             function() {
400                 console.debug('printing ' + print_holds.length + ' holds');
401
402                 // holds fetched, send to print
403                 egCore.print.print({
404                     context : 'default', 
405                     template : 'hold_pull_list', 
406                     scope : {holds : print_holds}
407                 });
408             },
409             null, 
410             function(hold_data) {
411                 egProgressDialog.increment();
412                 egHolds.local_flesh(hold_data);
413                 print_holds.push(hold_data);
414                 hold_data.title = hold_data.mvr.title();
415                 hold_data.author = hold_data.mvr.author();
416                 hold_data.hold = egCore.idl.toHash(hold_data.hold);
417                 hold_data.copy = egCore.idl.toHash(hold_data.copy);
418                 hold_data.volume = egCore.idl.toHash(hold_data.volume);
419                 hold_data.part = egCore.idl.toHash(hold_data.part);
420             }
421         ).finally(egProgressDialog.close);
422     }
423
424 }])
425