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','egConfirmDialog','egAlertDialog',
700 function ( $timeout , $scope , $q, $window , egCore , egTagTable , egConfirmDialog , egAlertDialog ) {
703 $scope.onSaveCallback = $scope.onSave;
704 if (typeof $scope.onSaveCallback !== 'undefined' && !angular.isArray($scope.onSaveCallback))
705 $scope.onSaveCallback = [ $scope.onSaveCallback ];
707 MARC21.Record.delimiter = '$';
709 $scope.enable_fast_add = false;
710 $scope.fast_item_callnumber = '';
711 $scope.fast_item_barcode = '';
713 $scope.flatEditor = { isEnabled : $scope.flatOnly ? true : false };
715 egCore.hatch.getItem('cat.marcedit.flateditor').then(function(val) {
716 $scope.flatEditor.isEnabled = val;
719 $scope.$watch('flatEditor.isEnabled', function (newVal, oldVal) {
720 if (newVal != oldVal) egCore.hatch.setItem('cat.marcedit.flateditor', newVal);
723 // necessary to prevent ng-model scope hiding ugliness in egMarcEditBibSource:
724 $scope.bib_source = {
725 id : $scope.bibSource ? $scope.bibSource : null
727 $scope.brandNewRecord = false;
728 $scope.record_type = $scope.recordType || 'bre';
729 $scope.max_undo = $scope.maxUndo || 100;
730 $scope.record_undo_stack = [];
731 $scope.record_redo_stack = [];
732 $scope.in_undo = false;
733 $scope.in_redo = false;
734 $scope.record = new MARC21.Record();
735 $scope.save_stack_depth = 0;
736 $scope.controlfields = [];
737 $scope.datafields = [];
738 $scope.controlSet = egTagTable.getAuthorityControlSet();
739 $scope.showHelp = false;
740 $scope.stackSubfields = { enabled : false };
741 egCore.hatch.getItem('cat.marcedit.stack_subfields').then(function(val) {
742 $scope.stackSubfields.enabled = val;
744 $scope.$watch('stackSubfields.enabled', function (newVal, oldVal) {
745 if (newVal != oldVal) egCore.hatch.setItem('cat.marcedit.stack_subfields', newVal);
747 $scope.caretRecId = $scope.recordId;
749 egTagTable.loadTagTable({ marcRecordType : $scope.record_type });
751 $scope.saveFlatTextMARC = function () {
752 $scope.record = new MARC21.Record({ marcbreaker : $scope.flat_text_marc });
755 $scope.refreshVisual = function () {
756 if (!$scope.flatEditor.isEnabled) {
757 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
758 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
762 var addDatafield = function (e,before) {
763 var element = $(e.target);
765 var index_field = e.data.scope.field.position;
766 var new_field_index = index_field;
768 var new_field = new MARC21.Field({
770 subfields : [[' ','',0]]
773 if (Boolean(before)) {
774 e.data.scope.field.record.insertFieldsBefore(
779 e.data.scope.field.record.insertFieldsAfter(
786 $scope.current_event_target = 'r' + $scope.caretRecId +
787 'f' + new_field_index + 'tag';
789 $scope.current_event_target_cursor_pos = 0;
790 $scope.current_event_target_cursor_pos_end = 3;
791 $scope.force_render = true;
793 $timeout(function(){$scope.$digest()}).then(setCaret);
796 var deleteDatafield = function (e) {
797 var del_field = e.data.scope.field.position;
799 var sf901c = e.data.scope.field.record.subfield('901','c');
800 var recId = (sf901c === null) ? '' : sf901c[1];
801 var domnode = $('#r' + recId + 'f' + del_field);
803 e.data.scope.field.record.deleteFields(
807 domnode.scope().$destroy();
810 $scope.current_event_target = 'r' + $scope.caretRecId +
811 'f' + del_field + 'tag';
813 $scope.current_event_target_cursor_pos = 0;
814 $scope.current_event_target_cursor_pos_end = 0
815 $scope.force_render = true;
817 $timeout(function(){$scope.$digest()}).then(setCaret);
820 var add006 = function (e) {
821 e.data.scope.field.record.insertOrderedFields(
828 $scope.force_render = true;
829 $timeout(function(){$scope.$digest()}).then(setCaret);
832 var add007 = function (e) {
833 e.data.scope.field.record.insertOrderedFields(
840 $scope.force_render = true;
841 $timeout(function(){$scope.$digest()}).then(setCaret);
844 var reify008 = function (e) {
845 var new_008_data = e.data.scope.field.record.generate008();
848 var old_008s = e.data.scope.field.record.field('008',true);
849 old_008s.forEach(function(o) {
850 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
851 domnode.scope().$destroy();
853 e.data.scope.field.record.deleteFields(o);
856 e.data.scope.field.record.insertOrderedFields(
863 $scope.force_render = true;
864 $timeout(function(){$scope.$digest()}).then(setCaret);
867 $scope.context_functions = {
868 addDatafield : addDatafield,
869 deleteDatafield : deleteDatafield,
875 $scope.onKeydown = function (event) {
876 var event_return = true;
879 'keydown: which='+event.which+
880 ', ctrlKey='+event.ctrlKey+
881 ', shiftKey='+event.shiftKey+
882 ', altKey='+event.altKey+
883 ', metaKey='+event.altKey
886 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
887 event_return = $scope.processRedo();
888 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
889 event_return = $scope.processUndo();
890 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
892 var element = $(event.target);
893 var new_sf, index_sf, move_data;
895 if (element.hasClass('marcsfvalue')) {
896 index_sf = event.data.scope.subfield[2];
897 new_sf = index_sf + 1;
899 var start = event.target.selectionStart || getCaretPosEditableDiv(element);
901 if (event.target.value){
902 end = event.target.selectionEnd - event.target.selectionStart ?
903 event.target.selectionEnd :
904 event.target.value.length;
906 end = element.text().length;
909 move_data = element.value ?
910 element.value.substring(start,end) :
911 element.text().substring(start, end);
913 } else if (element.hasClass('marcsfcode')) {
914 index_sf = event.data.scope.subfield[2];
915 new_sf = index_sf + 1;
916 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
921 $scope.current_event_target = 'r' + $scope.caretRecId +
922 'f' + event.data.scope.field.position +
923 's' + new_sf + 'code';
925 event.data.scope.field.subfields.forEach(function(sf) {
926 if (sf[2] >= new_sf) sf[2]++;
927 if (sf[2] == index_sf) {
928 sf[1] = event.target.value ?
929 event.target.value.substring(0,start) + event.target.value.substring(end) :
930 element.text().substring(0, start);
933 event.data.scope.field.subfields.splice(
936 [' ', move_data, new_sf ]
939 $scope.current_event_target_cursor_pos = 0;
940 $scope.current_event_target_cursor_pos_end = 1;
942 $timeout(function(){$scope.$digest()}).then(setCaret);
944 event_return = false;
946 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
948 event_return = false;
950 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
952 event_return = false;
954 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
956 event_return = false;
958 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
959 addDatafield(event, event.shiftKey); // shift key inserts before
960 event_return = false;
962 } else if (event.which == 13 &&
963 ($(event.target).hasClass('marcsf') || $(event.target.parentNode).hasClass('marcsf'))
965 // bare return; don't allow it
966 event_return = false;
968 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
969 deleteDatafield(event);
970 event_return = false;
972 } else if (event.which == 46 && event.shiftKey && ($(event.target).hasClass('marcsf') || $(event.target.parentNode).hasClass('marcsf'))) {
973 // shift+del, remove subfield
975 var sf = event.data.scope.subfield[2] - 1;
976 if (sf == -1) sf = 0;
978 event.data.scope.field.deleteExactSubfields(
979 event.data.scope.subfield
982 if (!event.data.scope.field.subfields[sf]) {
983 $scope.current_event_target = 'r' + $scope.caretRecId +
984 'f' + event.data.scope.field.position +
987 $scope.current_event_target = 'r' + $scope.caretRecId +
988 'f' + event.data.scope.field.position +
992 $scope.current_event_target_cursor_pos = 0;
993 $scope.current_event_target_cursor_pos_end = 0;
994 $scope.force_render = true;
996 $timeout(function(){$scope.$digest()}).then(setCaret);
998 event_return = false;
1000 } else if (event.keyCode == 38) {
1001 if (event.ctrlKey) { // copy the field up
1002 var index_field = event.data.scope.field.position;
1005 if (event.data.scope.field.isControlfield()) {
1006 field_obj = new MARC21.Field({
1007 tag : event.data.scope.field.tag,
1008 data : event.data.scope.field.data
1012 for (var i in event.data.scope.field.subfields) {
1013 sf_clone.push(event.data.scope.field.subfields[i].slice());
1015 field_obj = new MARC21.Field({
1016 tag : event.data.scope.field.tag,
1017 ind1 : event.data.scope.field.ind1,
1018 ind2 : event.data.scope.field.ind2,
1019 subfields : sf_clone
1024 event.data.scope.field.record.insertFieldsBefore(
1025 event.data.scope.field,
1029 $scope.current_event_target = 'r' + $scope.caretRecId +
1030 'f' + index_field + 'tag';
1032 $scope.current_event_target_cursor_pos = 0;
1033 $scope.current_event_target_cursor_pos_end = 3;
1034 $scope.force_render = true;
1036 $timeout(function(){$scope.$digest()}).then(setCaret);
1038 } else { // jump to prev field
1039 if (event.data.scope.field.position > 0) {
1040 $timeout(function(){
1041 $scope.current_event_target_cursor_pos = 0;
1042 $scope.current_event_target_cursor_pos_end = 0;
1043 $scope.current_event_target = 'r' + $scope.caretRecId +
1044 'f' + (event.data.scope.field.position - 1) +
1050 event_return = false;
1052 } else if (event.keyCode == 40) { // down arrow...
1053 if (event.ctrlKey) { // copy the field down
1055 var index_field = event.data.scope.field.position;
1056 var new_field = index_field + 1;
1059 if (event.data.scope.field.isControlfield()) {
1060 field_obj = new MARC21.Field({
1061 tag : event.data.scope.field.tag,
1062 data : event.data.scope.field.data
1066 for (var i in event.data.scope.field.subfields) {
1067 sf_clone.push(event.data.scope.field.subfields[i].slice());
1069 field_obj = new MARC21.Field({
1070 tag : event.data.scope.field.tag,
1071 ind1 : event.data.scope.field.ind1,
1072 ind2 : event.data.scope.field.ind2,
1073 subfields : sf_clone
1077 event.data.scope.field.record.insertFieldsAfter(
1078 event.data.scope.field,
1082 $scope.current_event_target = 'r' + $scope.caretRecId +
1083 'f' + new_field + 'tag';
1085 $scope.current_event_target_cursor_pos = 0;
1086 $scope.current_event_target_cursor_pos_end = 3;
1087 $scope.force_render = true;
1089 $timeout(function(){$scope.$digest()}).then(setCaret);
1091 } else { // jump to next field
1092 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
1093 $timeout(function(){
1094 $scope.current_event_target_cursor_pos = 0;
1095 $scope.current_event_target_cursor_pos_end = 0;
1096 $scope.current_event_target = 'r' + $scope.caretRecId +
1097 'f' + (event.data.scope.field.position + 1) +
1103 event_return = false;
1105 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
1106 $scope.current_event_target = $(event.target).hasClass('focusable') ? $(event.target) : null;//.attr('id');
1107 if ($scope.current_event_target) {
1108 $scope.current_event_target_cursor_pos =
1109 event.target.selectionDirection=='backward' ?
1110 event.target.selectionStart :
1111 event.target.selectionEnd;
1115 return event_return;
1118 function setCaret() {
1119 if ($scope.current_event_target) {
1120 console.log("Putting caret in " + $scope.current_event_target);
1121 if (!$scope.current_event_target_cursor_pos_end)
1122 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
1124 var element = $('#'+$scope.current_event_target + " .focusable").get(0);
1127 if (element.setSelectionRange) {
1128 element.setSelectionRange(
1129 $scope.current_event_target_cursor_pos,
1130 $scope.current_event_target_cursor_pos_end
1134 $scope.current_event_cursor_pos_end = null;
1135 $scope.current_event_target = null;
1139 function getCaretPosEditableDiv(editableDiv){
1140 var caretPos = 0, sel, range;
1141 if (window.getSelection) {
1142 sel = window.getSelection();
1143 if (sel.rangeCount) {
1144 range = sel.getRangeAt(0);
1145 if (range.commonAncestorContainer.parentNode == editableDiv[0]) {
1146 caretPos = range.endOffset;
1153 function loadRecord() {
1154 return (function() {
1155 var deferred = $q.defer();
1156 if ($scope.recordId) {
1157 egCore.pcrud.retrieve(
1158 $scope.record_type, $scope.recordId
1159 ).then(function(rec) {
1160 deferred.resolve(rec);
1163 if ($scope.recordType == 'bre') {
1164 var bre = new egCore.idl.bre();
1165 bre.marc($scope.marcXml);
1166 deferred.resolve(bre);
1167 } else if ($scope.recordType == 'are') {
1168 var are = new egCore.idl.are();
1169 are.marc($scope.marcXml);
1170 deferred.resolve(are);
1171 } else if ($scope.recordType == 'sre') {
1172 var sre = new egCore.idl.sre();
1173 sre.marc($scope.marcXml);
1174 deferred.resolve(sre);
1176 $scope.brandNewRecord = true;
1178 return deferred.promise;
1179 })().then(function(rec) {
1180 $scope.in_redo = true;
1181 $scope[$scope.record_type] = rec;
1182 $scope.record = new MARC21.Record({ marcxml : $scope.Record().marc() });
1183 if (!$scope.recordId) {
1184 var sf901c = $scope.record.subfield('901', 'c');
1185 if (sf901c !== null) {
1186 $scope.caretRecId = sf901c[1];
1189 $scope.calculated_record_type = $scope.record.recordType();
1190 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1191 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1192 $scope.save_stack_depth = $scope.record_undo_stack.length;
1193 $scope.dirtyFlag = false;
1194 $scope.flat_text_marc = $scope.record.toBreaker();
1196 if ($scope.record_type == 'bre' && !$scope.brandNewRecord) {
1197 $scope.bib_source.id = $scope.bibSource = rec.source(); //$scope.Record().source();
1201 return egTagTable.fetchFFPosTable($scope.calculated_record_type)
1203 return egTagTable.fetchFFValueTable($scope.calculated_record_type)
1207 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
1208 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
1209 $scope.record_undo_stack.push({
1211 target: $scope.current_event_target,
1212 pos: $scope.current_event_target_cursor_pos
1215 if ($scope.force_render) {
1216 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1217 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1218 $scope.force_render = false;
1221 $scope.flat_text_marc = newVal;
1224 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
1225 $scope.dirtyFlag = true;
1227 $scope.dirtyFlag = false;
1230 if ($scope.record_undo_stack.length > $scope.max_undo)
1231 $scope.record_undo_stack.shift();
1233 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
1234 $scope.in_redo = false;
1235 $scope.in_undo = false;
1238 $scope.processUndo = function () {
1239 if ($scope.record_undo_stack.length) {
1240 $scope.in_undo = true;
1242 var undo_item = $scope.record_undo_stack.pop();
1243 $scope.record_redo_stack.push(undo_item);
1245 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
1246 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1247 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1249 $scope.current_event_target = undo_item.target;
1250 $scope.current_event_target_cursor_pos = undo_item.pos;
1251 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1253 $timeout(function(){$scope.$digest()}).then(setCaret);
1260 $scope.processRedo = function () {
1261 if ($scope.record_redo_stack.length) {
1262 $scope.in_redo = true;
1264 var redo_item = $scope.record_redo_stack.pop();
1265 $scope.record_undo_stack.push(redo_item);
1267 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
1268 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1269 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1271 $scope.current_event_target = redo_item.target;
1272 $scope.current_event_target_cursor_pos = redo_item.pos;
1273 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1275 $timeout(function(){$scope.$digest()}).then(setCaret);
1282 $scope.Record = function () {
1283 return $scope[$scope.record_type];
1286 $scope.deleteRecord = function () {
1287 egConfirmDialog.open(
1288 egCore.strings.CONFIRM_DELETE_RECORD,
1289 (($scope.record_type == 'bre') ?
1290 egCore.strings.CONFIRM_DELETE_BRE_MSG :
1291 egCore.strings.CONFIRM_DELETE_ARE_MSG),
1292 { id : $scope.recordId }
1293 ).result.then(function() {
1294 if ($scope.record_type == 'bre') {
1297 'open-ils.cat.biblio.record_entry.delete',
1298 egCore.auth.token(), $scope.recordId
1299 ).then(function(resp) {
1300 var evt = egCore.evt.parse(resp);
1302 return egAlertDialog.open(
1303 egCore.strings.ALERT_DELETE_FAILED,
1304 { id : $scope.recordId, desc : evt.desc }
1307 loadRecord().then(processOnSaveCallbacks);
1311 $scope.Record().deleted(true);
1312 return $scope.saveRecord();
1317 $scope.undeleteRecord = function () {
1318 $scope.Record().deleted(false);
1319 return $scope.saveRecord();
1322 $scope.validateHeadings = function () {
1323 if ($scope.record_type != 'bre') return;
1324 var chain = $q.when();
1325 angular.forEach($scope.record.fields, function(f) {
1326 if (!$scope.controlSet.bibFieldByTag(f.tag)) return;
1327 // if heading already has a $0, assume it's good
1328 if (f.subfield('0', true).length) {
1329 f.heading_checked = true;
1330 f.heading_valid = true;
1333 var auth_match = $scope.controlSet.bibToAuthorities(f);
1334 if (auth_match.length == 0) return;
1335 chain = chain.then(function() {
1336 var promise = egCore.net.request(
1338 'open-ils.search.authority.simple_heading.from_xml.batch.atomic',
1340 ).then(function (matches) {
1341 f.heading_valid = false;
1342 if (matches[0]) { // probably set
1343 for (var cset in matches[0]) {
1344 var arr = matches[0][cset];
1346 // protect against errant empty string values
1347 if (arr.length == 1 && arr[0] == '')
1349 f.heading_valid = true;
1354 f.heading_checked = true;
1361 processOnSaveCallbacks = function() {
1362 var deferred = $q.defer();
1363 if (typeof $scope.onSaveCallback !== 'undefined') {
1364 var promise = deferred.promise;
1366 angular.forEach($scope.onSaveCallback, function (f) {
1367 if (angular.isFunction(f)) promise = promise.then(f);
1371 return deferred.resolve($scope.recordId)
1374 $scope.saveRecord = function () {
1376 if ($scope.inPlaceMode) {
1377 $scope.marcXml = $scope.record.toXmlString();
1379 if ($scope.record_type == 'bre'){
1380 $scope.bibSource = $scope.bib_source.id;
1383 return $timeout(processOnSaveCallbacks);
1386 $scope.mangle_005();
1387 $scope.Record().editor(egCore.auth.user().id());
1388 $scope.Record().edit_date('now');
1389 $scope.record.pruneEmptyFieldsAndSubfields();
1390 $scope.Record().marc($scope.record.toXmlString());
1391 if ($scope.recordId) {
1392 return egCore.pcrud.update(
1394 ).then(function() { // success
1395 $scope.save_stack_depth = $scope.record_undo_stack.length;
1396 $scope.dirtyFlag = false;
1397 if ($scope.enable_fast_add) {
1400 'open-ils.actor.anon_cache.set_value',
1401 null, 'edit-these-copies', {
1402 record_id: $scope.recordId,
1404 label : $scope.fast_item_callnumber,
1405 barcode : $scope.fast_item_barcode,
1411 ).then(function(key) {
1413 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1414 $timeout(function() { $window.open(url, '_blank') });
1416 alert('Could not create anonymous cache key!');
1420 }, function() { // failure
1421 alert('Could not save the record!');
1422 }).then(loadRecord).then(processOnSaveCallbacks);
1424 $scope.Record().creator(egCore.auth.user().id());
1425 $scope.Record().create_date('now');
1426 return egCore.pcrud.create(
1428 ).then(function(bre) {
1429 $scope.recordId = bre.id();
1430 $scope.caretRecId = $scope.recordId;
1431 if ($scope.enable_fast_add) {
1434 'open-ils.actor.anon_cache.set_value',
1435 null, 'edit-these-copies', {
1436 record_id: $scope.recordId,
1438 label : $scope.fast_item_callnumber,
1439 barcode : $scope.fast_item_barcode,
1444 ).then(function(key) {
1446 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1447 $timeout(function() { $window.open(url, '_blank') });
1449 alert('Could not create anonymous cache key!');
1453 }).then(loadRecord).then(processOnSaveCallbacks);
1459 $scope.seeBreaker = function () {
1460 alert($scope.record.toBreaker());
1463 $scope.$watch('recordId',
1464 function(newVal, oldVal) {
1465 if (newVal && newVal !== oldVal) {
1470 $scope.$watch('marcXml',
1471 function(newVal, oldVal) {
1472 if (newVal && newVal !== oldVal) {
1478 var unregister = $scope.$watch(function() {
1479 return egTagTable.initialized();
1483 if ($scope.recordId || $scope.marcXml) {
1489 $scope.mangle_005 = function () {
1490 var now = new Date();
1491 var y = now.getUTCFullYear();
1493 var m = now.getUTCMonth() + 1;
1494 if (m < 10) m = '0' + m;
1496 var d = now.getUTCDate();
1497 if (d < 10) d = '0' + d;
1499 var H = now.getUTCHours();
1500 if (H < 10) H = '0' + H;
1502 var M = now.getUTCMinutes();
1503 if (M < 10) M = '0' + M;
1505 var S = now.getUTCSeconds();
1506 if (S < 10) S = '0' + S;
1508 var stamp = '' + y + m + d + H + M + S + '.0';
1509 var f = $scope.record.field('005',true)[0];
1513 $scope.record.insertOrderedFields(
1528 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
1532 template: '<span class="nullable">'+
1533 '<select class="form-control" ng-model="bib_source.id" ng-options="s.id() as s.source() for s in bib_sources | orderBy: \'source()\'">'+
1534 '<option value="">Select a Source</option>'+
1537 controller: ['$scope','egCore',
1538 function ($scope , egCore) {
1540 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
1541 .then(function(list) {
1542 $scope.bib_sources = list;
1545 $scope.$watch('bib_source.id',
1546 function(newVal, oldVal) {
1547 if (newVal !== oldVal) {
1548 $scope.bre.source(newVal);
1558 .directive("egMarcEditAuthorityLinker", function () {
1562 templateUrl: './cat/share/t_authority_linker',
1568 controller: ['$scope','$uibModal','egCore','egAuth',
1569 function ($scope , $uibModal, egCore, egAuth) {
1571 $scope.searchStr = '';
1572 var cni = egCore.env.aous['cat.marc_control_number_identifier'] ||
1573 'Set cat.marc_control_number_identifier in Library Settings';
1575 var axis_list = $scope.controlSet.bibFieldBrowseAxes($scope.bibField.tag);
1576 $scope.axis = axis_list[0];
1578 $scope._controlled_sf_list = {};
1579 $scope._controlled_auth_sf_list = {};
1581 angular.forEach($scope.controlSet.controlSetList(), function(acs_id) {
1582 if ($scope.controlSet.controlSet(acs_id).control_map[$scope.bibField.tag])
1583 found_acs.push(acs_id);
1585 if (found_acs.length) {
1586 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag],
1587 function(value, sf_label) {
1588 $scope._controlled_sf_list[ sf_label ] = 1;
1589 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag][sf_label],
1590 function(auth_sf, auth_tag) {
1591 if (!$scope._controlled_auth_sf_list[auth_tag]) {
1592 $scope._controlled_auth_sf_list[auth_tag] = { };
1594 $scope._controlled_auth_sf_list[auth_tag][auth_sf] = 1;
1601 $scope.bibField.subfields.forEach(function (sf) {
1602 if (sf[0] in $scope._controlled_sf_list) {
1604 sf.selectable = true;
1606 sf.selectable = false;
1609 $scope.summarizeField = function() {
1611 'tag': $scope.bibField.tag,
1612 'ind1': $scope.bibField.ind1,
1613 'ind2': $scope.bibField.ind2,
1616 $scope.bibField.subfields.forEach(function(sf) {
1618 source_f.subfields.push([ sf[0], sf[1] ]);
1623 $scope.getSearchString = function() {
1624 var source_f = $scope.summarizeField();
1626 angular.forEach(source_f.subfields, function(val) {
1627 values.push(val[1]);
1629 return values.join(' ');
1631 $scope.searchStr = $scope.getSearchString();
1632 $scope.$watch(function() {
1634 angular.forEach($scope.bibField.subfields, function(sf) {
1635 if (sf.selected) ct++
1639 function(newVal, oldVal) {
1640 $scope.searchStr = $scope.getSearchString();
1643 $scope.updateSubfieldZero = function(value) {
1644 $scope.changed = true;
1645 $scope.bibField.deleteSubfield({ code : ['0'] });
1646 $scope.bibField.subfields.push([
1647 '0', '(' + cni + ')' + value
1651 $scope.applyHeading = function(headingField) {
1652 // TODO: move the MARC21 rules for copying indicators
1654 if (headingField.tag == '130' && $scope.bibField.tag == '130') {
1655 $scope.bibField.ind1 = headingField.ind2;
1657 $scope.bibField.ind1 = headingField.ind1;
1659 // deal with 4xx and 5xx
1660 var authFallbackTag = '1' + headingField.tag.substr(1, 2);
1661 var _valid_auth_sfs = (headingField.tag in $scope._controlled_auth_sf_list) ?
1662 $scope._controlled_auth_sf_list[headingField.tag] :
1663 (authFallbackTag in $scope._controlled_auth_sf_list) ?
1664 $scope._controlled_auth_sf_list[authFallbackTag] :
1666 // save the $0 for later use
1668 if (headingField.subfield('0')) {
1669 sfZero = headingField.subfield('0')[1];
1671 // grab any bib subfields not under authority control
1672 // TODO do something about uncontrolled subdivisions
1673 var uncontrolledBibSf = [];
1674 angular.forEach($scope.bibField.subfields, function(sf) {
1675 if (!(sf[0] in $scope._controlled_sf_list) && (sf[0] != '0')) {
1676 uncontrolledBibSf.push([ sf[0], sf[1] ]);
1679 // grab the authority subfields
1680 var authoritySf = [];
1681 angular.forEach(headingField.subfields, function(sf) {
1682 if (sf[0] in _valid_auth_sfs) {
1683 authoritySf.push([ sf[0], sf[1] ]);
1686 $scope.bibField.subfields.length = 0;
1687 angular.forEach(authoritySf, function(sf) {
1688 $scope.bibField.addSubfields(sf[0], sf[1]);
1690 angular.forEach(uncontrolledBibSf, function(sf) {
1691 $scope.bibField.addSubfields(sf[0], sf[1]);
1694 $scope.bibField.addSubfields('0', sfZero);
1696 $scope.bibField.subfields.forEach(function (sf) {
1697 if (sf[0] in $scope._controlled_sf_list) {
1698 // intentionally not selecting any subfields
1699 // after we've applied an authority heading
1700 sf.selected = false;
1701 sf.selectable = true;
1703 sf.selectable = false;
1706 $scope.changed = true;
1709 $scope.createAuthorityFromBib = function(spawn_editor) {
1710 var source_f = $scope.summarizeField();
1712 var args = { authority_id : 0 };
1713 var method = (spawn_editor) ?
1714 'open-ils.cat.authority.record.create_from_bib.readonly' :
1715 'open-ils.cat.authority.record.create_from_bib';
1722 ).then(function(newAuthority) {
1725 templateUrl: './cat/share/t_edit_new_authority',
1728 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
1729 $scope.focusMe = true;
1731 $scope.dirty_flag = false;
1732 $scope.marc_xml = newAuthority,
1733 $scope.ok = function(args) { $uibModalInstance.close(args) }
1734 $scope.cancel = function () { $uibModalInstance.dismiss() }
1736 }).result.then(function (args) {
1737 if (!args || !args.authority_id) return;
1738 $scope.updateSubfieldZero(args.authority_id);
1741 $scope.updateSubfieldZero(newAuthority.id());
1751 .directive("egPhyscharWizard", ['$sce', function ($sce) {
1755 templateUrl: './cat/share/t_physchar_wizard',
1759 controller: ['$scope','$q','egTagTable',
1760 function ($scope , $q , egTagTable) {
1762 // $scope.step is the 1-based position in the list of
1763 // subfields for the currently selected type.
1764 // step==0 means we are currently selecting the type
1767 // position and offset of the "subfields" we're
1768 // currently editing; this is maintained as a convenience
1769 // for the highlighting of the currently active position
1773 if (!$scope.field.data)
1774 $scope.field.data = '';
1776 // currently selected subfield value selector option
1777 $scope.selected_option = null;
1779 function current_ptype() {
1780 return $scope.field.data.substr(0, 1);
1783 function current_subfield() {
1784 return egTagTable.getPhysCharSubfieldMap(current_ptype())
1785 .then(function(sf_list) {return sf_list[$scope.step-1]});
1788 $scope.values_for_step = [];
1789 function set_values_for_step() {
1792 if ($scope.step == 0) {
1795 promise = egTagTable.getPhysCharTypeMap();
1797 promise = current_subfield().then(
1798 function(subfield) {
1800 .getPhysCharValueMap(subfield.id());
1805 return promise.then(function(list) {
1806 $scope.values_for_step = list;
1807 set_selected_option_from_field();
1808 set_label_for_step();
1812 $scope.change_ptype = function(option) {
1813 $scope.selected_option = option;
1814 var new_val = option.ptype_key();
1815 if (current_ptype() != new_val) {
1816 $scope.field.data = new_val; // total reset
1820 $scope.change_option = function(option) {
1821 $scope.selected_option = option;
1822 var new_val = option.value();
1823 get_step_slot().then(function(slot) {
1824 var value = $scope.field.data;
1825 while (value.length < (slot[0] + slot[1]))
1827 var before = value.substr(0, slot[0]);
1828 var after = value.substr(slot[0] + slot[1]);
1830 before + new_val.substr(0, slot[1]) + after;
1831 $scope.offset = slot[0];
1832 $scope.len = slot[1];
1836 function get_step_slot() {
1837 if ($scope.step == 0) return $q.when([0, 1]);
1838 return current_subfield().then(function(sf) {
1839 return [sf.start_pos(), sf.length()]
1843 $scope.is_last_step = function() {
1844 // This one is called w/ every digest, so avoid async
1845 // calls. Wait until we have loaded the current ptype
1846 // subfields to determine if this is the last step.
1849 egTagTable.phys_char_sf_map[current_ptype()] &&
1850 egTagTable.phys_char_sf_map[current_ptype()].length
1855 $scope.label_for_step = '';
1856 function set_label_for_step() {
1857 if ($scope.step > 0) {
1858 current_subfield().then(function(sf) {
1859 $scope.label_for_step = sf.label();
1864 $scope.next_step = function() {
1866 set_values_for_step();
1869 $scope.prev_step = function() {
1871 set_values_for_step();
1874 function set_selected_option_from_field() {
1875 if ($scope.step == 0) {
1876 $scope.selected_option = $scope.values_for_step
1877 .filter(function(opt) {
1878 return (opt.ptype_key() == current_ptype())})[0];
1880 get_step_slot().then(function(slot) {
1881 $scope.offset = slot[0];
1882 $scope.len = slot[1];
1883 var val = String.prototype.substr.apply(
1884 $scope.field.data, slot);
1886 $scope.selected_option = $scope.values_for_step
1887 .filter(function(opt) {
1888 return (opt.value() == val)})[0];
1890 $scope.selected_option = null;
1896 $scope.highlightedFieldData = function() {
1898 $scope.len && $scope.field.data &&
1899 $scope.field.data.length > 0 &&
1900 $scope.field.data.length >= $scope.offset
1902 return $sce.trustAsHtml(
1903 $scope.field.data.substring(0, $scope.offset) +
1904 '<span class="active-physchar">' +
1905 $scope.field.data.substr($scope.offset, $scope.len) +
1907 $scope.field.data.substr($scope.offset + $scope.len)
1910 return $scope.field.data;
1914 set_values_for_step();
1921 .directive("egMarcEditAuthorityBrowser", function () {
1925 templateUrl: './cat/share/t_authority_browser',
1932 controller: ['$scope','$http',
1933 function ($scope , $http) {
1937 $scope.main_headings = [];
1939 function getHeadingString(headingField) {
1941 angular.forEach(headingField.subfields, function (sf) {
1942 if (['x', 'y', 'z'].indexOf(sf[0]) > -1) {
1953 $scope.doBrowse = function() {
1954 $scope.main_headings.length = 0;
1955 if ($scope.searchString.length == 0) return;
1956 var type = 'authority.'
1957 var url = '/opac/extras/browse/marcxml/'
1958 + 'authority.' + $scope.axis + '.refs'
1959 + '/1' // OU - currently unscoped
1960 + '/' + $scope.searchString
1962 + '/' + $scope.limit;
1966 transformResponse : function(data) {
1967 // use a bit of jQuery to deal with the XML
1968 var $xml = $( $.parseXML(data) );
1970 $xml.find('record').each(function() {
1971 var rec = new MARC21.Record();
1972 rec.fromXmlDocument($(this)[0].outerHTML);
1977 }).then(function(response) {
1978 angular.forEach(response.data, function(rec) {
1979 var authId = rec.subfield('901', 'c')[1];
1981 if (rec.field('003')) {
1982 auth_org = rec.field('003').data;
1984 var headingField = rec.field('1..');
1985 var seeFroms = rec.field('4..', true);
1986 var seeAlsos = rec.field('5..', true);
1988 var main_heading = {
1989 authority_id : authId,
1990 heading : getHeadingString(headingField),
1991 seealso_headings : [ ],
1992 seefrom_headings : [ ],
1997 sfZero = '(' + auth_org + ')';
2000 headingField.addSubfields('0', sfZero);
2002 main_heading['headingField'] = headingField;
2003 angular.forEach(seeAlsos, function(headingField) {
2004 main_heading.seealso_headings.push({
2005 heading : getHeadingString(headingField),
2006 headingField : headingField
2009 angular.forEach(seeFroms, function(headingField) {
2010 main_heading.seefrom_headings.push({
2011 heading : getHeadingString(headingField),
2012 headingField : headingField
2015 $scope.main_headings.push(main_heading);
2020 $scope.$watch('searchString',
2021 function(newVal, oldVal) {
2022 if (newVal !== oldVal) {
2027 $scope.$watch('page',
2028 function(newVal, oldVal) {
2029 if (newVal !== oldVal) {