LP#1402797 insert 00[678], building 008 from record data
authorMike Rylander <mrylander@gmail.com>
Mon, 16 Feb 2015 17:08:40 +0000 (12:08 -0500)
committerBill Erickson <berickxx@gmail.com>
Wed, 25 Feb 2015 16:16:09 +0000 (11:16 -0500)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/web/js/ui/default/staff/cat/services/marcedit.js
Open-ILS/web/js/ui/default/staff/marcrecord.js

index 20a793d..a127aa6 100644 (file)
@@ -317,6 +317,14 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
                 $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
@@ -366,6 +374,56 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
 
                         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);
@@ -376,10 +434,8 @@ angular.module('egMarcMod', ['egCoreMod', 'ui.bootstrap'])
                         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]]
                             })
                         );
 
index 77f989b..6041ddb 100644 (file)
@@ -23,30 +23,85 @@ var MARC = {
     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) },
@@ -71,18 +126,17 @@ var MARC = {
             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;
             }
         }
 
@@ -90,25 +144,23 @@ var MARC = {
             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++) {
@@ -116,12 +168,13 @@ var MARC = {
                         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;
         }
 
@@ -494,11 +547,11 @@ var MARC = {
     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;
         }