fieldmapper generator code
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 12 Aug 2005 20:53:02 +0000 (20:53 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 12 Aug 2005 20:53:02 +0000 (20:53 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1652 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/javascript/lib/js/opac/fmgen.js [new file with mode: 0644]

diff --git a/Open-ILS/src/javascript/lib/js/opac/fmgen.js b/Open-ILS/src/javascript/lib/js/opac/fmgen.js
new file mode 100644 (file)
index 0000000..3f2526e
--- /dev/null
@@ -0,0 +1,64 @@
+/* generate fieldmapper javascript classes.  This expects a global variable
+       called 'fmclasses' to be fleshed with the classes we need to build */
+
+function Fieldmapper() {}
+
+var errorstr = "Attempt to build fieldmapper object with non-array";
+
+Fieldmapper.prototype.clone = function() {
+       var obj = new this.constructor();
+
+       for( var i in this.a ) {
+               var thing = this.a[i];
+               if(thing == null) continue;
+
+               if( thing._isfieldmapper ) {
+                       obj.a[i] = thing.clone();
+               } else {
+
+                       if(instanceOf(thing, Array)) {
+                               obj.a[i] = new Array();
+
+                               for( var j in thing ) {
+
+                                       if( thing[j]._isfieldmapper )
+                                               obj.a[i][j] = thing[j].clone();
+                                       else
+                                               obj.a[i][j] = thing[j];
+                               }
+                       } else {
+                               obj.a[i] = thing;
+                       }
+               }
+       }
+       return obj;
+}
+Fieldmapper.prototype.isnew = function(n) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; }
+Fieldmapper.prototype.ischanged = function(n) { if(arguments.length == 1) this.a[1] =n; return this.a[1]; }
+Fieldmapper.prototype.isdeleted = function(a) { if(arguments.length == 1) this.a[0] =n; return this.a[0]; }
+function FMEX(message) { this.message = message; }
+FMEX.toString = function() { return "FieldmapperException: " + this.message + "\n"; }
+
+var string = "";
+for( var cl in fmclasses ) {
+       string += cl + ".prototype = new Fieldmapper(); " + 
+                                               cl + ".prototype.constructor = " + cl + ";" +
+                                               cl + ".baseClass = Fieldmapper.constructor;" +
+                                               "function " + cl + "(a) { " +
+                                                       "this.classname = \"" + cl + "\";" +
+                                                       "this._isfieldmapper = true;" +
+                                                       "if(a) { if(a.constructor == Array) this.a = a; else throw new FMEX(errorstr);} else this.a = []}"; 
+
+       string += cl + "._isfieldmapper=true;";
+
+       for( var pos in fmclasses[cl] ) {
+               var p = parseInt(pos) + 3;
+               var field = fmclasses[cl][pos];
+               string += cl + ".prototype." + field + 
+                       "=function(n){if(arguments.length == 1)this.a[" + 
+                       p + "]=n;return this.a[" + p + "];};";
+       }
+}
+eval(string);
+
+