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