]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
LP#1402797 Start styling marc editor via css
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / marcedit.js
1 /**
2  *  A MARC editor...
3  */
4
5 angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
6
7 .directive("egContextMenuItem", ['$timeout',function ($timeout) {
8     return {
9         restrict: 'E',
10         replace: true,
11         template: '<li><a ng-click="setContent(item.value,item.action)">{{item.label}}</a></li>',
12         scope: { item: '=', content: '=' },
13         controller: ['$scope','$element',
14             function ($scope , $element) {
15                 if (!$scope.item.label) $scope.item.label = $scope.item.value;
16                 if ($scope.item.divider) {
17                     $element.style.borderTop = 'solid 1px';
18                 }
19
20                 $scope.setContent = function (v, a) {
21                     var replace_with = v;
22                     if (a) replace_with = a($scope,$element,$scope.item.value,$scope.$parent.$parent.content);
23                     $timeout(function(){
24                         $scope.$parent.$parent.$apply(function(){
25                             $scope.$parent.$parent.content = replace_with
26                         })
27                     }, 0);
28                     $($element).parent().css({display: 'none'});
29                 }
30             }
31         ]
32     }
33 }])
34
35 .directive("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
36     return {
37         restrict: 'E',
38         replace: true,
39         template: '<input '+
40                       'style="font-family: \'Lucida Console\', Monaco, monospace;" '+
41                       'ng-model="content" '+
42                       'size="{{content.length * 1.1}}" '+
43                       'maxlength="{{max}}" '+
44                       'class="" '+
45                       'type="text" '+
46                   '/>',
47         scope: {
48             field: '=',
49             onKeydown: '=',
50             subfield: '=',
51             content: '=',
52             contextItemContainer: '@',
53             max: '@',
54             itype: '@'
55         },
56         controller : ['$scope',
57             function ( $scope ) {
58
59 /* XXX Example, for testing.  We'll get this from the tag-table services for realz
60  *
61                 if (!$scope.contextItemContainer) {
62                     $scope.contextItemContainer = "default_context";
63                     $scope[$scope.contextItemContainer] = [
64                         { value: 'a' },
65                         { value: 'b' },
66                         { value: 'c' },
67                     ];
68                 }
69
70  *
71  */
72
73                 if ($scope.contextItemContainer)
74                     $scope.item_container = $scope[$scope.contextItemContainer];
75
76                 $scope.showContext = function (event) {
77                     if ($scope.context_menu_element) {
78                         console.log('Reshowing context menu...');
79                         $($scope.context_menu_element).css({ display: 'block', top: event.pageY, left: event.pageX });
80                         $('body').on('click.context_menu',function() {
81                             $($scope.context_menu_element).css('display','none');
82                             $('body').off('click.context_menu');
83                         });
84                         return false;
85                     }
86
87                     if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
88                         console.log('Showing context menu...');
89
90                         var tmpl = 
91                             '<ul class="dropdown-menu" role="menu">'+
92                                 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
93                             '</ul>';
94             
95                         var tnode = angular.element(tmpl);
96                         $document.find('body').append(tnode);
97
98                         $(tnode).css({
99                             display: 'block',
100                             top: event.pageY,
101                             left: event.pageX
102                         });
103
104                         $scope.context_menu_element = tnode;
105
106                         $timeout(function() {
107                             var e = $compile(tnode)($scope);
108                         }, 0);
109
110
111                         $('body').on('click.context_menu',function() {
112                             $(tnode).css('display','none');
113                             $('body').off('click.context_menu');
114                         });
115
116                         return false;
117                     }
118             
119                     return true;
120                 }
121
122             }
123         ],
124         link: function (scope, element, attrs) {
125
126             if (scope.onKeydown) element.bind('keydown', scope.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 class="marcsfcode"><label class="marcedit"'+
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" '+
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 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 ng-repeat="field in controlfields" field="field" on-keydown="onKeydown"/></div>'+
274                     '<div><eg-marc-edit-datafield ng-repeat="field in datafields" field="field" on-keydown="onKeydown"/></div>'+
275                   '</div>'+
276                   '<button class="btn btn-default" type="submit">Save</button>'+
277                   '</form>'+
278                   '<button class="btn btn-default" ng-click="seeBreaker()">Breaker</button>',
279         restrict: 'E',
280         replace: false,
281         scope: { recordId : '=', maxUndo : '@' },
282         controller : ['$timeout','$scope','egCore',
283             function ( $timeout , $scope , egCore ) {
284
285                 $scope.max_undo = $scope.maxUndo || 100;
286                 $scope.record_undo_stack = [];
287                 $scope.record_redo_stack = [];
288                 $scope.in_undo = false;
289                 $scope.in_redo = false;
290                 $scope.record = new MARC.Record();
291                 $scope.save_stack_depth = 0;
292                 $scope.controlfields = [];
293                 $scope.datafields = [];
294
295                 $scope.onKeydown = function (event) {
296                     var event_return = true;
297
298                     if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
299                         event_return = $scope.processRedo();
300                     } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
301                         event_return = $scope.processUndo();
302                     } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
303                         $scope.current_event_target = $(event.target).attr('id');
304                         if ($scope.current_event_target) {
305                             $scope.current_event_target_cursor_pos =
306                                 event.target.selectionDirection=='backward' ?
307                                     event.target.selectionStart :
308                                     event.target.selectionEnd;
309                         }
310                     }
311
312                     return event_return;
313                 };
314
315                 function setCaret() {
316                     if ($scope.current_event_target) {
317                         var element = $('#'+$scope.current_event_target).get(0);
318                         element.focus();
319                         element.setSelectionRange(
320                             $scope.current_event_target_cursor_pos,
321                             $scope.current_event_target_cursor_pos
322                         );
323                         $scope.current_event_target = null;
324                     }
325                 }
326
327                 function loadRecord() {
328                     return egCore.pcrud.retrieve(
329                         'bre', $scope.recordId
330                     ).then(function(rec) {
331                         $scope.in_redo = true;
332                         $scope.bre = rec;
333                         $scope.record = new MARC.Record({ marcxml : $scope.bre.marc() });
334                         $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
335                         $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
336                         $scope.save_stack_depth = $scope.record_undo_stack.length;
337                     }).then(setCaret);
338                 }
339
340                 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
341                     if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
342                         $scope.record_undo_stack.push({
343                             breaker: oldVal,
344                             target: $scope.current_event_target,
345                             pos: $scope.current_event_target_cursor_pos
346                         });
347
348                         if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
349                             console.log('should get a listener... does not');
350                             $('body').on('beforeunload', function(){
351                                 return 'There is unsaved data in this record.'
352                             });
353                         } else {
354                             $('body').off('beforeunload');
355                         }
356                     }
357
358                     if ($scope.record_undo_stack.length > $scope.max_undo)
359                         $scope.record_undo_stack.shift();
360
361                     console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
362                     $scope.in_redo = false;
363                     $scope.in_undo = false;
364                 });
365
366                 $scope.processUndo = function () {
367                     if ($scope.record_undo_stack.length) {
368                         $scope.in_undo = true;
369
370                         var undo_item = $scope.record_undo_stack.pop();
371                         $scope.record_redo_stack.push(undo_item);
372
373                         $scope.record = new MARC.Record({ marcbreaker : undo_item.breaker });
374                         $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
375                         $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
376
377                         $scope.current_event_target = undo_item.target;
378                         $scope.current_event_target_cursor_pos = undo_item.pos;
379                         console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
380
381                         $timeout(function(){$scope.$digest()}).then(setCaret);
382                         return false;
383                     }
384
385                     return true;
386                 };
387
388                 $scope.processRedo = function () {
389                     if ($scope.record_redo_stack.length) {
390                         $scope.in_redo = true;
391
392                         var redo_item = $scope.record_redo_stack.pop();
393                         $scope.record_undo_stack.push(redo_item);
394
395                         $scope.record = new MARC.Record({ marcbreaker : redo_item.breaker });
396                         $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
397                         $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
398
399                         $scope.current_event_target = redo_item.target;
400                         $scope.current_event_target_cursor_pos = redo_item.pos;
401                         console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
402
403                         $timeout(function(){$scope.$digest()}).then(setCaret);
404                         return false;
405                     }
406
407                     return true;
408                 };
409
410                 $scope.saveRecord = function () {
411                     $scope.bre.marc($scope.record.toXmlString());
412                     return egCore.pcrud.update(
413                         $scope.bre
414                     ).then(loadRecord);
415                 };
416
417                 $scope.seeBreaker = function () {
418                     alert($scope.record.toBreaker());
419                 };
420
421                 $scope.$watch('recordId',
422                     function(newVal, oldVal) {
423                         if (newVal && newVal !== oldVal) {
424                             loadRecord();
425                         }
426                     }
427                 );
428
429                 if ($scope.recordId)
430                     loadRecord();
431
432             }
433         ]          
434     }
435 })
436
437 ;