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: '=' },
219 .directive("egMarcEditControlfield", function () {
224 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
225 '<span><eg-marc-edit-editable '+
228 'class="marcedit marcdata" '+
229 'content="field.data" '+
230 'on-keydown="onKeydown" '+
231 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
234 scope: { field: "=", onKeydown: '=' }
238 .directive("egMarcEditLeader", function () {
243 '<span><eg-marc-edit-editable '+
244 'class="marcedit marctag" '+
246 'on-keydown="onKeydown" '+
248 'disabled="disabled"'+
250 '<span><eg-marc-edit-editable '+
251 'class="marcedit marcdata" '+
253 'max="{{record.leader.length}}" '+
254 'content="record.leader" '+
255 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
256 'on-keydown="onKeydown"'+
259 controller : ['$scope',
260 function ( $scope ) {
264 scope: { record: "=", onKeydown: '=' }
268 /// TODO: fixed field editor and such
269 .directive("egMarcEditRecord", function () {
271 templateUrl : './cat/share/t_marcedit',
274 scope: { recordId : '=', recordType : '@', maxUndo : '@' },
275 link: function (scope, element, attrs) {
277 element.bind('click', function(e) {;
278 scope.current_event_target = $(e.target).attr('id');
279 if (scope.current_event_target) {
280 console.log('Recording click event on ' + scope.current_event_target);
281 scope.current_event_target_cursor_pos =
282 e.target.selectionDirection=='backward' ?
283 e.target.selectionStart :
284 e.target.selectionEnd;
289 controller : ['$timeout','$scope','egCore',
290 function ( $timeout , $scope , egCore ) {
292 $scope.bib_source = null;
293 $scope.record_type = $scope.recordType || 'bre';
294 $scope.max_undo = $scope.maxUndo || 100;
295 $scope.record_undo_stack = [];
296 $scope.record_redo_stack = [];
297 $scope.in_undo = false;
298 $scope.in_redo = false;
299 $scope.record = new MARC.Record();
300 $scope.save_stack_depth = 0;
301 $scope.controlfields = [];
302 $scope.datafields = [];
304 $scope.onKeydown = function (event) {
305 var event_return = true;
308 'keydown: which='+event.which+
309 ', ctrlKey='+event.ctrlKey+
310 ', shiftKey='+event.shiftKey+
311 ', altKey='+event.altKey+
312 ', metaKey='+event.altKey
315 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
316 event_return = $scope.processRedo();
317 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
318 event_return = $scope.processUndo();
319 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
321 var element = $(event.target);
322 var new_sf, index_sf, move_data;
324 if (element.hasClass('marcsfvalue')) {
325 index_sf = event.data.scope.subfield[2];
326 new_sf = index_sf + 1;
328 var start = event.target.selectionStart;
329 var end = event.target.selectionEnd - event.target.selectionStart ?
330 event.target.selectionEnd :
331 event.target.value.length;
333 move_data = event.target.value.substring(start,end);
335 } else if (element.hasClass('marcsfcode')) {
336 index_sf = event.data.scope.subfield[2];
337 new_sf = index_sf + 1;
338 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
343 $scope.current_event_target = 'r' + $scope.recordId +
344 'f' + event.data.scope.field.position +
345 's' + new_sf + 'code';
347 event.data.scope.field.subfields.forEach(function(sf) {
348 if (sf[2] >= new_sf) sf[2]++;
349 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
351 event.data.scope.field.subfields.splice(
354 [' ', move_data, new_sf ]
357 $scope.current_event_target_cursor_pos = 0;
358 $scope.current_event_target_cursor_pos_end = 1;
360 $timeout(function(){$scope.$digest()}).then(setCaret);
362 event_return = false;
364 } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
365 event.data.scope.field.record.insertOrderedFields(
372 $scope.force_render = true;
373 $timeout(function(){$scope.$digest()}).then(setCaret);
375 event_return = false;
377 } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
378 event.data.scope.field.record.insertOrderedFields(
385 $scope.force_render = true;
386 $timeout(function(){$scope.$digest()}).then(setCaret);
388 event_return = false;
390 } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
391 var new_008_data = event.data.scope.field.record.generate008();
394 var old_008s = event.data.scope.field.record.field('008',true);
395 old_008s.forEach(function(o) {
396 var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
397 domnode.scope().$destroy();
399 event.data.scope.field.record.deleteFields(o);
402 event.data.scope.field.record.insertOrderedFields(
409 $scope.force_render = true;
410 $timeout(function(){$scope.$digest()}).then(setCaret);
412 event_return = false;
414 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
416 var element = $(event.target);
418 var index_field = event.data.scope.field.position;
419 var new_field = index_field + 1;
421 event.data.scope.field.record.insertFieldsAfter(
422 event.data.scope.field,
425 subfields : [[' ','',0]]
429 $scope.current_event_target = 'r' + $scope.recordId +
430 'f' + new_field + 'tag';
432 $scope.current_event_target_cursor_pos = 0;
433 $scope.current_event_target_cursor_pos_end = 3;
434 $scope.force_render = true;
436 $timeout(function(){$scope.$digest()}).then(setCaret);
438 event_return = false;
440 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
442 var del_field = event.data.scope.field.position;
444 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
446 event.data.scope.field.record.deleteFields(
447 event.data.scope.field
450 domnode.scope().$destroy();
453 $scope.current_event_target = 'r' + $scope.recordId +
454 'f' + del_field + 'tag';
456 $scope.current_event_target_cursor_pos = 0;
457 $scope.current_event_target_cursor_pos_end = 0
458 $scope.force_render = true;
460 $timeout(function(){$scope.$digest()}).then(setCaret);
462 event_return = false;
464 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
466 var sf = event.data.scope.subfield[2] - 1;
467 if (sf == -1) sf = 0;
469 event.data.scope.field.deleteExactSubfields(
470 event.data.scope.subfield
473 if (!event.data.scope.field.subfields[sf]) {
474 $scope.current_event_target = 'r' + $scope.recordId +
475 'f' + event.data.scope.field.position +
478 $scope.current_event_target = 'r' + $scope.recordId +
479 'f' + event.data.scope.field.position +
483 $scope.current_event_target_cursor_pos = 0;
484 $scope.current_event_target_cursor_pos_end = 0;
485 $scope.force_render = true;
487 $timeout(function(){$scope.$digest()}).then(setCaret);
489 event_return = false;
491 } else if (event.keyCode == 38) {
492 if (event.ctrlKey) { // copy the field up
493 var index_field = event.data.scope.field.position;
496 if (event.data.scope.field.isControlfield()) {
497 field_obj = new MARC.Field({
498 tag : event.data.scope.field.tag,
499 data : event.data.scope.field.data
503 for (var i in event.data.scope.field.subfields) {
504 sf_clone.push(event.data.scope.field.subfields[i].slice());
506 field_obj = new MARC.Field({
507 tag : event.data.scope.field.tag,
508 ind1 : event.data.scope.field.ind1,
509 ind2 : event.data.scope.field.ind2,
515 event.data.scope.field.record.insertFieldsBefore(
516 event.data.scope.field,
520 $scope.current_event_target = 'r' + $scope.recordId +
521 'f' + index_field + 'tag';
523 $scope.current_event_target_cursor_pos = 0;
524 $scope.current_event_target_cursor_pos_end = 3;
525 $scope.force_render = true;
527 $timeout(function(){$scope.$digest()}).then(setCaret);
529 } else { // jump to prev field
530 if (event.data.scope.field.position > 0) {
532 $scope.current_event_target_cursor_pos = 0;
533 $scope.current_event_target_cursor_pos_end = 0;
534 $scope.current_event_target = 'r' + $scope.recordId +
535 'f' + (event.data.scope.field.position - 1) +
541 event_return = false;
543 } else if (event.keyCode == 40) { // down arrow...
544 if (event.ctrlKey) { // copy the field down
546 var index_field = event.data.scope.field.position;
547 var new_field = index_field + 1;
550 if (event.data.scope.field.isControlfield()) {
551 field_obj = new MARC.Field({
552 tag : event.data.scope.field.tag,
553 data : event.data.scope.field.data
557 for (var i in event.data.scope.field.subfields) {
558 sf_clone.push(event.data.scope.field.subfields[i].slice());
560 field_obj = new MARC.Field({
561 tag : event.data.scope.field.tag,
562 ind1 : event.data.scope.field.ind1,
563 ind2 : event.data.scope.field.ind2,
568 event.data.scope.field.record.insertFieldsAfter(
569 event.data.scope.field,
573 $scope.current_event_target = 'r' + $scope.recordId +
574 'f' + new_field + 'tag';
576 $scope.current_event_target_cursor_pos = 0;
577 $scope.current_event_target_cursor_pos_end = 3;
578 $scope.force_render = true;
580 $timeout(function(){$scope.$digest()}).then(setCaret);
582 } else { // jump to next field
583 if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
585 $scope.current_event_target_cursor_pos = 0;
586 $scope.current_event_target_cursor_pos_end = 0;
587 $scope.current_event_target = 'r' + $scope.recordId +
588 'f' + (event.data.scope.field.position + 1) +
594 event_return = false;
596 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
597 $scope.current_event_target = $(event.target).attr('id');
598 if ($scope.current_event_target) {
599 $scope.current_event_target_cursor_pos =
600 event.target.selectionDirection=='backward' ?
601 event.target.selectionStart :
602 event.target.selectionEnd;
609 function setCaret() {
610 if ($scope.current_event_target) {
611 console.log("Putting caret in " + $scope.current_event_target);
612 if (!$scope.current_event_target_cursor_pos_end)
613 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
615 var element = $('#'+$scope.current_event_target).get(0);
618 if (element.setSelectionRange) {
619 element.setSelectionRange(
620 $scope.current_event_target_cursor_pos,
621 $scope.current_event_target_cursor_pos_end
624 $scope.current_event_cursor_pos_end = null;
625 $scope.current_event_target = null;
630 function loadRecord() {
631 return egCore.pcrud.retrieve(
632 $scope.record_type, $scope.recordId
633 ).then(function(rec) {
634 $scope.in_redo = true;
635 $scope[$scope.record_type] = rec;
636 $scope.record = new MARC.Record({ marcxml : $scope[$scope.record_type].marc() });
637 $scope.calculated_record_type = $scope.record.recordType();
638 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
639 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
640 $scope.save_stack_depth = $scope.record_undo_stack.length;
642 if ($scope.record_type == 'bre') {
643 $scope.bib_source = $scope[$scope.record_type].source();
649 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
650 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
651 $scope.record_undo_stack.push({
653 target: $scope.current_event_target,
654 pos: $scope.current_event_target_cursor_pos
657 if ($scope.force_render) {
658 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
659 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
660 $scope.force_render = false;
663 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
664 console.log('should get a listener... does not');
665 $('body').on('beforeunload', function(){
666 return 'There is unsaved data in this record.'
669 $('body').off('beforeunload');
673 if ($scope.record_undo_stack.length > $scope.max_undo)
674 $scope.record_undo_stack.shift();
676 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
677 $scope.in_redo = false;
678 $scope.in_undo = false;
681 $scope.processUndo = function () {
682 if ($scope.record_undo_stack.length) {
683 $scope.in_undo = true;
685 var undo_item = $scope.record_undo_stack.pop();
686 $scope.record_redo_stack.push(undo_item);
688 $scope.record = new MARC.Record({ marcbreaker : undo_item.breaker });
689 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
690 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
692 $scope.current_event_target = undo_item.target;
693 $scope.current_event_target_cursor_pos = undo_item.pos;
694 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
696 $timeout(function(){$scope.$digest()}).then(setCaret);
703 $scope.processRedo = function () {
704 if ($scope.record_redo_stack.length) {
705 $scope.in_redo = true;
707 var redo_item = $scope.record_redo_stack.pop();
708 $scope.record_undo_stack.push(redo_item);
710 $scope.record = new MARC.Record({ marcbreaker : redo_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 = redo_item.target;
715 $scope.current_event_target_cursor_pos = redo_item.pos;
716 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
718 $timeout(function(){$scope.$digest()}).then(setCaret);
725 $scope.saveRecord = function () {
727 $scope[$scope.record_type].marc($scope.record.toXmlString());
728 return egCore.pcrud.update(
729 $scope[$scope.record_type]
733 $scope.seeBreaker = function () {
734 alert($scope.record.toBreaker());
737 $scope.$watch('recordId',
738 function(newVal, oldVal) {
739 if (newVal && newVal !== oldVal) {
748 $scope.mangle_005 = function () {
749 var now = new Date();
750 var y = now.getUTCFullYear();
752 var m = now.getUTCMonth() + 1;
753 if (m < 10) m = '0' + m;
755 var d = now.getUTCDate();
756 if (d < 10) d = '0' + d;
758 var H = now.getUTCHours();
759 if (H < 10) H = '0' + H;
761 var M = now.getUTCMinutes();
762 if (M < 10) M = '0' + M;
764 var S = now.getUTCSeconds();
765 if (S < 10) S = '0' + S;
767 var stamp = '' + y + m + d + H + M + S + '.0';
768 var f = $scope.record.field('005',true)[0];
772 $scope.record.insertOrderedFields(
787 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
791 template: '<span class="nullable">'+
792 '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
793 '<option value="">Select a Source</option>'+
796 controller: ['$scope','egCore',
797 function ($scope , egCore) {
799 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
800 .then(function(list) { $scope.bib_sources = list; });
802 $scope.$watch('bib_source',
803 function(newVal, oldVal) {
804 if (newVal !== oldVal) {
805 $scope.bre.source(newVal);