5 angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
7 .directive("egContextMenuItem", ['$timeout',function ($timeout) {
11 template: '<li><a ng-click="setContent(item.value,item.action)">{{item.label}}</a></li>',
12 scope: { item: '=', content: '=' },
13 controller: ['$scope','$element',
14 function ($scope , $element) {
15 if (!$scope.item.label) $scope.item.label = $scope.item.value;
16 if ($scope.item.divider) {
17 $element.style.borderTop = 'solid 1px';
20 $scope.setContent = function (v, a) {
22 if (a) replace_with = a($scope,$element,$scope.item.value,$scope.$parent.$parent.content);
24 $scope.$parent.$parent.$apply(function(){
25 $scope.$parent.$parent.content = replace_with
28 $($element).parent().css({display: 'none'});
35 .directive("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
40 'style="font-family: \'Lucida Console\', Monaco, monospace;" '+
41 'ng-model="content" '+
42 'size="{{content.length * 1.1}}" '+
43 'maxlength="{{max}}" '+
52 contextItemContainer: '@',
53 contextItemGenerator: '=',
57 controller : ['$scope',
60 if ($scope.contextItemContainer && angular.isArray($scope.$parent[$scope.contextItemContainer]))
61 $scope.item_container = $scope.$parent[$scope.contextItemContainer];
62 else if ($scope.contextItemGenerator)
63 $scope.item_generator = $scope.contextItemGenerator;
65 $scope.showContext = function (event) {
66 $scope.item_list = [];
67 if ($scope.item_container) {
68 $scope.item_list = $scope.item_container;
69 } else if ($scope.item_generator) {
70 // always recalculate; tag and/or subfield
71 // codes may have changed
72 $scope.item_list = $scope.item_generator();
77 if (angular.isArray($scope.item_list) && $scope.item_list.length > 0) { // we have a list of values or transforms
78 console.log('Showing context menu...');
79 $('body').trigger('click');
82 '<ul class="dropdown-menu" role="menu" style="z-index: 2000;">'+
83 '<eg-context-menu-item ng-repeat="item in item_list" item="item" content="content"/>'+
86 var tnode = angular.element(tmpl);
87 $document.find('body').append(tnode);
96 var e = $compile(tnode)($scope);
100 $('body').on('click.context_menu',function() {
101 $(tnode).css('display','none');
102 $('body').off('click.context_menu');
113 link: function (scope, element, attrs) {
115 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
117 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
119 element.bind('contextmenu', scope.showContext);
124 .directive("egMarcEditFixedField", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
128 template: '<div class="col-md-2">'+
129 '<div class="col-md-1"><label name="{{fixedField}}" for="{{fixedField}}_ff_input">{{fixedField}}</label></div>'+
130 '<div class="col-md-1"><input type="text" style="padding-left: 5px; margin-left: 1em" size="4" id="{{fixedField}}_ff_input"/></div>'+
132 scope: { record: "=", fixedField: "@" },
134 controller : ['$scope', '$element', 'egTagTable',
135 function ( $scope , $element , egTagTable) {
136 $($element).children().css({ display : 'none' });
138 $scope.content = null; // this is where context menus dump their values
139 $scope.item_container = [];
140 $scope.in_handler = false;
141 $scope.ready = false;
143 $scope.$watch('content', function (newVal, oldVal) {
144 var input = $($element).find('input');
146 input.trigger('keyup'); // cascade the update
149 $scope.$watch('record.ready', function (newVal, oldVal) { // wait for the record to be loaded
150 if (newVal && !$scope.ready) {
151 $scope.rtype = $scope.record.recordType();
153 egTagTable.fetchFFPosTable( $scope.rtype ).then(function (ff_list) {
154 angular.forEach(ff_list, function (ff) {
156 if (ff.fixed_field == $scope.fixedField && ff.rec_type == $scope.rtype) {
159 $($element).children().css({ display : 'inline' });
161 var input = $($element).find('input');
162 input.attr('maxlength', $scope.me.length);
163 input.val($scope.record.extractFixedField($scope.me.fixed_field));
164 input.on('keyup', function(e) {
165 $scope.in_handler = true;
166 $scope.record.setFixedField($scope.me.fixed_field, input.val());
167 try { $scope.$parent.$digest(); } catch(e) {};
173 }).then(function (me) {
177 return $scope.record.extractFixedField($scope.fixedField);
179 function (newVal, oldVal) {
180 if ($scope.in_handler) {
181 $scope.in_handler = false;
182 } else if (oldVal != newVal) {
183 $($element).find('input').val(newVal);
188 }).then(function () {
189 return egTagTable.fetchFFValueTable( $scope.rtype );
190 }).then(function (vlist) {
191 if (vlist[$scope.fixedField]) {
192 vlist[$scope.fixedField].forEach(function (v) {
193 if (v[0].length <= v[2]) {
194 $scope.item_container.push({ value : v[0], label : v[0] + ': ' + v[1] });
198 }).then(function () {
199 if ($scope.item_container && $scope.item_container.length)
200 $($element).bind('contextmenu', $scope.showContext);
206 $scope.showContext = function (event) {
207 if ($scope.context_menu_element) {
208 console.log('Reshowing context menu...');
209 $('body').trigger('click');
210 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
211 $('body').on('click.context_menu',function() {
212 $($scope.context_menu_element).css('display','none');
213 $('body').off('click.context_menu');
218 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
219 console.log('Showing context menu...');
220 $('body').trigger('click');
223 '<ul class="dropdown-menu" role="menu" style="z-index: 2000;">'+
224 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
227 var tnode = angular.element(tmpl);
228 $document.find('body').append(tnode);
236 $scope.context_menu_element = tnode;
238 $timeout(function() {
239 var e = $compile(tnode)($scope);
243 $('body').on('click.context_menu',function() {
244 $(tnode).css('display','none');
245 $('body').off('click.context_menu');
259 .directive("egMarcEditSubfield", function () {
264 '<span><label class="marcedit marcsfcodedelimiter"'+
265 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
266 '>‡</label><eg-marc-edit-editable '+
268 'class="marcedit marcsf marcsfcode" '+
270 'subfield="subfield" '+
271 'content="subfield[0]" '+
273 'on-keydown="onKeydown" '+
274 'context-item-generator="sf_code_options" '+
275 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
277 '<span><eg-marc-edit-editable '+
279 'class="marcedit marcsf marcsfvalue" '+
281 'subfield="subfield" '+
282 'content="subfield[1]" '+
283 'on-keydown="onKeydown" '+
284 'context-item-generator="sf_val_options" '+
285 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
288 scope: { field: "=", subfield: "=", onKeydown: '=' },
290 controller : ['$scope', 'egTagTable',
291 function ( $scope , egTagTable) {
293 $scope.sf_code_options = function () {
294 return egTagTable.getSubfieldCodes($scope.field.tag);
296 $scope.sf_val_options = function () {
297 return egTagTable.getSubfieldValues($scope.field.tag, $scope.subfield[0]);
304 .directive("egMarcEditInd", function () {
308 template: '<span><eg-marc-edit-editable '+
310 'class="marcedit marcind" '+
314 'on-keydown="onKeydown" '+
315 'context-item-generator="ind_val_options" '+
316 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
318 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
320 controller : ['$scope', 'egTagTable',
321 function ( $scope , egTagTable) {
323 $scope.ind_val_options = function () {
324 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
331 .directive("egMarcEditTag", function () {
335 template: '<span><eg-marc-edit-editable '+
337 'class="marcedit marctag" '+
341 'on-keydown="onKeydown" '+
342 'context-item-generator="tag_options" '+
343 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
345 scope: { tag : '=', field: '=', onKeydown: '=' },
347 controller : ['$scope', 'egTagTable',
348 function ( $scope , egTagTable) {
350 $scope.tag_options = function () {
351 return egTagTable.getFieldTags();
358 .directive("egMarcEditDatafield", function () {
363 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
364 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
365 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
366 '<span><eg-marc-edit-subfield ng-class="{ \'unvalidatedheading\' : field.heading_checked && !field.heading_valid}" ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
367 // FIXME: template should probably be moved to file to improve
369 '<button class="btn btn-info btn-xs" '+
370 'aria-label="Manage authority record links" '+
371 'ng-show="isAuthorityControlled(field)"'+
372 'ng-click="spawnAuthorityLinker()"'+
374 '<span class="glyphicon glyphicon-link"></span>'+
376 '<span ng-show="field.heading_checked && field.heading_valid" class="glyphicon glyphicon-ok-sign"></span>'+
377 '<span ng-show="field.heading_checked && !field.heading_valid" class="glyphicon glyphicon-question-sign"></span>'+
379 scope: { field: "=", onKeydown: '=' },
381 controller : ['$scope','$modal',
382 function ( $scope, $modal ) {
383 $scope.isAuthorityControlled = function () {
384 return ($scope.$parent.$parent.record_type == 'bre') &&
385 $scope.$parent.$parent.controlSet.bibFieldByTag($scope.field.tag);
387 $scope.spawnAuthorityLinker = function() {
388 // intentionally making a clone in case
389 // user decides to abandon the linking
390 var fieldCopy = new MARC21.Field({
391 tag : $scope.field.tag,
392 ind1 : $scope.field.ind1,
393 ind2 : $scope.field.ind2
395 angular.forEach($scope.field.subfields, function(sf) {
396 fieldCopy.subfields.push(sf.slice(0));
398 var cs = $scope.$parent.$parent.controlSet;
399 var args = { changed : false };
401 templateUrl: './cat/share/t_authority_link_dialog',
403 controller: ['$scope', '$modalInstance', function($scope, $modalInstance) {
404 $scope.controlSet = cs;
405 $scope.bibField = fieldCopy;
406 $scope.focusMe = true;
408 $scope.ok = function(args) { $modalInstance.close(args) };
409 $scope.cancel = function () { $modalInstance.dismiss() };
411 }).result.then(function (args) {
413 $scope.field.subfields.length = 0;
414 angular.forEach(fieldCopy.subfields, function(sf) {
415 $scope.field.addSubfields(sf[0], sf[1]);
425 .directive("egMarcEditControlfield", function () {
430 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
431 '<span><eg-marc-edit-editable '+
434 'class="marcedit marcdata" '+
435 'content="field.data" '+
436 'on-keydown="onKeydown" '+
437 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
440 scope: { field: "=", onKeydown: '=' }
444 .directive("egMarcEditLeader", function () {
449 '<span><eg-marc-edit-editable '+
450 'class="marcedit marctag" '+
452 'on-keydown="onKeydown" '+
454 'disabled="disabled"'+
456 '<span><eg-marc-edit-editable '+
457 'class="marcedit marcdata" '+
459 'max="{{record.leader.length}}" '+
460 'content="record.leader" '+
461 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
462 'on-keydown="onKeydown"'+
465 controller : ['$scope',
466 function ( $scope ) {
470 scope: { record: "=", onKeydown: '=' }
474 /// TODO: fixed field editor and such
475 .directive("egMarcEditRecord", function () {
477 templateUrl : './cat/share/t_marcedit',
484 // in-place mode means that the editor is being
485 // used just to munge some MARCXML client-side, rather
486 // than to (immediately) update the database
491 link: function (scope, element, attrs) {
493 element.bind('click', function(e) {;
494 scope.current_event_target = $(e.target).attr('id');
495 if (scope.current_event_target) {
496 console.log('Recording click event on ' + scope.current_event_target);
497 scope.current_event_target_cursor_pos =
498 e.target.selectionDirection=='backward' ?
499 e.target.selectionStart :
500 e.target.selectionEnd;
505 controller : ['$timeout','$scope','$q','$window','egCore', 'egTagTable',
506 function ( $timeout , $scope , $q, $window , egCore , egTagTable ) {
508 MARC21.Record.delimiter = '$';
510 $scope.enable_fast_add = false;
511 $scope.fast_item_callnumber = '';
512 $scope.fast_item_barcode = '';
513 $scope.flatEditor = false;
514 $scope.brandNewRecord = false;
515 $scope.bib_source = null;
516 $scope.record_type = $scope.recordType || 'bre';
517 $scope.max_undo = $scope.maxUndo || 100;
518 $scope.record_undo_stack = [];
519 $scope.record_redo_stack = [];
520 $scope.in_undo = false;
521 $scope.in_redo = false;
522 $scope.record = new MARC21.Record();
523 $scope.save_stack_depth = 0;
524 $scope.controlfields = [];
525 $scope.datafields = [];
526 $scope.controlSet = egTagTable.getAuthorityControlSet();
528 egTagTable.loadTagTable({ marcRecordType : $scope.record_type });
530 $scope.saveFlatTextMARC = function () {
531 $scope.record = new MARC21.Record({ marcbreaker : $scope.flat_text_marc });
534 $scope.refreshVisual = function () {
535 if (!$scope.flatEditor) {
536 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
537 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
541 $scope.onKeydown = function (event) {
542 var event_return = true;
545 'keydown: which='+event.which+
546 ', ctrlKey='+event.ctrlKey+
547 ', shiftKey='+event.shiftKey+
548 ', altKey='+event.altKey+
549 ', metaKey='+event.altKey
552 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
553 event_return = $scope.processRedo();
554 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
555 event_return = $scope.processUndo();
556 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
558 var element = $(event.target);
559 var new_sf, index_sf, move_data;
561 if (element.hasClass('marcsfvalue')) {
562 index_sf = event.data.scope.subfield[2];
563 new_sf = index_sf + 1;
565 var start = event.target.selectionStart;
566 var end = event.target.selectionEnd - event.target.selectionStart ?
567 event.target.selectionEnd :
568 event.target.value.length;
570 move_data = event.target.value.substring(start,end);
572 } else if (element.hasClass('marcsfcode')) {
573 index_sf = event.data.scope.subfield[2];
574 new_sf = index_sf + 1;
575 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
580 $scope.current_event_target = 'r' + $scope.recordId +
581 'f' + event.data.scope.field.position +
582 's' + new_sf + 'code';
584 event.data.scope.field.subfields.forEach(function(sf) {
585 if (sf[2] >= new_sf) sf[2]++;
586 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
588 event.data.scope.field.subfields.splice(
591 [' ', move_data, new_sf ]
594 $scope.current_event_target_cursor_pos = 0;
595 $scope.current_event_target_cursor_pos_end = 1;
597 $timeout(function(){$scope.$digest()}).then(setCaret);
599 event_return = false;
601 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
602 event.data.scope.field.record.insertOrderedFields(
609 $scope.force_render = true;
610 $timeout(function(){$scope.$digest()}).then(setCaret);
612 event_return = false;
614 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
615 event.data.scope.field.record.insertOrderedFields(
622 $scope.force_render = true;
623 $timeout(function(){$scope.$digest()}).then(setCaret);
625 event_return = false;
627 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
628 var new_008_data = event.data.scope.field.record.generate008();
631 var old_008s = event.data.scope.field.record.field('008',true);
632 old_008s.forEach(function(o) {
633 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
634 domnode.scope().$destroy();
636 event.data.scope.field.record.deleteFields(o);
639 event.data.scope.field.record.insertOrderedFields(
646 $scope.force_render = true;
647 $timeout(function(){$scope.$digest()}).then(setCaret);
649 event_return = false;
651 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
653 var element = $(event.target);
655 var index_field = event.data.scope.field.position;
656 var new_field = index_field + 1;
658 event.data.scope.field.record.insertFieldsAfter(
659 event.data.scope.field,
662 subfields : [[' ','',0]]
666 $scope.current_event_target = 'r' + $scope.recordId +
667 'f' + new_field + 'tag';
669 $scope.current_event_target_cursor_pos = 0;
670 $scope.current_event_target_cursor_pos_end = 3;
671 $scope.force_render = true;
673 $timeout(function(){$scope.$digest()}).then(setCaret);
675 event_return = false;
677 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
679 var del_field = event.data.scope.field.position;
681 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
683 event.data.scope.field.record.deleteFields(
684 event.data.scope.field
687 domnode.scope().$destroy();
690 $scope.current_event_target = 'r' + $scope.recordId +
691 'f' + del_field + 'tag';
693 $scope.current_event_target_cursor_pos = 0;
694 $scope.current_event_target_cursor_pos_end = 0
695 $scope.force_render = true;
697 $timeout(function(){$scope.$digest()}).then(setCaret);
699 event_return = false;
701 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
703 var sf = event.data.scope.subfield[2] - 1;
704 if (sf == -1) sf = 0;
706 event.data.scope.field.deleteExactSubfields(
707 event.data.scope.subfield
710 if (!event.data.scope.field.subfields[sf]) {
711 $scope.current_event_target = 'r' + $scope.recordId +
712 'f' + event.data.scope.field.position +
715 $scope.current_event_target = 'r' + $scope.recordId +
716 'f' + event.data.scope.field.position +
720 $scope.current_event_target_cursor_pos = 0;
721 $scope.current_event_target_cursor_pos_end = 0;
722 $scope.force_render = true;
724 $timeout(function(){$scope.$digest()}).then(setCaret);
726 event_return = false;
728 } else if (event.keyCode == 38) {
729 if (event.ctrlKey) { // copy the field up
730 var index_field = event.data.scope.field.position;
733 if (event.data.scope.field.isControlfield()) {
734 field_obj = new MARC21.Field({
735 tag : event.data.scope.field.tag,
736 data : event.data.scope.field.data
740 for (var i in event.data.scope.field.subfields) {
741 sf_clone.push(event.data.scope.field.subfields[i].slice());
743 field_obj = new MARC21.Field({
744 tag : event.data.scope.field.tag,
745 ind1 : event.data.scope.field.ind1,
746 ind2 : event.data.scope.field.ind2,
752 event.data.scope.field.record.insertFieldsBefore(
753 event.data.scope.field,
757 $scope.current_event_target = 'r' + $scope.recordId +
758 'f' + index_field + 'tag';
760 $scope.current_event_target_cursor_pos = 0;
761 $scope.current_event_target_cursor_pos_end = 3;
762 $scope.force_render = true;
764 $timeout(function(){$scope.$digest()}).then(setCaret);
766 } else { // jump to prev field
767 if (event.data.scope.field.position > 0) {
769 $scope.current_event_target_cursor_pos = 0;
770 $scope.current_event_target_cursor_pos_end = 0;
771 $scope.current_event_target = 'r' + $scope.recordId +
772 'f' + (event.data.scope.field.position - 1) +
778 event_return = false;
780 } else if (event.keyCode == 40) { // down arrow...
781 if (event.ctrlKey) { // copy the field down
783 var index_field = event.data.scope.field.position;
784 var new_field = index_field + 1;
787 if (event.data.scope.field.isControlfield()) {
788 field_obj = new MARC21.Field({
789 tag : event.data.scope.field.tag,
790 data : event.data.scope.field.data
794 for (var i in event.data.scope.field.subfields) {
795 sf_clone.push(event.data.scope.field.subfields[i].slice());
797 field_obj = new MARC21.Field({
798 tag : event.data.scope.field.tag,
799 ind1 : event.data.scope.field.ind1,
800 ind2 : event.data.scope.field.ind2,
805 event.data.scope.field.record.insertFieldsAfter(
806 event.data.scope.field,
810 $scope.current_event_target = 'r' + $scope.recordId +
811 'f' + new_field + 'tag';
813 $scope.current_event_target_cursor_pos = 0;
814 $scope.current_event_target_cursor_pos_end = 3;
815 $scope.force_render = true;
817 $timeout(function(){$scope.$digest()}).then(setCaret);
819 } else { // jump to next field
820 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
822 $scope.current_event_target_cursor_pos = 0;
823 $scope.current_event_target_cursor_pos_end = 0;
824 $scope.current_event_target = 'r' + $scope.recordId +
825 'f' + (event.data.scope.field.position + 1) +
831 event_return = false;
833 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
834 $scope.current_event_target = $(event.target).attr('id');
835 if ($scope.current_event_target) {
836 $scope.current_event_target_cursor_pos =
837 event.target.selectionDirection=='backward' ?
838 event.target.selectionStart :
839 event.target.selectionEnd;
846 function setCaret() {
847 if ($scope.current_event_target) {
848 console.log("Putting caret in " + $scope.current_event_target);
849 if (!$scope.current_event_target_cursor_pos_end)
850 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
852 var element = $('#'+$scope.current_event_target).get(0);
855 if (element.setSelectionRange) {
856 element.setSelectionRange(
857 $scope.current_event_target_cursor_pos,
858 $scope.current_event_target_cursor_pos_end
861 $scope.current_event_cursor_pos_end = null;
862 $scope.current_event_target = null;
867 function loadRecord() {
869 var deferred = $q.defer();
870 if ($scope.recordId) {
871 egCore.pcrud.retrieve(
872 $scope.record_type, $scope.recordId
873 ).then(function(rec) {
874 deferred.resolve(rec);
877 if ($scope.recordType == 'bre') {
878 var bre = new egCore.idl.bre();
879 bre.marc($scope.marcXml);
880 deferred.resolve(bre);
881 } else if ($scope.recordType == 'are') {
882 var are = new egCore.idl.are();
883 are.marc($scope.marcXml);
884 deferred.resolve(are);
886 $scope.brandNewRecord = true;
888 return deferred.promise;
889 })().then(function(rec) {
890 $scope.in_redo = true;
891 $scope[$scope.record_type] = rec;
892 $scope.record = new MARC21.Record({ marcxml : $scope.Record().marc() });
893 $scope.calculated_record_type = $scope.record.recordType();
894 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
895 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
896 $scope.save_stack_depth = $scope.record_undo_stack.length;
897 $scope.flat_text_marc = $scope.record.toBreaker();
899 if ($scope.record_type == 'bre') {
900 $scope.bib_source = $scope.Record().source();
904 return egTagTable.fetchFFPosTable($scope.calculated_record_type)
906 return egTagTable.fetchFFValueTable($scope.calculated_record_type)
910 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
911 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
912 $scope.record_undo_stack.push({
914 target: $scope.current_event_target,
915 pos: $scope.current_event_target_cursor_pos
918 if ($scope.force_render) {
919 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
920 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
921 $scope.force_render = false;
924 $scope.flat_text_marc = newVal;
927 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
928 $scope.dirtyFlag = true;
930 $scope.dirtyFlag = false;
933 if ($scope.record_undo_stack.length > $scope.max_undo)
934 $scope.record_undo_stack.shift();
936 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
937 $scope.in_redo = false;
938 $scope.in_undo = false;
941 $scope.processUndo = function () {
942 if ($scope.record_undo_stack.length) {
943 $scope.in_undo = true;
945 var undo_item = $scope.record_undo_stack.pop();
946 $scope.record_redo_stack.push(undo_item);
948 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
949 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
950 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
952 $scope.current_event_target = undo_item.target;
953 $scope.current_event_target_cursor_pos = undo_item.pos;
954 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
956 $timeout(function(){$scope.$digest()}).then(setCaret);
963 $scope.processRedo = function () {
964 if ($scope.record_redo_stack.length) {
965 $scope.in_redo = true;
967 var redo_item = $scope.record_redo_stack.pop();
968 $scope.record_undo_stack.push(redo_item);
970 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
971 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
972 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
974 $scope.current_event_target = redo_item.target;
975 $scope.current_event_target_cursor_pos = redo_item.pos;
976 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
978 $timeout(function(){$scope.$digest()}).then(setCaret);
985 $scope.Record = function () {
986 return $scope[$scope.record_type];
989 $scope.deleteRecord = function () {
990 $scope.Record().deleted(true);
991 return $scope.saveRecord();
994 $scope.undeleteRecord = function () {
995 $scope.Record().deleted(false);
996 return $scope.saveRecord();
999 $scope.validateHeadings = function () {
1000 if ($scope.record_type != 'bre') return;
1001 var chain = $q.when();
1002 angular.forEach($scope.record.fields, function(f) {
1003 if (!$scope.controlSet.bibFieldByTag(f.tag)) return;
1004 // if heading already has a $0, assume it's good
1005 if (f.subfield('0', true).length) {
1006 f.heading_checked = true;
1007 f.heading_valid = true;
1010 var auth_match = $scope.controlSet.bibToAuthorities(f);
1011 chain = chain.then(function() {
1012 var promise = egCore.net.request(
1014 'open-ils.search.authority.simple_heading.from_xml.batch.atomic',
1016 ).then(function (matches) {
1017 f.heading_valid = false;
1018 if (matches[0]) { // probably set
1019 for (var cset in matches[0]) {
1020 var arr = matches[0][cset];
1022 // protect against errant empty string values
1023 if (arr.length == 1 && arr[0] == '')
1025 f.heading_valid = true;
1030 f.heading_checked = true;
1037 $scope.saveRecord = function () {
1038 if ($scope.inPlaceMode) {
1039 $scope.marcXml = $scope.record.toXmlString();
1042 $scope.mangle_005();
1043 $scope.Record().editor(egCore.auth.user().id());
1044 $scope.Record().edit_date('now');
1045 $scope.record.pruneEmptyFieldsAndSubfields();
1046 $scope.Record().marc($scope.record.toXmlString());
1047 if ($scope.recordId) {
1048 return egCore.pcrud.update(
1051 if ($scope.enable_fast_add) {
1054 'open-ils.actor.anon_cache.set_value',
1055 null, 'edit-these-copies', {
1056 record_id: $scope.recordId,
1058 label : $scope.fast_item_callnumber,
1059 barcode : $scope.fast_item_barcode,
1064 ).then(function(key) {
1066 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1067 $timeout(function() { $window.open(url, '_blank') });
1069 alert('Could not create anonymous cache key!');
1073 }).then(loadRecord);
1075 $scope.Record().creator(egCore.auth.user().id());
1076 $scope.Record().create_date('now');
1077 return egCore.pcrud.create(
1079 ).then(function(bre) {
1080 $scope.recordId = bre.id();
1081 if ($scope.enable_fast_add) {
1084 'open-ils.actor.anon_cache.set_value',
1085 null, 'edit-these-copies', {
1086 record_id: $scope.recordId,
1088 label : $scope.fast_item_callnumber,
1089 barcode : $scope.fast_item_barcode,
1094 ).then(function(key) {
1096 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1097 $timeout(function() { $window.open(url, '_blank') });
1099 alert('Could not create anonymous cache key!');
1103 }).then(loadRecord);
1109 $scope.seeBreaker = function () {
1110 alert($scope.record.toBreaker());
1113 $scope.$watch('recordId',
1114 function(newVal, oldVal) {
1115 if (newVal && newVal !== oldVal) {
1120 $scope.$watch('marcXml',
1121 function(newVal, oldVal) {
1122 if (newVal && newVal !== oldVal) {
1128 var unregister = $scope.$watch(function() {
1129 return egTagTable.initialized();
1133 if ($scope.recordId || $scope.marcXml) {
1139 $scope.mangle_005 = function () {
1140 var now = new Date();
1141 var y = now.getUTCFullYear();
1143 var m = now.getUTCMonth() + 1;
1144 if (m < 10) m = '0' + m;
1146 var d = now.getUTCDate();
1147 if (d < 10) d = '0' + d;
1149 var H = now.getUTCHours();
1150 if (H < 10) H = '0' + H;
1152 var M = now.getUTCMinutes();
1153 if (M < 10) M = '0' + M;
1155 var S = now.getUTCSeconds();
1156 if (S < 10) S = '0' + S;
1158 var stamp = '' + y + m + d + H + M + S + '.0';
1159 var f = $scope.record.field('005',true)[0];
1163 $scope.record.insertOrderedFields(
1178 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
1182 template: '<span class="nullable">'+
1183 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
1184 '<option value="">Select a Source</option>'+
1187 controller: ['$scope','egCore',
1188 function ($scope , egCore) {
1190 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
1191 .then(function(list) { $scope.bib_sources = list; });
1193 $scope.$watch('bib_source',
1194 function(newVal, oldVal) {
1195 if (newVal !== oldVal) {
1196 $scope.bre.source(newVal);
1206 .directive("egMarcEditAuthorityLinker", function () {
1210 templateUrl: './cat/share/t_authority_linker',
1216 controller: ['$scope','$modal','egCore','egAuth',
1217 function ($scope , $modal, egCore, egAuth) {
1219 $scope.searchStr = '';
1220 var cni = egCore.env.aous['cat.marc_control_number_identifier'] ||
1221 'Set cat.marc_control_number_identifier in Library Settings';
1223 var axis_list = $scope.controlSet.bibFieldBrowseAxes($scope.bibField.tag);
1224 $scope.axis = axis_list[0];
1226 $scope._controlled_sf_list = {};
1227 $scope._controlled_auth_sf_list = {};
1229 angular.forEach($scope.controlSet.controlSetList(), function(acs_id) {
1230 if ($scope.controlSet.controlSet(acs_id).control_map[$scope.bibField.tag])
1231 found_acs.push(acs_id);
1233 if (found_acs.length) {
1234 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag],
1235 function(value, sf_label) {
1236 $scope._controlled_sf_list[ sf_label ] = 1;
1237 angular.forEach($scope.controlSet.controlSet(found_acs[0]).control_map[$scope.bibField.tag][sf_label],
1238 function(auth_sf, auth_tag) {
1239 if (!$scope._controlled_auth_sf_list[auth_tag]) {
1240 $scope._controlled_auth_sf_list[auth_tag] = { };
1242 $scope._controlled_auth_sf_list[auth_tag][auth_sf] = 1;
1249 $scope.bibField.subfields.forEach(function (sf) {
1250 if (sf[0] in $scope._controlled_sf_list) {
1252 sf.selectable = true;
1254 sf.selectable = false;
1257 $scope.summarizeField = function() {
1259 'tag': $scope.bibField.tag,
1260 'ind1': $scope.bibField.ind1,
1261 'ind2': $scope.bibField.ind2,
1264 $scope.bibField.subfields.forEach(function(sf) {
1266 source_f.subfields.push([ sf[0], sf[1] ]);
1271 $scope.getSearchString = function() {
1272 var source_f = $scope.summarizeField();
1274 angular.forEach(source_f.subfields, function(val) {
1275 values.push(val[1]);
1277 return values.join(' ');
1279 $scope.searchStr = $scope.getSearchString();
1280 $scope.$watch(function() {
1282 angular.forEach($scope.bibField.subfields, function(sf) {
1283 if (sf.selected) ct++
1287 function(newVal, oldVal) {
1288 $scope.searchStr = $scope.getSearchString();
1291 $scope.updateSubfieldZero = function(value) {
1292 $scope.changed = true;
1293 $scope.bibField.deleteSubfield({ code : ['0'] });
1294 $scope.bibField.subfields.push([
1295 '0', '(' + cni + ')' + value
1299 $scope.applyHeading = function(headingField) {
1300 // TODO: move the MARC21 rules for copying indicators
1302 if (headingField.tag == '130' && $scope.bibField.tag == '130') {
1303 $scope.bibField.ind1 = headingField.ind2;
1305 $scope.bibField.ind1 = headingField.ind1;
1307 // deal with 4xx and 5xx
1308 var authFallbackTag = '1' + headingField.tag.substr(1, 2);
1309 var _valid_auth_sfs = (headingField.tag in $scope._controlled_auth_sf_list) ?
1310 $scope._controlled_auth_sf_list[headingField.tag] :
1311 (authFallbackTag in $scope._controlled_auth_sf_list) ?
1312 $scope._controlled_auth_sf_list[authFallbackTag] :
1314 // save the $0 for later use
1316 if (headingField.subfield('0')) {
1317 sfZero = headingField.subfield('0')[1];
1319 // grab any bib subfields not under authority control
1320 // TODO do something about uncontrolled subdivisions
1321 var uncontrolledBibSf = [];
1322 angular.forEach($scope.bibField.subfields, function(sf) {
1323 if (!(sf[0] in $scope._controlled_sf_list) && (sf[0] != '0')) {
1324 uncontrolledBibSf.push([ sf[0], sf[1] ]);
1327 // grab the authority subfields
1328 var authoritySf = [];
1329 angular.forEach(headingField.subfields, function(sf) {
1330 if (sf[0] in _valid_auth_sfs) {
1331 authoritySf.push([ sf[0], sf[1] ]);
1334 $scope.bibField.subfields.length = 0;
1335 angular.forEach(authoritySf, function(sf) {
1336 $scope.bibField.addSubfields(sf[0], sf[1]);
1338 angular.forEach(uncontrolledBibSf, function(sf) {
1339 $scope.bibField.addSubfields(sf[0], sf[1]);
1342 $scope.bibField.addSubfields('0', sfZero);
1344 $scope.bibField.subfields.forEach(function (sf) {
1345 if (sf[0] in $scope._controlled_sf_list) {
1346 // intentionally not selecting any subfields
1347 // after we've applied an authority heading
1348 sf.selected = false;
1349 sf.selectable = true;
1351 sf.selectable = false;
1354 $scope.changed = true;
1357 $scope.createAuthorityFromBib = function(spawn_editor) {
1358 var source_f = $scope.summarizeField();
1360 var args = { authority_id : 0 };
1361 var method = (spawn_editor) ?
1362 'open-ils.cat.authority.record.create_from_bib.readonly' :
1363 'open-ils.cat.authority.record.create_from_bib';
1370 ).then(function(newAuthority) {
1373 templateUrl: './cat/share/t_edit_new_authority',
1376 ['$scope', '$modalInstance', function($scope, $modalInstance) {
1377 $scope.focusMe = true;
1379 $scope.dirty_flag = false;
1380 $scope.marc_xml = newAuthority,
1381 $scope.ok = function(args) { $modalInstance.close(args) }
1382 $scope.cancel = function () { $modalInstance.dismiss() }
1384 }).result.then(function (args) {
1385 if (!args || !args.authority_id) return;
1386 $scope.updateSubfieldZero(args.authority_id);
1389 $scope.updateSubfieldZero(newAuthority.id());
1399 .directive("egMarcEditAuthorityBrowser", function () {
1403 templateUrl: './cat/share/t_authority_browser',
1410 controller: ['$scope','$http',
1411 function ($scope , $http) {
1415 $scope.main_headings = [];
1417 function getHeadingString(headingField) {
1419 angular.forEach(headingField.subfields, function (sf) {
1420 if (['x', 'y', 'z'].indexOf(sf[0]) > -1) {
1431 $scope.doBrowse = function() {
1432 $scope.main_headings.length = 0;
1433 if ($scope.searchString.length == 0) return;
1434 var type = 'authority.'
1435 var url = '/opac/extras/browse/marcxml/'
1436 + 'authority.' + $scope.axis + '.refs'
1437 + '/1' // OU - currently unscoped
1438 + '/' + $scope.searchString
1440 + '/' + $scope.limit;
1444 transformResponse : function(data) {
1445 // use a bit of jQuery to deal with the XML
1446 var $xml = $( $.parseXML(data) );
1448 $xml.find('record').each(function() {
1449 var rec = new MARC21.Record();
1450 rec.fromXmlDocument($(this)[0].outerHTML);
1455 }).then(function(response) {
1456 angular.forEach(response.data, function(rec) {
1457 var authId = rec.subfield('901', 'c')[1];
1459 if (rec.field('003')) {
1460 auth_org = rec.field('003').data;
1462 var headingField = rec.field('1..');
1463 var seeFroms = rec.field('4..', true);
1464 var seeAlsos = rec.field('5..', true);
1466 var main_heading = {
1467 authority_id : authId,
1468 heading : getHeadingString(headingField),
1469 seealso_headings : [ ],
1470 seefrom_headings : [ ],
1475 sfZero = '(' + auth_org + ')';
1478 headingField.addSubfields('0', sfZero);
1480 main_heading['headingField'] = headingField;
1481 angular.forEach(seeAlsos, function(headingField) {
1482 main_heading.seealso_headings.push({
1483 heading : getHeadingString(headingField),
1484 headingField : headingField
1487 angular.forEach(seeFroms, function(headingField) {
1488 main_heading.seefrom_headings.push({
1489 heading : getHeadingString(headingField),
1490 headingField : headingField
1493 $scope.main_headings.push(main_heading);
1498 $scope.$watch('searchString',
1499 function(newVal, oldVal) {
1500 if (newVal !== oldVal) {
1505 $scope.$watch('page',
1506 function(newVal, oldVal) {
1507 if (newVal !== oldVal) {