SVGHMI: HMI:VarInit:"a string"@a_value : empty widget (i.e. empty svg:group) that initialize content of local variables to value given as first argument. svghmi
authorEdouard Tisserant
Thu, 13 Aug 2020 11:30:18 +0200
branchsvghmi
changeset 3025 48e7e336c052
parent 3024 0a9f6f29b7dd
child 3026 d454ed480c0f
SVGHMI: HMI:VarInit:"a string"@a_value : empty widget (i.e. empty svg:group) that initialize content of local variables to value given as first argument.
svghmi/gen_index_xhtml.xslt
svghmi/widgets_common.ysl2
tests/svghmi/svghmi_0@svghmi/svghmi.svg
--- a/svghmi/gen_index_xhtml.xslt	Wed Aug 12 15:24:02 2020 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Thu Aug 13 11:30:18 2020 +0200
@@ -875,7 +875,7 @@
               </xsl:when>
               <xsl:when test="@type = 'PAGE_LOCAL'">
                 <xsl:text>"</xsl:text>
-                <xsl:value-of select="substring(@value, 1)"/>
+                <xsl:value-of select="@value"/>
                 <xsl:text>"</xsl:text>
                 <xsl:if test="position()!=last()">
                   <xsl:text>,</xsl:text>
@@ -976,9 +976,10 @@
           <xsl:text> only applies to HMI variable.</xsl:text>
         </xsl:message>
       </xsl:if>
-      <xsl:value-of select="arg[0]"/>
-      <xsl:text>:</xsl:text>
+      <xsl:text>"</xsl:text>
       <xsl:value-of select="path/@value"/>
+      <xsl:text>":</xsl:text>
+      <xsl:value-of select="arg[1]/@value"/>
       <xsl:if test="position()!=last()">
         <xsl:text>,</xsl:text>
       </xsl:if>
@@ -1021,6 +1022,14 @@
 </xsl:text>
     <xsl:text>    }
 </xsl:text>
+    <xsl:text>    let defaultval = local_defaults[varname];
+</xsl:text>
+    <xsl:text>    console.log("page_local_index creat local", varname, pagename, new_index, defaultval);
+</xsl:text>
+    <xsl:text>    if(defaultval != undefined) 
+</xsl:text>
+    <xsl:text>        cache[new_index] = defaultval; 
+</xsl:text>
     <xsl:text>    return new_index;
 </xsl:text>
     <xsl:text>}
