5 angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
7 .directive("egContextMenuItem", ['$timeout',function ($timeout) {
11 template: '<li><a ng-click="setContent(item.value,item.action)">{{item.label}}</a></li>',
12 scope: { item: '=', content: '=' },
13 controller: ['$scope','$element',
14 function ($scope , $element) {
15 if (!$scope.item.label) $scope.item.label = $scope.item.value;
16 if ($scope.item.divider) {
17 $element.style.borderTop = 'solid 1px';
20 $scope.setContent = function (v, a) {
22 if (a) replace_with = a($scope,$element,$scope.item.value,$scope.$parent.$parent.content);
24 $scope.$parent.$parent.$apply(function(){
25 $scope.$parent.$parent.content = replace_with
28 $($element).parent().css({display: 'none'});
35 .directive("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
40 'style="font-family: \'Lucida Console\', Monaco, monospace;" '+
41 'ng-model="content" '+
42 'size="{{content.length * 1.1}}" '+
43 'maxlength="{{max}}" '+
52 contextItemContainer: '@',
53 contextItemGenerator: '=',
57 controller : ['$scope',
60 if ($scope.contextItemContainer && angular.isArray($scope.$parent[$scope.contextItemContainer]))
61 $scope.item_container = $scope.$parent[$scope.contextItemContainer];
62 else if ($scope.contextItemGenerator)
63 $scope.item_generator = $scope.contextItemGenerator;
65 $scope.showContext = function (event) {
66 $scope.item_list = [];
67 if ($scope.item_container) {
68 $scope.item_list = $scope.item_container;
69 } else if ($scope.item_generator) {
70 // always recalculate; tag and/or subfield
71 // codes may have changed
72 $scope.item_list = $scope.item_generator();
77 if (angular.isArray($scope.item_list) && $scope.item_list.length > 0) { // we have a list of values or transforms
78 console.log('Showing context menu...');
79 $('body').trigger('click');
82 '<ul class="dropdown-menu" role="menu" style="z-index: 2000;">'+
83 '<eg-context-menu-item ng-repeat="item in item_list" item="item" content="content"/>'+
86 var tnode = angular.element(tmpl);
87 $document.find('body').append(tnode);
96 var e = $compile(tnode)($scope);
100 $('body').on('click.context_menu',function() {
101 $(tnode).css('display','none');
102 $('body').off('click.context_menu');
113 link: function (scope, element, attrs) {
115 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
117 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
119 element.bind('contextmenu', scope.showContext);
124 .directive("egMarcEditFixedField", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
128 template: '<div class="col-md-2">'+
129 '<div class="col-md-1"><label name="{{fixedField}}" for="{{fixedField}}_ff_input">{{fixedField}}</label></div>'+
130 '<div class="col-md-1"><input type="text" style="padding-left: 5px; margin-left: 1em" size="4" id="{{fixedField}}_ff_input"/></div>'+
132 scope: { record: "=", fixedField: "@" },
134 controller : ['$scope', '$element', 'egTagTable',
135 function ( $scope , $element , egTagTable) {
136 $($element).children().css({ display : 'none' });
138 $scope.content = null; // this is where context menus dump their values
139 $scope.item_container = [];
140 $scope.in_handler = false;
141 $scope.ready = false;
143 $scope.$watch('content', function (newVal, oldVal) {
144 var input = $($element).find('input');
146 input.trigger('keyup'); // cascade the update
149 $scope.$watch('record.ready', function (newVal, oldVal) { // wait for the record to be loaded
150 if (newVal && !$scope.ready) {
151 $scope.rtype = $scope.record.recordType();
153 egTagTable.fetchFFPosTable( $scope.rtype ).then(function (ff_list) {
154 angular.forEach(ff_list, function (ff) {
156 if (ff.fixed_field == $scope.fixedField && ff.rec_type == $scope.rtype) {
159 $($element).children().css({ display : 'inline' });
161 var input = $($element).find('input');
162 input.attr('maxlength', $scope.me.length);
163 input.val($scope.record.extractFixedField($scope.me.fixed_field));
164 input.on('keyup', function(e) {
165 $scope.in_handler = true;
166 $scope.record.setFixedField($scope.me.fixed_field, input.val());
167 try { $scope.$parent.$digest(); } catch(e) {};
173 }).then(function (me) {
177 return $scope.record.extractFixedField($scope.fixedField);
179 function (newVal, oldVal) {
180 if ($scope.in_handler) {
181 $scope.in_handler = false;
182 } else if (oldVal != newVal) {
183 $($element).find('input').val(newVal);
188 }).then(function () {
189 return egTagTable.fetchFFValueTable( $scope.rtype );
190 }).then(function (vlist) {
191 if (vlist[$scope.fixedField]) {
192 vlist[$scope.fixedField].forEach(function (v) {
193 if (v[0].length <= v[2]) {
194 $scope.item_container.push({ value : v[0], label : v[0] + ': ' + v[1] });
198 }).then(function () {
199 if ($scope.item_container && $scope.item_container.length)
200 $($element).bind('contextmenu', $scope.showContext);
206 $scope.showContext = function (event) {
207 if ($scope.context_menu_element) {
208 console.log('Reshowing context menu...');
209 $('body').trigger('click');
210 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
211 $('body').on('click.context_menu',function() {
212 $($scope.context_menu_element).css('display','none');
213 $('body').off('click.context_menu');
218 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
219 console.log('Showing context menu...');
220 $('body').trigger('click');
223 '<ul class="dropdown-menu" role="menu" style="z-index: 2000;">'+
224 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
227 var tnode = angular.element(tmpl);
228 $document.find('body').append(tnode);
236 $scope.context_menu_element = tnode;
238 $timeout(function() {
239 var e = $compile(tnode)($scope);
243 $('body').on('click.context_menu',function() {
244 $(tnode).css('display','none');
245 $('body').off('click.context_menu');
259 .directive("egMarcEditSubfield", function () {
264 '<span><label class="marcedit marcsfcodedelimiter"'+
265 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
266 '>‡</label><eg-marc-edit-editable '+
268 'class="marcedit marcsf marcsfcode" '+
270 'subfield="subfield" '+
271 'content="subfield[0]" '+
273 'on-keydown="onKeydown" '+
274 'context-item-generator="sf_code_options" '+
275 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
277 '<span><eg-marc-edit-editable '+
279 'class="marcedit marcsf marcsfvalue" '+
281 'subfield="subfield" '+
282 'content="subfield[1]" '+
283 'on-keydown="onKeydown" '+
284 'context-item-generator="sf_val_options" '+
285 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
288 scope: { field: "=", subfield: "=", onKeydown: '=' },
290 controller : ['$scope', 'egTagTable',
291 function ( $scope , egTagTable) {
293 $scope.sf_code_options = function () {
294 return egTagTable.getSubfieldCodes($scope.field.tag);
296 $scope.sf_val_options = function () {
297 return egTagTable.getSubfieldValues($scope.field.tag, $scope.subfield[0]);
304 .directive("egMarcEditInd", function () {
308 template: '<span><eg-marc-edit-editable '+
310 'class="marcedit marcind" '+
314 'on-keydown="onKeydown" '+
315 'context-item-generator="ind_val_options" '+
316 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
318 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
320 controller : ['$scope', 'egTagTable',
321 function ( $scope , egTagTable) {
323 $scope.ind_val_options = function () {
324 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
331 .directive("egMarcEditTag", function () {
335 template: '<span><eg-marc-edit-editable '+
337 'class="marcedit marctag" '+
341 'on-keydown="onKeydown" '+
342 'context-item-generator="tag_options" '+
343 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
345 scope: { tag : '=', field: '=', onKeydown: '=' },
347 controller : ['$scope', 'egTagTable',
348 function ( $scope , egTagTable) {
350 $scope.tag_options = function () {
351 return egTagTable.getFieldTags();
358 .directive("egMarcEditDatafield", function () {
363 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
364 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
365 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
366 '<span><eg-marc-edit-subfield ng-class="{ \'unvalidatedheading\' : field.heading_checked && !field.heading_valid}" ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
367 // FIXME: template should probably be moved to file to improve
369 '<button class="btn btn-info btn-xs" '+
370 'aria-label="Manage authority record links" '+
371 'ng-show="isAuthorityControlled(field)"'+
372 'ng-click="spawnAuthorityLinker()"'+
374 '<span class="glyphicon glyphicon-link"></span>'+
376 '<span ng-show="field.heading_checked && field.heading_valid" class="glyphicon glyphicon-ok-sign"></span>'+
377 '<span ng-show="field.heading_checked && !field.heading_valid" class="glyphicon glyphicon-question-sign"></span>'+
379 scope: { field: "=", onKeydown: '=' },
381 controller : ['$scope','$modal',
382 function ( $scope, $modal ) {
383 $scope.isAuthorityControlled = function () {
384 return ($scope.$parent.$parent.record_type == 'bre') &&
385 $scope.$parent.$parent.controlSet.bibFieldByTag($scope.field.tag);
387 $scope.spawnAuthorityLinker = function() {
388 // intentionally making a clone in case
389 // user decides to abandon the linking
390 var fieldCopy = new MARC21.Field({
391 tag : $scope.field.tag,
392 ind1 : $scope.field.ind1,
393 ind2 : $scope.field.ind2
395 angular.forEach($scope.field.subfields, function(sf) {
396 fieldCopy.subfields.push(sf.slice(0));
398 var cs = $scope.$parent.$parent.controlSet;
399 var args = { changed : false };
401 templateUrl: './cat/share/t_authority_link_dialog',
403 controller: ['$scope', '$modalInstance', function($scope, $modalInstance) {
404 $scope.controlSet = cs;
405 $scope.bibField = fieldCopy;
406 $scope.focusMe = true;
408 $scope.ok = function(args) { $modalInstance.close(args) };
409 $scope.cancel = function () { $modalInstance.dismiss() };
411 }).result.then(function (args) {
413 $scope.field.subfields.length = 0;
414 angular.forEach(fieldCopy.subfields, function(sf) {
415 $scope.field.addSubfields(sf[0], sf[1]);
425 .directive("egMarcEditControlfield", function () {
430 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
431 '<span><eg-marc-edit-editable '+
434 'class="marcedit marcdata" '+
435 'content="field.data" '+
436 'on-keydown="onKeydown" '+
437 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
439 // TODO: move to TT2 template
440 '<button class="btn btn-info btn-xs" '+
441 'aria-label="Physical Characteristics Wizard" '+
442 'ng-show="showPhysCharLink()"'+
443 'ng-click="spawnPhysCharWizard()"'+
445 '<span class="glyphicon glyphicon-link"></span>'+
448 scope: { field: "=", onKeydown: '=' },
449 controller : ['$scope','$modal',
450 function ( $scope, $modal) {
451 $scope.showPhysCharLink = function () {
452 return ($scope.$parent.$parent.record_type == 'bre')
453 && $scope.field.tag == '007';
455 $scope.spawnPhysCharWizard = function() {
458 field : $scope.field,
459 orig_value : $scope.field.data
462 templateUrl: './cat/share/t_physchar_dialog',
463 controller: ['$scope','$modalInstance',
464 function( $scope , $modalInstance) {
465 $scope.focusMe = true;
467 $scope.ok = function(args) { $modalInstance.close(args) };
468 $scope.cancel = function () {
469 $modalInstance.dismiss();
470 args.field.data = args.orig_value;
473 }).result.then(function (args) {
474 // $scope.field.data is changed within the
475 // wizard. Nothing left to do on submit.
484 .directive("egMarcEditLeader", function () {
489 '<span><eg-marc-edit-editable '+
490 'class="marcedit marctag" '+
492 'on-keydown="onKeydown" '+
494 'disabled="disabled"'+
496 '<span><eg-marc-edit-editable '+
497 'class="marcedit marcdata" '+
499 'max="{{record.leader.length}}" '+
500 'content="record.leader" '+
501 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
502 'on-keydown="onKeydown"'+
505 controller : ['$scope',
506 function ( $scope ) {
510 scope: { record: "=", onKeydown: '=' }
514 /// TODO: fixed field editor and such
515 .directive("egMarcEditRecord", function () {
517 templateUrl : './cat/share/t_marcedit',
525 // in-place mode means that the editor is being
526 // used just to munge some MARCXML client-side, rather
527 // than to (immediately) update the database
532 link: function (scope, element, attrs) {
534 element.bind('click', function(e) {;
535 scope.current_event_target = $(e.target).attr('id');
536 if (scope.current_event_target) {
537 console.log('Recording click event on ' + scope.current_event_target);
538 scope.current_event_target_cursor_pos =
539 e.target.selectionDirection=='backward' ?
540 e.target.selectionStart :
541 e.target.selectionEnd;
546 controller : ['$timeout','$scope','$q','$window','egCore', 'egTagTable',
547 function ( $timeout , $scope , $q, $window , egCore , egTagTable ) {
550 $scope.onSaveCallback = $scope.onSave;
551 if (typeof $scope.onSaveCallback !== 'undefined' && !angular.isArray($scope.onSaveCallback))
552 $scope.onSaveCallback = [ $scope.onSaveCallback ];
554 MARC21.Record.delimiter = '$';
556 $scope.enable_fast_add = false;
557 $scope.fast_item_callnumber = '';
558 $scope.fast_item_barcode = '';
559 $scope.flatEditor = false;
560 $scope.brandNewRecord = false;
561 $scope.bib_source = null;
562 $scope.record_type = $scope.recordType || 'bre';
563 $scope.max_undo = $scope.maxUndo || 100;
564 $scope.record_undo_stack = [];
565 $scope.record_redo_stack = [];
566 $scope.in_undo = false;
567 $scope.in_redo = false;
568 $scope.record = new MARC21.Record();
569 $scope.save_stack_depth = 0;
570 $scope.controlfields = [];
571 $scope.datafields = [];
572 $scope.controlSet = egTagTable.getAuthorityControlSet();
574 egTagTable.loadTagTable({ marcRecordType : $scope.record_type });
576 $scope.saveFlatTextMARC = function () {
577 $scope.record = new MARC21.Record({ marcbreaker : $scope.flat_text_marc });
580 $scope.refreshVisual = function () {
581 if (!$scope.flatEditor) {
582 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
583 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
587 $scope.onKeydown = function (event) {
588 var event_return = true;
591 'keydown: which='+event.which+
592 ', ctrlKey='+event.ctrlKey+
593 ', shiftKey='+event.shiftKey+
594 ', altKey='+event.altKey+
595 ', metaKey='+event.altKey
598 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
599 event_return = $scope.processRedo();
600 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
601 event_return = $scope.processUndo();
602 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
604 var element = $(event.target);
605 var new_sf, index_sf, move_data;
607 if (element.hasClass('marcsfvalue')) {
608 index_sf = event.data.scope.subfield[2];
609 new_sf = index_sf + 1;
611 var start = event.target.selectionStart;
612 var end = event.target.selectionEnd - event.target.selectionStart ?
613 event.target.selectionEnd :
614 event.target.value.length;
616 move_data = event.target.value.substring(start,end);
618 } else if (element.hasClass('marcsfcode')) {
619 index_sf = event.data.scope.subfield[2];
620 new_sf = index_sf + 1;
621 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
626 $scope.current_event_target = 'r' + $scope.recordId +
627 'f' + event.data.scope.field.position +
628 's' + new_sf + 'code';
630 event.data.scope.field.subfields.forEach(function(sf) {
631 if (sf[2] >= new_sf) sf[2]++;
632 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
634 event.data.scope.field.subfields.splice(
637 [' ', move_data, new_sf ]
640 $scope.current_event_target_cursor_pos = 0;
641 $scope.current_event_target_cursor_pos_end = 1;
643 $timeout(function(){$scope.$digest()}).then(setCaret);
645 event_return = false;
647 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
648 event.data.scope.field.record.insertOrderedFields(
655 $scope.force_render = true;
656 $timeout(function(){$scope.$digest()}).then(setCaret);
658 event_return = false;
660 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
661 event.data.scope.field.record.insertOrderedFields(
668 $scope.force_render = true;
669 $timeout(function(){$scope.$digest()}).then(setCaret);
671 event_return = false;
673 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
674 var new_008_data = event.data.scope.field.record.generate008();
677 var old_008s = event.data.scope.field.record.field('008',true);
678 old_008s.forEach(function(o) {
679 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
680 domnode.scope().$destroy();
682 event.data.scope.field.record.deleteFields(o);
685 event.data.scope.field.record.insertOrderedFields(
692 $scope.force_render = true;
693 $timeout(function(){$scope.$digest()}).then(setCaret);
695 event_return = false;
697 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
699 var element = $(event.target);
701 var index_field = event.data.scope.field.position;
702 var new_field = index_field + 1;
704 event.data.scope.field.record.insertFieldsAfter(
705 event.data.scope.field,
708 subfields : [[' ','',0]]
712 $scope.current_event_target = 'r' + $scope.recordId +
713 'f' + new_field + 'tag';
715 $scope.current_event_target_cursor_pos = 0;
716 $scope.current_event_target_cursor_pos_end = 3;
717 $scope.force_render = true;
719 $timeout(function(){$scope.$digest()}).then(setCaret);
721 event_return = false;
723 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
725 var del_field = event.data.scope.field.position;
727 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
729 event.data.scope.field.record.deleteFields(
730 event.data.scope.field
733 domnode.scope().$destroy();
736 $scope.current_event_target = 'r' + $scope.recordId +
737 'f' + del_field + 'tag';
739 $scope.current_event_target_cursor_pos = 0;
740 $scope.current_event_target_cursor_pos_end = 0
741 $scope.force_render = true;
743 $timeout(function(){$scope.$digest()}).then(setCaret);
745 event_return = false;
747 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
749 var sf = event.data.scope.subfield[2] - 1;
750 if (sf == -1) sf = 0;
752 event.data.scope.field.deleteExactSubfields(
753 event.data.scope.subfield
756 if (!event.data.scope.field.subfields[sf]) {
757 $scope.current_event_target = 'r' + $scope.recordId +
758 'f' + event.data.scope.field.position +
761 $scope.current_event_target = 'r' + $scope.recordId +
762 'f' + event.data.scope.field.position +
766 $scope.current_event_target_cursor_pos = 0;
767 $scope.current_event_target_cursor_pos_end = 0;
768 $scope.force_render = true;
770 $timeout(function(){$scope.$digest()}).then(setCaret);
772 event_return = false;
774 } else if (event.keyCode == 38) {
775 if (event.ctrlKey) { // copy the field up
776 var index_field = event.data.scope.field.position;
779 if (event.data.scope.field.isControlfield()) {
780 field_obj = new MARC21.Field({
781 tag : event.data.scope.field.tag,
782 data : event.data.scope.field.data
786 for (var i in event.data.scope.field.subfields) {
787 sf_clone.push(event.data.scope.field.subfields[i].slice());
789 field_obj = new MARC21.Field({
790 tag : event.data.scope.field.tag,
791 ind1 : event.data.scope.field.ind1,
792 ind2 : event.data.scope.field.ind2,
798 event.data.scope.field.record.insertFieldsBefore(
799 event.data.scope.field,
803 $scope.current_event_target = 'r' + $scope.recordId +
804 'f' + index_field + 'tag';
806 $scope.current_event_target_cursor_pos = 0;
807 $scope.current_event_target_cursor_pos_end = 3;
808 $scope.force_render = true;
810 $timeout(function(){$scope.$digest()}).then(setCaret);
812 } else { // jump to prev field
813 if (event.data.scope.field.position > 0) {
815 $scope.current_event_target_cursor_pos = 0;
816 $scope.current_event_target_cursor_pos_end = 0;
817 $scope.current_event_target = 'r' + $scope.recordId +
818 'f' + (event.data.scope.field.position - 1) +
824 event_return = false;
826 } else if (event.keyCode == 40) { // down arrow...
827 if (event.ctrlKey) { // copy the field down
829 var index_field = event.data.scope.field.position;
830 var new_field = index_field + 1;
833 if (event.data.scope.field.isControlfield()) {
834 field_obj = new MARC21.Field({
835 tag : event.data.scope.field.tag,
836 data : event.data.scope.field.data
840 for (var i in event.data.scope.field.subfields) {
841 sf_clone.push(event.data.scope.field.subfields[i].slice());
843 field_obj = new MARC21.Field({
844 tag : event.data.scope.field.tag,
845 ind1 : event.data.scope.field.ind1,
846 ind2 : event.data.scope.field.ind2,
851 event.data.scope.field.record.insertFieldsAfter(
852 event.data.scope.field,
856 $scope.current_event_target = 'r' + $scope.recordId +
857 'f' + new_field + 'tag';
859 $scope.current_event_target_cursor_pos = 0;
860 $scope.current_event_target_cursor_pos_end = 3;
861 $scope.force_render = true;
863 $timeout(function(){$scope.$digest()}).then(setCaret);
865 } else { // jump to next field
866 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
868 $scope.current_event_target_cursor_pos = 0;
869 $scope.current_event_target_cursor_pos_end = 0;
870 $scope.current_event_target = 'r' + $scope.recordId +
871 'f' + (event.data.scope.field.position + 1) +
877 event_return = false;
879 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
880 $scope.current_event_target = $(event.target).attr('id');
881 if ($scope.current_event_target) {
882 $scope.current_event_target_cursor_pos =
883 event.target.selectionDirection=='backward' ?
884 event.target.selectionStart :
885 event.target.selectionEnd;
892 function setCaret() {
893 if ($scope.current_event_target) {
894 console.log("Putting caret in " + $scope.current_event_target);
895 if (!$scope.current_event_target_cursor_pos_end)
896 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
898 var element = $('#'+$scope.current_event_target).get(0);
901 if (element.setSelectionRange) {
902 element.setSelectionRange(
903 $scope.current_event_target_cursor_pos,
904 $scope.current_event_target_cursor_pos_end
907 $scope.current_event_cursor_pos_end = null;
908 $scope.current_event_target = null;
913 function loadRecord() {
915 var deferred = $q.defer();
916 if ($scope.recordId) {
917 egCore.pcrud.retrieve(
918 $scope.record_type, $scope.recordId
919 ).then(function(rec) {
920 deferred.resolve(rec);
923 if ($scope.recordType == 'bre') {
924 var bre = new egCore.idl.bre();
925 bre.marc($scope.marcXml);
926 deferred.resolve(bre);
927 } else if ($scope.recordType == 'are') {
928 var are = new egCore.idl.are();
929 are.marc($scope.marcXml);
930 deferred.resolve(are);
932 $scope.brandNewRecord = true;
934 return deferred.promise;
935 })().then(function(rec) {
936 $scope.in_redo = true;
937 $scope[$scope.record_type] = rec;
938 $scope.record = new MARC21.Record({ marcxml : $scope.Record().marc() });
939 $scope.calculated_record_type = $scope.record.recordType();
940 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
941 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
942 $scope.save_stack_depth = $scope.record_undo_stack.length;
943 $scope.flat_text_marc = $scope.record.toBreaker();
945 if ($scope.record_type == 'bre') {
946 $scope.bib_source = $scope.Record().source();
950 return egTagTable.fetchFFPosTable($scope.calculated_record_type)
952 return egTagTable.fetchFFValueTable($scope.calculated_record_type)
956 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
957 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
958 $scope.record_undo_stack.push({
960 target: $scope.current_event_target,
961 pos: $scope.current_event_target_cursor_pos
964 if ($scope.force_render) {
965 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
966 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
967 $scope.force_render = false;
970 $scope.flat_text_marc = newVal;
973 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
974 $scope.dirtyFlag = true;
976 $scope.dirtyFlag = false;
979 if ($scope.record_undo_stack.length > $scope.max_undo)
980 $scope.record_undo_stack.shift();
982 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
983 $scope.in_redo = false;
984 $scope.in_undo = false;
987 $scope.processUndo = function () {
988 if ($scope.record_undo_stack.length) {
989 $scope.in_undo = true;
991 var undo_item = $scope.record_undo_stack.pop();
992 $scope.record_redo_stack.push(undo_item);
994 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
995 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
996 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
998 $scope.current_event_target = undo_item.target;
999 $scope.current_event_target_cursor_pos = undo_item.pos;
1000 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1002 $timeout(function(){$scope.$digest()}).then(setCaret);
1009 $scope.processRedo = function () {
1010 if ($scope.record_redo_stack.length) {
1011 $scope.in_redo = true;
1013 var redo_item = $scope.record_redo_stack.pop();
1014 $scope.record_undo_stack.push(redo_item);
1016 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
1017 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1018 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1020 $scope.current_event_target = redo_item.target;
1021 $scope.current_event_target_cursor_pos = redo_item.pos;
1022 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1024 $timeout(function(){$scope.$digest()}).then(setCaret);
1031 $scope.Record = function () {
1032 return $scope[$scope.record_type];
1035 $scope.deleteRecord = function () {
1036 $scope.Record().deleted(true);
1037 return $scope.saveRecord();
1040 $scope.undeleteRecord = function () {
1041 $scope.Record().deleted(false);
1042 return $scope.saveRecord();
1045 $scope.validateHeadings = function () {
1046 if ($scope.record_type != 'bre') return;
1047 var chain = $q.when();
1048 angular.forEach($scope.record.fields, function(f) {
1049 if (!$scope.controlSet.bibFieldByTag(f.tag)) return;
1050 // if heading already has a $0, assume it's good
1051 if (f.subfield('0', true).length) {
1052 f.heading_checked = true;
1053 f.heading_valid = true;
1056 var auth_match = $scope.controlSet.bibToAuthorities(f);
1057 chain = chain.then(function() {
1058 var promise = egCore.net.request(
1060 'open-ils.search.authority.simple_heading.from_xml.batch.atomic',
1062 ).then(function (matches) {
1063 f.heading_valid = false;
1064 if (matches[0]) { // probably set
1065 for (var cset in matches[0]) {
1066 var arr = matches[0][cset];
1068 // protect against errant empty string values
1069 if (arr.length == 1 && arr[0] == '')
1071 f.heading_valid = true;
1076 f.heading_checked = true;
1083 processOnSaveCallbacks = function() {
1084 var deferred = $q.defer();
1085 if (typeof $scope.onSaveCallback !== 'undefined') {
1086 var promise = deferred.promise;
1088 angular.forEach($scope.onSaveCallback, function (f) {
1089 if (angular.isFunction(f)) promise = promise.then(f);
1093 return deferred.resolve($scope.recordId)
1096 $scope.saveRecord = function () {
1097 if ($scope.inPlaceMode) {
1098 $scope.marcXml = $scope.record.toXmlString();
1099 return processOnSaveCallbacks();
1101 $scope.mangle_005();
1102 $scope.Record().editor(egCore.auth.user().id());
1103 $scope.Record().edit_date('now');
1104 $scope.record.pruneEmptyFieldsAndSubfields();
1105 $scope.Record().marc($scope.record.toXmlString());
1106 if ($scope.recordId) {
1107 return egCore.pcrud.update(
1110 if ($scope.enable_fast_add) {
1113 'open-ils.actor.anon_cache.set_value',
1114 null, 'edit-these-copies', {
1115 record_id: $scope.recordId,
1117 label : $scope.fast_item_callnumber,
1118 barcode : $scope.fast_item_barcode,
1123 ).then(function(key) {
1125 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1126 $timeout(function() { $window.open(url, '_blank') });
1128 alert('Could not create anonymous cache key!');
1132 }).then(loadRecord).then(processOnSaveCallbacks);
1134 $scope.Record().creator(egCore.auth.user().id());
1135 $scope.Record().create_date('now');
1136 return egCore.pcrud.create(
1138 ).then(function(bre) {
1139 $scope.recordId = bre.id();
1140 if ($scope.enable_fast_add) {
1143 'open-ils.actor.anon_cache.set_value',
1144 null, 'edit-these-copies', {
1145 record_id: $scope.recordId,
1147 label : $scope.fast_item_callnumber,
1148 barcode : $scope.fast_item_barcode,
1153 ).then(function(key) {
1155 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1156 $timeout(function() { $window.open(url, '_blank') });
1158 alert('Could not create anonymous cache key!');
1162 }).then(loadRecord).then(processOnSaveCallbacks);
1168 $scope.seeBreaker = function () {
1169 alert($scope.record.toBreaker());
1172 $scope.markConjoined = function () {
1173 egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.recordId);
1176 $scope.markVolTransfer = function () {
1177 egCore.hatch.setLocalItem('eg.cat.marked_volume_transfer_record',$scope.recordId);
1180 $scope.markOverlay = function () {
1181 egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.recordId);
1184 $scope.$watch('recordId',
1185 function(newVal, oldVal) {
1186 if (newVal && newVal !== oldVal) {
1191 $scope.$watch('marcXml',
1192 function(newVal, oldVal) {
1193 if (newVal && newVal !== oldVal) {
1199 var unregister = $scope.$watch(function() {
1200 return egTagTable.initialized();
1204 if ($scope.recordId || $scope.marcXml) {
1210 $scope.mangle_005 = function () {
1211 var now = new Date();
1212 var y = now.getUTCFullYear();
1214 var m = now.getUTCMonth() + 1;
1215 if (m < 10) m = '0' + m;
1217 var d = now.getUTCDate();
1218 if (d < 10) d = '0' + d;
1220 var H = now.getUTCHours();
1221 if (H < 10) H = '0' + H;
1223 var M = now.getUTCMinutes();
1224 if (M < 10) M = '0' + M;
1226 var S = now.getUTCSeconds();
1227 if (S < 10) S = '0' + S;
1229 var stamp = '' + y + m + d + H + M + S + '.0';
1230 var f = $scope.record.field('005',true)[0];
1234 $scope.record.insertOrderedFields(
1249 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
1253 template: '<span class="nullable">'+
1254 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
1255 '<option value="">Select a Source</option>'+
1258 controller: ['$scope','egCore',
1259 function ($scope , egCore) {
1261 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
1262 .then(function(list) { $scope.bib_sources = list; });
1264 $scope.$watch('bib_source',
1265 function(newVal, oldVal) {
1266 if (newVal !== oldVal) {
1267 $scope.bre.source(newVal);
1277 .directive("egMarcEditAuthorityLinker", function () {
1281 templateUrl: './cat/share/t_authority_linker',
1287 controller: ['$scope','$modal','egCore','egAuth',
1288 function ($scope , $modal, egCore, egAuth) {
1290 $scope.searchStr = '';
1291 var cni = egCore.env.aous['cat.marc_control_number_identifier'] ||
1292 'Set cat.marc_control_number_identifier in Library Settings';
1294 var axis_list = $scope.controlSet.bibFieldBrowseAxes($scope.bibField.tag);
1295 $scope.axis = axis_list[0];
1297 $scope._controlled_sf_list = {};
1298 $scope._controlled_auth_sf_list = {};
1300 angular.forEach($scope.controlSet.controlSetList(), function(acs_id) {
1301 if ($scope.controlSet.controlSet(acs_id).control_map[$scope.bibField.tag])
1302 found_acs.push(acs_id);
1304 if (found_acs.length) {
1305 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag],
1306 function(value, sf_label) {
1307 $scope._controlled_sf_list[ sf_label ] = 1;
1308 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag][sf_label],
1309 function(auth_sf, auth_tag) {
1310 if (!$scope._controlled_auth_sf_list[auth_tag]) {
1311 $scope._controlled_auth_sf_list[auth_tag] = { };
1313 $scope._controlled_auth_sf_list[auth_tag][auth_sf] = 1;
1320 $scope.bibField.subfields.forEach(function (sf) {
1321 if (sf[0] in $scope._controlled_sf_list) {
1323 sf.selectable = true;
1325 sf.selectable = false;
1328 $scope.summarizeField = function() {
1330 'tag': $scope.bibField.tag,
1331 'ind1': $scope.bibField.ind1,
1332 'ind2': $scope.bibField.ind2,
1335 $scope.bibField.subfields.forEach(function(sf) {
1337 source_f.subfields.push([ sf[0], sf[1] ]);
1342 $scope.getSearchString = function() {
1343 var source_f = $scope.summarizeField();
1345 angular.forEach(source_f.subfields, function(val) {
1346 values.push(val[1]);
1348 return values.join(' ');
1350 $scope.searchStr = $scope.getSearchString();
1351 $scope.$watch(function() {
1353 angular.forEach($scope.bibField.subfields, function(sf) {
1354 if (sf.selected) ct++
1358 function(newVal, oldVal) {
1359 $scope.searchStr = $scope.getSearchString();
1362 $scope.updateSubfieldZero = function(value) {
1363 $scope.changed = true;
1364 $scope.bibField.deleteSubfield({ code : ['0'] });
1365 $scope.bibField.subfields.push([
1366 '0', '(' + cni + ')' + value
1370 $scope.applyHeading = function(headingField) {
1371 // TODO: move the MARC21 rules for copying indicators
1373 if (headingField.tag == '130' && $scope.bibField.tag == '130') {
1374 $scope.bibField.ind1 = headingField.ind2;
1376 $scope.bibField.ind1 = headingField.ind1;
1378 // deal with 4xx and 5xx
1379 var authFallbackTag = '1' + headingField.tag.substr(1, 2);
1380 var _valid_auth_sfs = (headingField.tag in $scope._controlled_auth_sf_list) ?
1381 $scope._controlled_auth_sf_list[headingField.tag] :
1382 (authFallbackTag in $scope._controlled_auth_sf_list) ?
1383 $scope._controlled_auth_sf_list[authFallbackTag] :
1385 // save the $0 for later use
1387 if (headingField.subfield('0')) {
1388 sfZero = headingField.subfield('0')[1];
1390 // grab any bib subfields not under authority control
1391 // TODO do something about uncontrolled subdivisions
1392 var uncontrolledBibSf = [];
1393 angular.forEach($scope.bibField.subfields, function(sf) {
1394 if (!(sf[0] in $scope._controlled_sf_list) && (sf[0] != '0')) {
1395 uncontrolledBibSf.push([ sf[0], sf[1] ]);
1398 // grab the authority subfields
1399 var authoritySf = [];
1400 angular.forEach(headingField.subfields, function(sf) {
1401 if (sf[0] in _valid_auth_sfs) {
1402 authoritySf.push([ sf[0], sf[1] ]);
1405 $scope.bibField.subfields.length = 0;
1406 angular.forEach(authoritySf, function(sf) {
1407 $scope.bibField.addSubfields(sf[0], sf[1]);
1409 angular.forEach(uncontrolledBibSf, function(sf) {
1410 $scope.bibField.addSubfields(sf[0], sf[1]);
1413 $scope.bibField.addSubfields('0', sfZero);
1415 $scope.bibField.subfields.forEach(function (sf) {
1416 if (sf[0] in $scope._controlled_sf_list) {
1417 // intentionally not selecting any subfields
1418 // after we've applied an authority heading
1419 sf.selected = false;
1420 sf.selectable = true;
1422 sf.selectable = false;
1425 $scope.changed = true;
1428 $scope.createAuthorityFromBib = function(spawn_editor) {
1429 var source_f = $scope.summarizeField();
1431 var args = { authority_id : 0 };
1432 var method = (spawn_editor) ?
1433 'open-ils.cat.authority.record.create_from_bib.readonly' :
1434 'open-ils.cat.authority.record.create_from_bib';
1441 ).then(function(newAuthority) {
1444 templateUrl: './cat/share/t_edit_new_authority',
1447 ['$scope', '$modalInstance', function($scope, $modalInstance) {
1448 $scope.focusMe = true;
1450 $scope.dirty_flag = false;
1451 $scope.marc_xml = newAuthority,
1452 $scope.ok = function(args) { $modalInstance.close(args) }
1453 $scope.cancel = function () { $modalInstance.dismiss() }
1455 }).result.then(function (args) {
1456 if (!args || !args.authority_id) return;
1457 $scope.updateSubfieldZero(args.authority_id);
1460 $scope.updateSubfieldZero(newAuthority.id());
1470 .directive("egPhyscharWizard", function () {
1474 templateUrl: './cat/share/t_physchar_wizard',
1478 controller: ['$scope','$q','egTagTable',
1479 function ($scope , $q , egTagTable) {
1481 // $scope.step is the 1-based position in the list of
1482 // subfields for the currently selected type.
1483 // step==0 means we are currently selecting the type
1486 if (!$scope.field.data)
1487 $scope.field.data = '';
1489 // currently selected subfield value selector option
1490 $scope.selected_option = null;
1492 function current_ptype() {
1493 return $scope.field.data.substr(0, 1);
1496 function current_subfield() {
1497 return egTagTable.getPhysCharSubfieldMap(current_ptype())
1498 .then(function(sf_list) {return sf_list[$scope.step-1]});
1501 $scope.values_for_step = [];
1502 function set_values_for_step() {
1505 if ($scope.step == 0) {
1506 promise = egTagTable.getPhysCharTypeMap();
1508 promise = current_subfield().then(
1509 function(subfield) {
1511 .getPhysCharValueMap(subfield.id());
1516 return promise.then(function(list) {
1517 $scope.values_for_step = list;
1518 set_selected_option_from_field();
1519 set_label_for_step();
1523 $scope.change_ptype = function(option) {
1524 $scope.selected_option = option;
1525 var new_val = option.ptype_key();
1526 if (current_ptype() != new_val) {
1527 $scope.field.data = new_val; // total reset
1531 $scope.change_option = function(option) {
1532 $scope.selected_option = option;
1533 var new_val = option.value();
1534 get_step_slot().then(function(slot) {
1535 var value = $scope.field.data;
1536 while (value.length < (slot[0] + slot[1]))
1538 var before = value.substr(0, slot[0]);
1539 var after = value.substr(slot[0] + slot[1]);
1541 before + new_val.substr(0, slot[1]) + after;
1545 function get_step_slot() {
1546 if ($scope.step == 0) return $q.when([0, 1]);
1547 return current_subfield().then(function(sf) {
1548 return [sf.start_pos(), sf.length()]
1552 $scope.is_last_step = function() {
1553 // This one is called w/ every digest, so avoid async
1554 // calls. Wait until we have loaded the current ptype
1555 // subfields to determine if this is the last step.
1558 egTagTable.phys_char_sf_map[current_ptype()] &&
1559 egTagTable.phys_char_sf_map[current_ptype()].length
1564 $scope.label_for_step = '';
1565 function set_label_for_step() {
1566 if ($scope.step > 0) {
1567 current_subfield().then(function(sf) {
1568 $scope.label_for_step = sf.label();
1573 $scope.next_step = function() {
1575 set_values_for_step();
1578 $scope.prev_step = function() {
1580 set_values_for_step();
1583 function set_selected_option_from_field() {
1584 if ($scope.step == 0) {
1585 $scope.selected_option = $scope.values_for_step
1586 .filter(function(opt) {
1587 return (opt.ptype_key() == current_ptype())})[0];
1589 get_step_slot().then(function(slot) {
1590 var val = String.prototype.substr.apply(
1591 $scope.field.data, slot);
1593 $scope.selected_option = $scope.values_for_step
1594 .filter(function(opt) {
1595 return (opt.value() == val)})[0];
1597 $scope.selected_option = null;
1602 set_values_for_step();
1609 .directive("egMarcEditAuthorityBrowser", function () {
1613 templateUrl: './cat/share/t_authority_browser',
1620 controller: ['$scope','$http',
1621 function ($scope , $http) {
1625 $scope.main_headings = [];
1627 function getHeadingString(headingField) {
1629 angular.forEach(headingField.subfields, function (sf) {
1630 if (['x', 'y', 'z'].indexOf(sf[0]) > -1) {
1641 $scope.doBrowse = function() {
1642 $scope.main_headings.length = 0;
1643 if ($scope.searchString.length == 0) return;
1644 var type = 'authority.'
1645 var url = '/opac/extras/browse/marcxml/'
1646 + 'authority.' + $scope.axis + '.refs'
1647 + '/1' // OU - currently unscoped
1648 + '/' + $scope.searchString
1650 + '/' + $scope.limit;
1654 transformResponse : function(data) {
1655 // use a bit of jQuery to deal with the XML
1656 var $xml = $( $.parseXML(data) );
1658 $xml.find('record').each(function() {
1659 var rec = new MARC21.Record();
1660 rec.fromXmlDocument($(this)[0].outerHTML);
1665 }).then(function(response) {
1666 angular.forEach(response.data, function(rec) {
1667 var authId = rec.subfield('901', 'c')[1];
1669 if (rec.field('003')) {
1670 auth_org = rec.field('003').data;
1672 var headingField = rec.field('1..');
1673 var seeFroms = rec.field('4..', true);
1674 var seeAlsos = rec.field('5..', true);
1676 var main_heading = {
1677 authority_id : authId,
1678 heading : getHeadingString(headingField),
1679 seealso_headings : [ ],
1680 seefrom_headings : [ ],
1685 sfZero = '(' + auth_org + ')';
1688 headingField.addSubfields('0', sfZero);
1690 main_heading['headingField'] = headingField;
1691 angular.forEach(seeAlsos, function(headingField) {
1692 main_heading.seealso_headings.push({
1693 heading : getHeadingString(headingField),
1694 headingField : headingField
1697 angular.forEach(seeFroms, function(headingField) {
1698 main_heading.seefrom_headings.push({
1699 heading : getHeadingString(headingField),
1700 headingField : headingField
1703 $scope.main_headings.push(main_heading);
1708 $scope.$watch('searchString',
1709 function(newVal, oldVal) {
1710 if (newVal !== oldVal) {
1715 $scope.$watch('page',
1716 function(newVal, oldVal) {
1717 if (newVal !== oldVal) {