]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Add checkboxes and logic for collapsing copy and vol detail
[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.holdings_ou = null;
175     $scope.$watch('holdings_show_copies',
176         function(newVal, oldVal) {
177             if (newVal != oldVal) {
178                 egCore.hatch.setItem('cat.holdings.show_copies', newVal);
179                 holdingsSvc.fetch({
180                     rid : $scope.record_id,
181                     org : $scope.holdings_ou,
182                     copy: newVal,
183                     vol : $scope.holdings_show_vols,
184                     empty: $scope.holdings_show_empty
185                 }).then(function() {
186                     $scope.holdingsGridDataProvider.refresh();
187                 });
188             }
189         }
190     );
191
192     $scope.$watch('holdings_show_vols',
193         function(newVal, oldVal) {
194             if (newVal != oldVal) {
195                 egCore.hatch.setItem('cat.holdings.show_vols', newVal);
196                 holdingsSvc.fetch({
197                     rid : $scope.record_id,
198                     org : $scope.holdings_ou,
199                     copy: $scope.holdings_show_copies,
200                     vol : newVal,
201                     empty: $scope.holdings_show_empty
202                 }).then(function() {
203                     $scope.holdingsGridDataProvider.refresh();
204                 });
205             }
206         }
207     );
208
209     $scope.$watch('holdings_show_empty',
210         function(newVal, oldVal) {
211             if (newVal != oldVal) {
212                 egCore.hatch.setItem('cat.holdings.show_empty', newVal);
213                 holdingsSvc.fetch({
214                     rid : $scope.record_id,
215                     org : $scope.holdings_ou,
216                     copy: $scope.holdings_show_copies,
217                     vol : $scope.holdings_show_vols,
218                     empty: newVal
219                 }).then(function() {
220                     $scope.holdingsGridDataProvider.refresh();
221                 });
222             }
223         }
224     );
225
226     egCore.hatch.getItem('cat.holdings.show_copies').then(function(x){
227         $scope.holdings_show_copies = x
228     });
229
230     egCore.hatch.getItem('cat.holdings.show_vols').then(function(x){
231         $scope.holdings_show_vols = x;
232     });
233
234     egCore.hatch.getItem('cat.holdings.show_emtpy').then(function(x){
235         $scope.holdings_show_empty = x;
236     });
237
238     $scope.holdings_checkbox_handler = function (item) { $scope[item.checkbox] = item.checked; }
239
240     $scope.stop_unload = false;
241     $scope.$watch('stop_unload',
242         function(newVal, oldVal) {
243             if (newVal && newVal != oldVal && $scope.opac_iframe) {
244                 $($scope.opac_iframe.dom.contentWindow).on('beforeunload', function(){
245                     return 'There is unsaved data in this record.'
246                 });
247             } else {
248                 if ($scope.opac_iframe)
249                     $($scope.opac_iframe.dom.contentWindow).off('beforeunload');
250             }
251         }
252     );
253
254     // Set the "last bib" cookie, if we have that
255     if ($scope.record_id)
256         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
257
258     // also set it when the iframe changes to a new record
259     $scope.handle_page = function(url) {
260
261         if (!url || url == 'about:blank') {
262             // nothing loaded.  If we already have a record ID, leave it.
263             return;
264         }
265
266         var match = url.match(/\/+opac\/+record\/+(\d+)/);
267         if (match) {
268             $scope.record_id = match[1];
269             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
270             init_parts_url();
271         } else {
272             delete $scope.record_id;
273             $scope.from_route = false;
274         }
275
276         // child scope is executing this function, so our digest doesn't fire ... thus,
277         $scope.$apply();
278
279         if (!$scope.in_opac_call) {
280             if ($scope.record_id) {
281                 $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
282                 tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
283             } else {
284                 tab = $routeParams.record_tab || 'catalog';
285             }
286             $scope.set_record_tab(tab);
287         } else {
288             $scope.in_opac_call = false;
289         }
290     }
291
292     // xulG catalog handlers
293     $scope.handlers = { }
294
295     // ------------------------------------------------------------------
296     // Holdings
297
298     $scope.holdingsGridControls = {};
299     $scope.holdingsGridDataProvider = egGridDataProvider.instance({
300         get : function(offset, count) {
301             return this.arrayNotifier(holdingsSvc.copies, offset, count);
302         }
303     });
304
305     // refresh the list of holdings when the filter lib is changed.
306     $scope.holdings_ou = egCore.org.get(egCore.auth.user().ws_ou());
307     $scope.holdings_ou_changed = function(org) {
308         $scope.holdings_ou = org;
309         holdingsSvc.fetch({
310             rid : $scope.record_id,
311             org : $scope.holdings_ou,
312             copy: $scope.holdings_show_copies,
313             vol : $scope.holdings_show_vols,
314             empty: $scope.holdings_show_empty
315         }).then(function() {
316             $scope.holdingsGridDataProvider.refresh();
317         });
318     }
319
320
321     // ------------------------------------------------------------------
322     // Holds 
323     var provider = egGridDataProvider.instance({});
324     $scope.hold_grid_data_provider = provider;
325     $scope.grid_actions = egHoldGridActions;
326     $scope.grid_actions.refresh = function () { provider.refresh() };
327     $scope.hold_grid_controls = {};
328
329     var hold_ids = []; // current list of holds
330     function fetchHolds(offset, count) {
331         var ids = hold_ids.slice(offset, offset + count);
332         return egHolds.fetch_holds(ids).then(null, null,
333             function(hold_data) { 
334                 return hold_data;
335             }
336         );
337     }
338
339     provider.get = function(offset, count) {
340         if ($scope.record_tab != 'holds') return $q.when();
341         var deferred = $q.defer();
342         hold_ids = []; // no caching ATM
343
344         // fetch the IDs
345         egCore.net.request(
346             'open-ils.circ',
347             'open-ils.circ.holds.retrieve_all_from_title',
348             egCore.auth.token(), $scope.record_id, 
349             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
350         ).then(
351             function(hold_data) {
352                 angular.forEach(hold_data, function(list, type) {
353                     hold_ids = hold_ids.concat(list);
354                 });
355                 fetchHolds(offset, count).then(
356                     deferred.resolve, null, deferred.notify);
357             }
358         );
359
360         return deferred.promise;
361     }
362
363     $scope.detail_view = function(action, user_data, items) {
364         if (h = items[0]) {
365             $scope.detail_hold_id = h.hold.id();
366         }
367     }
368
369     $scope.list_view = function(items) {
370          $scope.detail_hold_id = null;
371     }
372
373     // refresh the list of record holds when the pickup lib is changed.
374     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
375     $scope.pickup_ou_changed = function(org) {
376         $scope.pickup_ou = org;
377         provider.refresh();
378     }
379
380     $scope.print_holds = function() {
381         var holds = [];
382         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
383             holds.push({
384                 hold : egCore.idl.toHash(item.hold),
385                 patron_last : item.patron_last,
386                 patron_alias : item.patron_alias,
387                 patron_barcode : item.patron_barcode,
388                 copy : egCore.idl.toHash(item.copy),
389                 volume : egCore.idl.toHash(item.volume),
390                 title : item.mvr.title(),
391                 author : item.mvr.author()
392             });
393         });
394
395         egCore.print.print({
396             context : 'receipt', 
397             template : 'holds_for_bib', 
398             scope : {holds : holds}
399         });
400     }
401
402     $scope.mark_hold_transfer_dest = function() {
403         egCore.hatch.setLocalItem(
404             'eg.circ.hold.title_transfer_target', $scope.record_id);
405     }
406
407     // UI presents this option as "all holds"
408     $scope.transfer_holds_to_marked = function() {
409         var hold_ids = $scope.hold_grid_controls.allItems().map(
410             function(hold_data) {return hold_data.hold.id()});
411         egHolds.transfer_to_marked_title(hold_ids);
412     }
413
414     // ------------------------------------------------------------------
415     // Initialize the selected tab
416
417     function init_cat_url() {
418         // Set the initial catalog URL.  This only happens once.
419         // The URL is otherwise generated through user navigation.
420         if ($scope.catalog_url) return; 
421
422         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
423
424         // A record ID in the path indicates a request for the record-
425         // specific page.
426         if ($routeParams.record_id) {
427             url = url.replace(/advanced/, '/record/' + $scope.record_id);
428         }
429
430         $scope.catalog_url = url;
431     }
432
433     function init_parts_url() {
434         $scope.parts_url = $location
435             .absUrl()
436             .replace(
437                 /\/staff.*/,
438                 '/conify/global/biblio/monograph_part?r='+$scope.record_id
439             );
440     }
441
442     $scope.set_record_tab = function(tab) {
443         $scope.record_tab = tab;
444
445         switch(tab) {
446
447             case 'monoparts':
448                 init_parts_url();
449                 break;
450
451             case 'catalog':
452                 init_cat_url();
453                 break;
454
455             case 'holds':
456                 $scope.detail_hold_record_id = $scope.record_id; 
457                 // refresh the holds grid
458                 provider.refresh();
459                 break;
460         }
461     }
462
463     $scope.set_default_record_tab = function() {
464         egCore.hatch.setLocalItem(
465             'eg.cat.default_record_tab', $scope.record_tab);
466         $timeout(function(){$scope.default_tab = $scope.record_tab});
467     }
468
469     var tab;
470     if ($scope.record_id) {
471         $scope.default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
472         tab = $routeParams.record_tab || $scope.default_tab || 'catalog';
473
474
475         $timeout(function(){
476             holdingsSvc.fetch({
477                 rid : $scope.record_id,
478                 org : $scope.holdings_ou,
479                 copy: $scope.holdings_show_copies,
480                 vol : $scope.holdings_show_vols,
481                 empty: $scope.holdings_show_empty
482             }).then(function() {
483                 $scope.holdingsGridDataProvider.refresh();
484             });
485         });
486
487     } else {
488         tab = $routeParams.record_tab || 'catalog';
489     }
490     $scope.set_record_tab(tab);
491
492 }])
493
494 .controller('URLVerifyCtrl',
495        ['$scope','$location',
496 function($scope , $location) {
497     $scope.verifyurls_url = $location.absUrl().replace(/\/staff.*/, '/url_verify/sessions');
498 }])
499
500 .controller('VandelayCtrl',
501        ['$scope','$location',
502 function($scope , $location) {
503     $scope.vandelay_url = $location.absUrl().replace(/\/staff.*/, '/vandelay/vandelay');
504 }])
505
506 .controller('ManageAuthoritiesCtrl',
507        ['$scope','$location',
508 function($scope , $location) {
509     $scope.manageauthorities_url = $location.absUrl().replace(/\/staff.*/, '/cat/authority/list');
510 }])
511
512 .controller('BatchEditCtrl',
513        ['$scope','$location','$routeParams',
514 function($scope , $location , $routeParams) {
515     $scope.batchedit_url = $location.absUrl().replace(/\/eg.*/, '/opac/extras/merge_template');
516     if ($routeParams.container_type) {
517         switch ($routeParams.container_type) {
518             case 'bucket':
519                 $scope.batchedit_url += '?recordSource=b&containerid=' + $routeParams.container_id;
520                 break;
521             case 'record':
522                 $scope.batchedit_url += '?recordSource=r&recid=' + $routeParams.container_id;
523                 break;
524         };
525     }
526 }])
527
528  
529 .filter('boolText', function(){
530     return function (v) {
531         return v == 't';
532     }
533 })
534
535 .factory('holdingsSvc', 
536        ['egCore','$q',
537 function(egCore , $q) {
538
539     var service = {
540         copies : [], // record search results
541         index : 0, // search grid index
542         org : null,
543         rid : null
544     };
545
546     service.flesh = {   
547         flesh : 2, 
548         flesh_fields : {
549             acp : ['status','location'],
550             acn : ['prefix','suffix','copies']
551         }
552     }
553
554     // resolved with the last received copy
555     service.fetch = function(opts) {
556
557         var rid = opts.rid;
558         var org = opts.org;
559         var copy = opts.copy;
560         var vol = opts.vol;
561         var empty = opts.empty;
562
563         if (!rid) return $q.when();
564         if (!org) return $q.when();
565
566         service.rid = rid;
567         service.org = org;
568         service.copies = [];
569         service.index = 0;
570
571         var org_list = egCore.org.descendants(org.id(), true);
572
573         return egCore.pcrud.search(
574             'acn',
575             {record : rid, owning_lib : org_list, deleted : 'f'},
576             service.flesh
577         ).then(
578             function() { // finished
579                 service.copies = service.copies.sort(
580                     function (a, b) {
581                         function compare_array (x, y, i) {
582                             if (x[i] && y[i]) { // both have values
583                                 if (x[i] == y[i]) { // need to look deeper
584                                     return compare_array(x, y, ++i);
585                                 }
586
587                                 if (x[i] < y[i]) { // x is first
588                                     return -1;
589                                 } else if (x[i] > y[i]) { // y is first
590                                     return 1;
591                                 }
592
593                             } else { // no orgs to compare ...
594                                 if (x[i]) return -1;
595                                 if (y[i]) return 1;
596                             }
597                             return 0;
598                         }
599
600                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
601                         if (!owner_order) {
602                             // now compare on CN label
603                             if (a.call_number.label < b.call_number.label) return -1;
604                             if (a.call_number.label > b.call_number.label) return 1;
605
606                             // try copy number
607                             if (a.copy_number < b.copy_number) return -1;
608                             if (a.copy_number > b.copy_number) return 1;
609
610                             // finally, barcode
611                             if (a.barcode < b.barcode) return -1;
612                             if (a.barcode > b.barcode) return 1;
613                         }
614                         return owner_order;
615                     }
616                 );
617
618                 // create a label using just the unique part of the owner list
619                 var index = 0;
620                 var prev_owner_list;
621                 angular.forEach(service.copies, function (cp) {
622                     if (!prev_owner_list) {
623                         cp.owner_label = cp.owner_list.join(' ... ');
624                     } else {
625                         var current_owner_list = cp.owner_list.slice();
626                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
627                             current_owner_list.shift();
628                             prev_owner_list.shift();
629                         }
630                         cp.owner_label = current_owner_list.join(' ... ');
631                     }
632
633                     cp.index = index++;
634                     prev_owner_list = cp.owner_list.slice();
635                 });
636
637                 var new_list = service.copies;
638                 if (!copy || !vol) { // collapse copy rows, supply a count instead
639
640                     index = 0;
641                     var cp_list = [];
642                     var prev_key;
643                     var current_blob = {};
644                     angular.forEach(new_list, function (cp) {
645                         if (!prev_key) {
646                             prev_key = cp.owner_list.join('') + cp.call_number.label;
647                             if (cp.barcode) current_blob.copy_count = 1;
648                             current_blob.index = ++index;
649                             current_blob.call_number = cp.call_number;
650                             current_blob.owner_list = cp.owner_list;
651                             current_blob.owner_label = cp.owner_label;
652                         } else {
653                             var current_key = cp.owner_list.join('') + cp.call_number.label;
654                             if (prev_key == current_key) { // collapse into current_blob
655                                 current_blob.copy_count++;
656                             } else {
657                                 cp_list.push(current_blob);
658                                 prev_key = current_key;
659                                 current_blob = {};
660                                 if (cp.barcode) current_blob.copy_count = 1;
661                                 current_blob.index = ++index;
662                                 current_blob.owner_label = cp.owner_label;
663                                 current_blob.call_number = cp.call_number;
664                                 current_blob.owner_list = cp.owner_list;
665                             }
666                         }
667                     });
668
669                     cp_list.push(current_blob);
670                     new_list = cp_list;
671
672                     if (!vol) {
673
674                         index = 0;
675                         var cn_list = [];
676                         prev_key = '';
677                         var current_blob = {};
678                         angular.forEach(cp_list, function (cp) {
679                             if (!prev_key) {
680                                 prev_key = cp.owner_list.join('');
681                                 current_blob.index = ++index;
682                                 current_blob.cn_count = 1;
683                                 current_blob.copy_count = cp.copy_count;
684                                 current_blob.owner_list = cp.owner_list;
685                                 current_blob.owner_label = cp.owner_label;
686                             } else {
687                                 var current_key = cp.owner_list.join('');
688                                 if (prev_key == current_key) { // collapse into current_blob
689                                     current_blob.cn_count++;
690                                     current_blob.copy_count += cp.copy_count;
691                                 } else {
692                                     cn_list.push(current_blob);
693                                     prev_key = current_key;
694                                     current_blob = {};
695                                     current_blob.index = ++index;
696                                     current_blob.owner_label = cp.owner_label;
697                                     current_blob.cn_count = 1;
698                                     current_blob.copy_count = cp.copy_count;
699                                     current_blob.owner_list = cp.owner_list;
700                                 }
701                             }
702                         });
703     
704                         cn_list.push(current_blob);
705                         new_list = cn_list;
706     
707                     }
708                 }
709
710                 service.copies = new_list;
711
712             },
713
714             null, // error
715
716             // notify reads the stream of copies, one at a time.
717             function(cn) {
718
719                 var copies = cn.copies();
720                 cn.copies([]);
721
722                 angular.forEach(copies, function (cp) {
723                     cp.call_number(cn);
724                 });
725
726                 var flat = egCore.idl.toHash(copies);
727                 var owner = egCore.org.get(flat[0].call_number.owning_lib);
728
729                 var owner_name_list = [];
730                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
731                     owner_name_list.unshift(owner.name());
732                     owner = egCore.org.get(owner.parent_ou());
733                 }
734
735                 angular.forEach(flat, function (cp) {
736                     cp.owner_list = owner_name_list;
737                 });
738
739                 service.copies = service.copies.concat(flat);
740
741                 if (empty && flat.length == 0) {
742                     service.copies.push({
743                         owner_list : owner_name_list,
744                         call_number: egCore.idl.toHash(cn)
745                     });
746                 }
747
748                 return cn;
749             }
750         );
751     }
752
753     return service;
754 }])
755
756