SVGHMI: ForEach knows its own widgets svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Mon, 23 Mar 2020 10:16:38 +0100
branchsvghmi
changeset 2892 27dd409fba1d
parent 2891 8927ae8326b2
child 2893 d57a12b8f5db
SVGHMI: ForEach knows its own widgets
svghmi/gen_index_xhtml.xslt
svghmi/widget_foreach.ysl2
--- a/svghmi/gen_index_xhtml.xslt	Fri Mar 20 10:46:15 2020 +0100
+++ b/svghmi/gen_index_xhtml.xslt	Mon Mar 23 10:16:38 2020 +0100
@@ -680,51 +680,6 @@
       </xsl:otherwise>
     </xsl:choose>
   </func:function>
-  <xsl:template mode="widget_defs" match="widget[@type='ForEach']">
-    <xsl:param name="hmi_element"/>
-    <xsl:text>    frequency: 2,
-</xsl:text>
-    <xsl:text>    dispatch: function(value) {
-</xsl:text>
-    <xsl:text>                    // do something
-</xsl:text>
-    <xsl:text>    },
-</xsl:text>
-    <xsl:text>    init: function() {
-</xsl:text>
-    <xsl:for-each select="$hmi_element/*[regexp:test(@inkscape:label,'^[=+\-].+')]">
-      <xsl:text>        id("</xsl:text>
-      <xsl:value-of select="@id"/>
-      <xsl:text>").addEventListener(
-</xsl:text>
-      <xsl:text>            "click", 
-</xsl:text>
-      <xsl:text>            evt =&gt; {let new_val = "</xsl:text>
-      <xsl:value-of select="func:escape_quotes(@inkscape:label)"/>
-      <xsl:text>");
-</xsl:text>
-      <xsl:text>                    // do something with new_val
-</xsl:text>
-      <xsl:text>                   });
-</xsl:text>
-    </xsl:for-each>
-    <xsl:text>    },
-</xsl:text>
-  </xsl:template>
-  <xsl:template mode="widget_subscribe" match="widget[@type='ForEach']">
-    <xsl:text>    sub: function(off){
-</xsl:text>
-    <xsl:text>        subscribe.call(this,off)
-</xsl:text>
-    <xsl:text>    },
-</xsl:text>
-    <xsl:text>    unsub: function(){
-</xsl:text>
-    <xsl:text>        unsubscribe.call(this)
-</xsl:text>
-    <xsl:text>    },
-</xsl:text>
-  </xsl:template>
   <xsl:template mode="widget_defs" match="widget[@type='Display']">
     <xsl:param name="hmi_element"/>
     <xsl:text>    frequency: 5,
@@ -745,6 +700,80 @@
     <xsl:text>    },
 </xsl:text>
   </xsl:template>
