]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Holdings View!
[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.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','holdingsSvc',
152 function($scope , $routeParams , $location , $q , egCore , egHolds, 
153          egGridDataProvider , egHoldGridActions , $timeout , holdingsSvc) {
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     // Holdings
231
232     $scope.holdingsGridControls = {};
233     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
234         get : function(offset, count) {
235             return this.arrayNotifier(holdingsSvc.copies, offset, count);
236         }
237     });
238
239     // refresh the list of holdings when the filter lib is changed.
240     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
241     $scope.holdings_ou_changed = function(org) {
242         $scope.holdings_ou = org;
243         holdingsSvc.fetch($scope.record_id, $scope.holdings_ou).then(function() {
244             $scope.holdingsGridDataProvider.refresh();
245         });
246     }
247
248
249     // ------------------------------------------------------------------
250     // Holds 
251     var provider = egGridDataProvider.instance({});
252     $scope.hold_grid_data_provider = provider;
253     $scope.grid_actions = egHoldGridActions;
254     $scope.grid_actions.refresh = function () { provider.refresh() };
255     $scope.hold_grid_controls = {};
256
257     var hold_ids = []; // current list of holds
258     function fetchHolds(offset, count) {
259         var ids = hold_ids.slice(offset, offset + count);
260         return egHolds.fetch_holds(ids).then(null, null,
261             function(hold_data) { 
262                 return hold_data;
263             }
264         );
265     }
266
267     provider.get = function(offset, count) {
268         if ($scope.record_tab != 'holds') return $q.when();
269         var deferred = $q.defer();
270         hold_ids = []; // no caching ATM
271
272         // fetch the IDs
273         egCore.net.request(
274             'open-ils.circ',
275             'open-ils.circ.holds.retrieve_all_from_title',
276             egCore.auth.token(), $scope.record_id, 
277             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
278         ).then(
279             function(hold_data) {
280                 angular.forEach(hold_data, function(list, type) {
281                     hold_ids = hold_ids.concat(list);
282                 });
283                 fetchHolds(offset, count).then(
284                     deferred.resolve, null, deferred.notify);
285             }
286         );
287
288         return deferred.promise;
289     }
290
291     $scope.detail_view = function(action, user_data, items) {
292         if (h = items[0]) {
293             $scope.detail_hold_id = h.hold.id();
294         }
295     }
296
297     $scope.list_view = function(items) {
298          $scope.detail_hold_id = null;
299     }
300
301     // refresh the list of record holds when the pickup lib is changed.
302     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
303     $scope.pickup_ou_changed = function(org) {
304         $scope.pickup_ou = org;
305         provider.refresh();
306     }
307
308     $scope.print_holds = function() {
309         var holds = [];
310         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
311             holds.push({
312                 hold : egCore.idl.toHash(item.hold),
313                 patron_last : item.patron_last,
314                 patron_alias : item.patron_alias,
315                 patron_barcode : item.patron_barcode,
316                 copy : egCore.idl.toHash(item.copy),
317                 volume : egCore.idl.toHash(item.volume),
318                 title : item.mvr.title(),
319                 author : item.mvr.author()
320             });
321         });
322
323         egCore.print.print({
324             context : 'receipt', 
325             template : 'holds_for_bib', 
326             scope : {holds : holds}
327         });
328     }
329
330     $scope.mark_hold_transfer_dest = function() {
331         egCore.hatch.setLocalItem(
332             'eg.circ.hold.title_transfer_target', $scope.record_id);
333     }
334
335     // UI presents this option as "all holds"
336     $scope.transfer_holds_to_marked = function() {
337         var hold_ids = $scope.hold_grid_controls.allItems().map(
338             function(hold_data) {return hold_data.hold.id()});
339         egHolds.transfer_to_marked_title(hold_ids);
340     }
341
342     // ------------------------------------------------------------------
343     // Initialize the selected tab
344
345     function init_cat_url() {
346         // Set the initial catalog URL.  This only happens once.
347         // The URL is otherwise generated through user navigation.
348         if ($scope.catalog_url) return; 
349
350         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
351
352         // A record ID in the path indicates a request for the record-
353         // specific page.
354         if ($routeParams.record_id) {
355             url = url.replace(/advanced/, '/record/' + $scope.record_id);
356         }
357
358         $scope.catalog_url = url;
359     }
360
361     function init_parts_url() {
362         $scope.parts_url = $location
363             .absUrl()
364             .replace(
365                 /\/staff.*/,
366                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
367             );
368     }
369
370     $scope.set_record_tab = function(tab) {
371         $scope.record_tab = tab;
372
373         switch(tab) {
374
375             case 'monoparts':
376                 init_parts_url();
377                 break;
378
379             case 'catalog':
380                 init_cat_url();
381                 break;
382
383             case 'holds':
384                 $scope.detail_hold_record_id = $scope.record_id; 
385                 // refresh the holds grid
386                 provider.refresh();
387                 break;
388         }
389     }
390
391     $scope.set_default_record_tab = function() {
392         egCore.hatch.setLocalItem(
393             'eg.cat.default_record_tab', $scope.record_tab);
394         $timeout(function(){$scope.default_tab = $scope.record_tab});
395     }
396
397     var tab;
398     if ($scope.record_id) {
399         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
400         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
401
402         holdingsSvc.fetch($scope.record_id, $scope.holdings_ou).then(function() {
403             $scope.holdingsGridDataProvider.refresh();
404         });
405
406     } else {
407         tab = $routeParams.record_tab || 'catalog';
408     }
409     $scope.set_record_tab(tab);
410
411 }])
412
413 .controller('URLVerifyCtrl',
414        ['$scope','$location',
415 function($scope , $location) {
416     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
417 }])
418
419 .controller('VandelayCtrl',
420        ['$scope','$location',
421 function($scope , $location) {
422     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
423 }])
424
425 .controller('ManageAuthoritiesCtrl',
426        ['$scope','$location',
427 function($scope , $location) {
428     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
429 }])
430
431 .controller('BatchEditCtrl',
432        ['$scope','$location','$routeParams',
433 function($scope , $location , $routeParams) {
434     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
435     if ($routeParams.container_type) {
436         switch ($routeParams.container_type) {
437             case 'bucket':
438                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
439                 break;
440             case 'record':
441                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
442                 break;
443         };
444     }
445 }])
446
447  
448 .filter('boolText', function(){
449     return function (v) {
450         return v == 't';
451     }
452 })
453
454 .factory('holdingsSvc', 
455        ['egCore','$q',
456 function(egCore , $q) {
457
458     var service = {
459         copies : [], // record search results
460         index : 0, // search grid index
461         org : null,
462         rid : null
463     };
464
465     service.flesh = {   
466         flesh : 2, 
467         flesh_fields : {
468             acp : ['status','location'],
469             acn : ['prefix','suffix','copies']
470         }
471     }
472
473     // resolved with the last received copy
474     service.fetch = function(rid, org) {
475
476         if (!rid) return $q.when();
477         if (!org) return $q.when();
478         if (service.rid && service.org && service.rid == rid && service.org == org) return $q.when();
479
480         service.rid = rid;
481         service.org = org;
482         service.copies = [];
483         service.index = 0;
484
485         var org_list = egCore.org.descendants(org.id(), true);
486
487         return egCore.pcrud.search(
488             'acn',
489             {record : rid, owning_lib : org_list, deleted : 'f'},
490             service.flesh
491         ).then(
492             function() { // finished
493                 service.copies = service.copies.sort(
494                     function (a, b) {
495                         function compare_array (x, y, i) {
496                             if (x[i] && y[i]) { // both have values
497                                 if (x[i] == y[i]) { // need to look deeper
498                                     return compare_array(x, y, ++i);
499                                 }
500
501                                 if (x[i] < y[i]) { // x is first
502                                     return -1;
503                                 } else if (x[i] > y[i]) { // y is first
504                                     return 1;
505                                 }
506
507                             } else { // no orgs to compare ...
508                                 if (x[i]) return -1;
509                                 if (y[i]) return 1;
510                             }
511                             return 0;
512                         }
513
514                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
515                         if (!owner_order) {
516                             // now compare on CN label
517                             if (a.call_number.label < b.call_number.label) return -1;
518                             if (a.call_number.label > b.call_number.label) return 1;
519
520                             // try copy number
521                             if (a.copy_number < b.copy_number) return -1;
522                             if (a.copy_number > b.copy_number) return 1;
523
524                             // finally, barcode
525                             if (a.barcode < b.barcode) return -1;
526                             if (a.barcode > b.barcode) return 1;
527                         }
528                         return owner_order;
529                     }
530                 );
531
532                 // create a label using just the unique part of the owner list
533                 var index = 0;
534                 var prev_owner_list;
535                 angular.forEach(service.copies, function (cp) {
536                     if (!prev_owner_list) {
537                         cp.owner_label = cp.owner_list.join(' ... ');
538                     } else {
539                         var current_owner_list = cp.owner_list.slice();
540                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
541                             current_owner_list.shift();
542                             prev_owner_list.shift();
543                         }
544                         cp.owner_label = current_owner_list.join(' ... ');
545                     }
546
547                     cp.index = index++;
548                     prev_owner_list = cp.owner_list.slice();
549                 });
550             },
551
552             null, // error
553
554             // notify reads the stream of copies, one at a time.
555             function(cn) {
556
557                 var copies = cn.copies();
558                 cn.copies([]);
559
560                 angular.forEach(copies, function (cp) {
561                     cp.call_number(cn);
562                 });
563
564                 var flat = egCore.idl.toHash(copies);
565                 var owner = egCore.org.get(flat[0].call_number.owning_lib);
566
567                 var owner_name_list = [];
568                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
569                     owner_name_list.unshift(owner.name());
570                     owner = egCore.org.get(owner.parent_ou());
571                 }
572
573                 angular.forEach(flat, function (cp) {
574                     cp.owner_list = owner_name_list;
575                 });
576
577                 service.copies = service.copies.concat(flat);
578                 return cn;
579             }
580         );
581     }
582
583     return service;
584 }])
585
586