SVGHMI : still behave, even if important things are missing in SVG widget definitions (ex: needle missing for Meter widget) svghmi
authorEdouard Tisserant
Thu, 13 Feb 2020 09:43:35 +0100
branchsvghmi
changeset 2836 ce6cecdb7320
parent 2835 bc666f020ab3
child 2837 53d9eb6a7498
SVGHMI : still behave, even if important things are missing in SVG widget definitions (ex: needle missing for Meter widget)
svghmi/gen_index_xhtml.xslt
svghmi/gen_index_xhtml.ysl2
svghmi/svghmi.js
--- a/svghmi/gen_index_xhtml.xslt	Thu Feb 13 09:41:43 2020 +0100
+++ b/svghmi/gen_index_xhtml.xslt	Thu Feb 13 09:43:35 2020 +0100
@@ -386,6 +386,10 @@
 </xsl:text>
     <xsl:text>        }*/
 </xsl:text>
+    <xsl:text>    } catch(err) {
+</xsl:text>
+    <xsl:text>        console.log(err);
+</xsl:text>
     <xsl:text>    }
 </xsl:text>
     <xsl:text>}
@@ -434,9 +438,7 @@
 </xsl:text>
     <xsl:text>            } catch(err) {
 </xsl:text>
-    <xsl:text>                console.log("Widget initialization error : "+err.message);
-</xsl:text>
-    <xsl:text>
+    <xsl:text>                console.log(err);
 </xsl:text>
     <xsl:text>            }
 </xsl:text>
@@ -929,19 +931,25 @@
     <xsl:for-each select="str:split($labels)">
       <xsl:variable name="name" select="."/>
       <xsl:variable name="elt_id" select="$hmi_element//*[@inkscape:label=$name][1]/@id"/>
-      <xsl:if test="$mandatory='yes' and not($elt_id)">
-        <xsl:message terminate="no">
-          <xsl:value-of select="$widget_type"/>
-          <xsl:text> widget must have a </xsl:text>
+      <xsl:choose>
+        <xsl:when test="not($elt_id)">
+          <xsl:if test="$mandatory='yes'">
+            <xsl:message terminate="no">
+              <xsl:value-of select="$widget_type"/>
+              <xsl:text> widget must have a </xsl:text>
+              <xsl:value-of select="$name"/>
+              <xsl:text> element</xsl:text>
+            </xsl:message>
+          </xsl:if>
+        </xsl:when>
+        <xsl:otherwise>
           <xsl:value-of select="$name"/>
-          <xsl:text> element</xsl:text>
-        </xsl:message>
-      </xsl:if>
-      <xsl:value-of select="$name"/>
-      <xsl:text>_elt: document.getElementById("</xsl:text>
-      <xsl:value-of select="$elt_id"/>
-      <xsl:text>"),
-</xsl:text>
+          <xsl:text>_elt: document.getElementById("</xsl:text>
+          <xsl:value-of select="$elt_id"/>
+          <xsl:text>"),
+</xsl:text>
+        </xsl:otherwise>
+      </xsl:choose>
     </xsl:for-each>
   </xsl:template>
   <xsl:template mode="widget_defs" match="widget[@type='Display']">
@@ -1016,18 +1024,26 @@
   </func:function>
   <xsl:template mode="widget_defs" match="widget[@type='Input']">
     <xsl:param name="hmi_element"/>
-    <xsl:text>frequency: 5,
-</xsl:text>
-    <xsl:call-template name="defs_by_labels">
-      <xsl:with-param name="hmi_element" select="$hmi_element"/>
-      <xsl:with-param name="labels">
-        <xsl:text>value</xsl:text>
-      </xsl:with-param>
-    </xsl:call-template>
+    <xsl:variable name="value_elt">
+      <xsl:call-template name="defs_by_labels">
+        <xsl:with-param name="hmi_element" select="$hmi_element"/>
+        <xsl:with-param name="labels">
+          <xsl:text>value</xsl:text>
+        </xsl:with-param>
+        <xsl:with-param name="mandatory" select="'no'"/>
+      </xsl:call-template>
+    </xsl:variable>
+    <xsl:value-of select="$value_elt"/>
+    <xsl:if test="$value_elt">
+      <xsl:text>frequency: 5,
+</xsl:text>
+    </xsl:if>
     <xsl:text>dispatch: function(value) {
 </xsl:text>
-    <xsl:text>    this.value_elt.textContent = String(value);
-</xsl:text>
+    <xsl:if test="$value_elt">
+      <xsl:text>    this.value_elt.textContent = String(value);
+</xsl:text>
+    </xsl:if>
     <xsl:text>},
 </xsl:text>
     <xsl:variable name="edit_elt_id" select="$hmi_element/*[@inkscape:label='edit'][1]/@id"/>
--- a/svghmi/gen_index_xhtml.ysl2	Thu Feb 13 09:41:43 2020 +0100
+++ b/svghmi/gen_index_xhtml.ysl2	Thu Feb 13 09:43:35 2020 +0100
@@ -8,6 +8,12 @@
     with "labels"{text *ptr};
 };
 
+in xsl decl optional_labels(*ptr, name="defs_by_labels") alias call-template {
+    with "hmi_element", "$hmi_element";
+    with "labels"{text *ptr};
+    with "mandatory","'no'";
+};
+
 istylesheet
             /* From Inkscape */
             xmlns:dc="http://purl.org/dc/elements/1.1/"
@@ -337,9 +343,18 @@
         foreach "str:split($labels)" {
             const "name",".";
             const "elt_id","$hmi_element//*[@inkscape:label=$name][1]/@id";
-            if "$mandatory='yes' and not($elt_id)"
-                warning > «$widget_type» widget must have a «$name» element
-            | «$name»_elt: document.getElementById("«$elt_id»"),
+            choose {
+                when "not($elt_id)" {
+                    if "$mandatory='yes'" {
+                        // TODO FIXME error > «$widget_type» widget must have a «$name» element
+                        warning > «$widget_type» widget must have a «$name» element
+                    }
+                    // otherwise produce nothing 
+                }
+                otherwise {
+                    | «$name»_elt: document.getElementById("«$elt_id»"),
+                }
+            }
         }
     }
 
@@ -397,9 +412,14 @@
 
     template "widget[@type='Input']", mode="widget_defs" {
         param "hmi_element";
-        | frequency: 5,
-        labels("value");
+        const "value_elt" {
+            optional_labels("value");
+        }
+        value "$value_elt";
+        if "$value_elt"
+            | frequency: 5,
         | dispatch: function(value) {
+        if "$value_elt"
         |     this.value_elt.textContent = String(value);
         | },
         const "edit_elt_id","$hmi_element/*[@inkscape:label='edit'][1]/@id";
--- a/svghmi/svghmi.js	Thu Feb 13 09:41:43 2020 +0100
+++ b/svghmi/svghmi.js	Thu Feb 13 09:43:35 2020 +0100
@@ -14,6 +14,8 @@
         /*else {
             throw new Error("Dunno how to dispatch to widget at index = " + index);
         }*/
+    } catch(err) {
+        console.log(err);
     }
 }
 
@@ -38,8 +40,7 @@
             try {
                 init.call(widget);
             } catch(err) {
-                console.log("Widget initialization error : "+err.message);
-
+                console.log(err);
             }
         }
     });