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: '=', contextMenuEvent: '=' },
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.css('borderTop','solid 1px');
20 $scope.setContent = function (v, a) {
28 $scope.$parent.$parent.content,
29 $scope.contextMenuEvent
33 if (typeof replace_with !== 'undefined') {
35 $scope.$parent.$parent.$apply(function(){
36 $scope.$parent.$parent.content = replace_with
40 $($element).parent().css({display: 'none'});
47 .directive("contenteditable", function() {
51 link: function(scope,element,attrs,ngModel){
54 // save new text into model
55 var elhtml = element.text();
56 ngModel.$setViewValue(elhtml);
59 ngModel.$render = function(){
60 element.text(ngModel.$viewValue || "");
63 element.bind("blur.c_e keyup.c_e change.c_e", function(){
70 .directive("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
74 templateUrl: './cat/share/t_marcedit_editable',
80 contextItemContainer: '@',
81 contextItemGenerator: '=',
85 advanceFocusAfterInput: '=',
88 controller : ['$scope',
90 $scope.isInputDisabled = $scope.isDisabled == 'disabled';
91 if ($scope.contextItemContainer && angular.isArray($scope.$parent[$scope.contextItemContainer]))
92 $scope.item_container = $scope.$parent[$scope.contextItemContainer];
93 else if ($scope.contextItemGenerator)
94 $scope.item_generator = $scope.contextItemGenerator;
96 $scope.showContext = function (event) {
97 $scope.item_list = [];
98 if ($scope.item_container) {
99 $scope.item_list = $scope.item_container;
100 } else if ($scope.item_generator) {
101 // always recalculate; tag and/or subfield
102 // codes may have changed
104 var generator = $scope.item_generator;
105 if (!angular.isArray(generator)) generator = [generator];
108 angular.forEach(generator, function (g) {
113 else if (Boolean(sub_list[0]))
114 sub_list[0].divider = true;
116 $scope.item_list = $scope.item_list.concat(sub_list);
123 if (angular.isArray($scope.item_list) && $scope.item_list.length > 0) { // we have a list of values or transforms
124 console.log('Showing context menu...');
125 $('body').trigger('click');
127 $scope.contextMenuEvent = event;
129 '<ul class="dropdown-menu scrollable-menu" role="menu" style="z-index: 2000;">'+
130 '<eg-context-menu-item context-menu-event="contextMenuEvent" ng-repeat="item in item_list" item="item" content="content"/>'+
133 var tnode = angular.element(tmpl);
134 $document.find('body').append(tnode);
142 $timeout(function() {
143 var e = $compile(tnode)($scope);
147 $('body').on('click.context_menu',function() {
148 $(tnode).css('display','none');
149 $('body').off('click.context_menu');
160 link: function (scope, element, attrs) {
162 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
164 if (Boolean(scope.selectOnFocus)) {
165 element.addClass('noSelection');
166 element.bind('focus', function (e) {
167 var el = $(e.target).children('input').first();
168 if (el.select) { el.select(); }
172 element.children("div[contenteditable]").each(function() {
173 $(this).focus(function(e) {
174 var tNode = e.target.firstChild;
175 var range = document.createRange();
176 range.setStart(tNode, 0);
177 range.setEnd(tNode, tNode.length);
178 var sel = window.getSelection();
179 sel.removeAllRanges();
184 function findCaretTarget(id, itype) {
186 if (itype == 'tag') {
187 tgt = id.replace(/tag$/, 'i1');
188 } else if (itype == 'ind') {
189 if (id.match(/i1$/)) {
190 tgt = id.replace(/i1$/, 'i2');
191 } else if (id.match(/i2$/)) {
192 tgt = id.replace(/i2$/, 's0code');
194 } else if (itype == 'sfc') {
195 tgt = id.replace(/code$/, 'value');
199 if (Boolean(scope.advanceFocusAfterInput)) {
200 element.bind('input', function (e) {
201 if (scope.content.length == scope.max) {
202 var tgt = findCaretTarget(e.currentTarget.id, scope.itype);
204 var element = $('#' + tgt).get(0);
213 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
215 element.bind('contextmenu', {scope : scope}, scope.showContext);
220 .directive("egMarcEditFixedField", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
224 template: '<div class="col-md-2">'+
225 '<div class="col-md-1"><label name="{{fixedField}}" for="{{fixedField}}_ff_input">{{fixedFieldLabel}}</label></div>'+
226 '<div class="col-md-1"><input type="text" style="padding-left: 5px; margin-left: 1em" size="4" id="{{fixedField}}_ff_input"/></div>'+
228 scope: { record: "=", fixedField: "@", fixedFieldLabel: "@" },
230 controller : ['$scope', '$element', 'egTagTable',
231 function ( $scope , $element , egTagTable) {
232 $($element).removeClass('fixed-field-box');
233 $($element).children().css({ display : 'none' });
234 $scope.fixedFieldLabel = $scope.fixedFieldLabel || $scope.fixedField;
236 $scope.content = null; // this is where context menus dump their values
237 $scope.item_container = [];
238 $scope.in_handler = false;
239 $scope.ready = false;
240 $element.find('input').bind('focus', function (e) { e.target.select() });
241 $element.find('input').bind('mouseup', function(e) {
246 $scope.$watch('content', function (newVal, oldVal) {
247 var input = $($element).find('input');
249 input.trigger('keyup'); // cascade the update
252 $scope.$watch('record.ready', function (newVal, oldVal) { // wait for the record to be loaded
253 if (newVal && !$scope.ready) {
254 $scope.rtype = $scope.record.recordType();
256 egTagTable.fetchFFPosTable( $scope.rtype ).then(function (ff_list) {
257 angular.forEach(ff_list, function (ff) {
259 if (ff.fixed_field == $scope.fixedField && ff.rec_type == $scope.rtype) {
262 $($element).addClass('fixed-field-box');
263 $($element).children().css({ display : 'inline' });
265 var input = $($element).find('input');
266 input.attr('maxlength', $scope.me.length);
267 input.val($scope.record.extractFixedField($scope.me.fixed_field));
268 input.on('keyup', function(e) {
269 $scope.in_handler = true;
270 $scope.record.setFixedField($scope.me.fixed_field, input.val());
271 try { $scope.$parent.$digest(); } catch(e) {};
277 }).then(function (me) {
281 return $scope.record.extractFixedField($scope.fixedField);
283 function (newVal, oldVal) {
284 if ($scope.in_handler) {
285 $scope.in_handler = false;
286 } else if (oldVal != newVal) {
287 $($element).find('input').val(newVal);
292 }).then(function () {
293 return egTagTable.fetchFFValueTable( $scope.rtype );
294 }).then(function (vlist) {
295 if (vlist[$scope.fixedField]) {
296 vlist[$scope.fixedField].forEach(function (v) {
297 if (v[0].length <= v[2]) {
298 $scope.item_container.push({ value : v[0], label : v[0] + ': ' + v[1] });
302 }).then(function () {
303 if ($scope.item_container && $scope.item_container.length)
304 $($element).bind('contextmenu', $scope.showContext);
310 $scope.showContext = function (event) {
311 if ($scope.context_menu_element) {
312 console.log('Reshowing context menu...');
313 $('body').trigger('click');
314 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
315 $('body').on('click.context_menu',function() {
316 $($scope.context_menu_element).css('display','none');
317 $('body').off('click.context_menu');
322 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
323 console.log('Showing context menu...');
324 $('body').trigger('click');
327 '<ul class="dropdown-menu scrollable-menu" role="menu" style="z-index: 2000;">'+
328 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
331 var tnode = angular.element(tmpl);
332 $document.find('body').append(tnode);
340 $scope.context_menu_element = tnode;
342 $timeout(function() {
343 var e = $compile(tnode)($scope);
347 $('body').on('click.context_menu',function() {
348 $(tnode).css('display','none');
349 $('body').off('click.context_menu');
363 .directive("egMarcEditSubfield", function () {
368 '<span><label class="marcedit marcsfcodedelimiter"'+
369 'for="r{{field.record.subfield(\'901\',\'c\')[1] || 0}}f{{field.position}}s{{subfield[2]}}code" '+
370 '>‡</label><eg-marc-edit-editable '+
372 'select-on-focus="true" '+
373 'advance-focus-after-input="true" '+
374 'class="marcedit marcsf marcsfcode" '+
376 'subfield="subfield" '+
377 'content="subfield[0]" '+
379 'on-keydown="onKeydown" '+
380 'context-item-generator="sf_code_options" '+
381 'id="r{{field.record.subfield(\'901\',\'c\')[1] || 0}}f{{field.position}}s{{subfield[2]}}code" '+
383 '<span><eg-marc-edit-editable '+
385 'select-on-focus="true" '+
386 'class="marcedit marcsf marcsfvalue" '+
388 'subfield="subfield" '+
389 'content="subfield[1]" '+
390 'on-keydown="onKeydown" '+
391 'context-item-generator="sf_val_options" '+
392 'id="r{{field.record.subfield(\'901\',\'c\')[1] || 0}}f{{field.position}}s{{subfield[2]}}value" '+
395 scope: { field: "=", subfield: "=", onKeydown: '=' },
397 controller : ['$scope', 'egTagTable',
398 function ( $scope , egTagTable) {
400 $scope.sf_code_options = function () {
401 return egTagTable.getSubfieldCodes($scope.field.tag);
403 $scope.sf_val_options = function () {
404 return egTagTable.getSubfieldValues($scope.field.tag, $scope.subfield[0]);
411 .directive("egMarcEditInd", function () {
415 template: '<span><eg-marc-edit-editable '+
417 'class="marcedit marcind" '+
418 'select-on-focus="true" '+
419 'advance-focus-after-input="true" '+
423 'on-keydown="onKeydown" '+
424 'context-item-generator="ind_val_options" '+
425 'id="r{{field.record.subfield(\'901\',\'c\')[1] || 0}}f{{field.position}}i{{indNumber}}"'+
427 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
429 controller : ['$scope', 'egTagTable',
430 function ( $scope , egTagTable) {
432 $scope.ind_val_options = function () {
433 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
440 .directive("egMarcEditTag", function () {
444 template: '<span><eg-marc-edit-editable '+
446 'class="marcedit marctag" '+
447 'select-on-focus="true" '+
448 'advance-focus-after-input="true" '+
452 'on-keydown="onKeydown" '+
453 'context-item-generator="tag_options" '+
454 'id="r{{field.record.subfield(\'901\',\'c\')[1] || 0}}f{{field.position}}tag"'+
456 scope: { tag : '=', field: '=', onKeydown: '=', contextFunctions: '=' },
458 controller : ['$scope', 'egTagTable', 'egCore',
459 function ( $scope , egTagTable, egCore) {
461 $scope.tag_options = [
464 { label : egCore.strings.ADD_006, action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.add006(e) } },
465 { label : egCore.strings.ADD_007, action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.add007(e) } },
466 { label : egCore.strings.ADD_REPLACE_008, action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.reify008(e) } },
469 if (!$scope.field.isControlfield()) {
470 options = options.concat([
471 { label : egCore.strings.INSERT_FIELD_AFTER, action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.addDatafield(e) } },
472 { label : egCore.strings.INSERT_FIELD_BEFORE, action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.addDatafield(e,true) } },
476 options.push({ label : egCore.strings.DELETE_FIELD, action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.deleteDatafield(e) } });
479 function () { return egTagTable.getFieldTags() }
487 .directive("egMarcEditDatafield", function () {
492 '<span><eg-marc-edit-tag context-functions="contextFunctions" field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
493 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
494 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
495 '<span><eg-marc-edit-subfield ng-class="{ \'unvalidatedheading\' : field.heading_checked && !field.heading_valid, \'marcedit_stacked_subfield\' : stackSubfields.enabled }" ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
496 // FIXME: template should probably be moved to file to improve
498 '<span ng-class="{ \'marcedit_stacked_subfield\' : stackSubfields.enabled }">' +
499 '<button class="btn btn-info btn-xs" '+
500 'aria-label="Manage authority record links" '+
501 'ng-show="isAuthorityControlled(field)"'+
502 'ng-click="spawnAuthorityLinker()"'+
504 '<span class="glyphicon glyphicon-link"></span>'+
506 '<span ng-show="field.heading_checked && field.heading_valid" class="glyphicon glyphicon-ok-sign"></span>'+
507 '<span ng-show="field.heading_checked && !field.heading_valid" class="glyphicon glyphicon-question-sign"></span>'+
510 scope: { field: "=", onKeydown: '=', contextFunctions: '=' },
512 controller : ['$scope','$uibModal',
513 function ( $scope, $uibModal ) {
514 $scope.stackSubfields = $scope.$parent.$parent.stackSubfields;
515 $scope.isAuthorityControlled = function () {
516 return ($scope.$parent.$parent.record_type == 'bre') &&
517 $scope.$parent.$parent.controlSet.bibFieldByTag($scope.field.tag);
519 $scope.spawnAuthorityLinker = function() {
520 // intentionally making a clone in case
521 // user decides to abandon the linking
522 var fieldCopy = new MARC21.Field({
523 tag : $scope.field.tag,
524 ind1 : $scope.field.ind1,
525 ind2 : $scope.field.ind2
527 angular.forEach($scope.field.subfields, function(sf) {
528 fieldCopy.subfields.push(sf.slice(0));
530 var cs = $scope.$parent.$parent.controlSet;
531 var args = { changed : false };
533 templateUrl: './cat/share/t_authority_link_dialog',
536 controller: ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
537 $scope.controlSet = cs;
538 $scope.bibField = fieldCopy;
539 $scope.focusMe = true;
541 $scope.ok = function(args) { $uibModalInstance.close(args) };
542 $scope.cancel = function () { $uibModalInstance.dismiss() };
544 }).result.then(function (args) {
546 $scope.field.subfields.length = 0;
547 angular.forEach(fieldCopy.subfields, function(sf) {
548 $scope.field.addSubfields(sf[0], sf[1]);
558 .directive("egMarcEditControlfield", function () {
563 '<span><eg-marc-edit-tag context-functions="contextFunctions" field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
564 '<span><eg-marc-edit-editable '+
567 'class="marcedit marcdata" '+
568 'content="field.data" '+
569 'on-keydown="onKeydown" '+
570 'id="r{{field.record.subfield(\'901\',\'c\')[1] || 0}}f{{field.position}}data"'+
572 // TODO: move to TT2 template
573 '<button class="btn btn-info btn-xs" '+
574 'aria-label="Physical Characteristics Wizard" '+
575 'ng-show="showPhysCharLink()"'+
576 'ng-click="spawnPhysCharWizard()"'+
578 '<span class="glyphicon glyphicon-edit"></span>'+
581 scope: { field: "=", onKeydown: '=', contextFunctions: '=' },
582 controller : ['$scope','$uibModal',
583 function ( $scope, $uibModal) {
584 $scope.showPhysCharLink = function () {
585 return ($scope.$parent.$parent.record_type == 'bre')
586 && $scope.field.tag == '007';
588 $scope.spawnPhysCharWizard = function() {
591 field : $scope.field,
592 orig_value : $scope.field.data
595 templateUrl: './cat/share/t_physchar_dialog',
596 controller: ['$scope','$uibModalInstance',
597 function( $scope , $uibModalInstance) {
598 $scope.focusMe = true;
600 $scope.ok = function(args) { $uibModalInstance.close(args) };
601 $scope.cancel = function () {
602 $uibModalInstance.dismiss();
603 args.field.data = args.orig_value;
606 }).result.then(function (args) {
607 // $scope.field.data is changed within the
608 // wizard. Nothing left to do on submit.
617 .directive("egMarcEditLeader", function () {
622 '<span><eg-marc-edit-editable '+
623 'class="marcedit marctag" '+
625 'on-keydown="onKeydown" '+
627 'is-disabled="disabled"'+
629 '<span><eg-marc-edit-editable '+
630 'class="marcedit marcdata" '+
632 'max="{{record.leader.length}}" '+
633 'content="record.leader" '+
634 'id="r{{record.subfield(\'901\',\'c\')[1] || 0}}leaderdata" '+
635 'on-keydown="onKeydown"'+
638 controller : ['$scope',
639 function ( $scope ) {
643 scope: { record: "=", onKeydown: '=' }
647 /// TODO: fixed field editor and such
648 .directive("egMarcEditRecord", function () {
650 templateUrl : './cat/share/t_marcedit',
659 // in-place mode means that the editor is being
660 // used just to munge some MARCXML client-side, rather
661 // than to (immediately) update the database
663 // In short, we can use inPlaceMode as a way to skip
664 // "normal" bre saving and then process the MARC ourselves
667 // inPlaceMode is r/w to allow our Z39.50 import editor to be
668 // switched back into a normal editor after the initial import
677 link: function (scope, element, attrs) {
679 element.bind('mouseup', function(e) {;
680 scope.current_event_target = $(e.target).attr('id');
681 if (scope.current_event_target && $(e.target).hasClass('noSelection')) {
687 element.bind('click', function(e) {;
688 scope.current_event_target = $(e.target).attr('id');
689 if (scope.current_event_target) {
690 console.log('Recording click event on ' + scope.current_event_target);
691 scope.current_event_target_cursor_pos =
692 e.target.selectionDirection=='backward' ?
693 e.target.selectionStart :
694 e.target.selectionEnd;
699 controller : ['$timeout','$scope','$q','$window','egCore', 'egTagTable',
700 'egConfirmDialog','egAlertDialog','ngToast','egStrings','hotkeys',
701 function ( $timeout , $scope , $q, $window , egCore , egTagTable ,
702 egConfirmDialog , egAlertDialog , ngToast , egStrings, hotkeys) {
705 $scope.onSaveCallback = $scope.onSave;
706 if (typeof $scope.onSaveCallback !== 'undefined' && !angular.isArray($scope.onSaveCallback))
707 $scope.onSaveCallback = [ $scope.onSaveCallback ];
709 $scope.$watch('dirtyFlag',
710 function(newVal, oldVal) {
711 if (newVal && newVal != oldVal && !$scope.opac_iframe) {
712 $($window).on('beforeunload', function(){
713 return egCore.strings.DIRTY_MARC_WARNING;
716 if (!$scope.opac_iframe)
717 $($window).off('beforeunload');
724 description: egCore.strings.HOTKEY_SAVE_RECORD,
725 callback: function(event, hotkey) {
727 event.preventDefault();
728 if($scope.flatEditor.isEnabled){
729 $scope.saveFlatTextMARC();
733 allowIn : ['INPUT','SELECT','TEXTAREA']
738 description: egCore.strings.HOTKEY_FOCUS_EDITOR,
739 callback: function(event, hotkey) {
740 event.preventDefault();
741 if($scope.flatEditor.isEnabled){
742 var editor = $window.document.getElementsByTagName('textarea');
746 console.log('Jump focus to non-flattext editor not implemented.');
749 allowIn : ['INPUT','SELECT','TEXTAREA']
753 MARC21.Record.delimiter = '$';
755 $scope.enable_fast_add = false;
756 $scope.fast_item_callnumber = '';
757 $scope.fast_item_barcode = '';
759 $scope.flatEditor = { isEnabled : $scope.flatOnly ? true : false };
761 egCore.hatch.getItem('cat.marcedit.flateditor').then(function(val) {
762 $scope.flatEditor.isEnabled = val;
765 $scope.$watch('flatEditor.isEnabled', function (newVal, oldVal) {
766 if (newVal != oldVal) egCore.hatch.setItem('cat.marcedit.flateditor', newVal);
769 // necessary to prevent ng-model scope hiding ugliness in egMarcEditBibSource:
770 $scope.bib_source = {
771 id : $scope.bibSource ? $scope.bibSource : null,
774 $scope.brandNewRecord = false;
775 $scope.record_type = $scope.recordType || 'bre';
776 $scope.max_undo = $scope.maxUndo || 100;
777 $scope.record_undo_stack = [];
778 $scope.record_redo_stack = [];
779 $scope.in_undo = false;
780 $scope.in_redo = false;
781 $scope.record = new MARC21.Record();
782 $scope.save_stack_depth = 0;
783 $scope.controlfields = [];
784 $scope.datafields = [];
785 $scope.controlSet = egTagTable.getAuthorityControlSet();
786 $scope.showHelp = false;
787 $scope.stackSubfields = { enabled : false };
788 egCore.hatch.getItem('cat.marcedit.stack_subfields').then(function(val) {
789 $scope.stackSubfields.enabled = val;
791 $scope.$watch('stackSubfields.enabled', function (newVal, oldVal) {
792 if (newVal != oldVal) egCore.hatch.setItem('cat.marcedit.stack_subfields', newVal);
794 $scope.caretRecId = $scope.recordId;
796 egTagTable.loadTagTable({ marcRecordType : $scope.record_type });
798 $scope.saveFlatTextMARC = function () {
799 $scope.record = new MARC21.Record({ marcbreaker : $scope.flat_text_marc });
802 $scope.refreshVisual = function () {
803 if (!$scope.flatEditor.isEnabled) {
804 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
805 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
809 var addDatafield = function (e,before) {
810 var element = $(e.target);
812 var index_field = e.data.scope.field.position;
813 var new_field_index = index_field;
815 var new_field = new MARC21.Field({
817 subfields : [[' ','',0]]
820 if (Boolean(before)) {
821 e.data.scope.field.record.insertFieldsBefore(
826 e.data.scope.field.record.insertFieldsAfter(
833 $scope.current_event_target = 'r' + $scope.caretRecId +
834 'f' + new_field_index + 'tag';
836 $scope.current_event_target_cursor_pos = 0;
837 $scope.current_event_target_cursor_pos_end = 3;
838 $scope.force_render = true;
840 $timeout(function(){$scope.$digest()}).then(setCaret);
843 var deleteDatafield = function (e) {
844 var del_field = e.data.scope.field.position;
846 var sf901c = e.data.scope.field.record.subfield('901','c');
847 var recId = (sf901c === null) ? '' : sf901c[1];
848 var domnode = $('#r' + recId + 'f' + del_field);
850 e.data.scope.field.record.deleteFields(
854 domnode.scope().$destroy();
857 $scope.current_event_target = 'r' + $scope.caretRecId +
858 'f' + del_field + 'tag';
860 $scope.current_event_target_cursor_pos = 0;
861 $scope.current_event_target_cursor_pos_end = 0
862 $scope.force_render = true;
864 $timeout(function(){$scope.$digest()}).then(setCaret);
867 var add006 = function (e) {
868 e.data.scope.field.record.insertOrderedFields(
875 $scope.force_render = true;
876 $timeout(function(){$scope.$digest()}).then(setCaret);
879 var add007 = function (e) {
880 e.data.scope.field.record.insertOrderedFields(
887 $scope.force_render = true;
888 $timeout(function(){$scope.$digest()}).then(setCaret);
891 var reify008 = function (e) {
892 var new_008_data = e.data.scope.field.record.generate008();
895 var old_008s = e.data.scope.field.record.field('008',true);
896 old_008s.forEach(function(o) {
897 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
898 domnode.scope().$destroy();
900 e.data.scope.field.record.deleteFields(o);
903 e.data.scope.field.record.insertOrderedFields(
910 $scope.force_render = true;
911 $timeout(function(){$scope.$digest()}).then(setCaret);
914 $scope.context_functions = {
915 addDatafield : addDatafield,
916 deleteDatafield : deleteDatafield,
922 $scope.onKeydown = function (event) {
923 var event_return = true;
926 'keydown: which='+event.which+
927 ', ctrlKey='+event.ctrlKey+
928 ', shiftKey='+event.shiftKey+
929 ', altKey='+event.altKey+
930 ', metaKey='+event.altKey
933 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
934 event_return = $scope.processRedo();
935 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
936 event_return = $scope.processUndo();
937 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
939 var element = $(event.target);
940 var new_sf, index_sf, move_data;
942 if (element.hasClass('marcsfvalue')) {
943 index_sf = event.data.scope.subfield[2];
944 new_sf = index_sf + 1;
946 var start = event.target.selectionStart || getCaretPosEditableDiv(element);
948 if (event.target.value){
949 end = event.target.selectionEnd - event.target.selectionStart ?
950 event.target.selectionEnd :
951 event.target.value.length;
953 end = element.text().length;
956 move_data = element.value ?
957 element.value.substring(start,end) :
958 element.text().substring(start, end);
960 } else if (element.hasClass('marcsfcode')) {
961 index_sf = event.data.scope.subfield[2];
962 new_sf = index_sf + 1;
963 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
968 $scope.current_event_target = 'r' + $scope.caretRecId +
969 'f' + event.data.scope.field.position +
970 's' + new_sf + 'code';
972 event.data.scope.field.subfields.forEach(function(sf) {
973 if (sf[2] >= new_sf) sf[2]++;
974 if (sf[2] == index_sf) {
975 sf[1] = event.target.value ?
976 event.target.value.substring(0,start) + event.target.value.substring(end) :
977 element.text().substring(0, start);
980 event.data.scope.field.subfields.splice(
983 [' ', move_data, new_sf ]
986 $scope.current_event_target_cursor_pos = 0;
987 $scope.current_event_target_cursor_pos_end = 1;
989 $timeout(function(){$scope.$digest()}).then(setCaret);
991 event_return = false;
993 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
995 event_return = false;
997 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
999 event_return = false;
1001 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
1003 event_return = false;
1005 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
1006 addDatafield(event, event.shiftKey); // shift key inserts before
1007 event_return = false;
1009 } else if (event.which == 13 &&
1010 ($(event.target).hasClass('marcsf') || $(event.target.parentNode).hasClass('marcsf'))
1012 // bare return; don't allow it
1013 event_return = false;
1015 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
1016 deleteDatafield(event);
1017 event_return = false;
1019 } else if (event.which == 46 && event.shiftKey && ($(event.target).hasClass('marcsf') || $(event.target.parentNode).hasClass('marcsf'))) {
1020 // shift+del, remove subfield
1022 var sf = event.data.scope.subfield[2] - 1;
1023 if (sf == -1) sf = 0;
1025 event.data.scope.field.deleteExactSubfields(
1026 event.data.scope.subfield
1029 if (!event.data.scope.field.subfields[sf]) {
1030 $scope.current_event_target = 'r' + $scope.caretRecId +
1031 'f' + event.data.scope.field.position +
1034 $scope.current_event_target = 'r' + $scope.caretRecId +
1035 'f' + event.data.scope.field.position +
1039 $scope.current_event_target_cursor_pos = 0;
1040 $scope.current_event_target_cursor_pos_end = 0;
1041 $scope.force_render = true;
1043 $timeout(function(){$scope.$digest()}).then(setCaret);
1045 event_return = false;
1047 } else if (event.keyCode == 38) {
1048 if (event.ctrlKey) { // copy the field up
1049 var index_field = event.data.scope.field.position;
1052 if (event.data.scope.field.isControlfield()) {
1053 field_obj = new MARC21.Field({
1054 tag : event.data.scope.field.tag,
1055 data : event.data.scope.field.data
1059 for (var i in event.data.scope.field.subfields) {
1060 sf_clone.push(event.data.scope.field.subfields[i].slice());
1062 field_obj = new MARC21.Field({
1063 tag : event.data.scope.field.tag,
1064 ind1 : event.data.scope.field.ind1,
1065 ind2 : event.data.scope.field.ind2,
1066 subfields : sf_clone
1071 event.data.scope.field.record.insertFieldsBefore(
1072 event.data.scope.field,
1076 $scope.current_event_target = 'r' + $scope.caretRecId +
1077 'f' + index_field + 'tag';
1079 $scope.current_event_target_cursor_pos = 0;
1080 $scope.current_event_target_cursor_pos_end = 3;
1081 $scope.force_render = true;
1083 $timeout(function(){$scope.$digest()}).then(setCaret);
1085 } else { // jump to prev field
1086 if (event.data.scope.field.position > 0) {
1087 $timeout(function(){
1088 $scope.current_event_target_cursor_pos = 0;
1089 $scope.current_event_target_cursor_pos_end = 0;
1090 $scope.current_event_target = 'r' + $scope.caretRecId +
1091 'f' + (event.data.scope.field.position - 1) +
1097 event_return = false;
1099 } else if (event.keyCode == 40) { // down arrow...
1100 if (event.ctrlKey) { // copy the field down
1102 var index_field = event.data.scope.field.position;
1103 var new_field = index_field + 1;
1106 if (event.data.scope.field.isControlfield()) {
1107 field_obj = new MARC21.Field({
1108 tag : event.data.scope.field.tag,
1109 data : event.data.scope.field.data
1113 for (var i in event.data.scope.field.subfields) {
1114 sf_clone.push(event.data.scope.field.subfields[i].slice());
1116 field_obj = new MARC21.Field({
1117 tag : event.data.scope.field.tag,
1118 ind1 : event.data.scope.field.ind1,
1119 ind2 : event.data.scope.field.ind2,
1120 subfields : sf_clone
1124 event.data.scope.field.record.insertFieldsAfter(
1125 event.data.scope.field,
1129 $scope.current_event_target = 'r' + $scope.caretRecId +
1130 'f' + new_field + 'tag';
1132 $scope.current_event_target_cursor_pos = 0;
1133 $scope.current_event_target_cursor_pos_end = 3;
1134 $scope.force_render = true;
1136 $timeout(function(){$scope.$digest()}).then(setCaret);
1138 } else { // jump to next field
1139 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
1140 $timeout(function(){
1141 $scope.current_event_target_cursor_pos = 0;
1142 $scope.current_event_target_cursor_pos_end = 0;
1143 $scope.current_event_target = 'r' + $scope.caretRecId +
1144 'f' + (event.data.scope.field.position + 1) +
1150 event_return = false;
1152 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
1153 $scope.current_event_target = $(event.target).hasClass('focusable') ? $(event.target) : null;//.attr('id');
1154 if ($scope.current_event_target) {
1155 $scope.current_event_target_cursor_pos =
1156 event.target.selectionDirection=='backward' ?
1157 event.target.selectionStart :
1158 event.target.selectionEnd;
1162 return event_return;
1165 function setCaret() {
1166 if ($scope.current_event_target) {
1167 console.log("Putting caret in " + $scope.current_event_target);
1168 if (!$scope.current_event_target_cursor_pos_end)
1169 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
1171 var element = $('#'+$scope.current_event_target + " .focusable").get(0);
1174 if (element.setSelectionRange) {
1175 element.setSelectionRange(
1176 $scope.current_event_target_cursor_pos,
1177 $scope.current_event_target_cursor_pos_end
1181 $scope.current_event_cursor_pos_end = null;
1182 $scope.current_event_target = null;
1186 function getCaretPosEditableDiv(editableDiv){
1187 var caretPos = 0, sel, range;
1188 if (window.getSelection) {
1189 sel = window.getSelection();
1190 if (sel.rangeCount) {
1191 range = sel.getRangeAt(0);
1192 if (range.commonAncestorContainer.parentNode == editableDiv[0]) {
1193 caretPos = range.endOffset;
1200 function loadRecord() {
1201 return (function() {
1202 var deferred = $q.defer();
1203 if ($scope.recordId) {
1204 egCore.pcrud.retrieve(
1205 $scope.record_type, $scope.recordId
1206 ).then(function(rec) {
1207 deferred.resolve(rec);
1210 if ($scope.recordType == 'bre') {
1211 var bre = new egCore.idl.bre();
1212 bre.marc($scope.marcXml);
1213 deferred.resolve(bre);
1214 } else if ($scope.recordType == 'are') {
1215 var are = new egCore.idl.are();
1216 are.marc($scope.marcXml);
1217 deferred.resolve(are);
1218 } else if ($scope.recordType == 'sre') {
1219 var sre = new egCore.idl.sre();
1220 sre.marc($scope.marcXml);
1221 deferred.resolve(sre);
1223 $scope.brandNewRecord = true;
1225 return deferred.promise;
1226 })().then(function(rec) {
1227 $scope.in_redo = true;
1228 $scope[$scope.record_type] = rec;
1229 $scope.record = new MARC21.Record({ marcxml : $scope.Record().marc() });
1230 if (!$scope.recordId) {
1231 var sf901c = $scope.record.subfield('901', 'c');
1232 if (sf901c !== null) {
1233 $scope.caretRecId = sf901c[1];
1236 $scope.calculated_record_type = $scope.record.recordType();
1237 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1238 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1239 $scope.save_stack_depth = $scope.record_undo_stack.length;
1240 $scope.dirtyFlag = false;
1241 $scope.flat_text_marc = $scope.record.toBreaker();
1243 if ($scope.record_type == 'bre' && !$scope.brandNewRecord) {
1244 $scope.bib_source.id = $scope.bibSource = rec.source(); //$scope.Record().source();
1248 return egTagTable.fetchFFPosTable($scope.calculated_record_type)
1250 return egTagTable.fetchFFValueTable($scope.calculated_record_type)
1254 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
1255 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
1256 $scope.record_undo_stack.push({
1258 target: $scope.current_event_target,
1259 pos: $scope.current_event_target_cursor_pos
1262 if ($scope.force_render) {
1263 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1264 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1265 $scope.force_render = false;
1268 $scope.flat_text_marc = newVal;
1271 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
1272 $scope.dirtyFlag = true;
1274 $scope.dirtyFlag = false;
1277 if ($scope.record_undo_stack.length > $scope.max_undo)
1278 $scope.record_undo_stack.shift();
1280 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
1281 $scope.in_redo = false;
1282 $scope.in_undo = false;
1285 $scope.processUndo = function () {
1286 if ($scope.record_undo_stack.length) {
1287 $scope.in_undo = true;
1289 var undo_item = $scope.record_undo_stack.pop();
1290 $scope.record_redo_stack.push(undo_item);
1292 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
1293 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1294 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1296 $scope.current_event_target = undo_item.target;
1297 $scope.current_event_target_cursor_pos = undo_item.pos;
1298 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1300 $timeout(function(){$scope.$digest()}).then(setCaret);
1307 $scope.processRedo = function () {
1308 if ($scope.record_redo_stack.length) {
1309 $scope.in_redo = true;
1311 var redo_item = $scope.record_redo_stack.pop();
1312 $scope.record_undo_stack.push(redo_item);
1314 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
1315 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1316 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1318 $scope.current_event_target = redo_item.target;
1319 $scope.current_event_target_cursor_pos = redo_item.pos;
1320 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1322 $timeout(function(){$scope.$digest()}).then(setCaret);
1329 $scope.Record = function () {
1330 return $scope[$scope.record_type];
1333 $scope.deleteRecord = function () {
1334 egConfirmDialog.open(
1335 egCore.strings.CONFIRM_DELETE_RECORD,
1336 (($scope.record_type == 'bre') ?
1337 egCore.strings.CONFIRM_DELETE_BRE_MSG :
1338 egCore.strings.CONFIRM_DELETE_ARE_MSG),
1339 { id : $scope.recordId }
1340 ).result.then(function() {
1341 if ($scope.record_type == 'bre') {
1344 'open-ils.cat.biblio.record_entry.delete',
1345 egCore.auth.token(), $scope.recordId
1346 ).then(function(resp) {
1347 var evt = egCore.evt.parse(resp);
1349 return egAlertDialog.open(
1350 egCore.strings.ALERT_DELETE_FAILED,
1351 { id : $scope.recordId, desc : evt.desc }
1354 loadRecord().then($scope.processOnSaveCallbacks);
1358 $scope.Record().deleted(true);
1359 return $scope.saveRecord();
1364 $scope.undeleteRecord = function () {
1365 if ($scope.record_type == 'bre') {
1368 'open-ils.cat.biblio.record_entry.undelete',
1369 egCore.auth.token(), $scope.recordId
1370 ).then(function(resp) {
1371 var evt = egCore.evt.parse(resp);
1373 return egAlertDialog.open(
1374 egCore.strings.ALERT_UNDELETE_FAILED,
1375 { id : $scope.recordId, desc : evt.desc }
1378 ngToast.create(egCore.strings.SUCCESS_UNDELETE_RECORD);
1379 loadRecord().then($scope.processOnSaveCallbacks);
1385 $scope.validateHeadings = function () {
1386 if ($scope.record_type != 'bre') return;
1387 var chain = $q.when();
1388 angular.forEach($scope.record.fields, function(f) {
1389 if (!$scope.controlSet.bibFieldByTag(f.tag)) return;
1390 // if heading already has a $0, assume it's good
1391 if (f.subfield('0', true).length) {
1392 f.heading_checked = true;
1393 f.heading_valid = true;
1396 var auth_match = $scope.controlSet.bibToAuthorities(f);
1397 if (auth_match.length == 0) return;
1398 chain = chain.then(function() {
1399 var promise = egCore.net.request(
1401 'open-ils.search.authority.simple_heading.from_xml.batch.atomic',
1403 ).then(function (matches) {
1404 f.heading_valid = false;
1405 if (matches[0]) { // probably set
1406 for (var cset in matches[0]) {
1407 var arr = matches[0][cset];
1409 // protect against errant empty string values
1410 if (arr.length == 1 && arr[0] == '')
1412 f.heading_valid = true;
1417 f.heading_checked = true;
1424 $scope.processOnSaveCallbacks = function() {
1425 var deferred = $q.defer();
1426 if (typeof $scope.onSaveCallback !== 'undefined') {
1427 var promise = deferred.promise;
1429 angular.forEach($scope.onSaveCallback, function (f) {
1430 if (angular.isFunction(f)) promise = promise.then(f);
1434 return deferred.resolve($scope.recordId)
1437 // Returns a promise
1438 function createOrUpdateRecord() {
1441 if ($scope.recordId) {
1443 var method = $scope.record_type === 'bre' ?
1444 'open-ils.cat.biblio.record.xml.update' :
1445 'open-ils.cat.authority.record.overlay';
1447 promise = egCore.net.request(
1448 'open-ils.cat', method,
1449 egCore.auth.token(), $scope.recordId,
1450 $scope.Record().marc(), $scope.bib_source.name
1455 var method = $scope.record_type === 'bre' ?
1456 'open-ils.cat.biblio.record.xml.create' :
1457 'open-ils.cat.authority.record.import';
1459 promise = egCore.net.request(
1460 'open-ils.cat', method,
1461 egCore.auth.token(),
1462 $scope.Record().marc(),
1463 $scope.bib_source.name
1467 return promise.then(handleCreateOrUpdateResult);
1470 function handleCreateOrUpdateResult(result) {
1472 var evt = egCore.evt.parse(result)
1473 var mode = $scope.recordId ? 'update' : 'create';
1476 var msg = mode === 'update' ?
1477 egStrings.MARC_ALERT_UPDATE_FAILED :
1478 egStrings.MARC_ALERT_CREATE_FAILED;
1479 ngToast.warning(msg, {error: '' + evt});
1483 var msg = mode === 'update' ?
1484 egStrings.MARC_ALERT_UPDATE_SUCCESS :
1485 egStrings.MARC_ALERT_CREATE_SUCCESS;
1486 ngToast.create(msg);
1488 console.debug('MARC create/update returned', result);
1490 // synchronize values
1491 if (!$scope.recordId) {
1492 $scope.recordId = $scope.caretRecId = result.id();
1495 $scope.dirtyFlag = false;
1500 $scope.saveRecord = function () {
1502 if ($scope.inPlaceMode) {
1503 $scope.marcXml = $scope.record.toXmlString();
1505 if ($scope.record_type == 'bre'){
1506 $scope.bibSource = $scope.bib_source.id;
1509 return $timeout($scope.processOnSaveCallbacks);
1512 $scope.mangle_005();
1513 $scope.record.pruneEmptyFieldsAndSubfields();
1514 $scope.Record().marc($scope.record.toXmlString());
1516 var updating = Boolean($scope.recordId);
1517 return createOrUpdateRecord().then(function(record) {
1520 $scope.save_stack_depth = $scope.record_undo_stack.length;
1523 if (!$scope.enable_fast_add) {
1529 'open-ils.actor.anon_cache.set_value',
1530 null, 'edit-these-copies', {
1531 record_id: $scope.recordId,
1533 label : $scope.fast_item_callnumber,
1534 barcode : $scope.fast_item_barcode,
1540 ).then(function(key) {
1542 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1543 $timeout(function() { $window.open(url, '_blank') });
1545 alert('Could not create anonymous cache key!');
1548 }).then(loadRecord).then($scope.processOnSaveCallbacks);
1551 $scope.seeBreaker = function () {
1552 alert($scope.record.toBreaker());
1555 $scope.$watch('recordId',
1556 function(newVal, oldVal) {
1557 if (newVal && newVal !== oldVal) {
1562 $scope.$watch('marcXml',
1563 function(newVal, oldVal) {
1564 if (newVal && newVal !== oldVal) {
1570 var unregister = $scope.$watch(function() {
1571 return egTagTable.initialized();
1575 if ($scope.recordId || $scope.marcXml) {
1581 $scope.mangle_005 = function () {
1582 var now = new Date();
1583 var y = now.getUTCFullYear();
1585 var m = now.getUTCMonth() + 1;
1586 if (m < 10) m = '0' + m;
1588 var d = now.getUTCDate();
1589 if (d < 10) d = '0' + d;
1591 var H = now.getUTCHours();
1592 if (H < 10) H = '0' + H;
1594 var M = now.getUTCMinutes();
1595 if (M < 10) M = '0' + M;
1597 var S = now.getUTCSeconds();
1598 if (S < 10) S = '0' + S;
1600 var stamp = '' + y + m + d + H + M + S + '.0';
1601 var f = $scope.record.field('005',true)[0];
1605 $scope.record.insertOrderedFields(
1620 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
1624 template: '<span class="nullable">'+
1625 '<select class="form-control" ng-model="bib_source.id" ng-options="s.id() as s.source() for s in bib_sources | orderBy: \'source()\'">'+
1626 '<option value="">Select a Source</option>'+
1629 controller: ['$scope','egCore',
1630 function ($scope , egCore) {
1632 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
1633 .then(function(list) {
1634 $scope.bib_sources = list;
1637 $scope.$watch('bib_source.id',
1638 function(newVal, oldVal) {
1639 if (newVal !== oldVal) {
1640 $scope.bre.source(newVal);
1641 var cbs = $scope.bib_sources.filter(function(s) { return s.id() == newVal });
1642 $scope.$parent.bib_source.name = (cbs && cbs[0]) ? cbs[0].source() : null;
1652 .directive("egMarcEditAuthorityLinker", function () {
1656 templateUrl: './cat/share/t_authority_linker',
1662 controller: ['$scope','$uibModal','egCore','egAuth',
1663 function ($scope , $uibModal, egCore, egAuth) {
1665 $scope.searchStr = '';
1666 var cni = 'Set cat.marc_control_number_identifier in Library Settings';
1667 egCore.org.settings([
1668 'cat.marc_control_number_identifier'
1669 ]).then(function(settings) {
1670 if (settings['cat.marc_control_number_identifier']) {
1671 cni = settings['cat.marc_control_number_identifier'];
1675 var axis_list = $scope.controlSet.bibFieldBrowseAxes($scope.bibField.tag);
1676 $scope.axis = axis_list[0];
1678 $scope._controlled_sf_list = {};
1679 $scope._controlled_auth_sf_list = {};
1681 angular.forEach($scope.controlSet.controlSetList(), function(acs_id) {
1682 if ($scope.controlSet.controlSet(acs_id).control_map[$scope.bibField.tag])
1683 found_acs.push(acs_id);
1685 if (found_acs.length) {
1686 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag],
1687 function(value, sf_label) {
1688 $scope._controlled_sf_list[ sf_label ] = 1;
1689 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag][sf_label],
1690 function(auth_sf, auth_tag) {
1691 if (!$scope._controlled_auth_sf_list[auth_tag]) {
1692 $scope._controlled_auth_sf_list[auth_tag] = { };
1694 $scope._controlled_auth_sf_list[auth_tag][auth_sf] = 1;
1701 $scope.bibField.subfields.forEach(function (sf) {
1702 if (sf[0] in $scope._controlled_sf_list) {
1704 sf.selectable = true;
1706 sf.selectable = false;
1709 $scope.summarizeField = function() {
1711 'tag': $scope.bibField.tag,
1712 'ind1': $scope.bibField.ind1,
1713 'ind2': $scope.bibField.ind2,
1716 $scope.bibField.subfields.forEach(function(sf) {
1718 source_f.subfields.push([ sf[0], sf[1] ]);
1723 $scope.getSearchString = function() {
1724 var source_f = $scope.summarizeField();
1726 angular.forEach(source_f.subfields, function(val) {
1727 values.push(val[1]);
1729 return values.join(' ');
1731 $scope.searchStr = $scope.getSearchString();
1732 $scope.$watch(function() {
1734 angular.forEach($scope.bibField.subfields, function(sf) {
1735 if (sf.selected) ct++
1739 function(newVal, oldVal) {
1740 $scope.searchStr = $scope.getSearchString();
1743 $scope.updateSubfieldZero = function(value) {
1744 $scope.changed = true;
1745 $scope.bibField.deleteSubfield({ code : ['0'] });
1746 $scope.bibField.subfields.push([
1747 '0', '(' + cni + ')' + value
1751 $scope.applyHeading = function(headingField) {
1752 // TODO: move the MARC21 rules for copying indicators
1754 if (headingField.tag == '130' && $scope.bibField.tag == '130') {
1755 $scope.bibField.ind1 = headingField.ind2;
1757 $scope.bibField.ind1 = headingField.ind1;
1759 // deal with 4xx and 5xx
1760 var authFallbackTag = '1' + headingField.tag.substr(1, 2);
1761 var _valid_auth_sfs = (headingField.tag in $scope._controlled_auth_sf_list) ?
1762 $scope._controlled_auth_sf_list[headingField.tag] :
1763 (authFallbackTag in $scope._controlled_auth_sf_list) ?
1764 $scope._controlled_auth_sf_list[authFallbackTag] :
1766 // save the $0 for later use
1768 if (headingField.subfield('0')) {
1769 sfZero = headingField.subfield('0')[1];
1771 // grab any bib subfields not under authority control
1772 // TODO do something about uncontrolled subdivisions
1773 var uncontrolledBibSf = [];
1774 angular.forEach($scope.bibField.subfields, function(sf) {
1775 if (!(sf[0] in $scope._controlled_sf_list) && (sf[0] != '0')) {
1776 uncontrolledBibSf.push([ sf[0], sf[1] ]);
1779 // grab the authority subfields
1780 var authoritySf = [];
1781 angular.forEach(headingField.subfields, function(sf) {
1782 if (sf[0] in _valid_auth_sfs) {
1783 authoritySf.push([ sf[0], sf[1] ]);
1786 $scope.bibField.subfields.length = 0;
1787 angular.forEach(authoritySf, function(sf) {
1788 $scope.bibField.addSubfields(sf[0], sf[1]);
1790 angular.forEach(uncontrolledBibSf, function(sf) {
1791 $scope.bibField.addSubfields(sf[0], sf[1]);
1794 $scope.bibField.addSubfields('0', sfZero);
1796 $scope.bibField.subfields.forEach(function (sf) {
1797 if (sf[0] in $scope._controlled_sf_list) {
1798 // intentionally not selecting any subfields
1799 // after we've applied an authority heading
1800 sf.selected = false;
1801 sf.selectable = true;
1803 sf.selectable = false;
1806 $scope.changed = true;
1809 $scope.createAuthorityFromBib = function(spawn_editor) {
1810 var source_f = $scope.summarizeField();
1812 var args = { authority_id : 0 };
1813 var method = (spawn_editor) ?
1814 'open-ils.cat.authority.record.create_from_bib.readonly' :
1815 'open-ils.cat.authority.record.create_from_bib';
1822 ).then(function(newAuthority) {
1825 templateUrl: './cat/share/t_edit_new_authority',
1828 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
1829 $scope.focusMe = true;
1831 $scope.dirty_flag = false;
1832 $scope.marc_xml = newAuthority,
1833 $scope.ok = function(args) { $uibModalInstance.close(args) }
1834 $scope.cancel = function () { $uibModalInstance.dismiss() }
1836 }).result.then(function (args) {
1837 if (!args || !args.authority_id) return;
1838 $scope.updateSubfieldZero(args.authority_id);
1841 $scope.updateSubfieldZero(newAuthority.id());
1851 .directive("egPhyscharWizard", ['$sce', function ($sce) {
1855 templateUrl: './cat/share/t_physchar_wizard',
1859 controller: ['$scope','$q','egTagTable',
1860 function ($scope , $q , egTagTable) {
1862 // $scope.step is the 1-based position in the list of
1863 // subfields for the currently selected type.
1864 // step==0 means we are currently selecting the type
1867 // position and offset of the "subfields" we're
1868 // currently editing; this is maintained as a convenience
1869 // for the highlighting of the currently active position
1873 if (!$scope.field.data)
1874 $scope.field.data = '';
1876 // currently selected subfield value selector option
1877 $scope.selected_option = null;
1879 function current_ptype() {
1880 return $scope.field.data.substr(0, 1);
1883 function current_subfield() {
1884 return egTagTable.getPhysCharSubfieldMap(current_ptype())
1885 .then(function(sf_list) {return sf_list[$scope.step-1]});
1888 $scope.values_for_step = [];
1889 function set_values_for_step() {
1892 if ($scope.step == 0) {
1895 promise = egTagTable.getPhysCharTypeMap();
1897 promise = current_subfield().then(
1898 function(subfield) {
1900 .getPhysCharValueMap(subfield.id());
1905 return promise.then(function(list) {
1906 $scope.values_for_step = list;
1907 set_selected_option_from_field();
1908 set_label_for_step();
1912 $scope.change_ptype = function(option) {
1913 $scope.selected_option = option;
1914 var new_val = option.ptype_key();
1915 if (current_ptype() != new_val) {
1916 $scope.field.data = new_val; // total reset
1920 $scope.change_option = function(option) {
1921 $scope.selected_option = option;
1922 var new_val = option.value();
1923 get_step_slot().then(function(slot) {
1924 var value = $scope.field.data;
1925 while (value.length < (slot[0] + slot[1]))
1927 var before = value.substr(0, slot[0]);
1928 var after = value.substr(slot[0] + slot[1]);
1930 before + new_val.substr(0, slot[1]) + after;
1931 $scope.offset = slot[0];
1932 $scope.len = slot[1];
1936 function get_step_slot() {
1937 if ($scope.step == 0) return $q.when([0, 1]);
1938 return current_subfield().then(function(sf) {
1939 return [sf.start_pos(), sf.length()]
1943 $scope.is_last_step = function() {
1944 // This one is called w/ every digest, so avoid async
1945 // calls. Wait until we have loaded the current ptype
1946 // subfields to determine if this is the last step.
1949 egTagTable.phys_char_sf_map[current_ptype()] &&
1950 egTagTable.phys_char_sf_map[current_ptype()].length
1955 $scope.label_for_step = '';
1956 function set_label_for_step() {
1957 if ($scope.step > 0) {
1958 current_subfield().then(function(sf) {
1959 $scope.label_for_step = sf.label();
1964 $scope.next_step = function() {
1966 set_values_for_step();
1969 $scope.prev_step = function() {
1971 set_values_for_step();
1974 function set_selected_option_from_field() {
1975 if ($scope.step == 0) {
1976 $scope.selected_option = $scope.values_for_step
1977 .filter(function(opt) {
1978 return (opt.ptype_key() == current_ptype())})[0];
1980 get_step_slot().then(function(slot) {
1981 $scope.offset = slot[0];
1982 $scope.len = slot[1];
1983 var val = String.prototype.substr.apply(
1984 $scope.field.data, slot);
1986 $scope.selected_option = $scope.values_for_step
1987 .filter(function(opt) {
1988 return (opt.value() == val)})[0];
1990 $scope.selected_option = null;
1996 $scope.highlightedFieldData = function() {
1998 $scope.len && $scope.field.data &&
1999 $scope.field.data.length > 0 &&
2000 $scope.field.data.length >= $scope.offset
2002 return $sce.trustAsHtml(
2003 $scope.field.data.substring(0, $scope.offset) +
2004 '<span class="active-physchar">' +
2005 $scope.field.data.substr($scope.offset, $scope.len) +
2007 $scope.field.data.substr($scope.offset + $scope.len)
2010 return $scope.field.data;
2014 set_values_for_step();
2021 .directive("egMarcEditAuthorityBrowser", function () {
2025 templateUrl: './cat/share/t_authority_browser',
2032 controller: ['$scope','$http',
2033 function ($scope , $http) {
2037 $scope.main_headings = [];
2039 function getHeadingString(headingField) {
2041 angular.forEach(headingField.subfields, function (sf) {
2042 if (['x', 'y', 'z'].indexOf(sf[0]) > -1) {
2053 $scope.doBrowse = function() {
2054 $scope.main_headings.length = 0;
2055 if ($scope.searchString.length == 0) return;
2056 var type = 'authority.'
2057 var url = '/opac/extras/browse/marcxml/'
2058 + 'authority.' + $scope.axis + '.refs'
2059 + '/1' // OU - currently unscoped
2060 + '/' + $scope.searchString
2062 + '/' + $scope.limit;
2066 transformResponse : function(data) {
2067 // use a bit of jQuery to deal with the XML
2068 var $xml = $( $.parseXML(data) );
2070 $xml.find('record').each(function() {
2071 var rec = new MARC21.Record();
2072 rec.fromXmlDocument($(this)[0].outerHTML);
2077 }).then(function(response) {
2078 angular.forEach(response.data, function(rec) {
2079 var authId = rec.subfield('901', 'c')[1];
2081 if (rec.field('003')) {
2082 auth_org = rec.field('003').data;
2084 var headingField = rec.field('1..');
2085 var seeFroms = rec.field('4..', true);
2086 var seeAlsos = rec.field('5..', true);
2088 var main_heading = {
2089 authority_id : authId,
2090 heading : getHeadingString(headingField),
2091 seealso_headings : [ ],
2092 seefrom_headings : [ ],
2097 sfZero = '(' + auth_org + ')';
2100 headingField.addSubfields('0', sfZero);
2102 main_heading['headingField'] = headingField;
2103 angular.forEach(seeAlsos, function(headingField) {
2104 main_heading.seealso_headings.push({
2105 heading : getHeadingString(headingField),
2106 headingField : headingField
2109 angular.forEach(seeFroms, function(headingField) {
2110 main_heading.seefrom_headings.push({
2111 heading : getHeadingString(headingField),
2112 headingField : headingField
2115 $scope.main_headings.push(main_heading);
2120 $scope.$watch('searchString',
2121 function(newVal, oldVal) {
2122 if (newVal !== oldVal) {
2127 $scope.$watch('page',
2128 function(newVal, oldVal) {
2129 if (newVal !== oldVal) {