]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/circ/holds/app.js
LP#1402797 Hold Shefl: Use max_chunk_size to pass updates in a timely fashion; Notify...
[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 .factory('holdUiSvc', function() {
39     return {
40         holds : [] // cache
41     }
42 })
43
44 .controller('HoldsShelfCtrl',
45        ['$scope','$q','$routeParams','$window','$location','egCore','egHolds','egHoldGridActions','egCirc','egGridDataProvider',
46 function($scope , $q , $routeParams , $window , $location , egCore , egHolds , egHoldGridActions , egCirc , egGridDataProvider)  {
47     $scope.detail_hold_id = $routeParams.hold_id;
48
49     var hold_ids = [];
50     var holds = [];
51     var clear_mode = false;
52     $scope.gridControls = {};
53     $scope.grid_actions = egHoldGridActions;
54
55     function fetch_holds(offset, count) {
56         var ids = hold_ids.slice(offset, offset + count);
57         return egHolds.fetch_holds(ids).then(null, null,
58             function(hold_data) { 
59                 holds.push(hold_data);
60                 return hold_data; // to the grid
61             }
62         );
63     }
64
65     var provider = egGridDataProvider.instance({});
66     $scope.gridDataProvider = provider;
67
68     function refresh_page() {
69         holds = [];
70         hold_ids = [];
71         provider.refresh();
72     }
73     // called after any egHoldGridActions action occurs
74     $scope.grid_actions.refresh = refresh_page;
75
76     provider.get = function(offset, count) {
77
78         // see if we have the requested range cached
79         if (holds[offset]) {
80             return provider.arrayNotifier(holds, offset, count);
81         }
82
83         // see if we have the holds IDs for this range already loaded
84         if (hold_ids[offset]) {
85             return fetch_holds(offset, count);
86         }
87
88         var deferred = $q.defer();
89         hold_ids = [];
90         holds = [];
91
92         var method = 'open-ils.circ.captured_holds.id_list.on_shelf.retrieve.authoritative.atomic';
93         if (clear_mode) 
94             method = 'open-ils.circ.captured_holds.id_list.expired_on_shelf_or_wrong_shelf.retrieve.atomic';
95
96         egCore.net.request(
97             'open-ils.circ', method,
98             egCore.auth.token(), $scope.pickup_ou.id()
99
100         ).then(function(ids) {
101             if (!ids.length) { 
102                 deferred.resolve(); 
103                 return; 
104             }
105
106             hold_ids = ids;
107             fetch_holds(offset, count)
108             .then(deferred.resolve, null, deferred.notify);
109         });
110
111         return deferred.promise;
112     }
113
114     // re-draw the grid when user changes the org selector
115     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
116     $scope.$watch('pickup_ou', function(newVal, oldVal) {
117         if (newVal && newVal != oldVal) 
118             refresh_page();
119     });
120
121     $scope.detail_view = function(action, user_data, items) {
122         if (h = items[0]) {
123             $location.path('/circ/holds/shelf/' + h.hold.id());
124         }
125     }
126
127     $scope.list_view = function(items) {
128         $location.path('/circ/holds/shelf');
129     }
130
131     // when the detail hold is fetched (and updated), update the bib
132     // record summary display record id.
133     $scope.set_hold = function(hold_data) {
134         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
135     }
136
137     // manage active vs. clearable holds display
138     var clearing = false; // true if actively clearing holds (below)
139     $scope.is_clearing = function() { return clearing };
140     $scope.active_mode = function() {return !clear_mode}
141     $scope.clear_mode = function() {return clear_mode}
142     $scope.show_clearable = function() { clear_mode = true; refresh_page() }
143     $scope.show_active = function() { clear_mode = false; refresh_page() }
144     $scope.disable_clear = function() { return clearing || !clear_mode }
145
146     // udpate the in-grid hold with the clear-shelf cached response info.
147     function handle_clear_cache_resp(resp) {
148         if (!angular.isArray(resp)) resp = [resp];
149         angular.forEach(resp, function(info) {
150             if (info.action) {
151                 var grid_item = holds.filter(function(item) {
152                     return item.hold.id() == info.hold_details.id
153                 })[0];
154
155                 // there will be no grid item if the hold is off-page
156                 if (grid_item) {
157                     grid_item.post_clear = 
158                         egCore.strings['CLEAR_SHELF_ACTION_' + info.action];
159                 }
160             }
161         });
162     }
163
164     $scope.clear_holds = function() {
165         clearing = true;
166         $scope.clear_progress = {max : 0, value : 0};
167
168         // we want to see all processed holds, so (effectively) remove
169         // the grid limit.
170         $scope.gridControls.setLimit(1000); 
171
172         // initiate clear shelf and grab cache key
173         egCore.net.request(
174             'open-ils.circ',
175             'open-ils.circ.hold.clear_shelf.process',
176             egCore.auth.token(), $scope.pickup_ou.id(),
177             null, 1
178
179         // request responses from the clear shelf cache
180         ).then(
181             
182             // clear shelf done; fetch the cached results.
183             function(resp) {
184                 clearing = false;
185                 egCore.net.request(
186                     'open-ils.circ',
187                     'open-ils.circ.hold.clear_shelf.get_cache',
188                     egCore.auth.token(), resp.cache_key, 1
189                 ).then(null, null, handle_clear_cache_resp);
190             }, 
191
192             null,
193
194             // handle streamed clear_shelf progress updates
195             function(resp) {
196                 if (resp.maximum) 
197                     $scope.clear_progress.max = resp.maximum;
198                 if (resp.progress)
199                     $scope.clear_progress.value = resp.progress;
200             }
201
202         );
203     }
204
205     refresh_page();
206
207 }])
208
209 .controller('HoldsPullListCtrl',
210        ['$scope','$q','$routeParams','$window','$location','egCore','egHolds','egCirc','egGridDataProvider','egHoldGridActions','holdUiSvc',
211 function($scope , $q , $routeParams , $window , $location , egCore , egHolds , egCirc , egGridDataProvider , egHoldGridActions , holdUiSvc)  {
212     $scope.detail_hold_id = $routeParams.hold_id;
213
214     var provider = egGridDataProvider.instance({});
215     $scope.gridDataProvider = provider;
216
217     $scope.grid_actions = egHoldGridActions;
218     $scope.grid_actions.refresh = function() {
219         holdUiSvc.holds = [];
220         provider.refresh();
221     }
222
223     provider.get = function(offset, count) {
224
225         if (holdUiSvc.holds[offset]) {
226             return provider.arrayNotifier(holdUiSvc.holds, offset, count);
227         }
228
229         var deferred = $q.defer();
230         var recv_index = 0;
231
232         // fetch the IDs
233         egCore.net.request(
234             'open-ils.circ',
235             'open-ils.circ.hold_pull_list.fleshed.stream',
236             egCore.auth.token(), count, offset
237         ).then(
238             deferred.resolve, null, 
239             function(hold_data) {
240                 egHolds.local_flesh(hold_data);
241                 holdUiSvc.holds[offset + recv_index++] = hold_data;
242                 deferred.notify(hold_data);
243             }
244         );
245
246         return deferred.promise;
247     }
248
249     $scope.detail_view = function(action, user_data, items) {
250         if (h = items[0]) {
251             $location.path('/circ/holds/pull/' + h.hold.id());
252         }
253     }
254
255     $scope.list_view = function(items) {
256         $location.path('/circ/holds/pull');
257     }
258
259     // when the detail hold is fetched (and updated), update the bib
260     // record summary display record id.
261     $scope.set_hold = function(hold_data) {
262         $scope.detail_hold_record_id = hold_data.mvr.doc_id();
263     }
264
265     // By default, this action is hidded from the UI, but leaving it
266     // here in case it's needed in the future
267     $scope.print_list_alt = function() {
268         var url = '/opac/extras/circ/alt_holds_print.html';
269         var win = $window.open(url, '_blank');
270         win.ses = function() {return egCore.auth.token()};
271         win.open();
272         win.focus();
273     }
274
275     $scope.print_list_progress = null;
276     $scope.print_full_list = function() {
277         var print_holds = [];
278         $scope.print_list_loading = true;
279         $scope.print_list_progress = 0;
280
281         // collect the full list of holds
282         egCore.net.request(
283             'open-ils.circ',
284             'open-ils.circ.hold_pull_list.fleshed.stream',
285             egCore.auth.token(), 10000, 0
286         ).then(
287             function() {
288                 console.debug('printing ' + print_holds.length + ' holds');
289
290                 // holds fetched, send to print
291                 egCore.print.print({
292                     context : 'default', 
293                     template : 'hold_pull_list', 
294                     scope : {holds : print_holds}
295                 });
296             },
297             null, 
298             function(hold_data) {
299                 $scope.print_list_progress++;
300                 egHolds.local_flesh(hold_data);
301                 print_holds.push(hold_data);
302                 hold_data.title = hold_data.mvr.title();
303                 hold_data.author = hold_data.mvr.author();
304                 hold_data.hold = egCore.idl.toHash(hold_data.hold);
305                 hold_data.copy = egCore.idl.toHash(hold_data.copy);
306                 hold_data.volume = egCore.idl.toHash(hold_data.volume);
307                 hold_data.part = egCore.idl.toHash(hold_data.part);
308             }
309         ).finally(function() {
310             $scope.print_list_loading = false;
311             $scope.print_list_progress = null;
312         });
313     }
314
315 }])
316