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