]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
LP#1402797 clean up action rows and styling
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / marcedit.js
1 /**
2  *  A MARC editor...
3  */
4
5 angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
6
7 .directive("egContextMenuItem", ['$timeout',function ($timeout) {
8     return {
9         restrict: 'E',
10         replace: true,
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';
18                 }
19
20                 $scope.setContent = function (v, a) {
21                     var replace_with = v;
22                     if (a) replace_with = a($scope,$element,$scope.item.value,$scope.$parent.$parent.content);
23                     $timeout(function(){
24                         $scope.$parent.$parent.$apply(function(){
25                             $scope.$parent.$parent.content = replace_with
26                         })
27                     }, 0);
28                     $($element).parent().css({display: 'none'});
29                 }
30             }
31         ]
32     }
33 }])
34
35 .directive("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
36     return {
37         restrict: 'E',
38         replace: true,
39         template: '<input '+
40                       'style="font-family: \'Lucida Console\', Monaco, monospace;" '+
41                       'ng-model="content" '+
42                       'size="{{content.length * 1.1}}" '+
43                       'maxlength="{{max}}" '+
44                       'class="" '+
45                       'type="text" '+
46                   '/>',
47         scope: {
48             field: '=',
49             onKeydown: '=',
50             subfield: '=',
51             content: '=',
52             contextItemContainer: '@',
53             max: '@',
54             itype: '@'
55         },
56         controller : ['$scope',
57             function ( $scope ) {
58
59 /* XXX Example, for testing.  We'll get this from the tag-table services for realz
60  *
61                 if (!$scope.contextItemContainer) {
62                     $scope.contextItemContainer = "default_context";
63                     $scope[$scope.contextItemContainer] = [
64                         { value: 'a' },
65                         { value: 'b' },
66                         { value: 'c' },
67                     ];
68                 }
69
70  *
71  */
72
73                 if ($scope.contextItemContainer)
74                     $scope.item_container = $scope[$scope.contextItemContainer];
75
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');
83                         });
84                         return false;
85                     }
86
87                     if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
88                         console.log('Showing context menu...');
89
90                         var tmpl = 
91                             '<ul class="dropdown-menu" role="menu">'+
92                                 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
93                             '</ul>';
94             
95                         var tnode = angular.element(tmpl);
96                         $document.find('body').append(tnode);
97
98                         $(tnode).css({
99                             display: 'block',
100                             top: event.pageY,
101                             left: event.pageX
102                         });
103
104                         $scope.context_menu_element = tnode;
105
106                         $timeout(function() {
107                             var e = $compile(tnode)($scope);
108                         }, 0);
109
110
111                         $('body').on('click.context_menu',function() {
112                             $(tnode).css('display','none');
113                             $('body').off('click.context_menu');
114                         });
115
116                         return false;
117                     }
118             
119                     return true;
120                 }
121
122             }
123         ],
124         link: function (scope, element, attrs) {
125
126             if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
127
128             element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
129
130             if (scope.contextItemContainer && angular.isArray(scope[scope.contextItemContainer]))
131                 element.bind('contextmenu', scope.showContext);
132         }
133     }
134 }])
135
136 .directive("egMarcEditSubfield", function () {
137     return {
138         transclude: true,
139         restrict: 'E',
140         template: '<span>'+
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 '+
144                         'itype="sfc" '+
145                         'class="marcedit marcsf marcsfcode" '+
146                         'field="field" '+
147                         'subfield="subfield" '+
148                         'content="subfield[0]" '+
149                         'max="1" '+
150                         'on-keydown="onKeydown" '+
151                         'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
152                     '/></span>'+
153                     '<span><eg-marc-edit-editable '+
154                         'itype="sfv" '+
155                         'class="marcedit marcsf marcsfvalue" '+
156                         'field="field" '+
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" '+
161                     '/></span>'+
162                   '</span>',
163         scope: { field: "=", subfield: "=", onKeydown: '=' },
164         replace: true
165     }
166 })
167
168 .directive("egMarcEditInd", function () {
169     return {
170         transclude: true,
171         restrict: 'E',
172         template: '<span><eg-marc-edit-editable '+
173                       'itype="ind" '+
174                       'class="marcedit marcind" '+
175                       'field="field" '+
176                       'content="ind" '+
177                       'max="1" '+
178                       'on-keydown="onKeydown" '+
179                       'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
180                       '/></span>',
181         scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
182         replace: true
183     }
184 })
185
186 .directive("egMarcEditTag", function () {
187     return {
188         transclude: true,
189         restrict: 'E',
190         template: '<span><eg-marc-edit-editable '+
191                       'itype="tag" '+
192                       'class="marcedit marctag" '+
193                       'field="field" '+
194                       'content="tag" '+
195                       'max="3" '+
196                       'on-keydown="onKeydown" '+
197                       'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
198                       '/></span>',
199         scope: { tag : '=', field: '=', onKeydown: '=' },
200         replace: true
201     }
202 })
203
204 .directive("egMarcEditDatafield", function () {
205     return {
206         transclude: true,
207         restrict: 'E',
208         template: '<div>'+
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>'+
213                   '</div>',
214         scope: { field: "=", onKeydown: '=' },
215         replace: true
216     }
217 })
218
219 .directive("egMarcEditControlfield", function () {
220     return {
221         transclude: true,
222         restrict: 'E',
223         template: '<div>'+
224                     '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
225                     '<span><eg-marc-edit-editable '+
226                       'itype="cfld" '+
227                       'field="field" '+
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"'+
232                       '/></span>'+
233                   '</div>',
234         scope: { field: "=", onKeydown: '=' }
235     }
236 })
237
238 .directive("egMarcEditLeader", function () {
239     return {
240         transclude: true,
241         restrict: 'E',
242         template: '<div>'+
243                     '<span><eg-marc-edit-editable '+
244                       'class="marcedit marctag" '+
245                       'content="tag" '+
246                       'on-keydown="onKeydown" '+
247                       'id="leadertag" '+
248                       'disabled="disabled"'+
249                       '/></span>'+
250                     '<span><eg-marc-edit-editable '+
251                       'class="marcedit marcdata" '+
252                       'itype="ldr" '+
253                       'max="{{record.leader.length}}" '+
254                       'content="record.leader" '+
255                       'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
256                       'on-keydown="onKeydown"'+
257                       '/></span>'+
258                   '</div>',
259         controller : ['$scope',
260             function ( $scope ) {
261                 $scope.tag = 'LDR';
262             }
263         ],
264         scope: { record: "=", onKeydown: '=' }
265     }
266 })
267
268 /// TODO: fixed field editor and such
269 .directive("egMarcEditRecord", function () {
270     return {
271         templateUrl : './cat/share/t_marcedit',
272         restrict: 'E',
273         replace: true,
274         scope: { recordId : '=', recordType : '@', maxUndo : '@' },
275         link: function (scope, element, attrs) {
276
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;
285                 }
286             });
287
288         },
289         controller : ['$timeout','$scope','egCore',
290             function ( $timeout , $scope , egCore ) {
291
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 = [];
303
304                 $scope.onKeydown = function (event) {
305                     var event_return = true;
306
307                     console.log(
308                         'keydown: which='+event.which+
309                         ', ctrlKey='+event.ctrlKey+
310                         ', shiftKey='+event.shiftKey+
311                         ', altKey='+event.altKey+
312                         ', metaKey='+event.altKey
313                     );
314
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
320
321                         var element = $(event.target);
322                         var new_sf, index_sf, move_data;
323
324                         if (element.hasClass('marcsfvalue')) {
325                             index_sf = event.data.scope.subfield[2];
326                             new_sf = index_sf + 1;
327
328                             var start = event.target.selectionStart;
329                             var end = event.target.selectionEnd - event.target.selectionStart ?
330                                     event.target.selectionEnd :
331                                     event.target.value.length;
332
333                             move_data = event.target.value.substring(start,end);
334
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')) {
339                             index_sf = 0;
340                             new_sf = index_sf;
341                         }
342
343                         $scope.current_event_target = 'r' + $scope.recordId +
344                                                       'f' + event.data.scope.field.position + 
345                                                       's' + new_sf + 'code';
346
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);
350                         });
351                         event.data.scope.field.subfields.splice(
352                             new_sf,
353                             0,
354                             [' ', move_data, new_sf ]
355                         );
356
357                         $scope.current_event_target_cursor_pos = 0;
358                         $scope.current_event_target_cursor_pos_end = 1;
359
360                         $timeout(function(){$scope.$digest()}).then(setCaret);
361
362                         event_return = false;
363
364                     } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
365                         event.data.scope.field.record.insertOrderedFields(
366                             new MARC.Field({
367                                 tag : '006',
368                                 data : '                                        '
369                             })
370                         );
371
372                         $scope.force_render = true;
373                         $timeout(function(){$scope.$digest()}).then(setCaret);
374
375                         event_return = false;
376
377                     } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
378                         event.data.scope.field.record.insertOrderedFields(
379                             new MARC.Field({
380                                 tag : '007',
381                                 data : '                                        '
382                             })
383                         );
384
385                         $scope.force_render = true;
386                         $timeout(function(){$scope.$digest()}).then(setCaret);
387
388                         event_return = false;
389
390                     } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
391                         var new_008_data = event.data.scope.field.record.generate008();
392
393
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();
398                             domnode.remove();
399                             event.data.scope.field.record.deleteFields(o);
400                         });
401
402                         event.data.scope.field.record.insertOrderedFields(
403                             new MARC.Field({
404                                 tag : '008',
405                                 data : new_008_data
406                             })
407                         );
408
409                         $scope.force_render = true;
410                         $timeout(function(){$scope.$digest()}).then(setCaret);
411
412                         event_return = false;
413
414                     } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
415
416                         var element = $(event.target);
417
418                         var index_field = event.data.scope.field.position;
419                         var new_field = index_field + 1;
420
421                         event.data.scope.field.record.insertFieldsAfter(
422                             event.data.scope.field,
423                             new MARC.Field({
424                                 tag : '999',
425                                 subfields : [[' ','',0]]
426                             })
427                         );
428
429                         $scope.current_event_target = 'r' + $scope.recordId +
430                                                       'f' + new_field + 'tag';
431
432                         $scope.current_event_target_cursor_pos = 0;
433                         $scope.current_event_target_cursor_pos_end = 3;
434                         $scope.force_render = true;
435
436                         $timeout(function(){$scope.$digest()}).then(setCaret);
437
438                         event_return = false;
439
440                     } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
441
442                         var del_field = event.data.scope.field.position;
443
444                         var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
445
446                         event.data.scope.field.record.deleteFields(
447                             event.data.scope.field
448                         );
449
450                         domnode.scope().$destroy();
451                         domnode.remove();
452
453                         $scope.current_event_target = 'r' + $scope.recordId +
454                                                       'f' + del_field + 'tag';
455
456                         $scope.current_event_target_cursor_pos = 0;
457                         $scope.current_event_target_cursor_pos_end = 0
458                         $scope.force_render = true;
459
460                         $timeout(function(){$scope.$digest()}).then(setCaret);
461
462                         event_return = false;
463
464                     } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
465
466                         var sf = event.data.scope.subfield[2] - 1;
467                         if (sf == -1) sf = 0;
468
469                         event.data.scope.field.deleteExactSubfields(
470                             event.data.scope.subfield
471                         );
472
473                         if (!event.data.scope.field.subfields[sf]) {
474                             $scope.current_event_target = 'r' + $scope.recordId +
475                                                           'f' + event.data.scope.field.position + 
476                                                           'tag';
477                         } else {
478                             $scope.current_event_target = 'r' + $scope.recordId +
479                                                           'f' + event.data.scope.field.position + 
480                                                           's' + sf + 'value';
481                         }
482
483                         $scope.current_event_target_cursor_pos = 0;
484                         $scope.current_event_target_cursor_pos_end = 0;
485                         $scope.force_render = true;
486
487                         $timeout(function(){$scope.$digest()}).then(setCaret);
488
489                         event_return = false;
490
491                     } else if (event.keyCode == 38) {
492                         if (event.ctrlKey) { // copy the field up
493                             var index_field = event.data.scope.field.position;
494
495                             var field_obj;
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
500                                 });
501                             } else {
502                                 var sf_clone = [];
503                                 for (var i in event.data.scope.field.subfields) {
504                                     sf_clone.push(event.data.scope.field.subfields[i].slice());
505                                 }
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,
510                                     subfields : sf_clone
511                                 });
512                             }
513
514
515                             event.data.scope.field.record.insertFieldsBefore(
516                                 event.data.scope.field,
517                                 field_obj
518                             );
519
520                             $scope.current_event_target = 'r' + $scope.recordId +
521                                                           'f' + index_field + 'tag';
522
523                             $scope.current_event_target_cursor_pos = 0;
524                             $scope.current_event_target_cursor_pos_end = 3;
525                             $scope.force_render = true;
526
527                             $timeout(function(){$scope.$digest()}).then(setCaret);
528
529                         } else { // jump to prev field
530                             if (event.data.scope.field.position > 0) {
531                                 $timeout(function(){
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) +
536                                                                   'tag';
537                                 }).then(setCaret);
538                             }
539                         }
540
541                         event_return = false;
542
543                     } else if (event.keyCode == 40) { // down arrow...
544                         if (event.ctrlKey) { // copy the field down
545
546                             var index_field = event.data.scope.field.position;
547                             var new_field = index_field + 1;
548
549                             var field_obj;
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
554                                 });
555                             } else {
556                                 var sf_clone = [];
557                                 for (var i in event.data.scope.field.subfields) {
558                                     sf_clone.push(event.data.scope.field.subfields[i].slice());
559                                 }
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,
564                                     subfields : sf_clone
565                                 });
566                             }
567
568                             event.data.scope.field.record.insertFieldsAfter(
569                                 event.data.scope.field,
570                                 field_obj
571                             );
572
573                             $scope.current_event_target = 'r' + $scope.recordId +
574                                                           'f' + new_field + 'tag';
575
576                             $scope.current_event_target_cursor_pos = 0;
577                             $scope.current_event_target_cursor_pos_end = 3;
578                             $scope.force_render = true;
579
580                             $timeout(function(){$scope.$digest()}).then(setCaret);
581
582                         } else { // jump to next field
583                             if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
584                                 $timeout(function(){
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) +
589                                                                   'tag';
590                                 }).then(setCaret);
591                             }
592                         }
593
594                         event_return = false;
595
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;
603                         }
604                     }
605
606                     return event_return;
607                 };
608
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
614
615                         var element = $('#'+$scope.current_event_target).get(0);
616                         if (element) {
617                             element.focus();
618                             if (element.setSelectionRange) {
619                                 element.setSelectionRange(
620                                     $scope.current_event_target_cursor_pos,
621                                     $scope.current_event_target_cursor_pos_end
622                                 );
623                             }
624                             $scope.current_event_cursor_pos_end = null;
625                             $scope.current_event_target = null;
626                         }
627                     }
628                 }
629
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;
641
642                         if ($scope.record_type == 'bre') {
643                             $scope.bib_source = $scope[$scope.record_type].source();
644                         }
645
646                     }).then(setCaret);
647                 }
648
649                 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
650                     if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
651                         $scope.record_undo_stack.push({
652                             breaker: oldVal,
653                             target: $scope.current_event_target,
654                             pos: $scope.current_event_target_cursor_pos
655                         });
656
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;
661                         }
662
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.'
667                             });
668                         } else {
669                             $('body').off('beforeunload');
670                         }
671                     }
672
673                     if ($scope.record_undo_stack.length > $scope.max_undo)
674                         $scope.record_undo_stack.shift();
675
676                     console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
677                     $scope.in_redo = false;
678                     $scope.in_undo = false;
679                 });
680
681                 $scope.processUndo = function () {
682                     if ($scope.record_undo_stack.length) {
683                         $scope.in_undo = true;
684
685                         var undo_item = $scope.record_undo_stack.pop();
686                         $scope.record_redo_stack.push(undo_item);
687
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() });
691
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);
695
696                         $timeout(function(){$scope.$digest()}).then(setCaret);
697                         return false;
698                     }
699
700                     return true;
701                 };
702
703                 $scope.processRedo = function () {
704                     if ($scope.record_redo_stack.length) {
705                         $scope.in_redo = true;
706
707                         var redo_item = $scope.record_redo_stack.pop();
708                         $scope.record_undo_stack.push(redo_item);
709
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() });
713
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);
717
718                         $timeout(function(){$scope.$digest()}).then(setCaret);
719                         return false;
720                     }
721
722                     return true;
723                 };
724
725                 $scope.saveRecord = function () {
726                     $scope.mangle_005();
727                     $scope[$scope.record_type].marc($scope.record.toXmlString());
728                     return egCore.pcrud.update(
729                         $scope[$scope.record_type]
730                     ).then(loadRecord);
731                 };
732
733                 $scope.seeBreaker = function () {
734                     alert($scope.record.toBreaker());
735                 };
736
737                 $scope.$watch('recordId',
738                     function(newVal, oldVal) {
739                         if (newVal && newVal !== oldVal) {
740                             loadRecord();
741                         }
742                     }
743                 );
744
745                 if ($scope.recordId)
746                     loadRecord();
747
748                 $scope.mangle_005 = function () {
749                     var now = new Date();
750                     var y = now.getUTCFullYear();
751                 
752                     var m = now.getUTCMonth() + 1;
753                     if (m < 10) m = '0' + m;
754                 
755                     var d = now.getUTCDate();
756                     if (d < 10) d = '0' + d;
757                 
758                     var H = now.getUTCHours();
759                     if (H < 10) H = '0' + H;
760                 
761                     var M = now.getUTCMinutes();
762                     if (M < 10) M = '0' + M;
763                 
764                     var S = now.getUTCSeconds();
765                     if (S < 10) S = '0' + S;
766                 
767                     var stamp = '' + y + m + d + H + M + S + '.0';
768                     var f = $scope.record.field('005',true)[0];
769                     if (f) {
770                         f.data = stamp;
771                     } else {
772                         $scope.record.insertOrderedFields(
773                             new MARC.Field({
774                                 tag : '005',
775                                 data: stamp
776                             })
777                         );
778                     }
779                 
780                 }
781
782             }
783         ]          
784     }
785 })
786
787 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
788     return {
789         restrict: 'E',
790         replace: true,
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>'+
794                     '</select>'+
795                   '</span>',
796         controller: ['$scope','egCore',
797             function ($scope , egCore) {
798
799                 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
800                     .then(function(list) { $scope.bib_sources = list; });
801
802                 $scope.$watch('bib_source',
803                     function(newVal, oldVal) {
804                         if (newVal !== oldVal) {
805                             $scope.bre.source(newVal);
806                         }
807                     }
808                 );
809
810             }
811         ]
812     }
813 }])
814
815 ;