]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
950079f9c42186eff3a76b18d15591c3c320e2a8
[working/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.otherwise({redirectTo : '/cat/catalog/index'});
70 })
71
72
73 /**
74  * */
75 .controller('CatalogRecordRetrieve',
76        ['$scope','$routeParams','$location','$q','egCore',
77 function($scope , $routeParams , $location , $q , egCore ) {
78
79     $scope.focusMe = true;
80
81     // jump to the patron checkout UI
82     function loadRecord(record_id) {
83         $location
84         .path('/cat/catalog/record/' + record_id);
85     }
86
87     $scope.submitId = function(args) {
88         $scope.recordNotFound = null;
89         if (!args.record_id) return;
90
91         // blur so next time it's set to true it will re-apply select()
92         $scope.selectMe = false;
93
94         return loadRecord(args.record_id);
95     }
96
97     $scope.submitTCN = function(args) {
98         $scope.recordNotFound = null;
99         $scope.moreRecordsFound = null;
100         if (!args.record_tcn) return;
101
102         // blur so next time it's set to true it will re-apply select()
103         $scope.selectMe = false;
104
105         // lookup TCN
106         egCore.net.request(
107             'open-ils.search',
108             'open-ils.search.biblio.tcn',
109             args.record_tcn)
110
111         .then(function(resp) { // get_barcodes
112
113             if (evt = egCore.evt.parse(resp)) {
114                 alert(evt); // FIXME
115                 return;
116             }
117
118             if (!resp.count) {
119                 $scope.recordNotFound = args.record_tcn;
120                 $scope.selectMe = true;
121                 return;
122             }
123
124             if (resp.count > 1) {
125                 $scope.moreRecordsFound = args.record_tcn;
126                 $scope.selectMe = true;
127                 return;
128             }
129
130             var record_id = resp.ids[0];
131             return loadRecord(record_id);
132         });
133     }
134
135 }])
136
137 .controller('CatalogCtrl',
138        ['$scope','$routeParams','$location','$q','egCore','egHolds',
139         'egGridDataProvider','egHoldGridActions',
140 function($scope , $routeParams , $location , $q , egCore , egHolds, 
141          egGridDataProvider , egHoldGridActions) {
142
143     // set record ID on page load if available...
144     $scope.record_id = $routeParams.record_id;
145
146     if ($routeParams.record_id) $scope.from_route = true;
147     else $scope.from_route = false;
148
149     // will hold a ref to the opac iframe
150     $scope.opac_iframe = null;
151     $scope.in_opac_call = false;
152     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
153         if ($scope.opac_iframe) {
154             if (force_opac_tab) $scope.record_tab = 'catalog';
155             $scope.in_opac_call = true;
156             $scope.opac_iframe.contentWindow[opac_frame_function]();
157         }
158     }
159
160     $scope.stop_unload = false;
161     $scope.$watch('stop_unload',
162         function(newVal, oldVal) {
163             if (newVal && newVal != oldVal && $scope.opac_iframe) {
164                 $($scope.opac_iframe.contentWindow).on('beforeunload', function(){
165                     return 'There is unsaved data in this record.'
166                 });
167             } else {
168                 $($scope.opac_iframe.contentWindow).off('beforeunload');
169             }
170         }
171     );
172
173     // Set the "last bib" cookie, if we have that
174     if ($scope.record_id)
175         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
176
177     // also set it when the iframe changes to a new record
178     $scope.handle_page = function(url) {
179
180         if (!url || url == 'about:blank') {
181             // nothing loaded.  If we already have a record ID, leave it.
182             return;
183         }
184
185         var match = url.match(/\/+opac\/+record\/+(\d+)/);
186         if (match) {
187             $scope.record_id = match[1];
188             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
189         } else {
190             delete $scope.record_id;
191         }
192
193         // child scope is executing this function, so our digest doesn't fire ... thus,
194         $scope.$apply();
195
196         if (!$scope.in_opac_call) {
197             if ($scope.record_id) {
198                 var default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
199                 tab = $routeParams.record_tab || default_tab || 'catalog';
200             } else {
201                 tab = $routeParams.record_tab || 'catalog';
202             }
203             $scope.set_record_tab(tab);
204         } else {
205             $scope.in_opac_call = false;
206         }
207     }
208
209     // xulG catalog handlers
210     $scope.handlers = { }
211
212     // ------------------------------------------------------------------
213     // Holds 
214     var provider = egGridDataProvider.instance({});
215     $scope.hold_grid_data_provider = provider;
216     $scope.grid_actions = egHoldGridActions;
217     $scope.grid_actions.refresh = function () { provider.refresh() };
218     $scope.hold_grid_controls = {};
219
220     var hold_ids = []; // current list of holds
221     function fetchHolds(offset, count) {
222         var ids = hold_ids.slice(offset, offset + count);
223         return egHolds.fetch_holds(ids).then(null, null,
224             function(hold_data) { 
225                 return hold_data;
226             }
227         );
228     }
229
230     provider.get = function(offset, count) {
231         if ($scope.record_tab != 'holds') return $q.when();
232         var deferred = $q.defer();
233         hold_ids = []; // no caching ATM
234
235         // fetch the IDs
236         egCore.net.request(
237             'open-ils.circ',
238             'open-ils.circ.holds.retrieve_all_from_title',
239             egCore.auth.token(), $scope.record_id, 
240             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
241         ).then(
242             function(hold_data) {
243                 angular.forEach(hold_data, function(list, type) {
244                     hold_ids = hold_ids.concat(list);
245                 });
246                 fetchHolds(offset, count).then(
247                     deferred.resolve, null, deferred.notify);
248             }
249         );
250
251         return deferred.promise;
252     }
253
254     $scope.detail_view = function(action, user_data, items) {
255         if (h = items[0]) {
256             $scope.detail_hold_id = h.hold.id();
257         }
258     }
259
260     $scope.list_view = function(items) {
261          $scope.detail_hold_id = null;
262     }
263
264     // refresh the list of record holds when the pickup lib is changed.
265     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
266     $scope.pickup_ou_changed = function(org) {
267         $scope.pickup_ou = org;
268         provider.refresh();
269     }
270
271     $scope.print_holds = function() {
272         var holds = [];
273         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
274             holds.push({
275                 hold : egCore.idl.toHash(item.hold),
276                 patron_last : item.patron_last,
277                 patron_alias : item.patron_alias,
278                 patron_barcode : item.patron_barcode,
279                 copy : egCore.idl.toHash(item.copy),
280                 volume : egCore.idl.toHash(item.volume),
281                 title : item.mvr.title(),
282                 author : item.mvr.author()
283             });
284         });
285
286         egCore.print.print({
287             context : 'receipt', 
288             template : 'holds_for_bib', 
289             scope : {holds : holds}
290         });
291     }
292
293     $scope.mark_hold_transfer_dest = function() {
294         egCore.hatch.setLocalItem(
295             'eg.circ.hold.title_transfer_target', $scope.record_id);
296     }
297
298     // UI presents this option as "all holds"
299     $scope.transfer_holds_to_marked = function() {
300         var hold_ids = $scope.hold_grid_controls.allItems().map(
301             function(hold_data) {return hold_data.hold.id()});
302         egHolds.transfer_to_marked_title(hold_ids);
303     }
304
305     // ------------------------------------------------------------------
306     // Initialize the selected tab
307
308     function init_cat_url() {
309         // Set the initial catalog URL.  This only happens once.
310         // The URL is otherwise generated through user navigation.
311         if ($scope.catalog_url) return; 
312
313         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
314
315         // A record ID in the path indicates a request for the record-
316         // specific page.
317         if ($routeParams.record_id) {
318             url = url.replace(/advanced/, '/record/' + $scope.record_id);
319         }
320
321         $scope.catalog_url = url;
322     }
323
324     $scope.set_record_tab = function(tab) {
325         $scope.record_tab = tab;
326
327         switch(tab) {
328
329             case 'catalog':
330                 init_cat_url();
331                 break;
332
333             case 'holds':
334                 $scope.detail_hold_record_id = $scope.record_id; 
335                 // refresh the holds grid
336                 provider.refresh();
337                 break;
338         }
339     }
340
341     $scope.set_default_record_tab = function() {
342         egCore.hatch.setLocalItem(
343             'eg.cat.default_record_tab', $scope.record_tab);
344     }
345
346     var tab;
347     if ($scope.record_id) {
348         var default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
349         tab = $routeParams.record_tab || default_tab || 'catalog';
350     } else {
351         tab = $routeParams.record_tab || 'catalog';
352     }
353     $scope.set_record_tab(tab);
354
355 }])
356
357 .controller('VandelayCtrl',
358        ['$scope','$location',
359 function($scope , $location) {
360     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
361 }])
362
363 .controller('BatchEditCtrl',
364        ['$scope','$location','$routeParams',
365 function($scope , $location , $routeParams) {
366     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
367     if ($routeParams.container_type) {
368         switch ($routeParams.container_type) {
369             case 'bucket':
370                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
371                 break;
372             case 'record':
373                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
374                 break;
375         };
376     }
377 }])
378
379