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: '@',
56 controller : ['$scope',
59 /* XXX Example, for testing. We'll get this from the tag-table services for realz
61 if (!$scope.contextItemContainer) {
62 $scope.contextItemContainer = "default_context";
63 $scope[$scope.contextItemContainer] = [
73 if ($scope.contextItemContainer)
74 $scope.item_container = $scope[$scope.contextItemContainer];
76 $scope.showContext = function (event) {
77 if ($scope.context_menu_element) {
78 console.log('Reshowing context menu...');
79 $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
80 $('body').on('click.context_menu',function() {
81 $($scope.context_menu_element).css('display','none');
82 $('body').off('click.context_menu');
87 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
88 console.log('Showing context menu...');
91 '<ul class="dropdown-menu" role="menu">'+
92 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
95 var tnode = angular.element(tmpl);
96 $document.find('body').append(tnode);
104 $scope.context_menu_element = tnode;
106 $timeout(function() {
107 var e = $compile(tnode)($scope);
111 $('body').on('click.context_menu',function() {
112 $(tnode).css('display','none');
113 $('body').off('click.context_menu');
124 link: function (scope, element, attrs) {
126 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
128 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
130 if (scope.contextItemContainer && angular.isArray(scope[scope.contextItemContainer]))
131 element.bind('contextmenu', scope.showContext);
136 .directive("egMarcEditSubfield", function () {
141 '<span><label class="marcedit marcsfcodedelimiter"'+
142 'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
143 '>‡</label><eg-marc-edit-editable '+
145 'class="marcedit marcsf marcsfcode" '+
147 'subfield="subfield" '+
148 'content="subfield[0]" '+
150 'on-keydown="onKeydown" '+
151 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
153 '<span><eg-marc-edit-editable '+
155 'class="marcedit marcsf marcsfvalue" '+
157 'subfield="subfield" '+
158 'content="subfield[1]" '+
159 'on-keydown="onKeydown" '+
160 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
163 scope: { field: "=", subfield: "=", onKeydown: '=' },
168 .directive("egMarcEditInd", function () {
172 template: '<span><eg-marc-edit-editable '+
174 'class="marcedit marcind" '+
178 'on-keydown="onKeydown" '+
179 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
181 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
186 .directive("egMarcEditTag", function () {
190 template: '<span><eg-marc-edit-editable '+
192 'class="marcedit marctag" '+
196 'on-keydown="onKeydown" '+
197 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
199 scope: { tag : '=', field: '=', onKeydown: '=' },
204 .directive("egMarcEditDatafield", function () {
209 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
210 '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
211 '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
212 '<span><eg-marc-edit-subfield ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
214 scope: { field: "=", onKeydown: '=' }
218 .directive("egMarcEditControlfield", function () {
223 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
224 '<span><eg-marc-edit-editable '+
227 'class="marcedit marcdata" '+
228 'content="field.data" '+
229 'on-keydown="onKeydown" '+
230 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
233 scope: { field: "=", onKeydown: '=' }
237 .directive("egMarcEditLeader", function () {
242 '<span><eg-marc-edit-editable '+
243 'class="marcedit marctag" '+
245 'on-keydown="onKeydown" '+
247 'disabled="disabled"'+
249 '<span><eg-marc-edit-editable '+
250 'class="marcedit marcdata" '+
252 'max="{{record.leader.length}}" '+
253 'content="record.leader" '+
254 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
255 'on-keydown="onKeydown"'+
258 controller : ['$scope',
259 function ( $scope ) {
263 scope: { record: "=", onKeydown: '=' }
267 /// TODO: fixed field editor and such
268 .directive("egMarcEditRecord", function () {
270 templateUrl : './cat/share/t_marcedit',
273 scope: { recordId : '=', recordType : '@', maxUndo : '@' },
274 link: function (scope, element, attrs) {
276 element.bind('click', function(e) {;
277 scope.current_event_target = $(e.target).attr('id');
278 if (scope.current_event_target) {
279 console.log('Recording click event on ' + scope.current_event_target);
280 scope.current_event_target_cursor_pos =
281 e.target.selectionDirection=='backward' ?
282 e.target.selectionStart :
283 e.target.selectionEnd;
288 controller : ['$timeout','$scope','egCore',
289 function ( $timeout , $scope , egCore ) {
291 $scope.bib_source = null;
292 $scope.record_type = $scope.recordType || 'bre';
293 $scope.max_undo = $scope.maxUndo || 100;
294 $scope.record_undo_stack = [];
295 $scope.record_redo_stack = [];
296 $scope.in_undo = false;
297 $scope.in_redo = false;
298 $scope.record = new MARC.Record();
299 $scope.save_stack_depth = 0;
300 $scope.controlfields = [];
301 $scope.datafields = [];
303 $scope.onKeydown = function (event) {
304 var event_return = true;
307 'keydown: which='+event.which+
308 ', ctrlKey='+event.ctrlKey+
309 ', shiftKey='+event.shiftKey+
310 ', altKey='+event.altKey+
311 ', metaKey='+event.altKey
314 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
315 event_return = $scope.processRedo();
316 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
317 event_return = $scope.processUndo();
318 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
320 var element = $(event.target);
321 var new_sf, index_sf, move_data;
323 if (element.hasClass('marcsfvalue')) {
324 index_sf = event.data.scope.subfield[2];
325 new_sf = index_sf + 1;
327 var start = event.target.selectionStart;
328 var end = event.target.selectionEnd - event.target.selectionStart ?
329 event.target.selectionEnd :
330 event.target.value.length;
332 move_data = event.target.value.substring(start,end);
334 } else if (element.hasClass('marcsfcode')) {
335 index_sf = event.data.scope.subfield[2];
336 new_sf = index_sf + 1;
337 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
342 $scope.current_event_target = 'r' + $scope.recordId +
343 'f' + event.data.scope.field.position +
344 's' + new_sf + 'code';
346 event.data.scope.field.subfields.forEach(function(sf) {
347 if (sf[2] >= new_sf) sf[2]++;
348 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
350 event.data.scope.field.subfields.splice(
353 [' ', move_data, new_sf ]
356 $scope.current_event_target_cursor_pos = 0;
357 $scope.current_event_target_cursor_pos_end = 1;
359 $timeout(function(){$scope.$digest()}).then(setCaret);
361 event_return = false;
363 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
364 event.data.scope.field.record.insertOrderedFields(
371 $scope.force_render = true;
372 $timeout(function(){$scope.$digest()}).then(setCaret);
374 event_return = false;
376 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
377 event.data.scope.field.record.insertOrderedFields(
384 $scope.force_render = true;
385 $timeout(function(){$scope.$digest()}).then(setCaret);
387 event_return = false;
389 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
390 var new_008_data = event.data.scope.field.record.generate008();
393 var old_008s = event.data.scope.field.record.field('008',true);
394 old_008s.forEach(function(o) {
395 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
396 domnode.scope().$destroy();
398 event.data.scope.field.record.deleteFields(o);
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 == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
415 var element = $(event.target);
417 var index_field = event.data.scope.field.position;
418 var new_field = index_field + 1;
420 event.data.scope.field.record.insertFieldsAfter(
421 event.data.scope.field,
424 subfields : [[' ','',0]]
428 $scope.current_event_target = 'r' + $scope.recordId +
429 'f' + new_field + 'tag';
431 $scope.current_event_target_cursor_pos = 0;
432 $scope.current_event_target_cursor_pos_end = 3;
433 $scope.force_render = true;
435 $timeout(function(){$scope.$digest()}).then(setCaret);
437 event_return = false;
439 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
441 var del_field = event.data.scope.field.position;
443 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
445 event.data.scope.field.record.deleteFields(
446 event.data.scope.field
449 domnode.scope().$destroy();
452 $scope.current_event_target = 'r' + $scope.recordId +
453 'f' + del_field + 'tag';
455 $scope.current_event_target_cursor_pos = 0;
456 $scope.current_event_target_cursor_pos_end = 0
457 $scope.force_render = true;
459 $timeout(function(){$scope.$digest()}).then(setCaret);
461 event_return = false;
463 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
465 var sf = event.data.scope.subfield[2] - 1;
466 if (sf == -1) sf = 0;
468 event.data.scope.field.deleteExactSubfields(
469 event.data.scope.subfield
472 if (!event.data.scope.field.subfields[sf]) {
473 $scope.current_event_target = 'r' + $scope.recordId +
474 'f' + event.data.scope.field.position +
477 $scope.current_event_target = 'r' + $scope.recordId +
478 'f' + event.data.scope.field.position +
482 $scope.current_event_target_cursor_pos = 0;
483 $scope.current_event_target_cursor_pos_end = 0;
484 $scope.force_render = true;
486 $timeout(function(){$scope.$digest()}).then(setCaret);
488 event_return = false;
490 } else if (event.keyCode == 38) {
491 if (event.ctrlKey) { // copy the field up
492 var index_field = event.data.scope.field.position;
495 if (event.data.scope.field.isControlfield()) {
496 field_obj = new MARC.Field({
497 tag : event.data.scope.field.tag,
498 data : event.data.scope.field.data
502 for (var i in event.data.scope.field.subfields) {
503 sf_clone.push(event.data.scope.field.subfields[i].slice());
505 field_obj = new MARC.Field({
506 tag : event.data.scope.field.tag,
507 ind1 : event.data.scope.field.ind1,
508 ind2 : event.data.scope.field.ind2,
514 event.data.scope.field.record.insertFieldsBefore(
515 event.data.scope.field,
519 $scope.current_event_target = 'r' + $scope.recordId +
520 'f' + index_field + 'tag';
522 $scope.current_event_target_cursor_pos = 0;
523 $scope.current_event_target_cursor_pos_end = 3;
524 $scope.force_render = true;
526 $timeout(function(){$scope.$digest()}).then(setCaret);
528 } else { // jump to prev field
529 if (event.data.scope.field.position > 0) {
531 $scope.current_event_target_cursor_pos = 0;
532 $scope.current_event_target_cursor_pos_end = 0;
533 $scope.current_event_target = 'r' + $scope.recordId +
534 'f' + (event.data.scope.field.position - 1) +
540 event_return = false;
542 } else if (event.keyCode == 40) { // down arrow...
543 if (event.ctrlKey) { // copy the field down
545 var index_field = event.data.scope.field.position;
546 var new_field = index_field + 1;
549 if (event.data.scope.field.isControlfield()) {
550 field_obj = new MARC.Field({
551 tag : event.data.scope.field.tag,
552 data : event.data.scope.field.data
556 for (var i in event.data.scope.field.subfields) {
557 sf_clone.push(event.data.scope.field.subfields[i].slice());
559 field_obj = new MARC.Field({
560 tag : event.data.scope.field.tag,
561 ind1 : event.data.scope.field.ind1,
562 ind2 : event.data.scope.field.ind2,
567 event.data.scope.field.record.insertFieldsAfter(
568 event.data.scope.field,
572 $scope.current_event_target = 'r' + $scope.recordId +
573 'f' + new_field + 'tag';
575 $scope.current_event_target_cursor_pos = 0;
576 $scope.current_event_target_cursor_pos_end = 3;
577 $scope.force_render = true;
579 $timeout(function(){$scope.$digest()}).then(setCaret);
581 } else { // jump to next field
582 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
584 $scope.current_event_target_cursor_pos = 0;
585 $scope.current_event_target_cursor_pos_end = 0;
586 $scope.current_event_target = 'r' + $scope.recordId +
587 'f' + (event.data.scope.field.position + 1) +
593 event_return = false;
595 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
596 $scope.current_event_target = $(event.target).attr('id');
597 if ($scope.current_event_target) {
598 $scope.current_event_target_cursor_pos =
599 event.target.selectionDirection=='backward' ?
600 event.target.selectionStart :
601 event.target.selectionEnd;
608 function setCaret() {
609 if ($scope.current_event_target) {
610 console.log("Putting caret in " + $scope.current_event_target);
611 if (!$scope.current_event_target_cursor_pos_end)
612 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
614 var element = $('#'+$scope.current_event_target).get(0);
617 if (element.setSelectionRange) {
618 element.setSelectionRange(
619 $scope.current_event_target_cursor_pos,
620 $scope.current_event_target_cursor_pos_end
623 $scope.current_event_cursor_pos_end = null;
624 $scope.current_event_target = null;
629 function loadRecord() {
630 return egCore.pcrud.retrieve(
631 $scope.record_type, $scope.recordId
632 ).then(function(rec) {
633 $scope.in_redo = true;
634 $scope[$scope.record_type] = rec;
635 $scope.record = new MARC.Record({ marcxml : $scope[$scope.record_type].marc() });
636 $scope.calculated_record_type = $scope.record.recordType();
637 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
638 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
639 $scope.save_stack_depth = $scope.record_undo_stack.length;
641 if ($scope.record_type == 'bre') {
642 $scope.bib_source = $scope[$scope.record_type].source();
648 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
649 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
650 $scope.record_undo_stack.push({
652 target: $scope.current_event_target,
653 pos: $scope.current_event_target_cursor_pos
656 if ($scope.force_render) {
657 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
658 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
659 $scope.force_render = false;
662 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
663 console.log('should get a listener... does not');
664 $('body').on('beforeunload', function(){
665 return 'There is unsaved data in this record.'
668 $('body').off('beforeunload');
672 if ($scope.record_undo_stack.length > $scope.max_undo)
673 $scope.record_undo_stack.shift();
675 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
676 $scope.in_redo = false;
677 $scope.in_undo = false;
680 $scope.processUndo = function () {
681 if ($scope.record_undo_stack.length) {
682 $scope.in_undo = true;
684 var undo_item = $scope.record_undo_stack.pop();
685 $scope.record_redo_stack.push(undo_item);
687 $scope.record = new MARC.Record({ marcbreaker : undo_item.breaker });
688 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
689 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
691 $scope.current_event_target = undo_item.target;
692 $scope.current_event_target_cursor_pos = undo_item.pos;
693 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
695 $timeout(function(){$scope.$digest()}).then(setCaret);
702 $scope.processRedo = function () {
703 if ($scope.record_redo_stack.length) {
704 $scope.in_redo = true;
706 var redo_item = $scope.record_redo_stack.pop();
707 $scope.record_undo_stack.push(redo_item);
709 $scope.record = new MARC.Record({ marcbreaker : redo_item.breaker });
710 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
711 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
713 $scope.current_event_target = redo_item.target;
714 $scope.current_event_target_cursor_pos = redo_item.pos;
715 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
717 $timeout(function(){$scope.$digest()}).then(setCaret);
724 $scope.saveRecord = function () {
726 $scope[$scope.record_type].marc($scope.record.toXmlString());
727 return egCore.pcrud.update(
728 $scope[$scope.record_type]
732 $scope.seeBreaker = function () {
733 alert($scope.record.toBreaker());
736 $scope.$watch('recordId',
737 function(newVal, oldVal) {
738 if (newVal && newVal !== oldVal) {
747 $scope.mangle_005 = function () {
748 var now = new Date();
749 var y = now.getUTCFullYear();
751 var m = now.getUTCMonth() + 1;
752 if (m < 10) m = '0' + m;
754 var d = now.getUTCDate();
755 if (d < 10) d = '0' + d;
757 var H = now.getUTCHours();
758 if (H < 10) H = '0' + H;
760 var M = now.getUTCMinutes();
761 if (M < 10) M = '0' + M;
763 var S = now.getUTCSeconds();
764 if (S < 10) S = '0' + S;
766 var stamp = '' + y + m + d + H + M + S + '.0';
767 var f = $scope.record.field('005',true)[0];
771 $scope.record.insertOrderedFields(
786 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
790 template: '<span class="nullable">'+
791 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
792 '<option value="">Select a Source</option>'+
795 controller: ['$scope','egCore',
796 function ($scope , egCore) {
798 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
799 .then(function(list) { $scope.bib_sources = list; });
801 $scope.$watch('bib_source',
802 function(newVal, oldVal) {
803 if (newVal !== oldVal) {
804 $scope.bre.source(newVal);