]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/holds/app.js
LP#1653001 webstaff: Holds pull list sortable columns
[working/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',
249 function($scope , $q , $routeParams , $window , $location , egCore , 
250          egHolds , egCirc , egHoldGridActions) {
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         itemRetrieved : function(item) {
265             if (!cached_details[item.id]) {
266                 details_needed[item.id] = item;
267             }
268         },
269         allItemsRetrieved : flesh_holds
270     }
271
272
273     // Fetches hold detail data for each hold in the grid and links
274     // the detail data to the related grid item so egHoldGridActions 
275     // and friends have access to holds data they understand.
276     // Only fetch not-yet-cached data.
277     function flesh_holds() {
278
279         // Start by fleshing hold details from our cached data.
280         var items = $scope.gridControls.allItems();
281         angular.forEach(items, function(item) {
282             if (!cached_details[item.id]) return;
283             angular.forEach(cached_details[item.id], 
284                 function(val, key) { item[key] = val })
285         });
286
287         // Exit if all needed details were already cached
288         if (Object.keys(details_needed).length == 0) return;
289
290         $scope.print_list_loading = true;
291         $scope.print_list_progress = 0;
292
293         egCore.net.request(
294             'open-ils.circ',
295             'open-ils.circ.hold.details.batch.retrieve.authoritative',
296             egCore.auth.token(), Object.keys(details_needed)
297         ).then(
298             function() {
299                 $scope.print_list_loading = false;
300                 $scope.print_list_progress = null;
301             }, null,
302             function(hold_info) {
303                 var hold_id = hold_info.hold.id();
304                 cached_details[hold_id] = hold_info;
305                 var item = details_needed[hold_id];
306                 delete details_needed[hold_id];
307                 angular.forEach(hold_info, 
308                     function(val, key) { item[key] = val });
309                 $scope.print_list_progress++;
310             }
311         );
312     }
313
314     $scope.grid_actions = egHoldGridActions;
315     $scope.grid_actions.refresh = function() {
316         cached_details = {}; // un-cache details after edit actions.
317         $scope.gridControls.refresh();
318     }
319
320     $scope.detail_view = function(action, user_data, items) {
321         if (h = items[0]) {
322             $location.path('/circ/holds/pull/' + h.hold.id());
323         }
324     }
325
326     $scope.list_view = function(items) {
327         $location.path('/circ/holds/pull');
328     }
329
330     // when the detail hold is fetched (and updated), update the bib
331     // record summary display record id.
332     $scope.set_hold = function(hold_data) {
333         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
334     }
335
336     // By default, this action is hidded from the UI, but leaving it
337     // here in case it's needed in the future
338     $scope.print_list_alt = function() {
339         var url = '/opac/extras/circ/alt_holds_print.html';
340         var win = $window.open(url, '_blank');
341         win.ses = function() {return egCore.auth.token()};
342         win.open();
343         win.focus();
344     }
345
346     $scope.print_list_progress = null;
347     $scope.print_full_list = function() {
348         var print_holds = [];
349         $scope.print_list_loading = true;
350         $scope.print_list_progress = 0;
351
352         // collect the full list of holds
353         egCore.net.request(
354             'open-ils.circ',
355             'open-ils.circ.hold_pull_list.fleshed.stream',
356             egCore.auth.token(), 10000, 0
357         ).then(
358             function() {
359                 console.debug('printing ' + print_holds.length + ' holds');
360
361                 // holds fetched, send to print
362                 egCore.print.print({
363                     context : 'default', 
364                     template : 'hold_pull_list', 
365                     scope : {holds : print_holds}
366                 });
367             },
368             null, 
369             function(hold_data) {
370                 $scope.print_list_progress++;
371                 egHolds.local_flesh(hold_data);
372                 print_holds.push(hold_data);
373                 hold_data.title = hold_data.mvr.title();
374                 hold_data.author = hold_data.mvr.author();
375                 hold_data.hold = egCore.idl.toHash(hold_data.hold);
376                 hold_data.copy = egCore.idl.toHash(hold_data.copy);
377                 hold_data.volume = egCore.idl.toHash(hold_data.volume);
378                 hold_data.part = egCore.idl.toHash(hold_data.part);
379             }
380         ).finally(function() {
381             $scope.print_list_loading = false;
382             $scope.print_list_progress = null;
383         });
384     }
385
386 }])
387