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 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
69 $('body').on('click.context_menu',function() {
70 $($scope.context_menu_element).css('display','none');
71 $('body').off('click.context_menu');
76 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
77 console.log('Showing context menu...');
80 '<ul class="dropdown-menu" role="menu">'+
81 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
84 var tnode = angular.element(tmpl);
85 $document.find('body').append(tnode);
93 $scope.context_menu_element = 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 if (scope.item_container && scope.item_container.length)
120 element.bind('contextmenu', scope.showContext);
125 .directive("egMarcEditSubfield", function () {
130 '<span><label class="marcedit marcsfcodedelimiter"'+
131 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
132 '>‡</label><eg-marc-edit-editable '+
134 'class="marcedit marcsf marcsfcode" '+
136 'subfield="subfield" '+
137 'content="subfield[0]" '+
139 'on-keydown="onKeydown" '+
140 'context-item-generator="sf_code_options" '+
141 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
143 '<span><eg-marc-edit-editable '+
145 'class="marcedit marcsf marcsfvalue" '+
147 'subfield="subfield" '+
148 'content="subfield[1]" '+
149 'on-keydown="onKeydown" '+
150 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
153 scope: { field: "=", subfield: "=", onKeydown: '=' },
155 controller : ['$scope', 'egTagTable',
156 function ( $scope , egTagTable) {
158 $scope.sf_code_options = function () {
159 return egTagTable.getSubfieldCodes($scope.field.tag);
166 .directive("egMarcEditInd", function () {
170 template: '<span><eg-marc-edit-editable '+
172 'class="marcedit marcind" '+
176 'on-keydown="onKeydown" '+
177 'context-item-generator="ind_val_options" '+
178 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
180 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
182 controller : ['$scope', 'egTagTable',
183 function ( $scope , egTagTable) {
185 $scope.ind_val_options = function () {
186 return egTagTable.getIndicatorValues($scope.field.tag, $scope.indNumber);
193 .directive("egMarcEditTag", function () {
197 template: '<span><eg-marc-edit-editable '+
199 'class="marcedit marctag" '+
203 'on-keydown="onKeydown" '+
204 'context-item-generator="tag_options" '+
205 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
207 scope: { tag : '=', field: '=', onKeydown: '=' },
209 controller : ['$scope', 'egTagTable',
210 function ( $scope , egTagTable) {
212 $scope.tag_options = function () {
213 return egTagTable.getFieldTags();
220 .directive("egMarcEditDatafield", function () {
225 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
226 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
227 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
228 '<span><eg-marc-edit-subfield ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
230 scope: { field: "=", onKeydown: '=' },
235 .directive("egMarcEditControlfield", function () {
240 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
241 '<span><eg-marc-edit-editable '+
244 'class="marcedit marcdata" '+
245 'content="field.data" '+
246 'on-keydown="onKeydown" '+
247 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
250 scope: { field: "=", onKeydown: '=' }
254 .directive("egMarcEditLeader", function () {
259 '<span><eg-marc-edit-editable '+
260 'class="marcedit marctag" '+
262 'on-keydown="onKeydown" '+
264 'disabled="disabled"'+
266 '<span><eg-marc-edit-editable '+
267 'class="marcedit marcdata" '+
269 'max="{{record.leader.length}}" '+
270 'content="record.leader" '+
271 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
272 'on-keydown="onKeydown"'+
275 controller : ['$scope',
276 function ( $scope ) {
280 scope: { record: "=", onKeydown: '=' }
284 /// TODO: fixed field editor and such
285 .directive("egMarcEditRecord", function () {
287 templateUrl : './cat/share/t_marcedit',
296 link: function (scope, element, attrs) {
298 element.bind('click', function(e) {;
299 scope.current_event_target = $(e.target).attr('id');
300 if (scope.current_event_target) {
301 console.log('Recording click event on ' + scope.current_event_target);
302 scope.current_event_target_cursor_pos =
303 e.target.selectionDirection=='backward' ?
304 e.target.selectionStart :
305 e.target.selectionEnd;
310 controller : ['$timeout','$scope','egCore', 'egTagTable',
311 function ( $timeout , $scope , egCore , egTagTable ) {
313 $scope.bib_source = null;
314 $scope.record_type = $scope.recordType || 'bre';
315 $scope.max_undo = $scope.maxUndo || 100;
316 $scope.record_undo_stack = [];
317 $scope.record_redo_stack = [];
318 $scope.in_undo = false;
319 $scope.in_redo = false;
320 $scope.record = new MARC21.Record();
321 $scope.save_stack_depth = 0;
322 $scope.controlfields = [];
323 $scope.datafields = [];
325 egTagTable.loadTagTable();
327 $scope.onKeydown = function (event) {
328 var event_return = true;
331 'keydown: which='+event.which+
332 ', ctrlKey='+event.ctrlKey+
333 ', shiftKey='+event.shiftKey+
334 ', altKey='+event.altKey+
335 ', metaKey='+event.altKey
338 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
339 event_return = $scope.processRedo();
340 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
341 event_return = $scope.processUndo();
342 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
344 var element = $(event.target);
345 var new_sf, index_sf, move_data;
347 if (element.hasClass('marcsfvalue')) {
348 index_sf = event.data.scope.subfield[2];
349 new_sf = index_sf + 1;
351 var start = event.target.selectionStart;
352 var end = event.target.selectionEnd - event.target.selectionStart ?
353 event.target.selectionEnd :
354 event.target.value.length;
356 move_data = event.target.value.substring(start,end);
358 } else if (element.hasClass('marcsfcode')) {
359 index_sf = event.data.scope.subfield[2];
360 new_sf = index_sf + 1;
361 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
366 $scope.current_event_target = 'r' + $scope.recordId +
367 'f' + event.data.scope.field.position +
368 's' + new_sf + 'code';
370 event.data.scope.field.subfields.forEach(function(sf) {
371 if (sf[2] >= new_sf) sf[2]++;
372 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
374 event.data.scope.field.subfields.splice(
377 [' ', move_data, new_sf ]
380 $scope.current_event_target_cursor_pos = 0;
381 $scope.current_event_target_cursor_pos_end = 1;
383 $timeout(function(){$scope.$digest()}).then(setCaret);
385 event_return = false;
387 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
388 event.data.scope.field.record.insertOrderedFields(
395 $scope.force_render = true;
396 $timeout(function(){$scope.$digest()}).then(setCaret);
398 event_return = false;
400 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
401 event.data.scope.field.record.insertOrderedFields(
408 $scope.force_render = true;
409 $timeout(function(){$scope.$digest()}).then(setCaret);
411 event_return = false;
413 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
414 var new_008_data = event.data.scope.field.record.generate008();
417 var old_008s = event.data.scope.field.record.field('008',true);
418 old_008s.forEach(function(o) {
419 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
420 domnode.scope().$destroy();
422 event.data.scope.field.record.deleteFields(o);
425 event.data.scope.field.record.insertOrderedFields(
432 $scope.force_render = true;
433 $timeout(function(){$scope.$digest()}).then(setCaret);
435 event_return = false;
437 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
439 var element = $(event.target);
441 var index_field = event.data.scope.field.position;
442 var new_field = index_field + 1;
444 event.data.scope.field.record.insertFieldsAfter(
445 event.data.scope.field,
448 subfields : [[' ','',0]]
452 $scope.current_event_target = 'r' + $scope.recordId +
453 'f' + new_field + 'tag';
455 $scope.current_event_target_cursor_pos = 0;
456 $scope.current_event_target_cursor_pos_end = 3;
457 $scope.force_render = true;
459 $timeout(function(){$scope.$digest()}).then(setCaret);
461 event_return = false;
463 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
465 var del_field = event.data.scope.field.position;
467 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
469 event.data.scope.field.record.deleteFields(
470 event.data.scope.field
473 domnode.scope().$destroy();
476 $scope.current_event_target = 'r' + $scope.recordId +
477 'f' + del_field + 'tag';
479 $scope.current_event_target_cursor_pos = 0;
480 $scope.current_event_target_cursor_pos_end = 0
481 $scope.force_render = true;
483 $timeout(function(){$scope.$digest()}).then(setCaret);
485 event_return = false;
487 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
489 var sf = event.data.scope.subfield[2] - 1;
490 if (sf == -1) sf = 0;
492 event.data.scope.field.deleteExactSubfields(
493 event.data.scope.subfield
496 if (!event.data.scope.field.subfields[sf]) {
497 $scope.current_event_target = 'r' + $scope.recordId +
498 'f' + event.data.scope.field.position +
501 $scope.current_event_target = 'r' + $scope.recordId +
502 'f' + event.data.scope.field.position +
506 $scope.current_event_target_cursor_pos = 0;
507 $scope.current_event_target_cursor_pos_end = 0;
508 $scope.force_render = true;
510 $timeout(function(){$scope.$digest()}).then(setCaret);
512 event_return = false;
514 } else if (event.keyCode == 38) {
515 if (event.ctrlKey) { // copy the field up
516 var index_field = event.data.scope.field.position;
519 if (event.data.scope.field.isControlfield()) {
520 field_obj = new MARC21.Field({
521 tag : event.data.scope.field.tag,
522 data : event.data.scope.field.data
526 for (var i in event.data.scope.field.subfields) {
527 sf_clone.push(event.data.scope.field.subfields[i].slice());
529 field_obj = new MARC21.Field({
530 tag : event.data.scope.field.tag,
531 ind1 : event.data.scope.field.ind1,
532 ind2 : event.data.scope.field.ind2,
538 event.data.scope.field.record.insertFieldsBefore(
539 event.data.scope.field,
543 $scope.current_event_target = 'r' + $scope.recordId +
544 'f' + index_field + 'tag';
546 $scope.current_event_target_cursor_pos = 0;
547 $scope.current_event_target_cursor_pos_end = 3;
548 $scope.force_render = true;
550 $timeout(function(){$scope.$digest()}).then(setCaret);
552 } else { // jump to prev field
553 if (event.data.scope.field.position > 0) {
555 $scope.current_event_target_cursor_pos = 0;
556 $scope.current_event_target_cursor_pos_end = 0;
557 $scope.current_event_target = 'r' + $scope.recordId +
558 'f' + (event.data.scope.field.position - 1) +
564 event_return = false;
566 } else if (event.keyCode == 40) { // down arrow...
567 if (event.ctrlKey) { // copy the field down
569 var index_field = event.data.scope.field.position;
570 var new_field = index_field + 1;
573 if (event.data.scope.field.isControlfield()) {
574 field_obj = new MARC21.Field({
575 tag : event.data.scope.field.tag,
576 data : event.data.scope.field.data
580 for (var i in event.data.scope.field.subfields) {
581 sf_clone.push(event.data.scope.field.subfields[i].slice());
583 field_obj = new MARC21.Field({
584 tag : event.data.scope.field.tag,
585 ind1 : event.data.scope.field.ind1,
586 ind2 : event.data.scope.field.ind2,
591 event.data.scope.field.record.insertFieldsAfter(
592 event.data.scope.field,
596 $scope.current_event_target = 'r' + $scope.recordId +
597 'f' + new_field + 'tag';
599 $scope.current_event_target_cursor_pos = 0;
600 $scope.current_event_target_cursor_pos_end = 3;
601 $scope.force_render = true;
603 $timeout(function(){$scope.$digest()}).then(setCaret);
605 } else { // jump to next field
606 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
608 $scope.current_event_target_cursor_pos = 0;
609 $scope.current_event_target_cursor_pos_end = 0;
610 $scope.current_event_target = 'r' + $scope.recordId +
611 'f' + (event.data.scope.field.position + 1) +
617 event_return = false;
619 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
620 $scope.current_event_target = $(event.target).attr('id');
621 if ($scope.current_event_target) {
622 $scope.current_event_target_cursor_pos =
623 event.target.selectionDirection=='backward' ?
624 event.target.selectionStart :
625 event.target.selectionEnd;
632 function setCaret() {
633 if ($scope.current_event_target) {
634 console.log("Putting caret in " + $scope.current_event_target);
635 if (!$scope.current_event_target_cursor_pos_end)
636 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
638 var element = $('#'+$scope.current_event_target).get(0);
641 if (element.setSelectionRange) {
642 element.setSelectionRange(
643 $scope.current_event_target_cursor_pos,
644 $scope.current_event_target_cursor_pos_end
647 $scope.current_event_cursor_pos_end = null;
648 $scope.current_event_target = null;
653 function loadRecord() {
654 return egCore.pcrud.retrieve(
655 $scope.record_type, $scope.recordId
656 ).then(function(rec) {
657 $scope.in_redo = true;
658 $scope[$scope.record_type] = rec;
659 $scope.record = new MARC21.Record({ marcxml : $scope[$scope.record_type].marc() });
660 $scope.calculated_record_type = $scope.record.recordType();
661 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
662 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
663 $scope.save_stack_depth = $scope.record_undo_stack.length;
665 if ($scope.record_type == 'bre') {
666 $scope.bib_source = $scope[$scope.record_type].source();
672 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
673 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
674 $scope.record_undo_stack.push({
676 target: $scope.current_event_target,
677 pos: $scope.current_event_target_cursor_pos
680 if ($scope.force_render) {
681 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
682 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
683 $scope.force_render = false;
688 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
689 console.log('should get a listener... does not');
690 $scope.dirtyFlag = true;
692 $scope.dirtyFlag = false;
695 if ($scope.record_undo_stack.length > $scope.max_undo)
696 $scope.record_undo_stack.shift();
698 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
699 $scope.in_redo = false;
700 $scope.in_undo = false;
703 $scope.processUndo = function () {
704 if ($scope.record_undo_stack.length) {
705 $scope.in_undo = true;
707 var undo_item = $scope.record_undo_stack.pop();
708 $scope.record_redo_stack.push(undo_item);
710 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
711 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
712 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
714 $scope.current_event_target = undo_item.target;
715 $scope.current_event_target_cursor_pos = undo_item.pos;
716 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
718 $timeout(function(){$scope.$digest()}).then(setCaret);
725 $scope.processRedo = function () {
726 if ($scope.record_redo_stack.length) {
727 $scope.in_redo = true;
729 var redo_item = $scope.record_redo_stack.pop();
730 $scope.record_undo_stack.push(redo_item);
732 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
733 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
734 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
736 $scope.current_event_target = redo_item.target;
737 $scope.current_event_target_cursor_pos = redo_item.pos;
738 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
740 $timeout(function(){$scope.$digest()}).then(setCaret);
747 $scope.saveRecord = function () {
749 $scope[$scope.record_type].marc($scope.record.toXmlString());
750 return egCore.pcrud.update(
751 $scope[$scope.record_type]
755 $scope.seeBreaker = function () {
756 alert($scope.record.toBreaker());
759 $scope.$watch('recordId',
760 function(newVal, oldVal) {
761 if (newVal && newVal !== oldVal) {
770 $scope.mangle_005 = function () {
771 var now = new Date();
772 var y = now.getUTCFullYear();
774 var m = now.getUTCMonth() + 1;
775 if (m < 10) m = '0' + m;
777 var d = now.getUTCDate();
778 if (d < 10) d = '0' + d;
780 var H = now.getUTCHours();
781 if (H < 10) H = '0' + H;
783 var M = now.getUTCMinutes();
784 if (M < 10) M = '0' + M;
786 var S = now.getUTCSeconds();
787 if (S < 10) S = '0' + S;
789 var stamp = '' + y + m + d + H + M + S + '.0';
790 var f = $scope.record.field('005',true)[0];
794 $scope.record.insertOrderedFields(
809 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
813 template: '<span class="nullable">'+
814 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
815 '<option value="">Select a Source</option>'+
818 controller: ['$scope','egCore',
819 function ($scope , egCore) {
821 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
822 .then(function(list) { $scope.bib_sources = list; });
824 $scope.$watch('bib_source',
825 function(newVal, oldVal) {
826 if (newVal !== oldVal) {
827 $scope.bre.source(newVal);