+  <xsl:template mode="widget_defs" match="widget[@type='ForEach']">
+    <xsl:param name="hmi_element"/>
+    <xsl:variable name="widgets" select="func:refered_elements($forEach_widgets)[not(@id = $forEach_widgets_ids)]"/>
+    <xsl:text>    frequency: 2,
+</xsl:text>
+    <xsl:text>    dispatch: function(value) {
+</xsl:text>
+    <xsl:text>                    // do something
+</xsl:text>
+    <xsl:text>    },
+</xsl:text>
+    <xsl:text>    init: function() {
+</xsl:text>
+    <xsl:for-each select="$hmi_element/*[regexp:test(@inkscape:label,'^[=+\-].+')]">
+      <xsl:text>        id("</xsl:text>
+      <xsl:value-of select="@id"/>
+      <xsl:text>").addEventListener(
+</xsl:text>
+      <xsl:text>            "click", 
+</xsl:text>
+      <xsl:text>            evt =&gt; {let new_val = "</xsl:text>
+      <xsl:value-of select="func:escape_quotes(@inkscape:label)"/>
+      <xsl:text>");
+</xsl:text>
+      <xsl:text>                    // do something with new_val
+</xsl:text>
+      <xsl:text>                   });
+</xsl:text>
+    </xsl:for-each>
+    <xsl:text>    },
+</xsl:text>
+    <xsl:text>    widgets: [
+</xsl:text>
+    <xsl:variable name="labels_regex" select="concat('^',arg[1]/@value,':[0-9]+')"/>
+    <xsl:for-each select="$hmi_element/*[regexp:test(@inkscape:label, $labels_regex)]">
+      <xsl:text>      [ /* </xsl:text>
+      <xsl:value-of select="@inkscape:label"/>
+      <xsl:text> */
+</xsl:text>
+      <xsl:variable name="elt" select="."/>
+      <xsl:for-each select="func:refered_elements(.)[@id = $hmi_elements/@id][not(@id = $elt/@id)]">
+        <xsl:text>        hmi_widgets["</xsl:text>
+        <xsl:value-of select="@id"/>
+        <xsl:text>"]</xsl:text>
+        <xsl:if test="position()!=last()">
+          <xsl:text>,</xsl:text>
+        </xsl:if>
+        <xsl:text>
+</xsl:text>
+      </xsl:for-each>
+      <xsl:text>      ]</xsl:text>
+      <xsl:if test="position()!=last()">
+        <xsl:text>,</xsl:text>
+      </xsl:if>
+      <xsl:text>
+</xsl:text>
+    </xsl:for-each>
+    <xsl:text>    ],
+</xsl:text>
+  </xsl:template>
+  <xsl:template mode="widget_subscribe" match="widget[@type='ForEach']">
+    <xsl:text>    sub: function(off){
+</xsl:text>
+    <xsl:text>        subscribe.call(this,off)
+</xsl:text>
+    <xsl:text>    },
+</xsl:text>
+    <xsl:text>    unsub: function(){
+</xsl:text>
+    <xsl:text>        unsubscribe.call(this)
+</xsl:text>
+    <xsl:text>    },
+</xsl:text>
+  </xsl:template>
   <xsl:template mode="widget_defs" match="widget[@type='Input']">
     <xsl:param name="hmi_element"/>
     <xsl:variable name="value_elt">
--- a/svghmi/widget_foreach.ysl2	Fri Mar 20 10:46:15 2020 +0100
+++ b/svghmi/widget_foreach.ysl2	Mon Mar 23 10:16:38 2020 +0100
@@ -1,6 +1,7 @@
 
 template "widget[@type='ForEach']", mode="widget_defs" {
     param "hmi_element";
+    const "widgets", "func:refered_elements($forEach_widgets)[not(@id = $forEach_widgets_ids)]";
     |     frequency: 2,
     |     dispatch: function(value) {
     |                     // do something
@@ -14,15 +15,30 @@
     |                    });
     }
     |     },
+
+    |     widgets: [
+    const "labels_regex","concat('^',arg[1]/@value,':[0-9]+')";
+    foreach "$hmi_element/*[regexp:test(@inkscape:label, $labels_regex)]" {
+    |       [ /* «@inkscape:label» */
+        const "elt",".";
+        //foreach "$hmi_elements[ancestor::svg:*/@id = $_id]" {
+        foreach "func:refered_elements(.)[@id = $hmi_elements/@id][not(@id = $elt/@id)]" {
+    |         hmi_widgets["«@id»"]`if "position()!=last()" > ,`
+        }
+    |       ]`if "position()!=last()" > ,`
+    }
+    |     ],
 }
 
 template "widget[@type='ForEach']", mode="widget_subscribe"{
     // param "hmi_element";
     |     sub: function(off){
-    |         subscribe.call(this,off)
+    |         subscribe.call(this,off);
+        /* TODO */
     |     },
 
     |     unsub: function(){
-    |         unsubscribe.call(this)
+    |         unsubscribe.call(this);
+        /* TODO */
     |     },
 }