]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Enable next/prev/first/last/results buttons on all new search results
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
1 /**
2  * TPAC Frame App
3  *
4  * currently, this app doesn't use routes for each sub-ui, because 
5  * reloading the catalog each time is sloooow.  better so far to 
6  * swap out divs w/ ng-if / ng-show / ng-hide as needed.
7  *
8  */
9
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','egCoreMod','egGridMod', 'egMarcMod'])
11
12 .config(function($routeProvider, $locationProvider, $compileProvider) {
13     $locationProvider.html5Mode(true);
14     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
15
16     var resolver = {delay : 
17         ['egStartup', function(egStartup) {return egStartup.go()}]}
18
19     $routeProvider.when('/cat/catalog/index', {
20         templateUrl: './cat/catalog/t_catalog',
21         controller: 'CatalogCtrl',
22         resolve : resolver
23     });
24
25     $routeProvider.when('/cat/catalog/retrieve_by_id', {
26         templateUrl: './cat/catalog/t_retrieve_by_id',
27         controller: 'CatalogRecordRetrieve',
28         resolve : resolver
29     });
30
31     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
32         templateUrl: './cat/catalog/t_retrieve_by_tcn',
33         controller: 'CatalogRecordRetrieve',
34         resolve : resolver
35     });
36
37     // create some catalog page-specific mappings
38     $routeProvider.when('/cat/catalog/record/:record_id', {
39         templateUrl: './cat/catalog/t_catalog',
40         controller: 'CatalogCtrl',
41         resolve : resolver
42     });
43
44     // create some catalog page-specific mappings
45     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
46         templateUrl: './cat/catalog/t_catalog',
47         controller: 'CatalogCtrl',
48         resolve : resolver
49     });
50
51     $routeProvider.when('/cat/catalog/batchEdit', {
52         templateUrl: './cat/catalog/t_batchedit',
53         controller: 'BatchEditCtrl',
54         resolve : resolver
55     });
56
57     $routeProvider.when('/cat/catalog/batchEdit/:container_type/:container_id', {
58         templateUrl: './cat/catalog/t_batchedit',
59         controller: 'BatchEditCtrl',
60         resolve : resolver
61     });
62
63     $routeProvider.when('/cat/catalog/vandelay', {
64         templateUrl: './cat/catalog/t_vandelay',
65         controller: 'VandelayCtrl',
66         resolve : resolver
67     });
68
69     $routeProvider.when('/cat/catalog/verifyURLs', {
70         templateUrl: './cat/catalog/t_verifyurls',
71         controller: 'URLVerifyCtrl',
72         resolve : resolver
73     });
74
75     $routeProvider.when('/cat/catalog/manageAuthorities', {
76         templateUrl: './cat/catalog/t_manageauthorities',
77         controller: 'ManageAuthoritiesCtrl',
78         resolve : resolver
79     });
80
81     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
82 })
83
84
85 /**
86  * */
87 .controller('CatalogRecordRetrieve',
88        ['$scope','$routeParams','$location','$q','egCore',
89 function($scope , $routeParams , $location , $q , egCore ) {
90
91     $scope.focusMe = true;
92
93     // jump to the patron checkout UI
94     function loadRecord(record_id) {
95         $location
96         .path('/cat/catalog/record/' + record_id);
97     }
98
99     $scope.submitId = function(args) {
100         $scope.recordNotFound = null;
101         if (!args.record_id) return;
102
103         // blur so next time it's set to true it will re-apply select()
104         $scope.selectMe = false;
105
106         return loadRecord(args.record_id);
107     }
108
109     $scope.submitTCN = function(args) {
110         $scope.recordNotFound = null;
111         $scope.moreRecordsFound = null;
112         if (!args.record_tcn) return;
113
114         // blur so next time it's set to true it will re-apply select()
115         $scope.selectMe = false;
116
117         // lookup TCN
118         egCore.net.request(
119             'open-ils.search',
120             'open-ils.search.biblio.tcn',
121             args.record_tcn)
122
123         .then(function(resp) { // get_barcodes
124
125             if (evt = egCore.evt.parse(resp)) {
126                 alert(evt); // FIXME
127                 return;
128             }
129
130             if (!resp.count) {
131                 $scope.recordNotFound = args.record_tcn;
132                 $scope.selectMe = true;
133                 return;
134             }
135
136             if (resp.count > 1) {
137                 $scope.moreRecordsFound = args.record_tcn;
138                 $scope.selectMe = true;
139                 return;
140             }
141
142             var record_id = resp.ids[0];
143             return loadRecord(record_id);
144         });
145     }
146
147 }])
148
149 .controller('CatalogCtrl',
150        ['$scope','$routeParams','$location','$q','egCore','egHolds',
151         'egGridDataProvider','egHoldGridActions','$timeout',
152 function($scope , $routeParams , $location , $q , egCore , egHolds, 
153          egGridDataProvider , egHoldGridActions , $timeout) {
154
155     // set record ID on page load if available...
156     $scope.record_id = $routeParams.record_id;
157
158     if ($routeParams.record_id) $scope.from_route = true;
159     else $scope.from_route = false;
160
161     // will hold a ref to the opac iframe
162     $scope.opac_iframe = null;
163     $scope.in_opac_call = false;
164     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
165         if ($scope.opac_iframe) {
166             if (force_opac_tab) $scope.record_tab = 'catalog';
167             $scope.in_opac_call = true;
168             $scope.opac_iframe.contentWindow[opac_frame_function]();
169         }
170     }
171
172     $scope.stop_unload = false;
173     $scope.$watch('stop_unload',
174         function(newVal, oldVal) {
175             if (newVal && newVal != oldVal && $scope.opac_iframe) {
176                 $($scope.opac_iframe.contentWindow).on('beforeunload', function(){
177                     return 'There is unsaved data in this record.'
178                 });
179             } else {
180                 $($scope.opac_iframe.contentWindow).off('beforeunload');
181             }
182         }
183     );
184
185     // Set the "last bib" cookie, if we have that
186     if ($scope.record_id)
187         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
188
189     // also set it when the iframe changes to a new record
190     $scope.handle_page = function(url) {
191
192         if (!url || url == 'about:blank') {
193             // nothing loaded.  If we already have a record ID, leave it.
194             return;
195         }
196
197         var match = url.match(/\/+opac\/+record\/+(\d+)/);
198         if (match) {
199             $scope.record_id = match[1];
200             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
201         } else {
202             delete $scope.record_id;
203             $scope.from_route = false;
204         }
205
206         // child scope is executing this function, so our digest doesn't fire ... thus,
207         $scope.$apply();
208
209         if (!$scope.in_opac_call) {
210             if ($scope.record_id) {
211                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
212                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
213             } else {
214                 tab = $routeParams.record_tab || 'catalog';
215             }
216             $scope.set_record_tab(tab);
217         } else {
218             $scope.in_opac_call = false;
219         }
220     }
221
222     // xulG catalog handlers
223     $scope.handlers = { }
224
225     // ------------------------------------------------------------------
226     // Holds 
227     var provider = egGridDataProvider.instance({});
228     $scope.hold_grid_data_provider = provider;
229     $scope.grid_actions = egHoldGridActions;
230     $scope.grid_actions.refresh = function () { provider.refresh() };
231     $scope.hold_grid_controls = {};
232
233     var hold_ids = []; // current list of holds
234     function fetchHolds(offset, count) {
235         var ids = hold_ids.slice(offset, offset + count);
236         return egHolds.fetch_holds(ids).then(null, null,
237             function(hold_data) { 
238                 return hold_data;
239             }
240         );
241     }
242
243     provider.get = function(offset, count) {
244         if ($scope.record_tab != 'holds') return $q.when();
245         var deferred = $q.defer();
246         hold_ids = []; // no caching ATM
247
248         // fetch the IDs
249         egCore.net.request(
250             'open-ils.circ',
251             'open-ils.circ.holds.retrieve_all_from_title',
252             egCore.auth.token(), $scope.record_id, 
253             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
254         ).then(
255             function(hold_data) {
256                 angular.forEach(hold_data, function(list, type) {
257                     hold_ids = hold_ids.concat(list);
258                 });
259                 fetchHolds(offset, count).then(
260                     deferred.resolve, null, deferred.notify);
261             }
262         );
263
264         return deferred.promise;
265     }
266
267     $scope.detail_view = function(action, user_data, items) {
268         if (h = items[0]) {
269             $scope.detail_hold_id = h.hold.id();
270         }
271     }
272
273     $scope.list_view = function(items) {
274          $scope.detail_hold_id = null;
275     }
276
277     // refresh the list of record holds when the pickup lib is changed.
278     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
279     $scope.pickup_ou_changed = function(org) {
280         $scope.pickup_ou = org;
281         provider.refresh();
282     }
283
284     $scope.print_holds = function() {
285         var holds = [];
286         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
287             holds.push({
288                 hold : egCore.idl.toHash(item.hold),
289                 patron_last : item.patron_last,
290                 patron_alias : item.patron_alias,
291                 patron_barcode : item.patron_barcode,
292                 copy : egCore.idl.toHash(item.copy),
293                 volume : egCore.idl.toHash(item.volume),
294                 title : item.mvr.title(),
295                 author : item.mvr.author()
296             });
297         });
298
299         egCore.print.print({
300             context : 'receipt', 
301             template : 'holds_for_bib', 
302             scope : {holds : holds}
303         });
304     }
305
306     $scope.mark_hold_transfer_dest = function() {
307         egCore.hatch.setLocalItem(
308             'eg.circ.hold.title_transfer_target', $scope.record_id);
309     }
310
311     // UI presents this option as "all holds"
312     $scope.transfer_holds_to_marked = function() {
313         var hold_ids = $scope.hold_grid_controls.allItems().map(
314             function(hold_data) {return hold_data.hold.id()});
315         egHolds.transfer_to_marked_title(hold_ids);
316     }
317
318     // ------------------------------------------------------------------
319     // Initialize the selected tab
320
321     function init_cat_url() {
322         // Set the initial catalog URL.  This only happens once.
323         // The URL is otherwise generated through user navigation.
324         if ($scope.catalog_url) return; 
325
326         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
327
328         // A record ID in the path indicates a request for the record-
329         // specific page.
330         if ($routeParams.record_id) {
331             url = url.replace(/advanced/, '/record/' + $scope.record_id);
332         }
333
334         $scope.catalog_url = url;
335     }
336
337     $scope.set_record_tab = function(tab) {
338         $scope.record_tab = tab;
339
340         switch(tab) {
341
342             case 'catalog':
343                 init_cat_url();
344                 break;
345
346             case 'holds':
347                 $scope.detail_hold_record_id = $scope.record_id; 
348                 // refresh the holds grid
349                 provider.refresh();
350                 break;
351         }
352     }
353
354     $scope.set_default_record_tab = function() {
355         egCore.hatch.setLocalItem(
356             'eg.cat.default_record_tab', $scope.record_tab);
357         $timeout(function(){$scope.default_tab = $scope.record_tab});
358     }
359
360     var tab;
361     if ($scope.record_id) {
362         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
363         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
364     } else {
365         tab = $routeParams.record_tab || 'catalog';
366     }
367     $scope.set_record_tab(tab);
368
369 }])
370
371 .controller('URLVerifyCtrl',
372        ['$scope','$location',
373 function($scope , $location) {
374     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
375 }])
376
377 .controller('VandelayCtrl',
378        ['$scope','$location',
379 function($scope , $location) {
380     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
381 }])
382
383 .controller('ManageAuthoritiesCtrl',
384        ['$scope','$location',
385 function($scope , $location) {
386     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
387 }])
388
389 .controller('BatchEditCtrl',
390        ['$scope','$location','$routeParams',
391 function($scope , $location , $routeParams) {
392     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
393     if ($routeParams.container_type) {
394         switch ($routeParams.container_type) {
395             case 'bucket':
396                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
397                 break;
398             case 'record':
399                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
400                 break;
401         };
402     }
403 }])
404
405