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("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
52 'style="font-family: \'Lucida Console\', Monaco, monospace;" '+
53 'ng-model="content" '+
54 'size="{{content.length * 1.1}}" '+
55 'maxlength="{{max}}" '+
64 contextItemContainer: '@',
65 contextItemGenerator: '=',
70 controller : ['$scope',
73 if ($scope.contextItemContainer && angular.isArray($scope.$parent[$scope.contextItemContainer]))
74 $scope.item_container = $scope.$parent[$scope.contextItemContainer];
75 else if ($scope.contextItemGenerator)
76 $scope.item_generator = $scope.contextItemGenerator;
78 $scope.showContext = function (event) {
79 $scope.item_list = [];
80 if ($scope.item_container) {
81 $scope.item_list = $scope.item_container;
82 } else if ($scope.item_generator) {
83 // always recalculate; tag and/or subfield
84 // codes may have changed
86 var generator = $scope.item_generator;
87 if (!angular.isArray(generator)) generator = [generator];
90 angular.forEach(generator, function (g) {
95 else if (Boolean(sub_list[0]))
96 sub_list[0].divider = true;
98 $scope.item_list = $scope.item_list.concat(sub_list);
105 if (angular.isArray($scope.item_list) && $scope.item_list.length > 0) { // we have a list of values or transforms
106 console.log('Showing context menu...');
107 $('body').trigger('click');
109 $scope.contextMenuEvent = event;
111 '<ul class="dropdown-menu" role="menu" style="z-index: 2000;">'+
112 '<eg-context-menu-item context-menu-event="contextMenuEvent" ng-repeat="item in item_list" item="item" content="content"/>'+
115 var tnode = angular.element(tmpl);
116 $document.find('body').append(tnode);
124 $timeout(function() {
125 var e = $compile(tnode)($scope);
129 $('body').on('click.context_menu',function() {
130 $(tnode).css('display','none');
131 $('body').off('click.context_menu');
142 link: function (scope, element, attrs) {
144 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
146 if (Boolean(scope.selectOnFocus)) {
147 element.addClass('noSelection');
148 element.bind('focus', function () { element.select() });
151 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
153 element.bind('contextmenu', {scope : scope}, scope.showContext);
158 .directive("egMarcEditFixedField", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
162 template: '<div class="col-md-2">'+
163 '<div class="col-md-1"><label name="{{fixedField}}" for="{{fixedField}}_ff_input">{{fixedField}}</label></div>'+
164 '<div class="col-md-1"><input type="text" style="padding-left: 5px; margin-left: 1em" size="4" id="{{fixedField}}_ff_input"/></div>'+
166 scope: { record: "=", fixedField: "@" },
168 controller : ['$scope', '$element', 'egTagTable',
169 function ( $scope , $element , egTagTable) {
170 $($element).children().css({ display : 'none' });
172 $scope.content = null; // this is where context menus dump their values
173 $scope.item_container = [];
174 $scope.in_handler = false;
175 $scope.ready = false;
177 $scope.$watch('content', function (newVal, oldVal) {
178 var input = $($element).find('input');
180 input.trigger('keyup'); // cascade the update
183 $scope.$watch('record.ready', function (newVal, oldVal) { // wait for the record to be loaded
184 if (newVal && !$scope.ready) {
185 $scope.rtype = $scope.record.recordType();
187 egTagTable.fetchFFPosTable( $scope.rtype ).then(function (ff_list) {
188 angular.forEach(ff_list, function (ff) {
190 if (ff.fixed_field == $scope.fixedField && ff.rec_type == $scope.rtype) {
193 $($element).children().css({ display : 'inline' });
195 var input = $($element).find('input');
196 input.attr('maxlength', $scope.me.length);
197 input.val($scope.record.extractFixedField($scope.me.fixed_field));
198 input.on('keyup', function(e) {
199 $scope.in_handler = true;
200 $scope.record.setFixedField($scope.me.fixed_field, input.val());
201 try { $scope.$parent.$digest(); } catch(e) {};
207 }).then(function (me) {
211 return $scope.record.extractFixedField($scope.fixedField);
213 function (newVal, oldVal) {
214 if ($scope.in_handler) {
215 $scope.in_handler = false;
216 } else if (oldVal != newVal) {
217 $($element).find('input').val(newVal);
222 }).then(function () {
223 return egTagTable.fetchFFValueTable( $scope.rtype );
224 }).then(function (vlist) {
225 if (vlist[$scope.fixedField]) {
226 vlist[$scope.fixedField].forEach(function (v) {
227 if (v[0].length <= v[2]) {
228 $scope.item_container.push({ value : v[0], label : v[0] + ': ' + v[1] });
232 }).then(function () {
233 if ($scope.item_container && $scope.item_container.length)
234 $($element).bind('contextmenu', $scope.showContext);
240 $scope.showContext = function (event) {
241 if ($scope.context_menu_element) {
242 console.log('Reshowing context menu...');
243 $('body').trigger('click');
244 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
245 $('body').on('click.context_menu',function() {
246 $($scope.context_menu_element).css('display','none');
247 $('body').off('click.context_menu');
252 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
253 console.log('Showing context menu...');
254 $('body').trigger('click');
257 '<ul class="dropdown-menu" role="menu" style="z-index: 2000;">'+
258 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
261 var tnode = angular.element(tmpl);
262 $document.find('body').append(tnode);
270 $scope.context_menu_element = tnode;
272 $timeout(function() {
273 var e = $compile(tnode)($scope);
277 $('body').on('click.context_menu',function() {
278 $(tnode).css('display','none');
279 $('body').off('click.context_menu');
293 .directive("egMarcEditSubfield", function () {
298 '<span><label class="marcedit marcsfcodedelimiter"'+
299 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
300 '>‡</label><eg-marc-edit-editable '+
302 'select-on-focus="true" '+
303 'class="marcedit marcsf marcsfcode" '+
305 'subfield="subfield" '+
306 'content="subfield[0]" '+
308 'on-keydown="onKeydown" '+
309 'context-item-generator="sf_code_options" '+
310 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
312 '<span><eg-marc-edit-editable '+
314 'class="marcedit marcsf marcsfvalue" '+
316 'subfield="subfield" '+
317 'content="subfield[1]" '+
318 'on-keydown="onKeydown" '+
319 'context-item-generator="sf_val_options" '+
320 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
323 scope: { field: "=", subfield: "=", onKeydown: '=' },
325 controller : ['$scope', 'egTagTable',
326 function ( $scope , egTagTable) {
328 $scope.sf_code_options = function () {
329 return egTagTable.getSubfieldCodes($scope.field.tag);
331 $scope.sf_val_options = function () {
332 return egTagTable.getSubfieldValues($scope.field.tag, $scope.subfield[0]);
339 .directive("egMarcEditInd", function () {
343 template: '<span><eg-marc-edit-editable '+
345 'class="marcedit marcind" '+
346 'select-on-focus="true" '+
350 'on-keydown="onKeydown" '+
351 'context-item-generator="ind_val_options" '+
352 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
354 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
356 controller : ['$scope', 'egTagTable',
357 function ( $scope , egTagTable) {
359 $scope.ind_val_options = function () {
360 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
367 .directive("egMarcEditTag", function () {
371 template: '<span><eg-marc-edit-editable '+
373 'class="marcedit marctag" '+
374 'select-on-focus="true" '+
378 'on-keydown="onKeydown" '+
379 'context-item-generator="tag_options" '+
380 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
382 scope: { tag : '=', field: '=', onKeydown: '=', contextFunctions: '=' },
384 controller : ['$scope', 'egTagTable',
385 function ( $scope , egTagTable) {
387 $scope.tag_options = [
390 { label : 'Add 006', action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.add006(e) } },
391 { label : 'Add 007', action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.add007(e) } },
392 { label : 'Add/Replace 008', action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.reify008(e) } },
395 if (!$scope.field.isControlfield()) {
396 options = options.concat([
397 { label : 'Insert field after ', action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.addDatafield(e) } },
398 { label : 'Insert field before', action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.addDatafield(e,true) } },
402 options.push({ label : 'Delete Field', action : function(j1,j2,j3,j4,e) { $scope.contextFunctions.deleteDatafield(e) } });
405 function () { return egTagTable.getFieldTags() }
413 .directive("egMarcEditDatafield", function () {
418 '<span><eg-marc-edit-tag context-functions="contextFunctions" field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
419 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
420 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
421 '<span><eg-marc-edit-subfield ng-class="{ \'unvalidatedheading\' : field.heading_checked && !field.heading_valid}" ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
422 // FIXME: template should probably be moved to file to improve
424 '<button class="btn btn-info btn-xs" '+
425 'aria-label="Manage authority record links" '+
426 'ng-show="isAuthorityControlled(field)"'+
427 'ng-click="spawnAuthorityLinker()"'+
429 '<span class="glyphicon glyphicon-link"></span>'+
431 '<span ng-show="field.heading_checked && field.heading_valid" class="glyphicon glyphicon-ok-sign"></span>'+
432 '<span ng-show="field.heading_checked && !field.heading_valid" class="glyphicon glyphicon-question-sign"></span>'+
434 scope: { field: "=", onKeydown: '=', contextFunctions: '=' },
436 controller : ['$scope','$modal',
437 function ( $scope, $modal ) {
438 $scope.isAuthorityControlled = function () {
439 return ($scope.$parent.$parent.record_type == 'bre') &&
440 $scope.$parent.$parent.controlSet.bibFieldByTag($scope.field.tag);
442 $scope.spawnAuthorityLinker = function() {
443 // intentionally making a clone in case
444 // user decides to abandon the linking
445 var fieldCopy = new MARC21.Field({
446 tag : $scope.field.tag,
447 ind1 : $scope.field.ind1,
448 ind2 : $scope.field.ind2
450 angular.forEach($scope.field.subfields, function(sf) {
451 fieldCopy.subfields.push(sf.slice(0));
453 var cs = $scope.$parent.$parent.controlSet;
454 var args = { changed : false };
456 templateUrl: './cat/share/t_authority_link_dialog',
458 controller: ['$scope', '$modalInstance', function($scope, $modalInstance) {
459 $scope.controlSet = cs;
460 $scope.bibField = fieldCopy;
461 $scope.focusMe = true;
463 $scope.ok = function(args) { $modalInstance.close(args) };
464 $scope.cancel = function () { $modalInstance.dismiss() };
466 }).result.then(function (args) {
468 $scope.field.subfields.length = 0;
469 angular.forEach(fieldCopy.subfields, function(sf) {
470 $scope.field.addSubfields(sf[0], sf[1]);
480 .directive("egMarcEditControlfield", function () {
485 '<span><eg-marc-edit-tag context-functions="contextFunctions" field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
486 '<span><eg-marc-edit-editable '+
489 'class="marcedit marcdata" '+
490 'content="field.data" '+
491 'on-keydown="onKeydown" '+
492 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
494 // TODO: move to TT2 template
495 '<button class="btn btn-info btn-xs" '+
496 'aria-label="Physical Characteristics Wizard" '+
497 'ng-show="showPhysCharLink()"'+
498 'ng-click="spawnPhysCharWizard()"'+
500 '<span class="glyphicon glyphicon-link"></span>'+
503 scope: { field: "=", onKeydown: '=', contextFunctions: '=' },
504 controller : ['$scope','$modal',
505 function ( $scope, $modal) {
506 $scope.showPhysCharLink = function () {
507 return ($scope.$parent.$parent.record_type == 'bre')
508 && $scope.field.tag == '007';
510 $scope.spawnPhysCharWizard = function() {
513 field : $scope.field,
514 orig_value : $scope.field.data
517 templateUrl: './cat/share/t_physchar_dialog',
518 controller: ['$scope','$modalInstance',
519 function( $scope , $modalInstance) {
520 $scope.focusMe = true;
522 $scope.ok = function(args) { $modalInstance.close(args) };
523 $scope.cancel = function () {
524 $modalInstance.dismiss();
525 args.field.data = args.orig_value;
528 }).result.then(function (args) {
529 // $scope.field.data is changed within the
530 // wizard. Nothing left to do on submit.
539 .directive("egMarcEditLeader", function () {
544 '<span><eg-marc-edit-editable '+
545 'class="marcedit marctag" '+
547 'on-keydown="onKeydown" '+
549 'disabled="disabled"'+
551 '<span><eg-marc-edit-editable '+
552 'class="marcedit marcdata" '+
554 'max="{{record.leader.length}}" '+
555 'content="record.leader" '+
556 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
557 'on-keydown="onKeydown"'+
560 controller : ['$scope',
561 function ( $scope ) {
565 scope: { record: "=", onKeydown: '=' }
569 /// TODO: fixed field editor and such
570 .directive("egMarcEditRecord", function () {
572 templateUrl : './cat/share/t_marcedit',
580 // in-place mode means that the editor is being
581 // used just to munge some MARCXML client-side, rather
582 // than to (immediately) update the database
587 link: function (scope, element, attrs) {
589 element.bind('mouseup', function(e) {;
590 scope.current_event_target = $(e.target).attr('id');
591 if (scope.current_event_target && $(e.target).hasClass('noSelection')) {
597 element.bind('click', function(e) {;
598 scope.current_event_target = $(e.target).attr('id');
599 if (scope.current_event_target) {
600 console.log('Recording click event on ' + scope.current_event_target);
601 scope.current_event_target_cursor_pos =
602 e.target.selectionDirection=='backward' ?
603 e.target.selectionStart :
604 e.target.selectionEnd;
609 controller : ['$timeout','$scope','$q','$window','egCore', 'egTagTable',
610 function ( $timeout , $scope , $q, $window , egCore , egTagTable ) {
613 $scope.onSaveCallback = $scope.onSave;
614 if (typeof $scope.onSaveCallback !== 'undefined' && !angular.isArray($scope.onSaveCallback))
615 $scope.onSaveCallback = [ $scope.onSaveCallback ];
617 MARC21.Record.delimiter = '$';
619 $scope.enable_fast_add = false;
620 $scope.fast_item_callnumber = '';
621 $scope.fast_item_barcode = '';
622 $scope.flatEditor = false;
623 $scope.brandNewRecord = false;
624 $scope.bib_source = null;
625 $scope.record_type = $scope.recordType || 'bre';
626 $scope.max_undo = $scope.maxUndo || 100;
627 $scope.record_undo_stack = [];
628 $scope.record_redo_stack = [];
629 $scope.in_undo = false;
630 $scope.in_redo = false;
631 $scope.record = new MARC21.Record();
632 $scope.save_stack_depth = 0;
633 $scope.controlfields = [];
634 $scope.datafields = [];
635 $scope.controlSet = egTagTable.getAuthorityControlSet();
637 egTagTable.loadTagTable({ marcRecordType : $scope.record_type });
639 $scope.saveFlatTextMARC = function () {
640 $scope.record = new MARC21.Record({ marcbreaker : $scope.flat_text_marc });
643 $scope.refreshVisual = function () {
644 if (!$scope.flatEditor) {
645 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
646 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
650 var addDatafield = function (e,before) {
651 var element = $(e.target);
653 var index_field = e.data.scope.field.position;
654 var new_field = index_field + 1;
656 var new_field = new MARC21.Field({
658 subfields : [[' ','',0]]
661 if (Boolean(before)) {
662 e.data.scope.field.record.insertFieldsBefore(
667 e.data.scope.field.record.insertFieldsAfter(
673 $scope.current_event_target = 'r' + $scope.recordId +
674 'f' + new_field + 'tag';
676 $scope.current_event_target_cursor_pos = 0;
677 $scope.current_event_target_cursor_pos_end = 3;
678 $scope.force_render = true;
680 $timeout(function(){$scope.$digest()}).then(setCaret);
683 var deleteDatafield = function (e) {
684 var del_field = e.data.scope.field.position;
686 var domnode = $('#r'+e.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
688 e.data.scope.field.record.deleteFields(
692 domnode.scope().$destroy();
695 $scope.current_event_target = 'r' + $scope.recordId +
696 'f' + del_field + 'tag';
698 $scope.current_event_target_cursor_pos = 0;
699 $scope.current_event_target_cursor_pos_end = 0
700 $scope.force_render = true;
702 $timeout(function(){$scope.$digest()}).then(setCaret);
705 var add006 = function (e) {
706 e.data.scope.field.record.insertOrderedFields(
713 $scope.force_render = true;
714 $timeout(function(){$scope.$digest()}).then(setCaret);
717 var add007 = function (e) {
718 e.data.scope.field.record.insertOrderedFields(
725 $scope.force_render = true;
726 $timeout(function(){$scope.$digest()}).then(setCaret);
729 var reify008 = function (e) {
730 var new_008_data = e.data.scope.field.record.generate008();
733 var old_008s = e.data.scope.field.record.field('008',true);
734 old_008s.forEach(function(o) {
735 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
736 domnode.scope().$destroy();
738 e.data.scope.field.record.deleteFields(o);
741 e.data.scope.field.record.insertOrderedFields(
748 $scope.force_render = true;
749 $timeout(function(){$scope.$digest()}).then(setCaret);
752 $scope.context_functions = {
753 addDatafield : addDatafield,
754 deleteDatafield : deleteDatafield,
760 $scope.onKeydown = function (event) {
761 var event_return = true;
764 'keydown: which='+event.which+
765 ', ctrlKey='+event.ctrlKey+
766 ', shiftKey='+event.shiftKey+
767 ', altKey='+event.altKey+
768 ', metaKey='+event.altKey
771 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
772 event_return = $scope.processRedo();
773 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
774 event_return = $scope.processUndo();
775 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
777 var element = $(event.target);
778 var new_sf, index_sf, move_data;
780 if (element.hasClass('marcsfvalue')) {
781 index_sf = event.data.scope.subfield[2];
782 new_sf = index_sf + 1;
784 var start = event.target.selectionStart;
785 var end = event.target.selectionEnd - event.target.selectionStart ?
786 event.target.selectionEnd :
787 event.target.value.length;
789 move_data = event.target.value.substring(start,end);
791 } else if (element.hasClass('marcsfcode')) {
792 index_sf = event.data.scope.subfield[2];
793 new_sf = index_sf + 1;
794 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
799 $scope.current_event_target = 'r' + $scope.recordId +
800 'f' + event.data.scope.field.position +
801 's' + new_sf + 'code';
803 event.data.scope.field.subfields.forEach(function(sf) {
804 if (sf[2] >= new_sf) sf[2]++;
805 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
807 event.data.scope.field.subfields.splice(
810 [' ', move_data, new_sf ]
813 $scope.current_event_target_cursor_pos = 0;
814 $scope.current_event_target_cursor_pos_end = 1;
816 $timeout(function(){$scope.$digest()}).then(setCaret);
818 event_return = false;
820 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
822 event_return = false;
824 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
826 event_return = false;
828 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
830 event_return = false;
832 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
833 addDatafield(event, event.shiftKey); // shift key inserts before
834 event_return = false;
836 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
837 deleteDatafield(event);
838 event_return = false;
840 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
842 var sf = event.data.scope.subfield[2] - 1;
843 if (sf == -1) sf = 0;
845 event.data.scope.field.deleteExactSubfields(
846 event.data.scope.subfield
849 if (!event.data.scope.field.subfields[sf]) {
850 $scope.current_event_target = 'r' + $scope.recordId +
851 'f' + event.data.scope.field.position +
854 $scope.current_event_target = 'r' + $scope.recordId +
855 'f' + event.data.scope.field.position +
859 $scope.current_event_target_cursor_pos = 0;
860 $scope.current_event_target_cursor_pos_end = 0;
861 $scope.force_render = true;
863 $timeout(function(){$scope.$digest()}).then(setCaret);
865 event_return = false;
867 } else if (event.keyCode == 38) {
868 if (event.ctrlKey) { // copy the field up
869 var index_field = event.data.scope.field.position;
872 if (event.data.scope.field.isControlfield()) {
873 field_obj = new MARC21.Field({
874 tag : event.data.scope.field.tag,
875 data : event.data.scope.field.data
879 for (var i in event.data.scope.field.subfields) {
880 sf_clone.push(event.data.scope.field.subfields[i].slice());
882 field_obj = new MARC21.Field({
883 tag : event.data.scope.field.tag,
884 ind1 : event.data.scope.field.ind1,
885 ind2 : event.data.scope.field.ind2,
891 event.data.scope.field.record.insertFieldsBefore(
892 event.data.scope.field,
896 $scope.current_event_target = 'r' + $scope.recordId +
897 'f' + index_field + 'tag';
899 $scope.current_event_target_cursor_pos = 0;
900 $scope.current_event_target_cursor_pos_end = 3;
901 $scope.force_render = true;
903 $timeout(function(){$scope.$digest()}).then(setCaret);
905 } else { // jump to prev field
906 if (event.data.scope.field.position > 0) {
908 $scope.current_event_target_cursor_pos = 0;
909 $scope.current_event_target_cursor_pos_end = 0;
910 $scope.current_event_target = 'r' + $scope.recordId +
911 'f' + (event.data.scope.field.position - 1) +
917 event_return = false;
919 } else if (event.keyCode == 40) { // down arrow...
920 if (event.ctrlKey) { // copy the field down
922 var index_field = event.data.scope.field.position;
923 var new_field = index_field + 1;
926 if (event.data.scope.field.isControlfield()) {
927 field_obj = new MARC21.Field({
928 tag : event.data.scope.field.tag,
929 data : event.data.scope.field.data
933 for (var i in event.data.scope.field.subfields) {
934 sf_clone.push(event.data.scope.field.subfields[i].slice());
936 field_obj = new MARC21.Field({
937 tag : event.data.scope.field.tag,
938 ind1 : event.data.scope.field.ind1,
939 ind2 : event.data.scope.field.ind2,
944 event.data.scope.field.record.insertFieldsAfter(
945 event.data.scope.field,
949 $scope.current_event_target = 'r' + $scope.recordId +
950 'f' + new_field + 'tag';
952 $scope.current_event_target_cursor_pos = 0;
953 $scope.current_event_target_cursor_pos_end = 3;
954 $scope.force_render = true;
956 $timeout(function(){$scope.$digest()}).then(setCaret);
958 } else { // jump to next field
959 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
961 $scope.current_event_target_cursor_pos = 0;
962 $scope.current_event_target_cursor_pos_end = 0;
963 $scope.current_event_target = 'r' + $scope.recordId +
964 'f' + (event.data.scope.field.position + 1) +
970 event_return = false;
972 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
973 $scope.current_event_target = $(event.target).attr('id');
974 if ($scope.current_event_target) {
975 $scope.current_event_target_cursor_pos =
976 event.target.selectionDirection=='backward' ?
977 event.target.selectionStart :
978 event.target.selectionEnd;
985 function setCaret() {
986 if ($scope.current_event_target) {
987 console.log("Putting caret in " + $scope.current_event_target);
988 if (!$scope.current_event_target_cursor_pos_end)
989 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
991 var element = $('#'+$scope.current_event_target).get(0);
994 if (element.setSelectionRange) {
995 element.setSelectionRange(
996 $scope.current_event_target_cursor_pos,
997 $scope.current_event_target_cursor_pos_end
1000 $scope.current_event_cursor_pos_end = null;
1001 $scope.current_event_target = null;
1006 function loadRecord() {
1007 return (function() {
1008 var deferred = $q.defer();
1009 if ($scope.recordId) {
1010 egCore.pcrud.retrieve(
1011 $scope.record_type, $scope.recordId
1012 ).then(function(rec) {
1013 deferred.resolve(rec);
1016 if ($scope.recordType == 'bre') {
1017 var bre = new egCore.idl.bre();
1018 bre.marc($scope.marcXml);
1019 deferred.resolve(bre);
1020 } else if ($scope.recordType == 'are') {
1021 var are = new egCore.idl.are();
1022 are.marc($scope.marcXml);
1023 deferred.resolve(are);
1025 $scope.brandNewRecord = true;
1027 return deferred.promise;
1028 })().then(function(rec) {
1029 $scope.in_redo = true;
1030 $scope[$scope.record_type] = rec;
1031 $scope.record = new MARC21.Record({ marcxml : $scope.Record().marc() });
1032 $scope.calculated_record_type = $scope.record.recordType();
1033 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1034 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1035 $scope.save_stack_depth = $scope.record_undo_stack.length;
1036 $scope.flat_text_marc = $scope.record.toBreaker();
1038 if ($scope.record_type == 'bre') {
1039 $scope.bib_source = $scope.Record().source();
1043 return egTagTable.fetchFFPosTable($scope.calculated_record_type)
1045 return egTagTable.fetchFFValueTable($scope.calculated_record_type)
1049 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
1050 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
1051 $scope.record_undo_stack.push({
1053 target: $scope.current_event_target,
1054 pos: $scope.current_event_target_cursor_pos
1057 if ($scope.force_render) {
1058 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1059 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1060 $scope.force_render = false;
1063 $scope.flat_text_marc = newVal;
1066 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
1067 $scope.dirtyFlag = true;
1069 $scope.dirtyFlag = false;
1072 if ($scope.record_undo_stack.length > $scope.max_undo)
1073 $scope.record_undo_stack.shift();
1075 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
1076 $scope.in_redo = false;
1077 $scope.in_undo = false;
1080 $scope.processUndo = function () {
1081 if ($scope.record_undo_stack.length) {
1082 $scope.in_undo = true;
1084 var undo_item = $scope.record_undo_stack.pop();
1085 $scope.record_redo_stack.push(undo_item);
1087 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
1088 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1089 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1091 $scope.current_event_target = undo_item.target;
1092 $scope.current_event_target_cursor_pos = undo_item.pos;
1093 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1095 $timeout(function(){$scope.$digest()}).then(setCaret);
1102 $scope.processRedo = function () {
1103 if ($scope.record_redo_stack.length) {
1104 $scope.in_redo = true;
1106 var redo_item = $scope.record_redo_stack.pop();
1107 $scope.record_undo_stack.push(redo_item);
1109 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
1110 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
1111 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
1113 $scope.current_event_target = redo_item.target;
1114 $scope.current_event_target_cursor_pos = redo_item.pos;
1115 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
1117 $timeout(function(){$scope.$digest()}).then(setCaret);
1124 $scope.Record = function () {
1125 return $scope[$scope.record_type];
1128 $scope.deleteRecord = function () {
1129 $scope.Record().deleted(true);
1130 return $scope.saveRecord();
1133 $scope.undeleteRecord = function () {
1134 $scope.Record().deleted(false);
1135 return $scope.saveRecord();
1138 $scope.validateHeadings = function () {
1139 if ($scope.record_type != 'bre') return;
1140 var chain = $q.when();
1141 angular.forEach($scope.record.fields, function(f) {
1142 if (!$scope.controlSet.bibFieldByTag(f.tag)) return;
1143 // if heading already has a $0, assume it's good
1144 if (f.subfield('0', true).length) {
1145 f.heading_checked = true;
1146 f.heading_valid = true;
1149 var auth_match = $scope.controlSet.bibToAuthorities(f);
1150 chain = chain.then(function() {
1151 var promise = egCore.net.request(
1153 'open-ils.search.authority.simple_heading.from_xml.batch.atomic',
1155 ).then(function (matches) {
1156 f.heading_valid = false;
1157 if (matches[0]) { // probably set
1158 for (var cset in matches[0]) {
1159 var arr = matches[0][cset];
1161 // protect against errant empty string values
1162 if (arr.length == 1 && arr[0] == '')
1164 f.heading_valid = true;
1169 f.heading_checked = true;
1176 processOnSaveCallbacks = function() {
1177 var deferred = $q.defer();
1178 if (typeof $scope.onSaveCallback !== 'undefined') {
1179 var promise = deferred.promise;
1181 angular.forEach($scope.onSaveCallback, function (f) {
1182 if (angular.isFunction(f)) promise = promise.then(f);
1186 return deferred.resolve($scope.recordId)
1189 $scope.saveRecord = function () {
1190 if ($scope.inPlaceMode) {
1191 $scope.marcXml = $scope.record.toXmlString();
1192 return processOnSaveCallbacks();
1194 $scope.mangle_005();
1195 $scope.Record().editor(egCore.auth.user().id());
1196 $scope.Record().edit_date('now');
1197 $scope.record.pruneEmptyFieldsAndSubfields();
1198 $scope.Record().marc($scope.record.toXmlString());
1199 if ($scope.recordId) {
1200 return egCore.pcrud.update(
1203 if ($scope.enable_fast_add) {
1206 'open-ils.actor.anon_cache.set_value',
1207 null, 'edit-these-copies', {
1208 record_id: $scope.recordId,
1210 label : $scope.fast_item_callnumber,
1211 barcode : $scope.fast_item_barcode,
1216 ).then(function(key) {
1218 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1219 $timeout(function() { $window.open(url, '_blank') });
1221 alert('Could not create anonymous cache key!');
1225 }).then(loadRecord).then(processOnSaveCallbacks);
1227 $scope.Record().creator(egCore.auth.user().id());
1228 $scope.Record().create_date('now');
1229 return egCore.pcrud.create(
1231 ).then(function(bre) {
1232 $scope.recordId = bre.id();
1233 if ($scope.enable_fast_add) {
1236 'open-ils.actor.anon_cache.set_value',
1237 null, 'edit-these-copies', {
1238 record_id: $scope.recordId,
1240 label : $scope.fast_item_callnumber,
1241 barcode : $scope.fast_item_barcode,
1246 ).then(function(key) {
1248 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1249 $timeout(function() { $window.open(url, '_blank') });
1251 alert('Could not create anonymous cache key!');
1255 }).then(loadRecord).then(processOnSaveCallbacks);
1261 $scope.seeBreaker = function () {
1262 alert($scope.record.toBreaker());
1265 $scope.markConjoined = function () {
1266 egCore.hatch.setLocalItem('eg.cat.marked_conjoined_record',$scope.recordId);
1269 $scope.markVolTransfer = function () {
1270 egCore.hatch.setLocalItem('eg.cat.marked_volume_transfer_record',$scope.recordId);
1273 $scope.markOverlay = function () {
1274 egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.recordId);
1277 $scope.$watch('recordId',
1278 function(newVal, oldVal) {
1279 if (newVal && newVal !== oldVal) {
1284 $scope.$watch('marcXml',
1285 function(newVal, oldVal) {
1286 if (newVal && newVal !== oldVal) {
1292 var unregister = $scope.$watch(function() {
1293 return egTagTable.initialized();
1297 if ($scope.recordId || $scope.marcXml) {
1303 $scope.mangle_005 = function () {
1304 var now = new Date();
1305 var y = now.getUTCFullYear();
1307 var m = now.getUTCMonth() + 1;
1308 if (m < 10) m = '0' + m;
1310 var d = now.getUTCDate();
1311 if (d < 10) d = '0' + d;
1313 var H = now.getUTCHours();
1314 if (H < 10) H = '0' + H;
1316 var M = now.getUTCMinutes();
1317 if (M < 10) M = '0' + M;
1319 var S = now.getUTCSeconds();
1320 if (S < 10) S = '0' + S;
1322 var stamp = '' + y + m + d + H + M + S + '.0';
1323 var f = $scope.record.field('005',true)[0];
1327 $scope.record.insertOrderedFields(
1342 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
1346 template: '<span class="nullable">'+
1347 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
1348 '<option value="">Select a Source</option>'+
1351 controller: ['$scope','egCore',
1352 function ($scope , egCore) {
1354 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
1355 .then(function(list) { $scope.bib_sources = list; });
1357 $scope.$watch('bib_source',
1358 function(newVal, oldVal) {
1359 if (newVal !== oldVal) {
1360 $scope.bre.source(newVal);
1370 .directive("egMarcEditAuthorityLinker", function () {
1374 templateUrl: './cat/share/t_authority_linker',
1380 controller: ['$scope','$modal','egCore','egAuth',
1381 function ($scope , $modal, egCore, egAuth) {
1383 $scope.searchStr = '';
1384 var cni = egCore.env.aous['cat.marc_control_number_identifier'] ||
1385 'Set cat.marc_control_number_identifier in Library Settings';
1387 var axis_list = $scope.controlSet.bibFieldBrowseAxes($scope.bibField.tag);
1388 $scope.axis = axis_list[0];
1390 $scope._controlled_sf_list = {};
1391 $scope._controlled_auth_sf_list = {};
1393 angular.forEach($scope.controlSet.controlSetList(), function(acs_id) {
1394 if ($scope.controlSet.controlSet(acs_id).control_map[$scope.bibField.tag])
1395 found_acs.push(acs_id);
1397 if (found_acs.length) {
1398 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag],
1399 function(value, sf_label) {
1400 $scope._controlled_sf_list[ sf_label ] = 1;
1401 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag][sf_label],
1402 function(auth_sf, auth_tag) {
1403 if (!$scope._controlled_auth_sf_list[auth_tag]) {
1404 $scope._controlled_auth_sf_list[auth_tag] = { };
1406 $scope._controlled_auth_sf_list[auth_tag][auth_sf] = 1;
1413 $scope.bibField.subfields.forEach(function (sf) {
1414 if (sf[0] in $scope._controlled_sf_list) {
1416 sf.selectable = true;
1418 sf.selectable = false;
1421 $scope.summarizeField = function() {
1423 'tag': $scope.bibField.tag,
1424 'ind1': $scope.bibField.ind1,
1425 'ind2': $scope.bibField.ind2,
1428 $scope.bibField.subfields.forEach(function(sf) {
1430 source_f.subfields.push([ sf[0], sf[1] ]);
1435 $scope.getSearchString = function() {
1436 var source_f = $scope.summarizeField();
1438 angular.forEach(source_f.subfields, function(val) {
1439 values.push(val[1]);
1441 return values.join(' ');
1443 $scope.searchStr = $scope.getSearchString();
1444 $scope.$watch(function() {
1446 angular.forEach($scope.bibField.subfields, function(sf) {
1447 if (sf.selected) ct++
1451 function(newVal, oldVal) {
1452 $scope.searchStr = $scope.getSearchString();
1455 $scope.updateSubfieldZero = function(value) {
1456 $scope.changed = true;
1457 $scope.bibField.deleteSubfield({ code : ['0'] });
1458 $scope.bibField.subfields.push([
1459 '0', '(' + cni + ')' + value
1463 $scope.applyHeading = function(headingField) {
1464 // TODO: move the MARC21 rules for copying indicators
1466 if (headingField.tag == '130' && $scope.bibField.tag == '130') {
1467 $scope.bibField.ind1 = headingField.ind2;
1469 $scope.bibField.ind1 = headingField.ind1;
1471 // deal with 4xx and 5xx
1472 var authFallbackTag = '1' + headingField.tag.substr(1, 2);
1473 var _valid_auth_sfs = (headingField.tag in $scope._controlled_auth_sf_list) ?
1474 $scope._controlled_auth_sf_list[headingField.tag] :
1475 (authFallbackTag in $scope._controlled_auth_sf_list) ?
1476 $scope._controlled_auth_sf_list[authFallbackTag] :
1478 // save the $0 for later use
1480 if (headingField.subfield('0')) {
1481 sfZero = headingField.subfield('0')[1];
1483 // grab any bib subfields not under authority control
1484 // TODO do something about uncontrolled subdivisions
1485 var uncontrolledBibSf = [];
1486 angular.forEach($scope.bibField.subfields, function(sf) {
1487 if (!(sf[0] in $scope._controlled_sf_list) && (sf[0] != '0')) {
1488 uncontrolledBibSf.push([ sf[0], sf[1] ]);
1491 // grab the authority subfields
1492 var authoritySf = [];
1493 angular.forEach(headingField.subfields, function(sf) {
1494 if (sf[0] in _valid_auth_sfs) {
1495 authoritySf.push([ sf[0], sf[1] ]);
1498 $scope.bibField.subfields.length = 0;
1499 angular.forEach(authoritySf, function(sf) {
1500 $scope.bibField.addSubfields(sf[0], sf[1]);
1502 angular.forEach(uncontrolledBibSf, function(sf) {
1503 $scope.bibField.addSubfields(sf[0], sf[1]);
1506 $scope.bibField.addSubfields('0', sfZero);
1508 $scope.bibField.subfields.forEach(function (sf) {
1509 if (sf[0] in $scope._controlled_sf_list) {
1510 // intentionally not selecting any subfields
1511 // after we've applied an authority heading
1512 sf.selected = false;
1513 sf.selectable = true;
1515 sf.selectable = false;
1518 $scope.changed = true;
1521 $scope.createAuthorityFromBib = function(spawn_editor) {
1522 var source_f = $scope.summarizeField();
1524 var args = { authority_id : 0 };
1525 var method = (spawn_editor) ?
1526 'open-ils.cat.authority.record.create_from_bib.readonly' :
1527 'open-ils.cat.authority.record.create_from_bib';
1534 ).then(function(newAuthority) {
1537 templateUrl: './cat/share/t_edit_new_authority',
1540 ['$scope', '$modalInstance', function($scope, $modalInstance) {
1541 $scope.focusMe = true;
1543 $scope.dirty_flag = false;
1544 $scope.marc_xml = newAuthority,
1545 $scope.ok = function(args) { $modalInstance.close(args) }
1546 $scope.cancel = function () { $modalInstance.dismiss() }
1548 }).result.then(function (args) {
1549 if (!args || !args.authority_id) return;
1550 $scope.updateSubfieldZero(args.authority_id);
1553 $scope.updateSubfieldZero(newAuthority.id());
1563 .directive("egPhyscharWizard", function () {
1567 templateUrl: './cat/share/t_physchar_wizard',
1571 controller: ['$scope','$q','egTagTable',
1572 function ($scope , $q , egTagTable) {
1574 // $scope.step is the 1-based position in the list of
1575 // subfields for the currently selected type.
1576 // step==0 means we are currently selecting the type
1579 if (!$scope.field.data)
1580 $scope.field.data = '';
1582 // currently selected subfield value selector option
1583 $scope.selected_option = null;
1585 function current_ptype() {
1586 return $scope.field.data.substr(0, 1);
1589 function current_subfield() {
1590 return egTagTable.getPhysCharSubfieldMap(current_ptype())
1591 .then(function(sf_list) {return sf_list[$scope.step-1]});
1594 $scope.values_for_step = [];
1595 function set_values_for_step() {
1598 if ($scope.step == 0) {
1599 promise = egTagTable.getPhysCharTypeMap();
1601 promise = current_subfield().then(
1602 function(subfield) {
1604 .getPhysCharValueMap(subfield.id());
1609 return promise.then(function(list) {
1610 $scope.values_for_step = list;
1611 set_selected_option_from_field();
1612 set_label_for_step();
1616 $scope.change_ptype = function(option) {
1617 $scope.selected_option = option;
1618 var new_val = option.ptype_key();
1619 if (current_ptype() != new_val) {
1620 $scope.field.data = new_val; // total reset
1624 $scope.change_option = function(option) {
1625 $scope.selected_option = option;
1626 var new_val = option.value();
1627 get_step_slot().then(function(slot) {
1628 var value = $scope.field.data;
1629 while (value.length < (slot[0] + slot[1]))
1631 var before = value.substr(0, slot[0]);
1632 var after = value.substr(slot[0] + slot[1]);
1634 before + new_val.substr(0, slot[1]) + after;
1638 function get_step_slot() {
1639 if ($scope.step == 0) return $q.when([0, 1]);
1640 return current_subfield().then(function(sf) {
1641 return [sf.start_pos(), sf.length()]
1645 $scope.is_last_step = function() {
1646 // This one is called w/ every digest, so avoid async
1647 // calls. Wait until we have loaded the current ptype
1648 // subfields to determine if this is the last step.
1651 egTagTable.phys_char_sf_map[current_ptype()] &&
1652 egTagTable.phys_char_sf_map[current_ptype()].length
1657 $scope.label_for_step = '';
1658 function set_label_for_step() {
1659 if ($scope.step > 0) {
1660 current_subfield().then(function(sf) {
1661 $scope.label_for_step = sf.label();
1666 $scope.next_step = function() {
1668 set_values_for_step();
1671 $scope.prev_step = function() {
1673 set_values_for_step();
1676 function set_selected_option_from_field() {
1677 if ($scope.step == 0) {
1678 $scope.selected_option = $scope.values_for_step
1679 .filter(function(opt) {
1680 return (opt.ptype_key() == current_ptype())})[0];
1682 get_step_slot().then(function(slot) {
1683 var val = String.prototype.substr.apply(
1684 $scope.field.data, slot);
1686 $scope.selected_option = $scope.values_for_step
1687 .filter(function(opt) {
1688 return (opt.value() == val)})[0];
1690 $scope.selected_option = null;
1695 set_values_for_step();
1702 .directive("egMarcEditAuthorityBrowser", function () {
1706 templateUrl: './cat/share/t_authority_browser',
1713 controller: ['$scope','$http',
1714 function ($scope , $http) {
1718 $scope.main_headings = [];
1720 function getHeadingString(headingField) {
1722 angular.forEach(headingField.subfields, function (sf) {
1723 if (['x', 'y', 'z'].indexOf(sf[0]) > -1) {
1734 $scope.doBrowse = function() {
1735 $scope.main_headings.length = 0;
1736 if ($scope.searchString.length == 0) return;
1737 var type = 'authority.'
1738 var url = '/opac/extras/browse/marcxml/'
1739 + 'authority.' + $scope.axis + '.refs'
1740 + '/1' // OU - currently unscoped
1741 + '/' + $scope.searchString
1743 + '/' + $scope.limit;
1747 transformResponse : function(data) {
1748 // use a bit of jQuery to deal with the XML
1749 var $xml = $( $.parseXML(data) );
1751 $xml.find('record').each(function() {
1752 var rec = new MARC21.Record();
1753 rec.fromXmlDocument($(this)[0].outerHTML);
1758 }).then(function(response) {
1759 angular.forEach(response.data, function(rec) {
1760 var authId = rec.subfield('901', 'c')[1];
1762 if (rec.field('003')) {
1763 auth_org = rec.field('003').data;
1765 var headingField = rec.field('1..');
1766 var seeFroms = rec.field('4..', true);
1767 var seeAlsos = rec.field('5..', true);
1769 var main_heading = {
1770 authority_id : authId,
1771 heading : getHeadingString(headingField),
1772 seealso_headings : [ ],
1773 seefrom_headings : [ ],
1778 sfZero = '(' + auth_org + ')';
1781 headingField.addSubfields('0', sfZero);
1783 main_heading['headingField'] = headingField;
1784 angular.forEach(seeAlsos, function(headingField) {
1785 main_heading.seealso_headings.push({
1786 heading : getHeadingString(headingField),
1787 headingField : headingField
1790 angular.forEach(seeFroms, function(headingField) {
1791 main_heading.seefrom_headings.push({
1792 heading : getHeadingString(headingField),
1793 headingField : headingField
1796 $scope.main_headings.push(main_heading);
1801 $scope.$watch('searchString',
1802 function(newVal, oldVal) {
1803 if (newVal !== oldVal) {
1808 $scope.$watch('page',
1809 function(newVal, oldVal) {
1810 if (newVal !== oldVal) {