]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Integrate Monograph Parts 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.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.parts_iframe = null;
164
165     $scope.in_opac_call = false;
166     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
167         if ($scope.opac_iframe) {
168             if (force_opac_tab) $scope.record_tab = 'catalog';
169             $scope.in_opac_call = true;
170             $scope.opac_iframe.dom.contentWindow[opac_frame_function]();
171         }
172     }
173
174     $scope.stop_unload = false;
175     $scope.$watch('stop_unload',
176         function(newVal, oldVal) {
177             if (newVal && newVal != oldVal && $scope.opac_iframe) {
178                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
179                     return 'There is unsaved data in this record.'
180                 });
181             } else {
182                 if ($scope.opac_iframe)
183                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
184             }
185         }
186     );
187
188     // Set the "last bib" cookie, if we have that
189     if ($scope.record_id)
190         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
191
192     // also set it when the iframe changes to a new record
193     $scope.handle_page = function(url) {
194
195         if (!url || url == 'about:blank') {
196             // nothing loaded.  If we already have a record ID, leave it.
197             return;
198         }
199
200         var match = url.match(/\/+opac\/+record\/+(\d+)/);
201         if (match) {
202             $scope.record_id = match[1];
203             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
204             init_parts_url();
205         } else {
206             delete $scope.record_id;
207             $scope.from_route = false;
208         }
209
210         // child scope is executing this function, so our digest doesn't fire ... thus,
211         $scope.$apply();
212
213         if (!$scope.in_opac_call) {
214             if ($scope.record_id) {
215                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
216                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
217             } else {
218                 tab = $routeParams.record_tab || 'catalog';
219             }
220             $scope.set_record_tab(tab);
221         } else {
222             $scope.in_opac_call = false;
223         }
224     }
225
226     // xulG catalog handlers
227     $scope.handlers = { }
228
229     // ------------------------------------------------------------------
230     // Holds 
231     var provider = egGridDataProvider.instance({});
232     $scope.hold_grid_data_provider = provider;
233     $scope.grid_actions = egHoldGridActions;
234     $scope.grid_actions.refresh = function () { provider.refresh() };
235     $scope.hold_grid_controls = {};
236
237     var hold_ids = []; // current list of holds
238     function fetchHolds(offset, count) {
239         var ids = hold_ids.slice(offset, offset + count);
240         return egHolds.fetch_holds(ids).then(null, null,
241             function(hold_data) { 
242                 return hold_data;
243             }
244         );
245     }
246
247     provider.get = function(offset, count) {
248         if ($scope.record_tab != 'holds') return $q.when();
249         var deferred = $q.defer();
250         hold_ids = []; // no caching ATM
251
252         // fetch the IDs
253         egCore.net.request(
254             'open-ils.circ',
255             'open-ils.circ.holds.retrieve_all_from_title',
256             egCore.auth.token(), $scope.record_id, 
257             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
258         ).then(
259             function(hold_data) {
260                 angular.forEach(hold_data, function(list, type) {
261                     hold_ids = hold_ids.concat(list);
262                 });
263                 fetchHolds(offset, count).then(
264                     deferred.resolve, null, deferred.notify);
265             }
266         );
267
268         return deferred.promise;
269     }
270
271     $scope.detail_view = function(action, user_data, items) {
272         if (h = items[0]) {
273             $scope.detail_hold_id = h.hold.id();
274         }
275     }
276
277     $scope.list_view = function(items) {
278          $scope.detail_hold_id = null;
279     }
280
281     // refresh the list of record holds when the pickup lib is changed.
282     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
283     $scope.pickup_ou_changed = function(org) {
284         $scope.pickup_ou = org;
285         provider.refresh();
286     }
287
288     $scope.print_holds = function() {
289         var holds = [];
290         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
291             holds.push({
292                 hold : egCore.idl.toHash(item.hold),
293                 patron_last : item.patron_last,
294                 patron_alias : item.patron_alias,
295                 patron_barcode : item.patron_barcode,
296                 copy : egCore.idl.toHash(item.copy),
297                 volume : egCore.idl.toHash(item.volume),
298                 title : item.mvr.title(),
299                 author : item.mvr.author()
300             });
301         });
302
303         egCore.print.print({
304             context : 'receipt', 
305             template : 'holds_for_bib', 
306             scope : {holds : holds}
307         });
308     }
309
310     $scope.mark_hold_transfer_dest = function() {
311         egCore.hatch.setLocalItem(
312             'eg.circ.hold.title_transfer_target', $scope.record_id);
313     }
314
315     // UI presents this option as "all holds"
316     $scope.transfer_holds_to_marked = function() {
317         var hold_ids = $scope.hold_grid_controls.allItems().map(
318             function(hold_data) {return hold_data.hold.id()});
319         egHolds.transfer_to_marked_title(hold_ids);
320     }
321
322     // ------------------------------------------------------------------
323     // Initialize the selected tab
324
325     function init_cat_url() {
326         // Set the initial catalog URL.  This only happens once.
327         // The URL is otherwise generated through user navigation.
328         if ($scope.catalog_url) return; 
329
330         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
331
332         // A record ID in the path indicates a request for the record-
333         // specific page.
334         if ($routeParams.record_id) {
335             url = url.replace(/advanced/, '/record/' + $scope.record_id);
336         }
337
338         $scope.catalog_url = url;
339     }
340
341     function init_parts_url() {
342         $scope.parts_url = $location
343             .absUrl()
344             .replace(
345                 /\/staff.*/,
346                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
347             );
348     }
349
350     $scope.set_record_tab = function(tab) {
351         $scope.record_tab = tab;
352
353         switch(tab) {
354
355             case 'monoparts':
356                 init_parts_url();
357                 break;
358
359             case 'catalog':
360                 init_cat_url();
361                 break;
362
363             case 'holds':
364                 $scope.detail_hold_record_id = $scope.record_id; 
365                 // refresh the holds grid
366                 provider.refresh();
367                 break;
368         }
369     }
370
371     $scope.set_default_record_tab = function() {
372         egCore.hatch.setLocalItem(
373             'eg.cat.default_record_tab', $scope.record_tab);
374         $timeout(function(){$scope.default_tab = $scope.record_tab});
375     }
376
377     var tab;
378     if ($scope.record_id) {
379         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
380         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
381     } else {
382         tab = $routeParams.record_tab || 'catalog';
383     }
384     $scope.set_record_tab(tab);
385
386 }])
387
388 .controller('URLVerifyCtrl',
389        ['$scope','$location',
390 function($scope , $location) {
391     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
392 }])
393
394 .controller('VandelayCtrl',
395        ['$scope','$location',
396 function($scope , $location) {
397     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
398 }])
399
400 .controller('ManageAuthoritiesCtrl',
401        ['$scope','$location',
402 function($scope , $location) {
403     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
404 }])
405
406 .controller('BatchEditCtrl',
407        ['$scope','$location','$routeParams',
408 function($scope , $location , $routeParams) {
409     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
410     if ($routeParams.container_type) {
411         switch ($routeParams.container_type) {
412             case 'bucket':
413                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
414                 break;
415             case 'record':
416                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
417                 break;
418         };
419     }
420 }])
421
422