]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
webstaff: MARC editor - AngularJS egTagTable service
[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             contextItemGenerator: '=',
54             max: '@',
55             itype: '@'
56         },
57         controller : ['$scope', 'egTagTable',
58             function ( $scope ,  egTagTable) {
59
60                 if (!$scope.contextItemContainer) {
61                     if ($scope.itype === 'tag') {
62                         $scope.contextItemContainer = "default_context";
63                         // FIXME use contextItemGenerator
64                         $scope.$parent[$scope.contextItemContainer] = egTagTable.getFieldTags();
65                     } else if ($scope.itype === 'sfc') {
66                         $scope.contextItemContainer = "default_context";
67                         $scope.$parent[$scope.contextItemContainer] = 
68                             egTagTable.getSubfieldCodes($scope.$parent.field.tag);
69                     } else if ($scope.itype === 'ind') {
70                         $scope.contextItemContainer = "default_context";
71                         $scope.$parent[$scope.contextItemContainer] = 
72                             egTagTable.getIndicatorValues($scope.$parent.field.tag, $scope.$parent.indNumber);
73                     }
74                 }
75
76
77                 if ($scope.contextItemContainer && angular.isArray($scope.$parent[$scope.contextItemContainer]))
78                     $scope.item_container = $scope.$parent[$scope.contextItemContainer];
79                 else if ($scope.contextItemGenerator)
80                     $scope.item_container = $scope.contextItemGenerator();
81
82                 $scope.showContext = function (event) {
83                     if ($scope.context_menu_element) {
84                         console.log('Reshowing context menu...');
85                         $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
86                         $('body').on('click.context_menu',function() {
87                             $($scope.context_menu_element).css('display','none');
88                             $('body').off('click.context_menu');
89                         });
90                         return false;
91                     }
92
93                     if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
94                         console.log('Showing context menu...');
95
96                         var tmpl = 
97                             '<ul class="dropdown-menu" role="menu">'+
98                                 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
99                             '</ul>';
100             
101                         var tnode = angular.element(tmpl);
102                         $document.find('body').append(tnode);
103
104                         $(tnode).css({
105                             display: 'block',
106                             top: event.pageY,
107                             left: event.pageX
108                         });
109
110                         $scope.context_menu_element = tnode;
111
112                         $timeout(function() {
113                             var e = $compile(tnode)($scope);
114                         }, 0);
115
116
117                         $('body').on('click.context_menu',function() {
118                             $(tnode).css('display','none');
119                             $('body').off('click.context_menu');
120                         });
121
122                         return false;
123                     }
124             
125                     return true;
126                 }
127
128             }
129         ],
130         link: function (scope, element, attrs) {
131
132             if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
133
134             element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
135
136             if (scope.item_container)
137                 element.bind('contextmenu', scope.showContext);
138         }
139     }
140 }])
141
142 .directive("egMarcEditSubfield", function () {
143     return {
144         transclude: true,
145         restrict: 'E',
146         template: '<span>'+
147                     '<span><label class="marcedit marcsfcodedelimiter"'+
148                         'for="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
149                         '>‡</label><eg-marc-edit-editable '+
150                         'itype="sfc" '+
151                         'class="marcedit marcsf marcsfcode" '+
152                         'field="field" '+
153                         'subfield="subfield" '+
154                         'content="subfield[0]" '+
155                         'max="1" '+
156                         'on-keydown="onKeydown" '+
157                         'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
158                     '/></span>'+
159                     '<span><eg-marc-edit-editable '+
160                         'itype="sfv" '+
161                         'class="marcedit marcsf marcsfvalue" '+
162                         'field="field" '+
163                         'subfield="subfield" '+
164                         'content="subfield[1]" '+
165                         'on-keydown="onKeydown" '+
166                         'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}value" '+
167                     '/></span>'+
168                   '</span>',
169         scope: { field: "=", subfield: "=", onKeydown: '=' },
170         replace: true
171     }
172 })
173
174 .directive("egMarcEditInd", function () {
175     return {
176         transclude: true,
177         restrict: 'E',
178         template: '<span><eg-marc-edit-editable '+
179                       'itype="ind" '+
180                       'class="marcedit marcind" '+
181                       'field="field" '+
182                       'content="ind" '+
183                       'max="1" '+
184                       'on-keydown="onKeydown" '+
185                       'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
186                       '/></span>',
187         scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
188         replace: true
189     }
190 })
191
192 .directive("egMarcEditTag", function () {
193     return {
194         transclude: true,
195         restrict: 'E',
196         template: '<span><eg-marc-edit-editable '+
197                       'itype="tag" '+
198                       'class="marcedit marctag" '+
199                       'field="field" '+
200                       'content="tag" '+
201                       'max="3" '+
202                       'on-keydown="onKeydown" '+
203                       'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
204                       '/></span>',
205         scope: { tag : '=', field: '=', onKeydown: '=' },
206         replace: true
207     }
208 })
209
210 .directive("egMarcEditDatafield", function () {
211     return {
212         transclude: true,
213         restrict: 'E',
214         template: '<div>'+
215                     '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
216                     '<span><eg-marc-edit-ind field="field" ind="field.ind1" on-keydown="onKeydown" ind-number="1"/></span>'+
217                     '<span><eg-marc-edit-ind field="field" ind="field.ind2" on-keydown="onKeydown" ind-number="2"/></span>'+
218                     '<span><eg-marc-edit-subfield ng-repeat="subfield in field.subfields" subfield="subfield" field="field" on-keydown="onKeydown"/></span>'+
219                   '</div>',
220         scope: { field: "=", onKeydown: '=' },
221         replace: true
222     }
223 })
224
225 .directive("egMarcEditControlfield", function () {
226     return {
227         transclude: true,
228         restrict: 'E',
229         template: '<div>'+
230                     '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
231                     '<span><eg-marc-edit-editable '+
232                       'itype="cfld" '+
233                       'field="field" '+
234                       'class="marcedit marcdata" '+
235                       'content="field.data" '+
236                       'on-keydown="onKeydown" '+
237                       'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}data"'+
238                       '/></span>'+
239                   '</div>',
240         scope: { field: "=", onKeydown: '=' }
241     }
242 })
243
244 .directive("egMarcEditLeader", function () {
245     return {
246         transclude: true,
247         restrict: 'E',
248         template: '<div>'+
249                     '<span><eg-marc-edit-editable '+
250                       'class="marcedit marctag" '+
251                       'content="tag" '+
252                       'on-keydown="onKeydown" '+
253                       'id="leadertag" '+
254                       'disabled="disabled"'+
255                       '/></span>'+
256                     '<span><eg-marc-edit-editable '+
257                       'class="marcedit marcdata" '+
258                       'itype="ldr" '+
259                       'max="{{record.leader.length}}" '+
260                       'content="record.leader" '+
261                       'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
262                       'on-keydown="onKeydown"'+
263                       '/></span>'+
264                   '</div>',
265         controller : ['$scope',
266             function ( $scope ) {
267                 $scope.tag = 'LDR';
268             }
269         ],
270         scope: { record: "=", onKeydown: '=' }
271     }
272 })
273
274 /// TODO: fixed field editor and such
275 .directive("egMarcEditRecord", function () {
276     return {
277         templateUrl : './cat/share/t_marcedit',
278         restrict: 'E',
279         replace: true,
280         scope: {
281             dirtyFlag : '=',
282             recordId : '=',
283             recordType : '@',
284             maxUndo : '@'
285         },
286         link: function (scope, element, attrs) {
287
288             element.bind('click', function(e) {;
289                 scope.current_event_target = $(e.target).attr('id');
290                 if (scope.current_event_target) {
291                     console.log('Recording click event on ' + scope.current_event_target);
292                     scope.current_event_target_cursor_pos =
293                         e.target.selectionDirection=='backward' ?
294                             e.target.selectionStart :
295                             e.target.selectionEnd;
296                 }
297             });
298
299         },
300         controller : ['$timeout','$scope','egCore', 'egTagTable',
301             function ( $timeout , $scope , egCore ,  egTagTable ) {
302
303                 $scope.bib_source = null;
304                 $scope.record_type = $scope.recordType || 'bre';
305                 $scope.max_undo = $scope.maxUndo || 100;
306                 $scope.record_undo_stack = [];
307                 $scope.record_redo_stack = [];
308                 $scope.in_undo = false;
309                 $scope.in_redo = false;
310                 $scope.record = new MARC21.Record();
311                 $scope.save_stack_depth = 0;
312                 $scope.controlfields = [];
313                 $scope.datafields = [];
314
315                 egTagTable.loadTagTable();
316
317                 $scope.onKeydown = function (event) {
318                     var event_return = true;
319
320                     console.log(
321                         'keydown: which='+event.which+
322                         ', ctrlKey='+event.ctrlKey+
323                         ', shiftKey='+event.shiftKey+
324                         ', altKey='+event.altKey+
325                         ', metaKey='+event.altKey
326                     );
327
328                     if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
329                         event_return = $scope.processRedo();
330                     } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
331                         event_return = $scope.processUndo();
332                     } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
333
334                         var element = $(event.target);
335                         var new_sf, index_sf, move_data;
336
337                         if (element.hasClass('marcsfvalue')) {
338                             index_sf = event.data.scope.subfield[2];
339                             new_sf = index_sf + 1;
340
341                             var start = event.target.selectionStart;
342                             var end = event.target.selectionEnd - event.target.selectionStart ?
343                                     event.target.selectionEnd :
344                                     event.target.value.length;
345
346                             move_data = event.target.value.substring(start,end);
347
348                         } else if (element.hasClass('marcsfcode')) {
349                             index_sf = event.data.scope.subfield[2];
350                             new_sf = index_sf + 1;
351                         } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
352                             index_sf = 0;
353                             new_sf = index_sf;
354                         }
355
356                         $scope.current_event_target = 'r' + $scope.recordId +
357                                                       'f' + event.data.scope.field.position + 
358                                                       's' + new_sf + 'code';
359
360                         event.data.scope.field.subfields.forEach(function(sf) {
361                             if (sf[2] >= new_sf) sf[2]++;
362                             if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
363                         });
364                         event.data.scope.field.subfields.splice(
365                             new_sf,
366                             0,
367                             [' ', move_data, new_sf ]
368                         );
369
370                         $scope.current_event_target_cursor_pos = 0;
371                         $scope.current_event_target_cursor_pos_end = 1;
372
373                         $timeout(function(){$scope.$digest()}).then(setCaret);
374
375                         event_return = false;
376
377                     } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
378                         event.data.scope.field.record.insertOrderedFields(
379                             new MARC21.Field({
380                                 tag : '006',
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 == 118 && event.shiftKey) { // shift + F7, insert 007
391                         event.data.scope.field.record.insertOrderedFields(
392                             new MARC21.Field({
393                                 tag : '007',
394                                 data : '                                        '
395                             })
396                         );
397
398                         $scope.force_render = true;
399                         $timeout(function(){$scope.$digest()}).then(setCaret);
400
401                         event_return = false;
402
403                     } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
404                         var new_008_data = event.data.scope.field.record.generate008();
405
406
407                         var old_008s = event.data.scope.field.record.field('008',true);
408                         old_008s.forEach(function(o) {
409                             var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
410                             domnode.scope().$destroy();
411                             domnode.remove();
412                             event.data.scope.field.record.deleteFields(o);
413                         });
414
415                         event.data.scope.field.record.insertOrderedFields(
416                             new MARC21.Field({
417                                 tag : '008',
418                                 data : new_008_data
419                             })
420                         );
421
422                         $scope.force_render = true;
423                         $timeout(function(){$scope.$digest()}).then(setCaret);
424
425                         event_return = false;
426
427                     } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
428
429                         var element = $(event.target);
430
431                         var index_field = event.data.scope.field.position;
432                         var new_field = index_field + 1;
433
434                         event.data.scope.field.record.insertFieldsAfter(
435                             event.data.scope.field,
436                             new MARC21.Field({
437                                 tag : '999',
438                                 subfields : [[' ','',0]]
439                             })
440                         );
441
442                         $scope.current_event_target = 'r' + $scope.recordId +
443                                                       'f' + new_field + 'tag';
444
445                         $scope.current_event_target_cursor_pos = 0;
446                         $scope.current_event_target_cursor_pos_end = 3;
447                         $scope.force_render = true;
448
449                         $timeout(function(){$scope.$digest()}).then(setCaret);
450
451                         event_return = false;
452
453                     } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
454
455                         var del_field = event.data.scope.field.position;
456
457                         var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
458
459                         event.data.scope.field.record.deleteFields(
460                             event.data.scope.field
461                         );
462
463                         domnode.scope().$destroy();
464                         domnode.remove();
465
466                         $scope.current_event_target = 'r' + $scope.recordId +
467                                                       'f' + del_field + 'tag';
468
469                         $scope.current_event_target_cursor_pos = 0;
470                         $scope.current_event_target_cursor_pos_end = 0
471                         $scope.force_render = true;
472
473                         $timeout(function(){$scope.$digest()}).then(setCaret);
474
475                         event_return = false;
476
477                     } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
478
479                         var sf = event.data.scope.subfield[2] - 1;
480                         if (sf == -1) sf = 0;
481
482                         event.data.scope.field.deleteExactSubfields(
483                             event.data.scope.subfield
484                         );
485
486                         if (!event.data.scope.field.subfields[sf]) {
487                             $scope.current_event_target = 'r' + $scope.recordId +
488                                                           'f' + event.data.scope.field.position + 
489                                                           'tag';
490                         } else {
491                             $scope.current_event_target = 'r' + $scope.recordId +
492                                                           'f' + event.data.scope.field.position + 
493                                                           's' + sf + 'value';
494                         }
495
496                         $scope.current_event_target_cursor_pos = 0;
497                         $scope.current_event_target_cursor_pos_end = 0;
498                         $scope.force_render = true;
499
500                         $timeout(function(){$scope.$digest()}).then(setCaret);
501
502                         event_return = false;
503
504                     } else if (event.keyCode == 38) {
505                         if (event.ctrlKey) { // copy the field up
506                             var index_field = event.data.scope.field.position;
507
508                             var field_obj;
509                             if (event.data.scope.field.isControlfield()) {
510                                 field_obj = new MARC21.Field({
511                                     tag : event.data.scope.field.tag,
512                                     data : event.data.scope.field.data
513                                 });
514                             } else {
515                                 var sf_clone = [];
516                                 for (var i in event.data.scope.field.subfields) {
517                                     sf_clone.push(event.data.scope.field.subfields[i].slice());
518                                 }
519                                 field_obj = new MARC21.Field({
520                                     tag : event.data.scope.field.tag,
521                                     ind1 : event.data.scope.field.ind1,
522                                     ind2 : event.data.scope.field.ind2,
523                                     subfields : sf_clone
524                                 });
525                             }
526
527
528                             event.data.scope.field.record.insertFieldsBefore(
529                                 event.data.scope.field,
530                                 field_obj
531                             );
532
533                             $scope.current_event_target = 'r' + $scope.recordId +
534                                                           'f' + index_field + 'tag';
535
536                             $scope.current_event_target_cursor_pos = 0;
537                             $scope.current_event_target_cursor_pos_end = 3;
538                             $scope.force_render = true;
539
540                             $timeout(function(){$scope.$digest()}).then(setCaret);
541
542                         } else { // jump to prev field
543                             if (event.data.scope.field.position > 0) {
544                                 $timeout(function(){
545                                     $scope.current_event_target_cursor_pos = 0;
546                                     $scope.current_event_target_cursor_pos_end = 0;
547                                     $scope.current_event_target = 'r' + $scope.recordId +
548                                                                   'f' + (event.data.scope.field.position - 1) +
549                                                                   'tag';
550                                 }).then(setCaret);
551                             }
552                         }
553
554                         event_return = false;
555
556                     } else if (event.keyCode == 40) { // down arrow...
557                         if (event.ctrlKey) { // copy the field down
558
559                             var index_field = event.data.scope.field.position;
560                             var new_field = index_field + 1;
561
562                             var field_obj;
563                             if (event.data.scope.field.isControlfield()) {
564                                 field_obj = new MARC21.Field({
565                                     tag : event.data.scope.field.tag,
566                                     data : event.data.scope.field.data
567                                 });
568                             } else {
569                                 var sf_clone = [];
570                                 for (var i in event.data.scope.field.subfields) {
571                                     sf_clone.push(event.data.scope.field.subfields[i].slice());
572                                 }
573                                 field_obj = new MARC21.Field({
574                                     tag : event.data.scope.field.tag,
575                                     ind1 : event.data.scope.field.ind1,
576                                     ind2 : event.data.scope.field.ind2,
577                                     subfields : sf_clone
578                                 });
579                             }
580
581                             event.data.scope.field.record.insertFieldsAfter(
582                                 event.data.scope.field,
583                                 field_obj
584                             );
585
586                             $scope.current_event_target = 'r' + $scope.recordId +
587                                                           'f' + new_field + 'tag';
588
589                             $scope.current_event_target_cursor_pos = 0;
590                             $scope.current_event_target_cursor_pos_end = 3;
591                             $scope.force_render = true;
592
593                             $timeout(function(){$scope.$digest()}).then(setCaret);
594
595                         } else { // jump to next field
596                             if (event.data.scope.field.record.fields[event.data.scope.field.position + 1]) {
597                                 $timeout(function(){
598                                     $scope.current_event_target_cursor_pos = 0;
599                                     $scope.current_event_target_cursor_pos_end = 0;
600                                     $scope.current_event_target = 'r' + $scope.recordId +
601                                                                   'f' + (event.data.scope.field.position + 1) +
602                                                                   'tag';
603                                 }).then(setCaret);
604                             }
605                         }
606
607                         event_return = false;
608
609                     } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
610                         $scope.current_event_target = $(event.target).attr('id');
611                         if ($scope.current_event_target) {
612                             $scope.current_event_target_cursor_pos =
613                                 event.target.selectionDirection=='backward' ?
614                                     event.target.selectionStart :
615                                     event.target.selectionEnd;
616                         }
617                     }
618
619                     return event_return;
620                 };
621
622                 function setCaret() {
623                     if ($scope.current_event_target) {
624                         console.log("Putting caret in " + $scope.current_event_target);
625                         if (!$scope.current_event_target_cursor_pos_end)
626                             $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
627
628                         var element = $('#'+$scope.current_event_target).get(0);
629                         if (element) {
630                             element.focus();
631                             if (element.setSelectionRange) {
632                                 element.setSelectionRange(
633                                     $scope.current_event_target_cursor_pos,
634                                     $scope.current_event_target_cursor_pos_end
635                                 );
636                             }
637                             $scope.current_event_cursor_pos_end = null;
638                             $scope.current_event_target = null;
639                         }
640                     }
641                 }
642
643                 function loadRecord() {
644                     return egCore.pcrud.retrieve(
645                         $scope.record_type, $scope.recordId
646                     ).then(function(rec) {
647                         $scope.in_redo = true;
648                         $scope[$scope.record_type] = rec;
649                         $scope.record = new MARC21.Record({ marcxml : $scope[$scope.record_type].marc() });
650                         $scope.calculated_record_type = $scope.record.recordType();
651                         $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
652                         $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
653                         $scope.save_stack_depth = $scope.record_undo_stack.length;
654
655                         if ($scope.record_type == 'bre') {
656                             $scope.bib_source = $scope[$scope.record_type].source();
657                         }
658
659                     }).then(setCaret);
660                 }
661
662                 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
663                     if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
664                         $scope.record_undo_stack.push({
665                             breaker: oldVal,
666                             target: $scope.current_event_target,
667                             pos: $scope.current_event_target_cursor_pos
668                         });
669
670                         if ($scope.force_render) {
671                             $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
672                             $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
673                             $scope.force_render = false;
674                         }
675
676                     }
677
678                     if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
679                         console.log('should get a listener... does not');
680                         $scope.dirtyFlag = true;
681                     } else {
682                         $scope.dirtyFlag = false;
683                     }
684
685                     if ($scope.record_undo_stack.length > $scope.max_undo)
686                         $scope.record_undo_stack.shift();
687
688                     console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
689                     $scope.in_redo = false;
690                     $scope.in_undo = false;
691                 });
692
693                 $scope.processUndo = function () {
694                     if ($scope.record_undo_stack.length) {
695                         $scope.in_undo = true;
696
697                         var undo_item = $scope.record_undo_stack.pop();
698                         $scope.record_redo_stack.push(undo_item);
699
700                         $scope.record = new MARC21.Record({ marcbreaker : undo_item.breaker });
701                         $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
702                         $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
703
704                         $scope.current_event_target = undo_item.target;
705                         $scope.current_event_target_cursor_pos = undo_item.pos;
706                         console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
707
708                         $timeout(function(){$scope.$digest()}).then(setCaret);
709                         return false;
710                     }
711
712                     return true;
713                 };
714
715                 $scope.processRedo = function () {
716                     if ($scope.record_redo_stack.length) {
717                         $scope.in_redo = true;
718
719                         var redo_item = $scope.record_redo_stack.pop();
720                         $scope.record_undo_stack.push(redo_item);
721
722                         $scope.record = new MARC21.Record({ marcbreaker : redo_item.breaker });
723                         $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
724                         $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
725
726                         $scope.current_event_target = redo_item.target;
727                         $scope.current_event_target_cursor_pos = redo_item.pos;
728                         console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
729
730                         $timeout(function(){$scope.$digest()}).then(setCaret);
731                         return false;
732                     }
733
734                     return true;
735                 };
736
737                 $scope.saveRecord = function () {
738                     $scope.mangle_005();
739                     $scope[$scope.record_type].marc($scope.record.toXmlString());
740                     return egCore.pcrud.update(
741                         $scope[$scope.record_type]
742                     ).then(loadRecord);
743                 };
744
745                 $scope.seeBreaker = function () {
746                     alert($scope.record.toBreaker());
747                 };
748
749                 $scope.$watch('recordId',
750                     function(newVal, oldVal) {
751                         if (newVal && newVal !== oldVal) {
752                             loadRecord();
753                         }
754                     }
755                 );
756
757                 if ($scope.recordId)
758                     loadRecord();
759
760                 $scope.mangle_005 = function () {
761                     var now = new Date();
762                     var y = now.getUTCFullYear();
763                 
764                     var m = now.getUTCMonth() + 1;
765                     if (m < 10) m = '0' + m;
766                 
767                     var d = now.getUTCDate();
768                     if (d < 10) d = '0' + d;
769                 
770                     var H = now.getUTCHours();
771                     if (H < 10) H = '0' + H;
772                 
773                     var M = now.getUTCMinutes();
774                     if (M < 10) M = '0' + M;
775                 
776                     var S = now.getUTCSeconds();
777                     if (S < 10) S = '0' + S;
778                 
779                     var stamp = '' + y + m + d + H + M + S + '.0';
780                     var f = $scope.record.field('005',true)[0];
781                     if (f) {
782                         f.data = stamp;
783                     } else {
784                         $scope.record.insertOrderedFields(
785                             new MARC21.Field({
786                                 tag : '005',
787                                 data: stamp
788                             })
789                         );
790                     }
791                 
792                 }
793
794             }
795         ]          
796     }
797 })
798
799 .directive("egMarcEditBibsource", ['$timeout',function ($timeout) {
800     return {
801         restrict: 'E',
802         replace: true,
803         template: '<span class="nullable">'+
804                     '<select class="form-control" ng-model="bib_source" ng-options="s.id() as s.source() for s in bib_sources">'+
805                       '<option value="">Select a Source</option>'+
806                     '</select>'+
807                   '</span>',
808         controller: ['$scope','egCore',
809             function ($scope , egCore) {
810
811                 egCore.pcrud.retrieveAll('cbs', {}, {atomic : true})
812                     .then(function(list) { $scope.bib_sources = list; });
813
814                 $scope.$watch('bib_source',
815                     function(newVal, oldVal) {
816                         if (newVal !== oldVal) {
817                             $scope.bre.source(newVal);
818                         }
819                     }
820                 );
821
822             }
823         ]
824     }
825 }])
826
827 ;