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_container = $scope.contextItemGenerator();
65 $scope.showContext = function (event) {
66 if ($scope.context_menu_element) {
67 console.log('Reshowing context menu...');
68 $('body').trigger('click');
69 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
70 $('body').on('click.context_menu',function() {
71 $($scope.context_menu_element).css('display','none');
72 $('body').off('click.context_menu');
77 if (angular.isArray($scope.item_container)) { // 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">'+
83 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
86 var tnode = angular.element(tmpl);
87 $document.find('body').append(tnode);
95 $scope.context_menu_element = tnode;
98 var e = $compile(tnode)($scope);
102 $('body').on('click.context_menu',function() {
103 $(tnode).css('display','none');
104 $('body').off('click.context_menu');
115 link: function (scope, element, attrs) {
117 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
119 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
121 if (scope.item_container && scope.item_container.length)
122 element.bind('contextmenu', scope.showContext);
127 .directive("egMarcEditFixedField", function () {
131 template: '<div class="col-md-2">'+
132 '<div class="col-md-1"><label name="{{fixedField}}" for="{{fixedField}}_ff_input">{{fixedField}}</label></div>'+
133 '<div class="col-md-1"><input type="text" style="padding-left: 5px; margin-left: 1em" size="4" id="{{fixedField}}_ff_input"/></div>'+
135 scope: { record: "=", fixedField: "@" },
137 controller : ['$scope', '$element', 'egTagTable',
138 function ( $scope , $element , egTagTable) {
139 $($element).children().css({ display : 'none' });
142 $scope.$watch('record.ready', function (newVal, oldVal) {
143 if (newVal && newVal != oldVal) {
144 $scope.rtype = $scope.record.recordType();
145 egTagTable.fetchFFPosTable( $scope.rtype ).then(function (ff_list) { // This does not work when fetching from hatch...
146 angular.forEach(ff_list, function (ff) {
148 if (ff.fixed_field == $scope.fixedField && ff.rec_type == $scope.rtype) {
150 $($element).children().css({ display : 'inline' });
152 var input = $($element).find('input');
153 input.attr('maxlength', $scope.me.length);
154 input.val($scope.record.extractFixedField($scope.me.fixed_field));
155 input.on('keyup', function(e) {
156 $scope.record.setFixedField($scope.me.fixed_field, input.val());
157 $scope.$parent.$digest();
170 .directive("egMarcEditSubfield", function () {
175 '<span><label class="marcedit marcsfcodedelimiter"'+
176 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
177 '>‡</label><eg-marc-edit-editable '+
179 'class="marcedit marcsf marcsfcode" '+
181 'subfield="subfield" '+
182 'content="subfield[0]" '+
184 'on-keydown="onKeydown" '+
185 'context-item-generator="sf_code_options" '+
186 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
188 '<span><eg-marc-edit-editable '+
190 'class="marcedit marcsf marcsfvalue" '+
192 'subfield="subfield" '+
193 'content="subfield[1]" '+
194 'on-keydown="onKeydown" '+
195 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
198 scope: { field: "=", subfield: "=", onKeydown: '=' },
200 controller : ['$scope', 'egTagTable',
201 function ( $scope , egTagTable) {
203 $scope.sf_code_options = function () {
204 return egTagTable.getSubfieldCodes($scope.field.tag);
211 .directive("egMarcEditInd", function () {
215 template: '<span><eg-marc-edit-editable '+
217 'class="marcedit marcind" '+
221 'on-keydown="onKeydown" '+
222 'context-item-generator="ind_val_options" '+
223 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
225 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
227 controller : ['$scope', 'egTagTable',
228 function ( $scope , egTagTable) {
230 $scope.ind_val_options = function () {
231 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
238 .directive("egMarcEditTag", function () {
242 template: '<span><eg-marc-edit-editable '+
244 'class="marcedit marctag" '+
248 'on-keydown="onKeydown" '+
249 'context-item-generator="tag_options" '+
250 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
252 scope: { tag : '=', field: '=', onKeydown: '=' },
254 controller : ['$scope', 'egTagTable',
255 function ( $scope , egTagTable) {
257 $scope.tag_options = function () {
258 return egTagTable.getFieldTags();
265 .directive("egMarcEditDatafield", function () {
270 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
271 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
272 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
273 '<span><eg-marc-edit-subfield ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
275 scope: { field: "=", onKeydown: '=' },
280 .directive("egMarcEditControlfield", function () {
285 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
286 '<span><eg-marc-edit-editable '+
289 'class="marcedit marcdata" '+
290 'content="field.data" '+
291 'on-keydown="onKeydown" '+
292 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
295 scope: { field: "=", onKeydown: '=' }
299 .directive("egMarcEditLeader", function () {
304 '<span><eg-marc-edit-editable '+
305 'class="marcedit marctag" '+
307 'on-keydown="onKeydown" '+
309 'disabled="disabled"'+
311 '<span><eg-marc-edit-editable '+
312 'class="marcedit marcdata" '+
314 'max="{{record.leader.length}}" '+
315 'content="record.leader" '+
316 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
317 'on-keydown="onKeydown"'+
320 controller : ['$scope',
321 function ( $scope ) {
325 scope: { record: "=", onKeydown: '=' }
329 /// TODO: fixed field editor and such
330 .directive("egMarcEditRecord", function () {
332 templateUrl : './cat/share/t_marcedit',
341 link: function (scope, element, attrs) {
343 element.bind('click', function(e) {;
344 scope.current_event_target = $(e.target).attr('id');
345 if (scope.current_event_target) {
346 console.log('Recording click event on ' + scope.current_event_target);
347 scope.current_event_target_cursor_pos =
348 e.target.selectionDirection=='backward' ?
349 e.target.selectionStart :
350 e.target.selectionEnd;
355 controller : ['$timeout','$scope','egCore', 'egTagTable',
356 function ( $timeout , $scope , egCore , egTagTable ) {
358 $scope.bib_source = null;
359 $scope.record_type = $scope.recordType || 'bre';
360 $scope.max_undo = $scope.maxUndo || 100;
361 $scope.record_undo_stack = [];
362 $scope.record_redo_stack = [];
363 $scope.in_undo = false;
364 $scope.in_redo = false;
365 $scope.record = new MARC21.Record();
366 $scope.save_stack_depth = 0;
367 $scope.controlfields = [];
368 $scope.datafields = [];
370 egTagTable.loadTagTable();
372 $scope.onKeydown = function (event) {
373 var event_return = true;
376 'keydown: which='+event.which+
377 ', ctrlKey='+event.ctrlKey+
378 ', shiftKey='+event.shiftKey+
379 ', altKey='+event.altKey+
380 ', metaKey='+event.altKey
383 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
384 event_return = $scope.processRedo();
385 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
386 event_return = $scope.processUndo();
387 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
389 var element = $(event.target);
390 var new_sf, index_sf, move_data;
392 if (element.hasClass('marcsfvalue')) {
393 index_sf = event.data.scope.subfield[2];
394 new_sf = index_sf + 1;
396 var start = event.target.selectionStart;
397 var end = event.target.selectionEnd - event.target.selectionStart ?
398 event.target.selectionEnd :
399 event.target.value.length;
401 move_data = event.target.value.substring(start,end);
403 } else if (element.hasClass('marcsfcode')) {
404 index_sf = event.data.scope.subfield[2];
405 new_sf = index_sf + 1;
406 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
411 $scope.current_event_target = 'r' + $scope.recordId +
412 'f' + event.data.scope.field.position +
413 's' + new_sf + 'code';
415 event.data.scope.field.subfields.forEach(function(sf) {
416 if (sf[2] >= new_sf) sf[2]++;
417 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
419 event.data.scope.field.subfields.splice(
422 [' ', move_data, new_sf ]
425 $scope.current_event_target_cursor_pos = 0;
426 $scope.current_event_target_cursor_pos_end = 1;
428 $timeout(function(){$scope.$digest()}).then(setCaret);
430 event_return = false;
432 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
433 event.data.scope.field.record.insertOrderedFields(
440 $scope.force_render = true;
441 $timeout(function(){$scope.$digest()}).then(setCaret);
443 event_return = false;
445 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
446 event.data.scope.field.record.insertOrderedFields(
453 $scope.force_render = true;
454 $timeout(function(){$scope.$digest()}).then(setCaret);
456 event_return = false;
458 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
459 var new_008_data = event.data.scope.field.record.generate008();
462 var old_008s = event.data.scope.field.record.field('008',true);
463 old_008s.forEach(function(o) {
464 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
465 domnode.scope().$destroy();
467 event.data.scope.field.record.deleteFields(o);
470 event.data.scope.field.record.insertOrderedFields(
477 $scope.force_render = true;
478 $timeout(function(){$scope.$digest()}).then(setCaret);
480 event_return = false;
482 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
484 var element = $(event.target);
486 var index_field = event.data.scope.field.position;
487 var new_field = index_field + 1;
489 event.data.scope.field.record.insertFieldsAfter(
490 event.data.scope.field,
493 subfields : [[' ','',0]]
497 $scope.current_event_target = 'r' + $scope.recordId +
498 'f' + new_field + 'tag';
500 $scope.current_event_target_cursor_pos = 0;
501 $scope.current_event_target_cursor_pos_end = 3;
502 $scope.force_render = true;
504 $timeout(function(){$scope.$digest()}).then(setCaret);
506 event_return = false;
508 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
510 var del_field = event.data.scope.field.position;
512 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
514 event.data.scope.field.record.deleteFields(
515 event.data.scope.field
518 domnode.scope().$destroy();
521 $scope.current_event_target = 'r' + $scope.recordId +
522 'f' + del_field + 'tag';
524 $scope.current_event_target_cursor_pos = 0;
525 $scope.current_event_target_cursor_pos_end = 0
526 $scope.force_render = true;
528 $timeout(function(){$scope.$digest()}).then(setCaret);
530 event_return = false;
532 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
534 var sf = event.data.scope.subfield[2] - 1;
535 if (sf == -1) sf = 0;
537 event.data.scope.field.deleteExactSubfields(
538 event.data.scope.subfield
541 if (!event.data.scope.field.subfields[sf]) {
542 $scope.current_event_target = 'r' + $scope.recordId +
543 'f' + event.data.scope.field.position +
546 $scope.current_event_target = 'r' + $scope.recordId +
547 'f' + event.data.scope.field.position +
551 $scope.current_event_target_cursor_pos = 0;
552 $scope.current_event_target_cursor_pos_end = 0;
553 $scope.force_render = true;
555 $timeout(function(){$scope.$digest()}).then(setCaret);
557 event_return = false;
559 } else if (event.keyCode == 38) {
560 if (event.ctrlKey) { // copy the field up
561 var index_field = event.data.scope.field.position;
564 if (event.data.scope.field.isControlfield()) {
565 field_obj = new MARC21.Field({
566 tag : event.data.scope.field.tag,
567 data : event.data.scope.field.data
571 for (var i in event.data.scope.field.subfields) {
572 sf_clone.push(event.data.scope.field.subfields[i].slice());
574 field_obj = new MARC21.Field({
575 tag : event.data.scope.field.tag,
576 ind1 : event.data.scope.field.ind1,
577 ind2 : event.data.scope.field.ind2,
583 event.data.scope.field.record.insertFieldsBefore(
584 event.data.scope.field,
588 $scope.current_event_target = 'r' + $scope.recordId +
589 'f' + index_field + 'tag';
591 $scope.current_event_target_cursor_pos = 0;
592 $scope.current_event_target_cursor_pos_end = 3;
593 $scope.force_render = true;
595 $timeout(function(){$scope.$digest()}).then(setCaret);
597 } else { // jump to prev field
598 if (event.data.scope.field.position > 0) {
600 $scope.current_event_target_cursor_pos = 0;
601 $scope.current_event_target_cursor_pos_end = 0;
602 $scope.current_event_target = 'r' + $scope.recordId +
603 'f' + (event.data.scope.field.position - 1) +
609 event_return = false;
611 } else if (event.keyCode == 40) { // down arrow...
612 if (event.ctrlKey) { // copy the field down
614 var index_field = event.data.scope.field.position;
615 var new_field = index_field + 1;
618 if (event.data.scope.field.isControlfield()) {
619 field_obj = new MARC21.Field({
620 tag : event.data.scope.field.tag,
621 data : event.data.scope.field.data
625 for (var i in event.data.scope.field.subfields) {
626 sf_clone.push(event.data.scope.field.subfields[i].slice());
628 field_obj = new MARC21.Field({
629 tag : event.data.scope.field.tag,
630 ind1 : event.data.scope.field.ind1,
631 ind2 : event.data.scope.field.ind2,
636 event.data.scope.field.record.insertFieldsAfter(
637 event.data.scope.field,
641 $scope.current_event_target = 'r' + $scope.recordId +
642 'f' + new_field + 'tag';
644 $scope.current_event_target_cursor_pos = 0;
645 $scope.current_event_target_cursor_pos_end = 3;
646 $scope.force_render = true;
648 $timeout(function(){$scope.$digest()}).then(setCaret);
650 } else { // jump to next field
651 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
653 $scope.current_event_target_cursor_pos = 0;
654 $scope.current_event_target_cursor_pos_end = 0;
655 $scope.current_event_target = 'r' + $scope.recordId +
656 'f' + (event.data.scope.field.position + 1) +
662 event_return = false;
664 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
665 $scope.current_event_target = $(event.target).attr('id');
666 if ($scope.current_event_target) {
667 $scope.current_event_target_cursor_pos =
668 event.target.selectionDirection=='backward' ?
669 event.target.selectionStart :
670 event.target.selectionEnd;
677 function setCaret() {
678 if ($scope.current_event_target) {
679 console.log("Putting caret in " + $scope.current_event_target);
680 if (!$scope.current_event_target_cursor_pos_end)
681 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
683 var element = $('#'+$scope.current_event_target).get(0);
686 if (element.setSelectionRange) {
687 element.setSelectionRange(
688 $scope.current_event_target_cursor_pos,
689 $scope.current_event_target_cursor_pos_end
692 $scope.current_event_cursor_pos_end = null;
693 $scope.current_event_target = null;
698 function loadRecord() {
699 return egCore.pcrud.retrieve(
700 $scope.record_type, $scope.recordId
701 ).then(function(rec) {
702 $scope.in_redo = true;
703 $scope[$scope.record_type] = rec;
704 $scope.record = new MARC21.Record({ marcxml : $scope[$scope.record_type].marc() });
705 $scope.calculated_record_type = $scope.record.recordType();
706 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
707 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
708 $scope.save_stack_depth = $scope.record_undo_stack.length;
710 if ($scope.record_type == 'bre') {
711 $scope.bib_source = $scope[$scope.record_type].source();
715 return egTagTable.fetchFFPosTable($scope.calculated_record_type)
717 return egTagTable.fetchFFValueTable($scope.calculated_record_type)
721 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
722 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
723 $scope.record_undo_stack.push({
725 target: $scope.current_event_target,
726 pos: $scope.current_event_target_cursor_pos
729 if ($scope.force_render) {
730 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
731 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
732 $scope.force_render = false;
737 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
738 $scope.dirtyFlag = true;
740 $scope.dirtyFlag = false;
743 if ($scope.record_undo_stack.length > $scope.max_undo)
744 $scope.record_undo_stack.shift();
746 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
747 $scope.in_redo = false;
748 $scope.in_undo = false;
751 $scope.processUndo = function () {
752 if ($scope.record_undo_stack.length) {
753 $scope.in_undo = true;
755 var undo_item = $scope.record_undo_stack.pop();
756 $scope.record_redo_stack.push(undo_item);
758 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
759 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
760 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
762 $scope.current_event_target = undo_item.target;
763 $scope.current_event_target_cursor_pos = undo_item.pos;
764 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
766 $timeout(function(){$scope.$digest()}).then(setCaret);
773 $scope.processRedo = function () {
774 if ($scope.record_redo_stack.length) {
775 $scope.in_redo = true;
777 var redo_item = $scope.record_redo_stack.pop();
778 $scope.record_undo_stack.push(redo_item);
780 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
781 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
782 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
784 $scope.current_event_target = redo_item.target;
785 $scope.current_event_target_cursor_pos = redo_item.pos;
786 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
788 $timeout(function(){$scope.$digest()}).then(setCaret);
795 $scope.saveRecord = function () {
797 $scope[$scope.record_type].marc($scope.record.toXmlString());
798 return egCore.pcrud.update(
799 $scope[$scope.record_type]
803 $scope.seeBreaker = function () {
804 alert($scope.record.toBreaker());
807 $scope.$watch('recordId',
808 function(newVal, oldVal) {
809 if (newVal && newVal !== oldVal) {
818 $scope.mangle_005 = function () {
819 var now = new Date();
820 var y = now.getUTCFullYear();
822 var m = now.getUTCMonth() + 1;
823 if (m < 10) m = '0' + m;
825 var d = now.getUTCDate();
826 if (d < 10) d = '0' + d;
828 var H = now.getUTCHours();
829 if (H < 10) H = '0' + H;
831 var M = now.getUTCMinutes();
832 if (M < 10) M = '0' + M;
834 var S = now.getUTCSeconds();
835 if (S < 10) S = '0' + S;
837 var stamp = '' + y + m + d + H + M + S + '.0';
838 var f = $scope.record.field('005',true)[0];
842 $scope.record.insertOrderedFields(
857 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
861 template: '<span class="nullable">'+
862 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
863 '<option value="">Select a Source</option>'+
866 controller: ['$scope','egCore',
867 function ($scope , egCore) {
869 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
870 .then(function(list) { $scope.bib_sources = list; });
872 $scope.$watch('bib_source',
873 function(newVal, oldVal) {
874 if (newVal !== oldVal) {
875 $scope.bre.source(newVal);