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("egMarcEditSubfield", function () {
132 '<span><label class="marcedit marcsfcodedelimiter"'+
133 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
134 '>‡</label><eg-marc-edit-editable '+
136 'class="marcedit marcsf marcsfcode" '+
138 'subfield="subfield" '+
139 'content="subfield[0]" '+
141 'on-keydown="onKeydown" '+
142 'context-item-generator="sf_code_options" '+
143 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
145 '<span><eg-marc-edit-editable '+
147 'class="marcedit marcsf marcsfvalue" '+
149 'subfield="subfield" '+
150 'content="subfield[1]" '+
151 'on-keydown="onKeydown" '+
152 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
155 scope: { field: "=", subfield: "=", onKeydown: '=' },
157 controller : ['$scope', 'egTagTable',
158 function ( $scope , egTagTable) {
160 $scope.sf_code_options = function () {
161 return egTagTable.getSubfieldCodes($scope.field.tag);
168 .directive("egMarcEditInd", function () {
172 template: '<span><eg-marc-edit-editable '+
174 'class="marcedit marcind" '+
178 'on-keydown="onKeydown" '+
179 'context-item-generator="ind_val_options" '+
180 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
182 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
184 controller : ['$scope', 'egTagTable',
185 function ( $scope , egTagTable) {
187 $scope.ind_val_options = function () {
188 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
195 .directive("egMarcEditTag", function () {
199 template: '<span><eg-marc-edit-editable '+
201 'class="marcedit marctag" '+
205 'on-keydown="onKeydown" '+
206 'context-item-generator="tag_options" '+
207 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
209 scope: { tag : '=', field: '=', onKeydown: '=' },
211 controller : ['$scope', 'egTagTable',
212 function ( $scope , egTagTable) {
214 $scope.tag_options = function () {
215 return egTagTable.getFieldTags();
222 .directive("egMarcEditDatafield", function () {
227 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
228 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
229 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
230 '<span><eg-marc-edit-subfield ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
232 scope: { field: "=", onKeydown: '=' },
237 .directive("egMarcEditControlfield", function () {
242 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
243 '<span><eg-marc-edit-editable '+
246 'class="marcedit marcdata" '+
247 'content="field.data" '+
248 'on-keydown="onKeydown" '+
249 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
252 scope: { field: "=", onKeydown: '=' }
256 .directive("egMarcEditLeader", function () {
261 '<span><eg-marc-edit-editable '+
262 'class="marcedit marctag" '+
264 'on-keydown="onKeydown" '+
266 'disabled="disabled"'+
268 '<span><eg-marc-edit-editable '+
269 'class="marcedit marcdata" '+
271 'max="{{record.leader.length}}" '+
272 'content="record.leader" '+
273 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
274 'on-keydown="onKeydown"'+
277 controller : ['$scope',
278 function ( $scope ) {
282 scope: { record: "=", onKeydown: '=' }
286 /// TODO: fixed field editor and such
287 .directive("egMarcEditRecord", function () {
289 templateUrl : './cat/share/t_marcedit',
298 link: function (scope, element, attrs) {
300 element.bind('click', function(e) {;
301 scope.current_event_target = $(e.target).attr('id');
302 if (scope.current_event_target) {
303 console.log('Recording click event on ' + scope.current_event_target);
304 scope.current_event_target_cursor_pos =
305 e.target.selectionDirection=='backward' ?
306 e.target.selectionStart :
307 e.target.selectionEnd;
312 controller : ['$timeout','$scope','egCore', 'egTagTable',
313 function ( $timeout , $scope , egCore , egTagTable ) {
315 $scope.bib_source = null;
316 $scope.record_type = $scope.recordType || 'bre';
317 $scope.max_undo = $scope.maxUndo || 100;
318 $scope.record_undo_stack = [];
319 $scope.record_redo_stack = [];
320 $scope.in_undo = false;
321 $scope.in_redo = false;
322 $scope.record = new MARC21.Record();
323 $scope.save_stack_depth = 0;
324 $scope.controlfields = [];
325 $scope.datafields = [];
327 egTagTable.loadTagTable();
329 $scope.onKeydown = function (event) {
330 var event_return = true;
333 'keydown: which='+event.which+
334 ', ctrlKey='+event.ctrlKey+
335 ', shiftKey='+event.shiftKey+
336 ', altKey='+event.altKey+
337 ', metaKey='+event.altKey
340 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
341 event_return = $scope.processRedo();
342 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
343 event_return = $scope.processUndo();
344 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
346 var element = $(event.target);
347 var new_sf, index_sf, move_data;
349 if (element.hasClass('marcsfvalue')) {
350 index_sf = event.data.scope.subfield[2];
351 new_sf = index_sf + 1;
353 var start = event.target.selectionStart;
354 var end = event.target.selectionEnd - event.target.selectionStart ?
355 event.target.selectionEnd :
356 event.target.value.length;
358 move_data = event.target.value.substring(start,end);
360 } else if (element.hasClass('marcsfcode')) {
361 index_sf = event.data.scope.subfield[2];
362 new_sf = index_sf + 1;
363 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
368 $scope.current_event_target = 'r' + $scope.recordId +
369 'f' + event.data.scope.field.position +
370 's' + new_sf + 'code';
372 event.data.scope.field.subfields.forEach(function(sf) {
373 if (sf[2] >= new_sf) sf[2]++;
374 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
376 event.data.scope.field.subfields.splice(
379 [' ', move_data, new_sf ]
382 $scope.current_event_target_cursor_pos = 0;
383 $scope.current_event_target_cursor_pos_end = 1;
385 $timeout(function(){$scope.$digest()}).then(setCaret);
387 event_return = false;
389 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
390 event.data.scope.field.record.insertOrderedFields(
397 $scope.force_render = true;
398 $timeout(function(){$scope.$digest()}).then(setCaret);
400 event_return = false;
402 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
403 event.data.scope.field.record.insertOrderedFields(
410 $scope.force_render = true;
411 $timeout(function(){$scope.$digest()}).then(setCaret);
413 event_return = false;
415 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
416 var new_008_data = event.data.scope.field.record.generate008();
419 var old_008s = event.data.scope.field.record.field('008',true);
420 old_008s.forEach(function(o) {
421 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
422 domnode.scope().$destroy();
424 event.data.scope.field.record.deleteFields(o);
427 event.data.scope.field.record.insertOrderedFields(
434 $scope.force_render = true;
435 $timeout(function(){$scope.$digest()}).then(setCaret);
437 event_return = false;
439 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
441 var element = $(event.target);
443 var index_field = event.data.scope.field.position;
444 var new_field = index_field + 1;
446 event.data.scope.field.record.insertFieldsAfter(
447 event.data.scope.field,
450 subfields : [[' ','',0]]
454 $scope.current_event_target = 'r' + $scope.recordId +
455 'f' + new_field + 'tag';
457 $scope.current_event_target_cursor_pos = 0;
458 $scope.current_event_target_cursor_pos_end = 3;
459 $scope.force_render = true;
461 $timeout(function(){$scope.$digest()}).then(setCaret);
463 event_return = false;
465 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
467 var del_field = event.data.scope.field.position;
469 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
471 event.data.scope.field.record.deleteFields(
472 event.data.scope.field
475 domnode.scope().$destroy();
478 $scope.current_event_target = 'r' + $scope.recordId +
479 'f' + del_field + 'tag';
481 $scope.current_event_target_cursor_pos = 0;
482 $scope.current_event_target_cursor_pos_end = 0
483 $scope.force_render = true;
485 $timeout(function(){$scope.$digest()}).then(setCaret);
487 event_return = false;
489 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
491 var sf = event.data.scope.subfield[2] - 1;
492 if (sf == -1) sf = 0;
494 event.data.scope.field.deleteExactSubfields(
495 event.data.scope.subfield
498 if (!event.data.scope.field.subfields[sf]) {
499 $scope.current_event_target = 'r' + $scope.recordId +
500 'f' + event.data.scope.field.position +
503 $scope.current_event_target = 'r' + $scope.recordId +
504 'f' + event.data.scope.field.position +
508 $scope.current_event_target_cursor_pos = 0;
509 $scope.current_event_target_cursor_pos_end = 0;
510 $scope.force_render = true;
512 $timeout(function(){$scope.$digest()}).then(setCaret);
514 event_return = false;
516 } else if (event.keyCode == 38) {
517 if (event.ctrlKey) { // copy the field up
518 var index_field = event.data.scope.field.position;
521 if (event.data.scope.field.isControlfield()) {
522 field_obj = new MARC21.Field({
523 tag : event.data.scope.field.tag,
524 data : event.data.scope.field.data
528 for (var i in event.data.scope.field.subfields) {
529 sf_clone.push(event.data.scope.field.subfields[i].slice());
531 field_obj = new MARC21.Field({
532 tag : event.data.scope.field.tag,
533 ind1 : event.data.scope.field.ind1,
534 ind2 : event.data.scope.field.ind2,
540 event.data.scope.field.record.insertFieldsBefore(
541 event.data.scope.field,
545 $scope.current_event_target = 'r' + $scope.recordId +
546 'f' + index_field + 'tag';
548 $scope.current_event_target_cursor_pos = 0;
549 $scope.current_event_target_cursor_pos_end = 3;
550 $scope.force_render = true;
552 $timeout(function(){$scope.$digest()}).then(setCaret);
554 } else { // jump to prev field
555 if (event.data.scope.field.position > 0) {
557 $scope.current_event_target_cursor_pos = 0;
558 $scope.current_event_target_cursor_pos_end = 0;
559 $scope.current_event_target = 'r' + $scope.recordId +
560 'f' + (event.data.scope.field.position - 1) +
566 event_return = false;
568 } else if (event.keyCode == 40) { // down arrow...
569 if (event.ctrlKey) { // copy the field down
571 var index_field = event.data.scope.field.position;
572 var new_field = index_field + 1;
575 if (event.data.scope.field.isControlfield()) {
576 field_obj = new MARC21.Field({
577 tag : event.data.scope.field.tag,
578 data : event.data.scope.field.data
582 for (var i in event.data.scope.field.subfields) {
583 sf_clone.push(event.data.scope.field.subfields[i].slice());
585 field_obj = new MARC21.Field({
586 tag : event.data.scope.field.tag,
587 ind1 : event.data.scope.field.ind1,
588 ind2 : event.data.scope.field.ind2,
593 event.data.scope.field.record.insertFieldsAfter(
594 event.data.scope.field,
598 $scope.current_event_target = 'r' + $scope.recordId +
599 'f' + new_field + 'tag';
601 $scope.current_event_target_cursor_pos = 0;
602 $scope.current_event_target_cursor_pos_end = 3;
603 $scope.force_render = true;
605 $timeout(function(){$scope.$digest()}).then(setCaret);
607 } else { // jump to next field
608 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
610 $scope.current_event_target_cursor_pos = 0;
611 $scope.current_event_target_cursor_pos_end = 0;
612 $scope.current_event_target = 'r' + $scope.recordId +
613 'f' + (event.data.scope.field.position + 1) +
619 event_return = false;
621 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
622 $scope.current_event_target = $(event.target).attr('id');
623 if ($scope.current_event_target) {
624 $scope.current_event_target_cursor_pos =
625 event.target.selectionDirection=='backward' ?
626 event.target.selectionStart :
627 event.target.selectionEnd;
634 function setCaret() {
635 if ($scope.current_event_target) {
636 console.log("Putting caret in " + $scope.current_event_target);
637 if (!$scope.current_event_target_cursor_pos_end)
638 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
640 var element = $('#'+$scope.current_event_target).get(0);
643 if (element.setSelectionRange) {
644 element.setSelectionRange(
645 $scope.current_event_target_cursor_pos,
646 $scope.current_event_target_cursor_pos_end
649 $scope.current_event_cursor_pos_end = null;
650 $scope.current_event_target = null;
655 function loadRecord() {
656 return egCore.pcrud.retrieve(
657 $scope.record_type, $scope.recordId
658 ).then(function(rec) {
659 $scope.in_redo = true;
660 $scope[$scope.record_type] = rec;
661 $scope.record = new MARC21.Record({ marcxml : $scope[$scope.record_type].marc() });
662 $scope.calculated_record_type = $scope.record.recordType();
663 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
664 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
665 $scope.save_stack_depth = $scope.record_undo_stack.length;
667 if ($scope.record_type == 'bre') {
668 $scope.bib_source = $scope[$scope.record_type].source();
674 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
675 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
676 $scope.record_undo_stack.push({
678 target: $scope.current_event_target,
679 pos: $scope.current_event_target_cursor_pos
682 if ($scope.force_render) {
683 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
684 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
685 $scope.force_render = false;
690 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
691 console.log('should get a listener... does not');
692 $scope.dirtyFlag = true;
694 $scope.dirtyFlag = false;
697 if ($scope.record_undo_stack.length > $scope.max_undo)
698 $scope.record_undo_stack.shift();
700 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
701 $scope.in_redo = false;
702 $scope.in_undo = false;
705 $scope.processUndo = function () {
706 if ($scope.record_undo_stack.length) {
707 $scope.in_undo = true;
709 var undo_item = $scope.record_undo_stack.pop();
710 $scope.record_redo_stack.push(undo_item);
712 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
713 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
714 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
716 $scope.current_event_target = undo_item.target;
717 $scope.current_event_target_cursor_pos = undo_item.pos;
718 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
720 $timeout(function(){$scope.$digest()}).then(setCaret);
727 $scope.processRedo = function () {
728 if ($scope.record_redo_stack.length) {
729 $scope.in_redo = true;
731 var redo_item = $scope.record_redo_stack.pop();
732 $scope.record_undo_stack.push(redo_item);
734 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
735 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
736 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
738 $scope.current_event_target = redo_item.target;
739 $scope.current_event_target_cursor_pos = redo_item.pos;
740 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
742 $timeout(function(){$scope.$digest()}).then(setCaret);
749 $scope.saveRecord = function () {
751 $scope[$scope.record_type].marc($scope.record.toXmlString());
752 return egCore.pcrud.update(
753 $scope[$scope.record_type]
757 $scope.seeBreaker = function () {
758 alert($scope.record.toBreaker());
761 $scope.$watch('recordId',
762 function(newVal, oldVal) {
763 if (newVal && newVal !== oldVal) {
772 $scope.mangle_005 = function () {
773 var now = new Date();
774 var y = now.getUTCFullYear();
776 var m = now.getUTCMonth() + 1;
777 if (m < 10) m = '0' + m;
779 var d = now.getUTCDate();
780 if (d < 10) d = '0' + d;
782 var H = now.getUTCHours();
783 if (H < 10) H = '0' + H;
785 var M = now.getUTCMinutes();
786 if (M < 10) M = '0' + M;
788 var S = now.getUTCSeconds();
789 if (S < 10) S = '0' + S;
791 var stamp = '' + y + m + d + H + M + S + '.0';
792 var f = $scope.record.field('005',true)[0];
796 $scope.record.insertOrderedFields(
811 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
815 template: '<span class="nullable">'+
816 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
817 '<option value="">Select a Source</option>'+
820 controller: ['$scope','egCore',
821 function ($scope , egCore) {
823 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
824 .then(function(list) { $scope.bib_sources = list; });
826 $scope.$watch('bib_source',
827 function(newVal, oldVal) {
828 if (newVal !== oldVal) {
829 $scope.bre.source(newVal);