5 angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
7 .directive("egContextMenuItem", ['$timeout',function ($timeout) {
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';
20 $scope.setContent = function (v, a) {
22 if (a) replace_with = a($scope,$element,$scope.item.value,$scope.$parent.$parent.content);
24 $scope.$parent.$parent.$apply(function(){
25 $scope.$parent.$parent.content = replace_with
28 $($element).parent().css({display: 'none'});
35 .directive("egMarcEditEditable", ['$timeout', '$compile', '$document', function ($timeout, $compile, $document) {
40 'style="font-family: \'Lucida Console\', Monaco, monospace;" '+
41 'ng-model="content" '+
42 'size="{{content.length * 1.1}}" '+
43 'maxlength="{{max}}" '+
52 contextItemContainer: '@',
56 controller : ['$scope',
59 /* XXX Example, for testing. We'll get this from the tag-table services for realz
61 if (!$scope.contextItemContainer) {
62 $scope.contextItemContainer = "default_context";
63 $scope[$scope.contextItemContainer] = [
73 if ($scope.contextItemContainer)
74 $scope.item_container = $scope[$scope.contextItemContainer];
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');
87 if (angular.isArray($scope.item_container)) { // we have a list of values or transforms
88 console.log('Showing context menu...');
91 '<ul class="dropdown-menu" role="menu">'+
92 '<eg-context-menu-item ng-repeat="item in item_container" item="item" content="content"/>'+
95 var tnode = angular.element(tmpl);
96 $document.find('body').append(tnode);
104 $scope.context_menu_element = tnode;
106 $timeout(function() {
107 var e = $compile(tnode)($scope);
111 $('body').on('click.context_menu',function() {
112 $(tnode).css('display','none');
113 $('body').off('click.context_menu');
124 link: function (scope, element, attrs) {
126 if (scope.onKeydown) element.bind('keydown', {scope : scope}, scope.onKeydown);
128 element.bind('change', function (e) { element.size = scope.max || parseInt(scope.content.length * 1.1) });
130 if (scope.contextItemContainer && angular.isArray(scope[scope.contextItemContainer]))
131 element.bind('contextmenu', scope.showContext);
136 .directive("egMarcEditSubfield", function () {
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 '+
145 'class="marcedit marcsf marcsfcode" '+
147 'subfield="subfield" '+
148 'content="subfield[0]" '+
150 'on-keydown="onKeydown" '+
151 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}s{{subfield[2]}}code" '+
153 '<span><eg-marc-edit-editable '+
155 'class="marcedit marcsf marcsfvalue" '+
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" '+
163 scope: { field: "=", subfield: "=", onKeydown: '=' },
168 .directive("egMarcEditInd", function () {
172 template: '<span><eg-marc-edit-editable '+
174 'class="marcedit marcind" '+
178 'on-keydown="onKeydown" '+
179 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}i{{indNumber}}"'+
181 scope: { ind : '=', field: '=', onKeydown: '=', indNumber: '@' },
186 .directive("egMarcEditTag", function () {
190 template: '<span><eg-marc-edit-editable '+
192 'class="marcedit marctag" '+
196 'on-keydown="onKeydown" '+
197 'id="r{{field.record.subfield(\'901\',\'c\')[1]}}f{{field.position}}tag"'+
199 scope: { tag : '=', field: '=', onKeydown: '=' },
204 .directive("egMarcEditDatafield", function () {
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>'+
214 scope: { field: "=", onKeydown: '=' }
218 .directive("egMarcEditControlfield", function () {
223 '<span><eg-marc-edit-tag field="field" tag="field.tag" on-keydown="onKeydown"/></span>'+
224 '<span><eg-marc-edit-editable '+
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"'+
233 scope: { field: "=", onKeydown: '=' }
237 .directive("egMarcEditLeader", function () {
242 '<span><eg-marc-edit-editable '+
243 'class="marcedit marctag" '+
245 'on-keydown="onKeydown" '+
247 'disabled="disabled"'+
249 '<span><eg-marc-edit-editable '+
250 'class="marcedit marcdata" '+
252 'max="{{record.leader.length}}" '+
253 'content="record.leader" '+
254 'id="r{{record.subfield(\'901\',\'c\')[1]}}leaderdata" '+
255 'on-keydown="onKeydown"'+
258 controller : ['$scope',
259 function ( $scope ) {
263 scope: { record: "=", onKeydown: '=' }
267 /// TODO: fixed field editor and such
268 .directive("egMarcEditRecord", function () {
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}}"'+
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}}"'+
284 '<button class="btn btn-default" type="submit">Save</button>'+
286 '<button class="btn btn-default" ng-click="seeBreaker()">Breaker</button>',
289 scope: { recordId : '=', maxUndo : '@' },
290 link: function (scope, element, attrs) {
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;
304 controller : ['$timeout','$scope','egCore',
305 function ( $timeout , $scope , egCore ) {
307 $scope.max_undo = $scope.maxUndo || 100;
308 $scope.record_undo_stack = [];
309 $scope.record_redo_stack = [];
310 $scope.in_undo = false;
311 $scope.in_redo = false;
312 $scope.record = new MARC.Record();
313 $scope.save_stack_depth = 0;
314 $scope.controlfields = [];
315 $scope.datafields = [];
317 $scope.onKeydown = function (event) {
318 var event_return = true;
320 if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
321 event_return = $scope.processRedo();
322 } else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
323 event_return = $scope.processUndo();
324 } else if ((event.which == 68 || event.which == 73) && event.ctrlKey) { // ctrl+d or ctrl+i, insert subfield
326 var element = $(event.target);
327 var new_sf, index_sf, move_data;
329 if (element.hasClass('marcsfvalue')) {
330 index_sf = event.data.scope.subfield[2];
331 new_sf = index_sf + 1;
333 var start = event.target.selectionStart;
334 var end = event.target.selectionEnd - event.target.selectionStart ?
335 event.target.selectionEnd :
336 event.target.value.length;
338 move_data = event.target.value.substring(start,end);
340 } else if (element.hasClass('marcsfcode')) {
341 index_sf = event.data.scope.subfield[2];
342 new_sf = index_sf + 1;
343 } else if (element.hasClass('marctag') || element.hasClass('marcind')) {
348 $scope.current_event_target = 'r' + $scope.recordId +
349 'f' + event.data.scope.field.position +
350 's' + new_sf + 'code';
352 event.data.scope.field.subfields.forEach(function(sf) {
353 if (sf[2] >= new_sf) sf[2]++;
354 if (sf[2] == index_sf) sf[1] = event.target.value.substring(0,start) + event.target.value.substring(end);
356 event.data.scope.field.subfields.splice(
359 [' ', move_data, new_sf ]
362 $scope.current_event_target_cursor_pos = 0;
363 $scope.current_event_target_cursor_pos_end = 1;
365 $timeout(function(){$scope.$digest()}).then(setCaret);
367 event_return = false;
369 } else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
371 var element = $(event.target);
373 var index_field = event.data.scope.field.position;
374 var new_field = index_field + 1;
376 event.data.scope.field.record.insertFieldsAfter(
377 event.data.scope.field,
379 record : event.data.scope.field.record,
381 subfields : [[' ','',0]],
386 $scope.current_event_target = 'r' + $scope.recordId +
387 'f' + new_field + 'tag';
389 $scope.current_event_target_cursor_pos = 0;
390 $scope.current_event_target_cursor_pos_end = 3;
391 $scope.force_render = true;
393 $timeout(function(){$scope.$digest()}).then(setCaret);
395 event_return = false;
397 } else if (event.which == 46 && event.ctrlKey) { // ctrl+del, remove field
399 var del_field = event.data.scope.field.position;
401 var domnode = $('#r'+event.data.scope.field.record.subfield('901','c')[1] + 'f' + del_field);
403 event.data.scope.field.record.deleteFields(
404 event.data.scope.field
407 domnode.scope().$destroy();
410 $scope.current_event_target = 'r' + $scope.recordId +
411 'f' + del_field + 'tag';
413 $scope.current_event_target_cursor_pos = 0;
414 $scope.current_event_target_cursor_pos_end = 0
415 $scope.force_render = true;
417 $timeout(function(){$scope.$digest()}).then(setCaret);
419 event_return = false;
421 } else if (event.which == 46 && event.shiftKey && $(event.target).hasClass('marcsf')) { // shift+del, remove subfield
423 var sf = event.data.scope.subfield[2] - 1;
424 if (sf == -1) sf = 0;
426 event.data.scope.field.deleteExactSubfields(
427 event.data.scope.subfield
430 if (!event.data.scope.field.subfields[sf]) {
431 $scope.current_event_target = 'r' + $scope.recordId +
432 'f' + event.data.scope.field.position +
435 $scope.current_event_target = 'r' + $scope.recordId +
436 'f' + event.data.scope.field.position +
440 $scope.current_event_target_cursor_pos = 0;
441 $scope.current_event_target_cursor_pos_end = 0;
442 $scope.force_render = true;
444 $timeout(function(){$scope.$digest()}).then(setCaret);
446 event_return = false;
448 } else { // Assumes only marc editor elements have IDs that can trigger this event handler.
449 $scope.current_event_target = $(event.target).attr('id');
450 if ($scope.current_event_target) {
451 $scope.current_event_target_cursor_pos =
452 event.target.selectionDirection=='backward' ?
453 event.target.selectionStart :
454 event.target.selectionEnd;
461 function setCaret() {
462 if ($scope.current_event_target) {
463 if (!$scope.current_event_target_cursor_pos_end)
464 $scope.current_event_target_cursor_pos_end = $scope.current_event_target_cursor_pos
466 var element = $('#'+$scope.current_event_target).get(0);
469 element.setSelectionRange(
470 $scope.current_event_target_cursor_pos,
471 $scope.current_event_target_cursor_pos_end
473 $scope.current_event_target = null;
478 function loadRecord() {
479 return egCore.pcrud.retrieve(
480 'bre', $scope.recordId
481 ).then(function(rec) {
482 $scope.in_redo = true;
484 $scope.record = new MARC.Record({ marcxml : $scope.bre.marc() });
485 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
486 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
487 $scope.save_stack_depth = $scope.record_undo_stack.length;
491 $scope.$watch('record.toBreaker()', function (newVal, oldVal) {
492 if (!$scope.in_undo && !$scope.in_redo && oldVal != newVal) {
493 $scope.record_undo_stack.push({
495 target: $scope.current_event_target,
496 pos: $scope.current_event_target_cursor_pos
499 if ($scope.force_render) {
500 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
501 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
502 $scope.force_render = false;
505 if ($scope.record_undo_stack.length != $scope.save_stack_depth) {
506 console.log('should get a listener... does not');
507 $('body').on('beforeunload', function(){
508 return 'There is unsaved data in this record.'
511 $('body').off('beforeunload');
515 if ($scope.record_undo_stack.length > $scope.max_undo)
516 $scope.record_undo_stack.shift();
518 console.log('undo stack is ' + $scope.record_undo_stack.length + ' deep');
519 $scope.in_redo = false;
520 $scope.in_undo = false;
523 $scope.processUndo = function () {
524 if ($scope.record_undo_stack.length) {
525 $scope.in_undo = true;
527 var undo_item = $scope.record_undo_stack.pop();
528 $scope.record_redo_stack.push(undo_item);
530 $scope.record = new MARC.Record({ marcbreaker : undo_item.breaker });
531 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
532 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
534 $scope.current_event_target = undo_item.target;
535 $scope.current_event_target_cursor_pos = undo_item.pos;
536 console.log('Undo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
538 $timeout(function(){$scope.$digest()}).then(setCaret);
545 $scope.processRedo = function () {
546 if ($scope.record_redo_stack.length) {
547 $scope.in_redo = true;
549 var redo_item = $scope.record_redo_stack.pop();
550 $scope.record_undo_stack.push(redo_item);
552 $scope.record = new MARC.Record({ marcbreaker : redo_item.breaker });
553 $scope.controlfields = $scope.record.fields.filter(function(f){ return f.isControlfield() });
554 $scope.datafields = $scope.record.fields.filter(function(f){ return !f.isControlfield() });
556 $scope.current_event_target = redo_item.target;
557 $scope.current_event_target_cursor_pos = redo_item.pos;
558 console.log('Redo targeting ' + $scope.current_event_target + ' position ' + $scope.current_event_target_cursor_pos);
560 $timeout(function(){$scope.$digest()}).then(setCaret);
567 $scope.saveRecord = function () {
568 $scope.bre.marc($scope.record.toXmlString());
569 return egCore.pcrud.update(
574 $scope.seeBreaker = function () {
575 alert($scope.record.toBreaker());
578 $scope.$watch('recordId',
579 function(newVal, oldVal) {
580 if (newVal && newVal !== oldVal) {