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