LP#1292705 trim unneeded fields from IDL2js
authorBill Erickson <berick@esilibrary.com>
Fri, 14 Mar 2014 20:42:11 +0000 (16:42 -0400)
committerBen Shum <bshum@biblio.org>
Thu, 7 Aug 2014 22:41:25 +0000 (18:41 -0400)
Avoid sending fields in the web-published IDL (IDL2js) that have no
value, as the keys in the JSON take up a lot of space.  Also avoid
repeating fields (selector) and default values (e.g. label defaults
to name) where the client can reasonably apply the defaults locally.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Ben Shum <bshum@biblio.org>
Open-ILS/web/js/dojo/fieldmapper/Fieldmapper.js
Open-ILS/web/js/dojo/fieldmapper/IDL.js
Open-ILS/xsl/fm_IDL2js.xsl

index 006bd09..3ef0ad9 100644 (file)
@@ -112,9 +112,10 @@ if(!dojo._hasResource["fieldmapper.Fieldmapper"]){
                 if (fieldmapper.IDL && fieldmapper.IDL.loaded) {
                     this.Structure = fieldmapper.IDL.fmclasses[this.classname];
 
+                    var array_pos = 0;
                     for (f in fieldmapper.IDL.fmclasses[this.classname].fields) {
                         var field = fieldmapper.IDL.fmclasses[this.classname].fields[f];
-                        p = field.array_position;
+                        p = array_pos++;
                         this._fields.push( field.name );
                         this[field.name]=new Function('n', 'if(arguments.length==1)this.a['+p+']=n;return this.a['+p+'];');
                     }
index 46f0241..b2793f0 100644 (file)
@@ -20,8 +20,16 @@ if(!dojo._hasResource["fieldmapper.IDL"]) {
                     preload.push(c);
                     fieldmapper.IDL.fmclasses[c] = window._preload_fieldmapper_IDL[c];
 
+                    if (!fieldmapper.IDL.fmclasses[c].label) {
+                        fieldmapper.IDL.fmclasses[c].label = 
+                            fieldmapper.IDL.fmclasses[c].name;
+                    }
+
                     window.fmclasses[c] = [];
-                    dojo.forEach(fieldmapper.IDL.fmclasses[c].fields, function(obj){ window.fmclasses[c].push(obj.name); });
+                    dojo.forEach(fieldmapper.IDL.fmclasses[c].fields, function(obj){ 
+                        if (!obj.label) obj.label = obj.name;
+                        window.fmclasses[c].push(obj.name); 
+                    });
 
                     if (classlist && classlist.length)
                         classlist = dojo.filter(classlist, function(x){return x != c;});
index 1a857bf..ecc374f 100644 (file)
@@ -19,7 +19,7 @@ var _preload_fieldmapper_IDL = {<xsl:apply-templates select="idl:IDL"/>};
 for (var c in _preload_fieldmapper_IDL) {
     var x = _preload_fieldmapper_IDL[c]; x.field_map = {};
     var p = x.fields.length;
-    for (var n in {isnew:1,ischanged:1,isdeleted:1}) x.fields[p] = {name:n,type:'field',virtual:true,array_position:p++};
+    for (var n in {isnew:1,ischanged:1,isdeleted:1}) x.fields[p] = {name:n,virtual:true};
     for (var f in x.fields) x.field_map[x.fields[f].name] = x.fields[f];
 }
     </xsl:template>
@@ -36,13 +36,15 @@ for (var c in _preload_fieldmapper_IDL) {
         </xsl:choose>
     </xsl:template>
  
-    <xsl:template match="idl:class"><xsl:value-of select="@id"/>:{name:"<xsl:value-of select="@id"/>",label:"<xsl:call-template name='defaultValue'><xsl:with-param name='v' select="@reporter:label"/><xsl:with-param name='d' select="@id"/></xsl:call-template>",restrict_primary:"<xsl:value-of select="@oils_persist:restrict_primary"/>",virtual:<xsl:call-template name='trueFalse'><xsl:with-param name='tf' select="@oils_persist:virtual"/></xsl:call-template>,pkey:"<xsl:value-of select="idl:fields/@oils_persist:primary"/>",pkey_sequence:"<xsl:value-of select="idl:fields/@oils_persist:sequence"/>",<xsl:apply-templates select="idl:fields"/><xsl:apply-templates select="permacrud:permacrud"/>}</xsl:template>
+    <xsl:template match="idl:class"><xsl:value-of select="@id"/>:{name:"<xsl:value-of select="@id"/>",<xsl:if test="@reporter:label">label:"<xsl:value-of select="@reporter:label"/>",</xsl:if><xsl:if test="@oils_persist:restrict_primary">restrict_primary:"<xsl:value-of select="@oils_persist:restrict_primary"/>",</xsl:if><xsl:if test="@oils_persist:virtual = 'true'">virtual:true,</xsl:if><xsl:if test="idl:fields/@oils_persist:primary">pkey:"<xsl:value-of select="idl:fields/@oils_persist:primary"/>",</xsl:if><xsl:if test="idl:fields/@oils_persist:sequence">pkey_sequence:"<xsl:value-of select="idl:fields/@oils_persist:sequence"/>",</xsl:if><xsl:apply-templates select="idl:fields"/><xsl:apply-templates select="permacrud:permacrud"/>}</xsl:template>
  
     <xsl:template match="idl:fields">fields:[<xsl:for-each select="idl:field"><xsl:call-template name="printField"><xsl:with-param name='pos' select="position()"/></xsl:call-template><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>]</xsl:template>
 
     <xsl:template match="permacrud:permacrud">,permacrud:{<xsl:for-each select="permacrud:actions/*"><xsl:if test="name() = 'delete'">"</xsl:if><xsl:value-of select="name()"/><xsl:if test="name() = 'delete'">"</xsl:if>:{<xsl:call-template name='pcrudPerms'/>}<xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>}</xsl:template>
  
-<xsl:template name='printField'><xsl:param name="pos"/>{name:"<xsl:value-of select="@name"/>",label:"<xsl:call-template name='defaultValue'><xsl:with-param name='v' select="@reporter:label"/><xsl:with-param name='d' select="@name"/></xsl:call-template>",datatype:"<xsl:call-template name='defaultValue'><xsl:with-param name='v' select="@reporter:datatype"/><xsl:with-param name='d' select="string('text')"/></xsl:call-template>",primitive:"<xsl:value-of select="@oils_persist:primitive"/>",selector:"<xsl:value-of select="@reporter:selector"/>",array_position:"<xsl:value-of select="$pos - 1"/>",<xsl:call-template name='fieldOrLink'><xsl:with-param name='f' select="."/></xsl:call-template>,virtual:<xsl:call-template name='trueFalse'><xsl:with-param name='tf' select="@oils_persist:virtual"/></xsl:call-template>,required:<xsl:call-template name='trueFalse'><xsl:with-param name='tf' select="@oils_obj:required"/></xsl:call-template>,i18n:<xsl:call-template name='trueFalse'><xsl:with-param name='tf' select="@oils_persist:i18n"/></xsl:call-template>}</xsl:template>
+<!-- to simplify the logic, the first and last field are assumed to
+     have values (and practically always will) -->
+<xsl:template name='printField'>{name:"<xsl:value-of select="@name"/>",<xsl:if test="@reporter:label != ''">label:"<xsl:value-of select="@reporter:label"/>",</xsl:if><xsl:if test="@oils_persist:primitive = 'true'">primitive:true,</xsl:if><xsl:if test="@reporter:selector != ''">selector:"<xsl:value-of select="@reporter:selector"/>",</xsl:if><xsl:if test="@oils_persist:virtual = 'true'">virtual:true,</xsl:if><xsl:if test="@oils_obj:required = 'true'">required:true,</xsl:if><xsl:if test="@oils_persist:i18n = 'true'">i18n:true,</xsl:if><xsl:call-template name='fieldOrLink'><xsl:with-param name='f' select="."/></xsl:call-template>datatype:"<xsl:call-template name='defaultValue'><xsl:with-param name='v' select="@reporter:datatype"/><xsl:with-param name='d' select="string('text')"/></xsl:call-template>"}</xsl:template>
  
 <xsl:template name="pcrudPerms">
     <xsl:if test="@permission">perms:[<xsl:for-each select="str:split(@permission,' ')">'<xsl:value-of select="./text()"/>'<xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>]</xsl:if>
@@ -50,10 +52,7 @@ for (var c in _preload_fieldmapper_IDL) {
 
 <xsl:template name="fieldOrLink">
     <xsl:param name="f"/>
-    <xsl:choose>
-        <xsl:when test="$f/../../idl:links/idl:link[@field=$f/@name]">type:"link",<xsl:apply-templates select="$f/../../idl:links/idl:link[@field=$f/@name]"></xsl:apply-templates></xsl:when>
-        <xsl:otherwise>type:"field"</xsl:otherwise>
-    </xsl:choose>
+    <xsl:if test="$f/../../idl:links/idl:link[@field=$f/@name]">type:"link",<xsl:apply-templates select="$f/../../idl:links/idl:link[@field=$f/@name]"></xsl:apply-templates>,</xsl:if>
 </xsl:template>
 
 <xsl:template match="idl:link">key:"<xsl:value-of select="@key"/>","class":"<xsl:value-of select="@class"/>",reltype:"<xsl:value-of select="@reltype"/>"</xsl:template>
@@ -74,6 +73,5 @@ for (var c in _preload_fieldmapper_IDL) {
         <xsl:otherwise><xsl:value-of select="$v"/></xsl:otherwise>
     </xsl:choose>
 </xsl:template>
+
 </xsl:stylesheet>