@@ -1132,7 +1141,11 @@
 </xsl:text>
     <xsl:text>    apply_cache() {
 </xsl:text>
-    <xsl:text>        if(!this.unsubscribable) for(let index of this.indexes){
+    <xsl:text>        let dispatch = this.dispatch;
+</xsl:text>
+    <xsl:text>        if(dispatch == undefined) return;
+</xsl:text>
+    <xsl:text>        if(!this.unsubscribable) for(let index in this.indexes){
 </xsl:text>
     <xsl:text>            /* dispatch current cache in newly opened page widgets */
 </xsl:text>
@@ -1142,7 +1155,15 @@
 </xsl:text>
     <xsl:text>            if(cached_val != undefined)
 </xsl:text>
-    <xsl:text>                this.new_hmi_value(realindex, cached_val, cached_val);
+    <xsl:text>                try {
+</xsl:text>
+    <xsl:text>                    dispatch.call(this, cached_val, cached_val, index);
+</xsl:text>
+    <xsl:text>                } catch(err) {
+</xsl:text>
+    <xsl:text>                    console.log(err);
+</xsl:text>
+    <xsl:text>                }
 </xsl:text>
     <xsl:text>        }
 </xsl:text>
@@ -1190,50 +1211,34 @@
 </xsl:text>
     <xsl:text>    new_hmi_value(index, value, oldval) {
 </xsl:text>
-    <xsl:text>        try {
-</xsl:text>
-    <xsl:text>            // TODO avoid searching, store index at sub()
-</xsl:text>
-    <xsl:text>            for(let i = 0; i &lt; this.indexes.length; i++) {
-</xsl:text>
-    <xsl:text>                let refindex = this.get_variable_index(i);
-</xsl:text>
-    <xsl:text>
-</xsl:text>
-    <xsl:text>                if(index == refindex) {
-</xsl:text>
-    <xsl:text>                    let d = this.dispatch;
-</xsl:text>
-    <xsl:text>                    if(typeof(d) == "function"){
-</xsl:text>
-    <xsl:text>                        d.call(this, value, oldval, i);
-</xsl:text>
-    <xsl:text>                    }
-</xsl:text>
-    <xsl:text>                    else if(typeof(d) == "object"){
-</xsl:text>
-    <xsl:text>                        d[i].call(this, value, oldval);
-</xsl:text>
-    <xsl:text>                    }
-</xsl:text>
-    <xsl:text>                    /* else dispatch_0, ..., dispatch_n ? */
-</xsl:text>
-    <xsl:text>                    /*else {
-</xsl:text>
-    <xsl:text>                        throw new Error("Dunno how to dispatch to widget at index = " + index);
-</xsl:text>
-    <xsl:text>                    }*/
-</xsl:text>
-    <xsl:text>                    break;
+    <xsl:text>        // TODO avoid searching, store index at sub()
+</xsl:text>
+    <xsl:text>        let dispatch = this.dispatch;
+</xsl:text>
+    <xsl:text>        if(dispatch == undefined) return;
+</xsl:text>
+    <xsl:text>        for(let i = 0; i &lt; this.indexes.length; i++) {
+</xsl:text>
+    <xsl:text>            let refindex = this.get_variable_index(i);
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+    <xsl:text>            if(index == refindex) {
+</xsl:text>
+    <xsl:text>                try {
+</xsl:text>
+    <xsl:text>                    dispatch.call(this, value, oldval, i);
+</xsl:text>
+    <xsl:text>                } catch(err) {
+</xsl:text>
+    <xsl:text>                    console.log(err);
 </xsl:text>
     <xsl:text>                }
 </xsl:text>
+    <xsl:text>                break;
+</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>    }
--- a/svghmi/widgets_common.ysl2	Wed Aug 12 15:24:02 2020 +0200
+++ b/svghmi/widgets_common.ysl2	Thu Aug 13 11:30:18 2020 +0200
@@ -32,7 +32,7 @@
                     when "not(@type)" 
                         error > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree
                     when "@type = 'PAGE_LOCAL'" 
-                        > "«substring(@value, 1)»"`if "position()!=last()" > ,`
+                        > "«@value»"`if "position()!=last()" > ,`
                     when "@type = 'HMI_LOCAL'" 
                         > hmi_local_index("«@value»")`if "position()!=last()" > ,`
                 }
@@ -75,6 +75,15 @@
     var last_remote_index = hmitree_types.length - 1;
     var next_available_index = hmitree_types.length;
 
+    const local_defaults = {
+    ||
+    foreach "$parsed_widgets/widget[@type = 'VarInit']"{
+        if "count(path) != 1" error > VarInit «@id» must have only one variable given.
+        if "path/@type != 'PAGE_LOCAL' and path/@type != 'HMI_LOCAL'" error > VarInit «@id» only applies to HMI variable.
+        | "«path/@value»":«arg[1]/@value»`if "position()!=last()" > ,`
+    }
+    ||
+    };
     var cache = hmitree_types.map(_ignored => undefined);
 
     function page_local_index(varname, pagename){
@@ -92,6 +101,9 @@
             new_index = next_available_index++;
             pagevars[varname] = new_index;
         }
+        let defaultval = local_defaults[varname];
+        if(defaultval != undefined) 
+            cache[new_index] = defaultval; 
         return new_index;
     }
 
@@ -146,12 +158,18 @@
         }
 
         apply_cache() {
-            if(!this.unsubscribable) for(let index of this.indexes){
+            let dispatch = this.dispatch;
+            if(dispatch == undefined) return;
+            if(!this.unsubscribable) for(let index in this.indexes){
                 /* dispatch current cache in newly opened page widgets */
                 let realindex = this.get_variable_index(index);
                 let cached_val = cache[realindex];
                 if(cached_val != undefined)
-                    this.new_hmi_value(realindex, cached_val, cached_val);
+                    try {
+                        dispatch.call(this, cached_val, cached_val, index);
+                    } catch(err) {
+                        console.log(err);
+                    }
             }
         }
 
@@ -175,28 +193,20 @@
         }
 
         new_hmi_value(index, value, oldval) {
-            try {
-                // TODO avoid searching, store index at sub()
-                for(let i = 0; i < this.indexes.length; i++) {
-                    let refindex = this.get_variable_index(i);
-
-                    if(index == refindex) {
-                        let d = this.dispatch;
-                        if(typeof(d) == "function"){
-                            d.call(this, value, oldval, i);
-                        }
-                        else if(typeof(d) == "object"){
-                            d[i].call(this, value, oldval);
-                        }
-                        /* else dispatch_0, ..., dispatch_n ? */
-                        /*else {
-                            throw new Error("Dunno how to dispatch to widget at index = " + index);
-                        }*/
-                        break;
+            // TODO avoid searching, store index at sub()
+            let dispatch = this.dispatch;
+            if(dispatch == undefined) return;
+            for(let i = 0; i < this.indexes.length; i++) {
+                let refindex = this.get_variable_index(i);
+
+                if(index == refindex) {
+                    try {
+                        dispatch.call(this, value, oldval, i);
+                    } catch(err) {
+                        console.log(err);
                     }
-                }
-            } catch(err) {
-                console.log(err);
+                    break;
+                }
             }
         }
         
--- a/tests/svghmi/svghmi_0@svghmi/svghmi.svg	Wed Aug 12 15:24:02 2020 +0200
+++ b/tests/svghmi/svghmi_0@svghmi/svghmi.svg	Thu Aug 13 11:30:18 2020 +0200
@@ -182,12 +182,12 @@
      inkscape:pageopacity="0"
      inkscape:pageshadow="2"
      inkscape:document-units="px"
-     inkscape:current-layer="g5231"
+     inkscape:current-layer="hmi0"
      showgrid="false"
      units="px"
-     inkscape:zoom="0.35355339"
-     inkscape:cx="228.48319"
-     inkscape:cy="161.32426"
+     inkscape:zoom="0.25"
+     inkscape:cx="840.93557"
+     inkscape:cy="117.51363"
      inkscape:window-width="1800"
      inkscape:window-height="836"
      inkscape:window-x="0"
@@ -5722,8 +5722,11 @@
        id="tspan1611-1">PAGE_LOCAL variables</tspan></text>
   <g
      id="g2387"
-     inkscape:label="HMI:VariableInit:42@level" />
+     inkscape:label="HMI:VarInit:42@level" />
   <g
-     inkscape:label="HMI:VariableInit:&quot;a string&quot;@paff"
+     inkscape:label="HMI:VarInit:&quot;a string&quot;@paff"
      id="g2389" />
+  <g
+     id="g825"
+     inkscape:label="HMI:VarInit:&quot;a page string&quot;@.piff" />
 </svg>