$scope.onKeydown = function (event) {
var event_return = true;
+ console.log(
+ 'keydown: which='+event.which+
+ ', ctrlKey='+event.ctrlKey+
+ ', shiftKey='+event.shiftKey+
+ ', altKey='+event.altKey+
+ ', metaKey='+event.altKey
+ );
+
if (event.which == 89 && event.ctrlKey) { // ctrl+y, redo
event_return = $scope.processRedo();
} else if (event.which == 90 && event.ctrlKey) { // ctrl+z, undo
event_return = false;
+ } else if (event.which == 117 && event.shiftKey) { // shift + F6, insert 006
+ event.data.scope.field.record.insertOrderedFields(
+ new MARC.Field({
+ tag : '006',
+ data : ' '
+ })
+ );
+
+ $scope.force_render = true;
+ $timeout(function(){$scope.$digest()}).then(setCaret);
+
+ event_return = false;
+
+ } else if (event.which == 118 && event.shiftKey) { // shift + F7, insert 007
+ event.data.scope.field.record.insertOrderedFields(
+ new MARC.Field({
+ tag : '007',
+ data : ' '
+ })
+ );
+
+ $scope.force_render = true;
+ $timeout(function(){$scope.$digest()}).then(setCaret);
+
+ event_return = false;
+
+ } else if (event.which == 119 && event.shiftKey) { // shift + F8, insert/replace 008
+ var new_008_data = event.data.scope.field.record.generate008();
+
+
+ var old_008s = event.data.scope.field.record.field('008',true);
+ old_008s.forEach(function(o) {
+ var domnode = $('#r'+o.record.subfield('901','c')[1] + 'f' + o.position);
+ domnode.scope().$destroy();
+ domnode.remove();
+ event.data.scope.field.record.deleteFields(o);
+ });
+
+ event.data.scope.field.record.insertOrderedFields(
+ new MARC.Field({
+ tag : '008',
+ data : new_008_data
+ })
+ );
+
+ $scope.force_render = true;
+ $timeout(function(){$scope.$digest()}).then(setCaret);
+
+ event_return = false;
+
} else if (event.which == 13 && event.ctrlKey) { // ctrl+enter, insert datafield
var element = $(event.target);
event.data.scope.field.record.insertFieldsAfter(
event.data.scope.field,
new MARC.Field({
- record : event.data.scope.field.record,
tag : '999',
- subfields : [[' ','',0]],
- position : new_field
+ subfields : [[' ','',0]]
})
);
Record : function(kwargs) {
if (!kwargs) kwargs = {};
+ this.generate008 = function () {
+ var f;
+ var s;
+ var orig008 = ' ';
+ var now = new Date();
+ var y = now.getUTCFullYear().toString().substr(2,2);
+ var m = now.getUTCMonth() + 1;
+ if (m < 10) m = '0' + m;
+ var d = now.getUTCDate();
+ if (d < 10) d = '0' + d;
+
+
+ if (f = this.field('008',true)[0]) {
+ orig008 = f.data;
+ }
+
+ /* lang code from 041a */
+ var lang = orig008.substr(35, 3);
+
+ if (f = this.field('041',true)[0]) {
+ if (s = f.subfield('a',true)[0]) {
+ if(s[1]) lang = s[1];
+ }
+ }
+
+ /* country code from 044a */
+ var country = orig008.substr(15, 3);
+ if (f = this.field('044',true)[0]) {
+ if (s = f.subfield('a',true)[0]) {
+ if(s[1]) country = s[1];
+ }
+ }
+ while (country.length < 3) country = country + ' ';
+ if (country.length > 3) country = country.substr(0,3);
+
+ /* date1 from 260c */
+ var date1 = now.getUTCFullYear().toString();
+ if (f = this.field('260',true)[0]) {
+ if (s = f.subfield('c',true)[0]) {
+ if (s[1]) {
+ var tmpd = s[1];
+ tmpd = tmpd.replace(/[^0-9]/g, '');
+ if (tmpd.match(/^\d\d\d\d/)) {
+ date1 = tmpd.substr(0, 4);
+ }
+ }
+ }
+ }
+
+ var date2 = orig008.substr(11, 4);
+ var datetype = orig008.substr(6, 1);
+ var modded = orig008.substr(38, 1);
+ var catsrc = orig008.substr(39, 1);
+
+ return '' + y + m + d + datetype + date1 + date2 + country + ' ' + lang + modded + catsrc;
+
+ }
+
this.title = function () { return this.subfield('245','a')[1] }
- this.field = function (spec) {
+ this.field = function (spec, wantarray) {
var list = this.fields.filter(function (f) {
if (f.tag.match(spec)) return true;
return false;
});
- if (list.length == 1) return list[0];
+ if (!wantarray && list.length == 1) return list[0];
return list;
}
this.subfield = function (spec, code) {
- var f = this.field(spec);
- if (Array.isArray(f)) {
- if (!f.length) return f;
- f = f[0];
- }
- return f.subfield(code)
+ var f = this.field(spec, true)[0];
+ if (f) return f.subfield(code)
+ return null;
}
this.appendFields = function () {
var me = this;
- Array.prototype.slice.call(arguments).forEach( function (f) { me.fields.push( f ) } );
+ Array.prototype.slice.call(arguments).forEach( function (f) { f.position = me.fields.length; me.fields.push( f ) } );
}
this.deleteField = function (f) { return this.deleteFields(f) },
var args = Array.prototype.slice.call(arguments);
args.splice(0,1);
var me = this;
- var done = false;
for (var j = 0; j < this.fields.length; j++) {
- if (!done && target === this.fields[j]) {
+ if (target === this.fields[j]) {
args.forEach( function (f) {
f.record = me;
- f.position = j++;
- me.fields.splice(j,0,f);
+ me.fields.splice(j++,0,f);
});
- j++;
- done = true;
+ break;
}
- if (done && this.fields[j]) this.fields[j].position += args.length - 1;
+ }
+ for (var j = 0; j < this.fields.length; j++) {
+ this.fields[j].position = j;
}
}
var args = Array.prototype.slice.call(arguments);
args.splice(0,1);
var me = this;
- var done = false;
for (var j = 0; j < this.fields.length; j++) {
- if (!done && target === this.fields[j]) {
+ if (target === this.fields[j]) {
args.forEach( function (f) {
f.record = me;
- f.position = ++j;
- me.fields.splice(j,0,f);
+ me.fields.splice(++j,0,f);
});
- j++;
- done = true;
+ break;
}
- if (done && this.fields[j]) this.fields[j].position += args.length - 1;
+ }
+ for (var j = 0; j < this.fields.length; j++) {
+ this.fields[j].position = j;
}
}
this.deleteFields = function () {
var me = this;
var counter = 0;
- var done = false;
for ( var i in arguments ) {
var f = arguments[i];
for (var j = 0; j < me.fields.length; j++) {
me.fields[j].record = null;
me.fields.splice(j,1);
counter++
- j++;
- done = true;
+ break;
}
- if (done) this.fields[j].position -= 1;
}
}
+ for (var j = 0; j < this.fields.length; j++) {
+ this.fields[j].position = j;
+ }
return counter;
}
Field : function (kwargs) {
if (!kwargs) kwargs = {};
- this.subfield = function (code) {
+ this.subfield = function (code, wantarray) {
var list = this.subfields.filter( function (s) {
if (s[0] == code) return true; return false;
});
- if (list.length == 1) return list[0];
+ if (!wantarray && list.length == 1) return list[0];
return list;
}