]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/holds/app.js
LP#1728147 - fix some Holds Pull List grid columns
[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',
41 function($scope , $q , $routeParams , $window , $location , egCore , egHolds , egHoldGridActions , egCirc , egGridDataProvider)  {
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     function fetch_holds(offset, count) {
51         var ids = hold_ids.slice(offset, offset + count);
52         return egHolds.fetch_holds(ids).then(null, null,
53             function(hold_data) { 
54                 holds.push(hold_data);
55                 return hold_data; // to the grid
56             }
57         );
58     }
59
60     var provider = egGridDataProvider.instance({});
61     $scope.gridDataProvider = provider;
62
63     function refresh_page() {
64         holds = [];
65         hold_ids = [];
66         provider.refresh();
67     }
68     // called after any egHoldGridActions action occurs
69     $scope.grid_actions.refresh = refresh_page;
70
71     provider.get = function(offset, count) {
72
73         // see if we have the requested range cached
74         if (holds[offset]) {
75             return provider.arrayNotifier(holds, offset, count);
76         }
77
78         // see if we have the holds IDs for this range already loaded
79         if (hold_ids[offset]) {
80             return fetch_holds(offset, count);
81         }
82
83         var deferred = $q.defer();
84         hold_ids = [];
85         holds = [];
86
87         var method = 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve.authoritative.atomic';
88         if (clear_mode) 
89             method = 'open-ils.circ.captured_holds.id_list.expired_on_shelf_or_wrong_shelf.retrieve.atomic';
90
91         egCore.net.request(
92             'open-ils.circ', method,
93             egCore.auth.token(), $scope.pickup_ou.id()
94
95         ).then(function(ids) {
96             if (!ids.length) { 
97                 deferred.resolve(); 
98                 return; 
99             }
100
101             hold_ids = ids;
102             fetch_holds(offset, count)
103             .then(deferred.resolve, null, deferred.notify);
104         });
105
106         return deferred.promise;
107     }
108
109     // re-draw the grid when user changes the org selector
110     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
111     $scope.$watch('pickup_ou', function(newVal, oldVal) {
112         if (newVal && newVal != oldVal) 
113             refresh_page();
114     });
115
116     $scope.detail_view = function(action, user_data, items) {
117         if (h = items[0]) {
118             $location.path('/circ/holds/shelf/' + h.hold.id());
119         }
120     }
121
122     $scope.list_view = function(items) {
123         $location.path('/circ/holds/shelf');
124     }
125
126     // when the detail hold is fetched (and updated), update the bib
127     // record summary display record id.
128     $scope.set_hold = function(hold_data) {
129         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
130     }
131
132     // manage active vs. clearable holds display
133     var clearing = false; // true if actively clearing holds (below)
134     $scope.is_clearing = function() { return clearing };
135     $scope.active_mode = function() {return !clear_mode}
136     $scope.clear_mode = function() {return clear_mode}
137     $scope.show_clearable = function() { clear_mode = true; refresh_page() }
138     $scope.show_active = function() { clear_mode = false; refresh_page() }
139     $scope.disable_clear = function() { return clearing || !clear_mode }
140
141     // udpate the in-grid hold with the clear-shelf cached response info.
142     function handle_clear_cache_resp(resp) {
143         if (!angular.isArray(resp)) resp = [resp];
144         angular.forEach(resp, function(info) {
145             if (info.action) {
146                 var grid_item = holds.filter(function(item) {
147                     return item.hold.id() == info.hold_details.id
148                 })[0];
149
150                 // there will be no grid item if the hold is off-page
151                 if (grid_item) {
152                     grid_item.post_clear = 
153                         egCore.strings['CLEAR_SHELF_ACTION_' + info.action];
154                 }
155             }
156         });
157     }
158
159     $scope.clear_holds = function() {
160         clearing = true;
161         $scope.clear_progress = {max : 0, value : 0};
162
163         // we want to see all processed holds, so (effectively) remove
164         // the grid limit.
165         $scope.gridControls.setLimit(1000, true); 
166
167         // initiate clear shelf and grab cache key
168         egCore.net.request(
169             'open-ils.circ',
170             'open-ils.circ.hold.clear_shelf.process',
171             egCore.auth.token(), $scope.pickup_ou.id(),
172             null, 1
173
174         // request responses from the clear shelf cache
175         ).then(
176             
177             // clear shelf done; fetch the cached results.
178             function(resp) {
179                 clearing = false;
180                 egCore.net.request(
181                     'open-ils.circ',
182                     'open-ils.circ.hold.clear_shelf.get_cache',
183                     egCore.auth.token(), resp.cache_key, 1
184                 ).then(null, null, handle_clear_cache_resp);
185             }, 
186
187             null,
188
189             // handle streamed clear_shelf progress updates
190             function(resp) {
191                 if (resp.maximum) 
192                     $scope.clear_progress.max = resp.maximum;
193                 if (resp.progress)
194                     $scope.clear_progress.value = resp.progress;
195             }
196
197         );
198     }
199
200     $scope.print_list_progress = null;
201     $scope.print_shelf_list = function() {
202         var print_holds = [];
203         $scope.print_list_loading = true;
204         $scope.print_list_progress = 0;
205
206         // collect the full list of holds
207         egCore.net.request(
208             'open-ils.circ',
209             'open-ils.circ.captured_holds.id_list.on_shelf.retrieve.authoritative.atomic',
210             egCore.auth.token(), $scope.pickup_ou.id()
211         ).then( function(idlist) {
212
213             egHolds.fetch_holds(idlist).then(
214                 function () {
215                     console.debug('printing ' + print_holds.length + ' holds');
216                     // holds fetched, send to print
217                     egCore.print.print({
218                         context : 'default', 
219                         template : 'hold_shelf_list', 
220                         scope : {holds : print_holds}
221                     })
222                 },
223                 null,
224                 function(hold_data) {
225                     $scope.print_list_progress++;
226                     egHolds.local_flesh(hold_data);
227                     print_holds.push(hold_data);
228                     hold_data.title = hold_data.mvr.title();
229                     hold_data.author = hold_data.mvr.author();
230                     hold_data.hold = egCore.idl.toHash(hold_data.hold);
231                     hold_data.copy = egCore.idl.toHash(hold_data.copy);
232                     hold_data.volume = egCore.idl.toHash(hold_data.volume);
233                     hold_data.part = egCore.idl.toHash(hold_data.part);
234                 }
235             )
236         }).finally(function() {
237             $scope.print_list_loading = false;
238             $scope.print_list_progress = null;
239         });
240     }
241
242     refresh_page();
243
244 }])
245
246 .controller('HoldsPullListCtrl',
247        ['$scope','$q','$routeParams','$window','$location','egCore',
248         'egHolds','egCirc','egHoldGridActions','egProgressDialog',
249 function($scope , $q , $routeParams , $window , $location , egCore , 
250          egHolds , egCirc , egHoldGridActions , egProgressDialog) {
251
252     $scope.detail_hold_id = $routeParams.hold_id;
253
254     var cached_details = {};
255     var details_needed = {};
256
257     $scope.gridControls = {
258         setQuery : function() {
259             return {'copy_circ_lib_id' : egCore.auth.user().ws_ou()}
260         },
261         setSort : function() {
262             return ['copy_location_order_position','call_number_sort_key']
263         },
264         collectStarted : function(offset) {
265             // Launch an indeterminate -> semi-determinate progress
266             // modal.  Using a determinate modal that starts counting
267             // on the post-grid holds data retrieval results in a modal
268             // that's stuck at 0% for most of its life, which is aggravating.
269             egProgressDialog.open();
270         },
271         itemRetrieved : function(item) {
272             egProgressDialog.increment();
273             if (!cached_details[item.id]) {
274                 details_needed[item.id] = item;
275             }
276         },
277         allItemsRetrieved : function() {
278             flesh_holds().finally(egProgressDialog.close);
279         }
280     }
281
282
283     // Fetches hold detail data for each hold in the grid and links
284     // the detail data to the related grid item so egHoldGridActions 
285     // and friends have access to holds data they understand.
286     // Only fetch not-yet-cached data.
287     function flesh_holds() {
288         egProgressDialog.increment();
289
290         // Start by fleshing hold details from our cached data.
291         var items = $scope.gridControls.allItems();
292         angular.forEach(items, function(item) {
293             if (!cached_details[item.id]) return $q.when();
294             angular.forEach(cached_details[item.id], 
295                 function(val, key) { item[key] = val })
296         });
297
298         // Exit if all needed details were already cached
299         if (Object.keys(details_needed).length == 0) return $q.when();
300
301         return egCore.net.request(
302             'open-ils.circ',
303             'open-ils.circ.hold.details.batch.retrieve.authoritative',
304             egCore.auth.token(), Object.keys(details_needed), {
305                 include_usr : true
306             }
307
308         ).then(null, null, function(hold_info) {
309             egProgressDialog.increment();
310             var hold_id = hold_info.hold.id();
311             cached_details[hold_id] = hold_info;
312             var item = details_needed[hold_id];
313             delete details_needed[hold_id];
314
315             // flesh the grid item from the blob of hold data.
316             angular.forEach(hold_info, 
317                 function(val, key) { item[key] = val });
318
319             // check if this is a staff-created hold
320             // i.e requestor's profile != 2 (patron)
321
322             item['_is_staff_hold'] = item.hold.requestor().profile != 2;
323
324         });
325     }
326
327     $scope.grid_actions = egHoldGridActions;
328     $scope.grid_actions.refresh = function() {
329         cached_details = {}; // un-cache details after edit actions.
330         $scope.gridControls.refresh();
331     }
332
333     $scope.detail_view = function(action, user_data, items) {
334         if (h = items[0]) {
335             $location.path('/circ/holds/pull/' + h.hold.id());
336         }
337     }
338
339     $scope.list_view = function(items) {
340         $location.path('/circ/holds/pull');
341     }
342
343     // when the detail hold is fetched (and updated), update the bib
344     // record summary display record id.
345     $scope.set_hold = function(hold_data) {
346         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
347     }
348
349     // By default, this action is hidded from the UI, but leaving it
350     // here in case it's needed in the future
351     $scope.print_list_alt = function() {
352         var url = '/opac/extras/circ/alt_holds_print.html';
353         var win = $window.open(url, '_blank');
354         win.ses = function() {return egCore.auth.token()};
355         win.open();
356         win.focus();
357     }
358
359     $scope.print_full_list = function() {
360         var print_holds = [];
361         egProgressDialog.open({value : 0});
362
363         // collect the full list of holds
364         egCore.net.request(
365             'open-ils.circ',
366             'open-ils.circ.hold_pull_list.fleshed.stream',
367             egCore.auth.token(), 10000, 0
368         ).then(
369             function() {
370                 console.debug('printing ' + print_holds.length + ' holds');
371
372                 // holds fetched, send to print
373                 egCore.print.print({
374                     context : 'default', 
375                     template : 'hold_pull_list', 
376                     scope : {holds : print_holds}
377                 });
378             },
379             null, 
380             function(hold_data) {
381                 egProgressDialog.increment();
382                 egHolds.local_flesh(hold_data);
383                 print_holds.push(hold_data);
384                 hold_data.title = hold_data.mvr.title();
385                 hold_data.author = hold_data.mvr.author();
386                 hold_data.hold = egCore.idl.toHash(hold_data.hold);
387                 hold_data.copy = egCore.idl.toHash(hold_data.copy);
388                 hold_data.volume = egCore.idl.toHash(hold_data.volume);
389                 hold_data.part = egCore.idl.toHash(hold_data.part);
390             }
391         ).finally(egProgressDialog.close);
392     }
393
394 }])
395