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