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