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 /* XXX Example, for testing. We'll get this from the tag-table services for realz
62 if (!$scope.contextItemContainer) {
63 $scope.contextItemContainer = "default_context";
64 $scope.$parent[$scope.contextItemContainer] = [
74 if ($scope.contextItemContainer && angular.isArray($scope.$parent[$scope.contextItemContainer]))
75 $scope.item_container = $scope.$parent[$scope.contextItemContainer];
76 else if ($scope.contextItemGenerator)
77 $scope.item_container = $scope.contextItemGenerator();
79 $scope.showContext = function (event) {
80 if ($scope.context_menu_element) {
81 console.log('Reshowing context menu...');
82 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
83 $('body').on('click.context_menu',function() {
84 $($scope.context_menu_element).css('display','none');
85 $('body').off('click.context_menu');
90 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
91 console.log('Showing context menu...');
94 '<ul class="dropdown-menu" role="menu">'+
95 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
98 var tnode = angular.element(tmpl);
99 $document.find('body').append(tnode);
107 $scope.context_menu_element = tnode;
109 $timeout(function() {
110 var e = $compile(tnode)($scope);
114 $('body').on('click.context_menu',function() {
115 $(tnode).css('display','none');
116 $('body').off('click.context_menu');
127 link: function (scope, element, attrs) {
129 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
131 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
133 if (scope.item_container)
134 element.bind('contextmenu', scope.showContext);
139 .directive("egMarcEditSubfield", function () {
144 '<span><label class="marcedit marcsfcodedelimiter"'+
145 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
146 '>‡</label><eg-marc-edit-editable '+
148 'class="marcedit marcsf marcsfcode" '+
150 'subfield="subfield" '+
151 'content="subfield[0]" '+
153 'on-keydown="onKeydown" '+
154 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
156 '<span><eg-marc-edit-editable '+
158 'class="marcedit marcsf marcsfvalue" '+
160 'subfield="subfield" '+
161 'content="subfield[1]" '+
162 'on-keydown="onKeydown" '+
163 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
166 scope: { field: "=", subfield: "=", onKeydown: '=' },
171 .directive("egMarcEditInd", function () {
175 template: '<span><eg-marc-edit-editable '+
177 'class="marcedit marcind" '+
181 'on-keydown="onKeydown" '+
182 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
184 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
189 .directive("egMarcEditTag", function () {
193 template: '<span><eg-marc-edit-editable '+
195 'class="marcedit marctag" '+
199 'on-keydown="onKeydown" '+
200 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
202 scope: { tag : '=', field: '=', onKeydown: '=' },
207 .directive("egMarcEditDatafield", function () {
212 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
213 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
214 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
215 '<span><eg-marc-edit-subfield ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
217 scope: { field: "=", onKeydown: '=' },
222 .directive("egMarcEditControlfield", function () {
227 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
228 '<span><eg-marc-edit-editable '+
231 'class="marcedit marcdata" '+
232 'content="field.data" '+
233 'on-keydown="onKeydown" '+
234 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
237 scope: { field: "=", onKeydown: '=' }
241 .directive("egMarcEditLeader", function () {
246 '<span><eg-marc-edit-editable '+
247 'class="marcedit marctag" '+
249 'on-keydown="onKeydown" '+
251 'disabled="disabled"'+
253 '<span><eg-marc-edit-editable '+
254 'class="marcedit marcdata" '+
256 'max="{{record.leader.length}}" '+
257 'content="record.leader" '+
258 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
259 'on-keydown="onKeydown"'+
262 controller : ['$scope',
263 function ( $scope ) {
267 scope: { record: "=", onKeydown: '=' }
271 /// TODO: fixed field editor and such
272 .directive("egMarcEditRecord", function () {
274 templateUrl : './cat/share/t_marcedit',
283 link: function (scope, element, attrs) {
285 element.bind('click', function(e) {;
286 scope.current_event_target = $(e.target).attr('id');
287 if (scope.current_event_target) {
288 console.log('Recording click event on ' + scope.current_event_target);
289 scope.current_event_target_cursor_pos =
290 e.target.selectionDirection=='backward' ?
291 e.target.selectionStart :
292 e.target.selectionEnd;
297 controller : ['$timeout','$scope','egCore',
298 function ( $timeout , $scope , egCore ) {
300 $scope.bib_source = null;
301 $scope.record_type = $scope.recordType || 'bre';
302 $scope.max_undo = $scope.maxUndo || 100;
303 $scope.record_undo_stack = [];
304 $scope.record_redo_stack = [];
305 $scope.in_undo = false;
306 $scope.in_redo = false;
307 $scope.record = new MARC21.Record();
308 $scope.save_stack_depth = 0;
309 $scope.controlfields = [];
310 $scope.datafields = [];
312 $scope.onKeydown = function (event) {
313 var event_return = true;
316 'keydown: which='+event.which+
317 ', ctrlKey='+event.ctrlKey+
318 ', shiftKey='+event.shiftKey+
319 ', altKey='+event.altKey+
320 ', metaKey='+event.altKey
323 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
324 event_return = $scope.processRedo();
325 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
326 event_return = $scope.processUndo();
327 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
329 var element = $(event.target);
330 var new_sf, index_sf, move_data;
332 if (element.hasClass('marcsfvalue')) {
333 index_sf = event.data.scope.subfield[2];
334 new_sf = index_sf + 1;
336 var start = event.target.selectionStart;
337 var end = event.target.selectionEnd - event.target.selectionStart ?
338 event.target.selectionEnd :
339 event.target.value.length;
341 move_data = event.target.value.substring(start,end);
343 } else if (element.hasClass('marcsfcode')) {
344 index_sf = event.data.scope.subfield[2];
345 new_sf = index_sf + 1;
346 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
351 $scope.current_event_target = 'r' + $scope.recordId +
352 'f' + event.data.scope.field.position +
353 's' + new_sf + 'code';
355 event.data.scope.field.subfields.forEach(function(sf) {
356 if (sf[2] >= new_sf) sf[2]++;
357 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
359 event.data.scope.field.subfields.splice(
362 [' ', move_data, new_sf ]
365 $scope.current_event_target_cursor_pos = 0;
366 $scope.current_event_target_cursor_pos_end = 1;
368 $timeout(function(){$scope.$digest()}).then(setCaret);
370 event_return = false;
372 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
373 event.data.scope.field.record.insertOrderedFields(
380 $scope.force_render = true;
381 $timeout(function(){$scope.$digest()}).then(setCaret);
383 event_return = false;
385 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
386 event.data.scope.field.record.insertOrderedFields(
393 $scope.force_render = true;
394 $timeout(function(){$scope.$digest()}).then(setCaret);
396 event_return = false;
398 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
399 var new_008_data = event.data.scope.field.record.generate008();
402 var old_008s = event.data.scope.field.record.field('008',true);
403 old_008s.forEach(function(o) {
404 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
405 domnode.scope().$destroy();
407 event.data.scope.field.record.deleteFields(o);
410 event.data.scope.field.record.insertOrderedFields(
417 $scope.force_render = true;
418 $timeout(function(){$scope.$digest()}).then(setCaret);
420 event_return = false;
422 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
424 var element = $(event.target);
426 var index_field = event.data.scope.field.position;
427 var new_field = index_field + 1;
429 event.data.scope.field.record.insertFieldsAfter(
430 event.data.scope.field,
433 subfields : [[' ','',0]]
437 $scope.current_event_target = 'r' + $scope.recordId +
438 'f' + new_field + 'tag';
440 $scope.current_event_target_cursor_pos = 0;
441 $scope.current_event_target_cursor_pos_end = 3;
442 $scope.force_render = true;
444 $timeout(function(){$scope.$digest()}).then(setCaret);
446 event_return = false;
448 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
450 var del_field = event.data.scope.field.position;
452 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
454 event.data.scope.field.record.deleteFields(
455 event.data.scope.field
458 domnode.scope().$destroy();
461 $scope.current_event_target = 'r' + $scope.recordId +
462 'f' + del_field + 'tag';
464 $scope.current_event_target_cursor_pos = 0;
465 $scope.current_event_target_cursor_pos_end = 0
466 $scope.force_render = true;
468 $timeout(function(){$scope.$digest()}).then(setCaret);
470 event_return = false;
472 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
474 var sf = event.data.scope.subfield[2] - 1;
475 if (sf == -1) sf = 0;
477 event.data.scope.field.deleteExactSubfields(
478 event.data.scope.subfield
481 if (!event.data.scope.field.subfields[sf]) {
482 $scope.current_event_target = 'r' + $scope.recordId +
483 'f' + event.data.scope.field.position +
486 $scope.current_event_target = 'r' + $scope.recordId +
487 'f' + event.data.scope.field.position +
491 $scope.current_event_target_cursor_pos = 0;
492 $scope.current_event_target_cursor_pos_end = 0;
493 $scope.force_render = true;
495 $timeout(function(){$scope.$digest()}).then(setCaret);
497 event_return = false;
499 } else if (event.keyCode == 38) {
500 if (event.ctrlKey) { // copy the field up
501 var index_field = event.data.scope.field.position;
504 if (event.data.scope.field.isControlfield()) {
505 field_obj = new MARC21.Field({
506 tag : event.data.scope.field.tag,
507 data : event.data.scope.field.data
511 for (var i in event.data.scope.field.subfields) {
512 sf_clone.push(event.data.scope.field.subfields[i].slice());
514 field_obj = new MARC21.Field({
515 tag : event.data.scope.field.tag,
516 ind1 : event.data.scope.field.ind1,
517 ind2 : event.data.scope.field.ind2,
523 event.data.scope.field.record.insertFieldsBefore(
524 event.data.scope.field,
528 $scope.current_event_target = 'r' + $scope.recordId +
529 'f' + index_field + 'tag';
531 $scope.current_event_target_cursor_pos = 0;
532 $scope.current_event_target_cursor_pos_end = 3;
533 $scope.force_render = true;
535 $timeout(function(){$scope.$digest()}).then(setCaret);
537 } else { // jump to prev field
538 if (event.data.scope.field.position > 0) {
540 $scope.current_event_target_cursor_pos = 0;
541 $scope.current_event_target_cursor_pos_end = 0;
542 $scope.current_event_target = 'r' + $scope.recordId +
543 'f' + (event.data.scope.field.position - 1) +
549 event_return = false;
551 } else if (event.keyCode == 40) { // down arrow...
552 if (event.ctrlKey) { // copy the field down
554 var index_field = event.data.scope.field.position;
555 var new_field = index_field + 1;
558 if (event.data.scope.field.isControlfield()) {
559 field_obj = new MARC21.Field({
560 tag : event.data.scope.field.tag,
561 data : event.data.scope.field.data
565 for (var i in event.data.scope.field.subfields) {
566 sf_clone.push(event.data.scope.field.subfields[i].slice());
568 field_obj = new MARC21.Field({
569 tag : event.data.scope.field.tag,
570 ind1 : event.data.scope.field.ind1,
571 ind2 : event.data.scope.field.ind2,
576 event.data.scope.field.record.insertFieldsAfter(
577 event.data.scope.field,
581 $scope.current_event_target = 'r' + $scope.recordId +
582 'f' + new_field + 'tag';
584 $scope.current_event_target_cursor_pos = 0;
585 $scope.current_event_target_cursor_pos_end = 3;
586 $scope.force_render = true;
588 $timeout(function(){$scope.$digest()}).then(setCaret);
590 } else { // jump to next field
591 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
593 $scope.current_event_target_cursor_pos = 0;
594 $scope.current_event_target_cursor_pos_end = 0;
595 $scope.current_event_target = 'r' + $scope.recordId +
596 'f' + (event.data.scope.field.position + 1) +
602 event_return = false;
604 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
605 $scope.current_event_target = $(event.target).attr('id');
606 if ($scope.current_event_target) {
607 $scope.current_event_target_cursor_pos =
608 event.target.selectionDirection=='backward' ?
609 event.target.selectionStart :
610 event.target.selectionEnd;
617 function setCaret() {
618 if ($scope.current_event_target) {
619 console.log("Putting caret in " + $scope.current_event_target);
620 if (!$scope.current_event_target_cursor_pos_end)
621 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
623 var element = $('#'+$scope.current_event_target).get(0);
626 if (element.setSelectionRange) {
627 element.setSelectionRange(
628 $scope.current_event_target_cursor_pos,
629 $scope.current_event_target_cursor_pos_end
632 $scope.current_event_cursor_pos_end = null;
633 $scope.current_event_target = null;
638 function loadRecord() {
639 return egCore.pcrud.retrieve(
640 $scope.record_type, $scope.recordId
641 ).then(function(rec) {
642 $scope.in_redo = true;
643 $scope[$scope.record_type] = rec;
644 $scope.record = new MARC21.Record({ marcxml : $scope[$scope.record_type].marc() });
645 $scope.calculated_record_type = $scope.record.recordType();
646 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
647 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
648 $scope.save_stack_depth = $scope.record_undo_stack.length;
650 if ($scope.record_type == 'bre') {
651 $scope.bib_source = $scope[$scope.record_type].source();
657 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
658 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
659 $scope.record_undo_stack.push({
661 target: $scope.current_event_target,
662 pos: $scope.current_event_target_cursor_pos
665 if ($scope.force_render) {
666 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
667 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
668 $scope.force_render = false;
673 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
674 console.log('should get a listener... does not');
675 $scope.dirtyFlag = true;
677 $scope.dirtyFlag = false;
680 if ($scope.record_undo_stack.length > $scope.max_undo)
681 $scope.record_undo_stack.shift();
683 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
684 $scope.in_redo = false;
685 $scope.in_undo = false;
688 $scope.processUndo = function () {
689 if ($scope.record_undo_stack.length) {
690 $scope.in_undo = true;
692 var undo_item = $scope.record_undo_stack.pop();
693 $scope.record_redo_stack.push(undo_item);
695 $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
696 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
697 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
699 $scope.current_event_target = undo_item.target;
700 $scope.current_event_target_cursor_pos = undo_item.pos;
701 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
703 $timeout(function(){$scope.$digest()}).then(setCaret);
710 $scope.processRedo = function () {
711 if ($scope.record_redo_stack.length) {
712 $scope.in_redo = true;
714 var redo_item = $scope.record_redo_stack.pop();
715 $scope.record_undo_stack.push(redo_item);
717 $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
718 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
719 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
721 $scope.current_event_target = redo_item.target;
722 $scope.current_event_target_cursor_pos = redo_item.pos;
723 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
725 $timeout(function(){$scope.$digest()}).then(setCaret);
732 $scope.saveRecord = function () {
734 $scope[$scope.record_type].marc($scope.record.toXmlString());
735 return egCore.pcrud.update(
736 $scope[$scope.record_type]
740 $scope.seeBreaker = function () {
741 alert($scope.record.toBreaker());
744 $scope.$watch('recordId',
745 function(newVal, oldVal) {
746 if (newVal && newVal !== oldVal) {
755 $scope.mangle_005 = function () {
756 var now = new Date();
757 var y = now.getUTCFullYear();
759 var m = now.getUTCMonth() + 1;
760 if (m < 10) m = '0' + m;
762 var d = now.getUTCDate();
763 if (d < 10) d = '0' + d;
765 var H = now.getUTCHours();
766 if (H < 10) H = '0' + H;
768 var M = now.getUTCMinutes();
769 if (M < 10) M = '0' + M;
771 var S = now.getUTCSeconds();
772 if (S < 10) S = '0' + S;
774 var stamp = '' + y + m + d + H + M + S + '.0';
775 var f = $scope.record.field('005',true)[0];
779 $scope.record.insertOrderedFields(
794 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
798 template: '<span class="nullable">'+
799 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
800 '<option value="">Select a Source</option>'+
803 controller: ['$scope','egCore',
804 function ($scope , egCore) {
806 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
807 .then(function(list) { $scope.bib_sources = list; });
809 $scope.$watch('bib_source',
810 function(newVal, oldVal) {
811 if (newVal !== oldVal) {
812 $scope.bre.source(newVal);