SVGHMI: moved JS code of foreach and jump widget in corresponding widget_*.ysl2. To be continued. svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Sun, 19 Apr 2020 22:44:41 +0200
branchsvghmi
changeset 2942 b07ad97e6019
parent 2941 ef13a4007538
child 2943 304e88bae115
child 2945 69f395c01c09
SVGHMI: moved JS code of foreach and jump widget in corresponding widget_*.ysl2. To be continued.
svghmi/gen_index_xhtml.xslt
svghmi/svghmi.js
svghmi/widget_foreach.ysl2
svghmi/widget_jump.ysl2
--- a/svghmi/gen_index_xhtml.xslt	Sun Apr 19 22:01:12 2020 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Sun Apr 19 22:44:41 2020 +0200
@@ -1544,6 +1544,115 @@
     <xsl:text>    apply_cache: foreach_apply_cache,
 </xsl:text>
   </xsl:template>
+  <epilogue:foreach/>
+  <xsl:template match="epilogue:foreach">
+    <xsl:text>function foreach_unsubscribe(){
+</xsl:text>
+    <xsl:text>    for(let item of this.items){
+</xsl:text>
+    <xsl:text>        for(let widget of item) {
+</xsl:text>
+    <xsl:text>            unsubscribe.call(widget);
+</xsl:text>
+    <xsl:text>        }
+</xsl:text>
+    <xsl:text>    }
+</xsl:text>
+    <xsl:text>    this.offset = 0;
+</xsl:text>
+    <xsl:text>}
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+    <xsl:text>function foreach_widgets_do(new_offset, todo){
+</xsl:text>
+    <xsl:text>    this.offset = new_offset;
+</xsl:text>
+    <xsl:text>    for(let i = 0; i &lt; this.items.length; i++) {
+</xsl:text>
+    <xsl:text>        let item = this.items[i];
+</xsl:text>
+    <xsl:text>        let orig_item_index = this.index_pool[i];
+</xsl:text>
+    <xsl:text>        let item_index = this.index_pool[i+this.item_offset];
+</xsl:text>
+    <xsl:text>        let item_index_offset = item_index - orig_item_index;
+</xsl:text>
+    <xsl:text>        for(let widget of item) {
+</xsl:text>
+    <xsl:text>            todo.call(widget, new_offset + item_index_offset);
+</xsl:text>
+    <xsl:text>        }
+</xsl:text>
+    <xsl:text>    }
+</xsl:text>
+    <xsl:text>}
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+    <xsl:text>function foreach_subscribe(new_offset=0){
+</xsl:text>
+    <xsl:text>    foreach_widgets_do.call(this, new_offset, subscribe);
+</xsl:text>
+    <xsl:text>}
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+    <xsl:text>function foreach_apply_cache() {
+</xsl:text>
+    <xsl:text>    foreach_widgets_do.call(this, this.offset, widget_apply_cache);
+</xsl:text>
+    <xsl:text>}
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+    <xsl:text>function foreach_onclick(opstr, evt) {
+</xsl:text>
+    <xsl:text>    new_item_offset = eval(String(this.item_offset)+opstr)
+</xsl:text>
+    <xsl:text>    if(new_item_offset + this.items.length &gt; this.index_pool.length) {
+</xsl:text>
+    <xsl:text>        if(this.item_offset + this.items.length == this.index_pool.length)
+</xsl:text>
+    <xsl:text>            new_item_offset = 0;
+</xsl:text>
+    <xsl:text>        else
+</xsl:text>
+    <xsl:text>            new_item_offset = this.index_pool.length - this.items.length;
+</xsl:text>
+    <xsl:text>    } else if(new_item_offset &lt; 0) {
+</xsl:text>
+    <xsl:text>        if(this.item_offset == 0)
+</xsl:text>
+    <xsl:text>            new_item_offset = this.index_pool.length - this.items.length;
+</xsl:text>
+    <xsl:text>        else
+</xsl:text>
+    <xsl:text>            new_item_offset = 0;
+</xsl:text>
+    <xsl:text>    }
+</xsl:text>
+    <xsl:text>    this.item_offset = new_item_offset;
+</xsl:text>
+    <xsl:text>    off = this.offset;
+</xsl:text>
+    <xsl:text>    foreach_unsubscribe.call(this);
+</xsl:text>
+    <xsl:text>    foreach_subscribe.call(this,off);
+</xsl:text>
+    <xsl:text>    update_subscriptions();
+</xsl:text>
+    <xsl:text>    need_cache_apply.push(this);
+</xsl:text>
+    <xsl:text>    jumps_need_update = true;
+</xsl:text>
+    <xsl:text>    requestHMIAnimation();
+</xsl:text>
+    <xsl:text>}
+</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">
@@ -1847,6 +1956,25 @@
       </xsl:if>
     </xsl:if>
   </xsl:template>
+  <epilogue:jump/>
+  <xsl:template match="epilogue:jump">
+    <xsl:text>var jumps_need_update = false;
+</xsl:text>
+    <xsl:text>var jump_history = [[default_page, undefined]];
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+    <xsl:text>function update_jumps() {
+</xsl:text>
+    <xsl:text>    page_desc[current_visible_page].jumps.map(w=&gt;w.notify_page_change(current_visible_page,current_page_index));
+</xsl:text>
+    <xsl:text>    jumps_need_update = false;
+</xsl:text>
+    <xsl:text>};
+</xsl:text>
+    <xsl:text>
+</xsl:text>
+  </xsl:template>
   <epilogue:keypad/>
   <xsl:template match="epilogue:keypad">
     <xsl:text>
@@ -2225,10 +2353,6 @@
 </xsl:text>
           <xsl:text>var need_cache_apply = []; 
 </xsl:text>
-          <xsl:text>var jumps_need_update = false;
-</xsl:text>
-          <xsl:text>var jump_history = [[default_page, undefined]];
-</xsl:text>
           <xsl:text>
 </xsl:text>
           <xsl:text>function dispatch_value_to_widget(widget, index, value, oldval) {
@@ -2921,182 +3045,74 @@
 </xsl:text>
           <xsl:text>
 </xsl:text>
-          <xsl:text>function foreach_unsubscribe(){
-</xsl:text>
-          <xsl:text>    for(let item of this.items){
-</xsl:text>
-          <xsl:text>        for(let widget of item) {
-</xsl:text>
-          <xsl:text>            unsubscribe.call(widget);
+          <xsl:text>function widget_apply_cache() {
+</xsl:text>
+          <xsl:text>    for(let index of this.indexes){
+</xsl:text>
+          <xsl:text>        /* dispatch current cache in newly opened page widgets */
+</xsl:text>
+          <xsl:text>        let realindex = index+this.offset;
+</xsl:text>
+          <xsl:text>        let cached_val = cache[realindex];
+</xsl:text>
+          <xsl:text>        if(cached_val != undefined)
+</xsl:text>
+          <xsl:text>            dispatch_value_to_widget(this, realindex, cached_val, cached_val);
+</xsl:text>
+          <xsl:text>    }
+</xsl:text>
+          <xsl:text>}
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>function switch_visible_page(page_name) {
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    let old_desc = page_desc[current_visible_page];
+</xsl:text>
+          <xsl:text>    let new_desc = page_desc[page_name];
+</xsl:text>
+          <xsl:text>
+</xsl:text>
+          <xsl:text>    if(old_desc){
+</xsl:text>
+          <xsl:text>        for(let eltid in old_desc.required_detachables){
+</xsl:text>
+          <xsl:text>            if(!(eltid in new_desc.required_detachables)){
+</xsl:text>
+          <xsl:text>                let [element, parent] = old_desc.required_detachables[eltid];
+</xsl:text>
+          <xsl:text>                parent.removeChild(element);
+</xsl:text>
+          <xsl:text>            }
 </xsl:text>
           <xsl:text>        }
 </xsl:text>
+          <xsl:text>        for(let eltid in new_desc.required_detachables){
+</xsl:text>
+          <xsl:text>            if(!(eltid in old_desc.required_detachables)){
+</xsl:text>
+          <xsl:text>                let [element, parent] = new_desc.required_detachables[eltid];
+</xsl:text>
+          <xsl:text>                parent.appendChild(element);
+</xsl:text>
+          <xsl:text>            }
+</xsl:text>
+          <xsl:text>        }
+</xsl:text>
+          <xsl:text>    }else{
+</xsl:text>
+          <xsl:text>        for(let eltid in new_desc.required_detachables){
+</xsl:text>
+          <xsl:text>            let [element, parent] = new_desc.required_detachables[eltid];
+</xsl:text>
+          <xsl:text>            parent.appendChild(element);
+</xsl:text>
+          <xsl:text>        }
+</xsl:text>
           <xsl:text>    }
 </xsl:text>
-          <xsl:text>    this.offset = 0;
-</xsl:text>
-          <xsl:text>}
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>function foreach_widgets_do(new_offset, todo){
-</xsl:text>
-          <xsl:text>    this.offset = new_offset;
-</xsl:text>
-          <xsl:text>    for(let i = 0; i &lt; this.items.length; i++) {
-</xsl:text>
-          <xsl:text>        let item = this.items[i];
-</xsl:text>
-          <xsl:text>        let orig_item_index = this.index_pool[i];
-</xsl:text>
-          <xsl:text>        let item_index = this.index_pool[i+this.item_offset];
-</xsl:text>
-          <xsl:text>        let item_index_offset = item_index - orig_item_index;
-</xsl:text>
-          <xsl:text>        for(let widget of item) {
-</xsl:text>
-          <xsl:text>            todo.call(widget, new_offset + item_index_offset);
-</xsl:text>
-          <xsl:text>        }
-</xsl:text>
-          <xsl:text>    }
-</xsl:text>
-          <xsl:text>}
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>function foreach_subscribe(new_offset=0){
-</xsl:text>
-          <xsl:text>    foreach_widgets_do.call(this, new_offset, subscribe);
-</xsl:text>
-          <xsl:text>}
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>function widget_apply_cache() {
-</xsl:text>
-          <xsl:text>    for(let index of this.indexes){
-</xsl:text>
-          <xsl:text>        /* dispatch current cache in newly opened page widgets */
-</xsl:text>
-          <xsl:text>        let realindex = index+this.offset;
-</xsl:text>
-          <xsl:text>        let cached_val = cache[realindex];
-</xsl:text>
-          <xsl:text>        if(cached_val != undefined)
-</xsl:text>
-          <xsl:text>            dispatch_value_to_widget(this, realindex, cached_val, cached_val);
-</xsl:text>
-          <xsl:text>    }
-</xsl:text>
-          <xsl:text>}
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>function foreach_apply_cache() {
-</xsl:text>
-          <xsl:text>    foreach_widgets_do.call(this, this.offset, widget_apply_cache);
-</xsl:text>
-          <xsl:text>}
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>function foreach_onclick(opstr, evt) {
-</xsl:text>
-          <xsl:text>    new_item_offset = eval(String(this.item_offset)+opstr)
-</xsl:text>
-          <xsl:text>    if(new_item_offset + this.items.length &gt; this.index_pool.length) {
-</xsl:text>
-          <xsl:text>        if(this.item_offset + this.items.length == this.index_pool.length)
-</xsl:text>
-          <xsl:text>            new_item_offset = 0;
-</xsl:text>
-          <xsl:text>        else
-</xsl:text>
-          <xsl:text>            new_item_offset = this.index_pool.length - this.items.length;
-</xsl:text>
-          <xsl:text>    } else if(new_item_offset &lt; 0) {
-</xsl:text>
-          <xsl:text>        if(this.item_offset == 0)
-</xsl:text>
-          <xsl:text>            new_item_offset = this.index_pool.length - this.items.length;
-</xsl:text>
-          <xsl:text>        else
-</xsl:text>
-          <xsl:text>            new_item_offset = 0;
-</xsl:text>
-          <xsl:text>    }
-</xsl:text>
-          <xsl:text>    this.item_offset = new_item_offset;
-</xsl:text>
-          <xsl:text>    off = this.offset;
-</xsl:text>
-          <xsl:text>    foreach_unsubscribe.call(this);
-</xsl:text>
-          <xsl:text>    foreach_subscribe.call(this,off);
-</xsl:text>
-          <xsl:text>    update_subscriptions();
-</xsl:text>
-          <xsl:text>    need_cache_apply.push(this);
-</xsl:text>
-          <xsl:text>    jumps_need_update = true;
-</xsl:text>
-          <xsl:text>    requestHMIAnimation();
-</xsl:text>
-          <xsl:text>}
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>function switch_visible_page(page_name) {
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>    let old_desc = page_desc[current_visible_page];
-</xsl:text>
-          <xsl:text>    let new_desc = page_desc[page_name];
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>    if(old_desc){
-</xsl:text>
-          <xsl:text>        for(let eltid in old_desc.required_detachables){
-</xsl:text>
-          <xsl:text>            if(!(eltid in new_desc.required_detachables)){
-</xsl:text>
-          <xsl:text>                let [element, parent] = old_desc.required_detachables[eltid];
-</xsl:text>
-          <xsl:text>                parent.removeChild(element);
-</xsl:text>
-          <xsl:text>            }
-</xsl:text>
-          <xsl:text>        }
-</xsl:text>
-          <xsl:text>        for(let eltid in new_desc.required_detachables){
-</xsl:text>
-          <xsl:text>            if(!(eltid in old_desc.required_detachables)){
-</xsl:text>
-          <xsl:text>                let [element, parent] = new_desc.required_detachables[eltid];
-</xsl:text>
-          <xsl:text>                parent.appendChild(element);
-</xsl:text>
-          <xsl:text>            }
-</xsl:text>
-          <xsl:text>        }
-</xsl:text>
-          <xsl:text>    }else{
-</xsl:text>
-          <xsl:text>        for(let eltid in new_desc.required_detachables){
-</xsl:text>
-          <xsl:text>            let [element, parent] = new_desc.required_detachables[eltid];
-</xsl:text>
-          <xsl:text>            parent.appendChild(element);
-</xsl:text>
-          <xsl:text>        }
-</xsl:text>
-          <xsl:text>    }
-</xsl:text>
           <xsl:text>
 </xsl:text>
           <xsl:text>    svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
@@ -3107,18 +3123,6 @@
 </xsl:text>
           <xsl:text>
 </xsl:text>
-          <xsl:text>function update_jumps() {
-</xsl:text>
-          <xsl:text>    page_desc[current_visible_page].jumps.map(w=&gt;w.notify_page_change(current_visible_page,current_page_index));
-</xsl:text>
-          <xsl:text>    jumps_need_update = false;
-</xsl:text>
-          <xsl:text>};
-</xsl:text>
-          <xsl:text>
-</xsl:text>
-          <xsl:text>
-</xsl:text>
           <xsl:text>// Once connection established
 </xsl:text>
           <xsl:text>ws.onopen = function (evt) {
--- a/svghmi/svghmi.js	Sun Apr 19 22:01:12 2020 +0200
+++ b/svghmi/svghmi.js	Sun Apr 19 22:44:41 2020 +0200
@@ -3,8 +3,6 @@
 var cache = hmitree_types.map(_ignored => undefined);
 var updates = {};
 var need_cache_apply = []; 
-var jumps_need_update = false;
-var jump_history = [[default_page, undefined]];
 
 function dispatch_value_to_widget(widget, index, value, oldval) {
     try {
@@ -351,32 +349,6 @@
     need_cache_apply.push(this); 
 }
 
-function foreach_unsubscribe(){
-    for(let item of this.items){
-        for(let widget of item) {
-            unsubscribe.call(widget);
-        }
-    }
-    this.offset = 0;
-}
-
-function foreach_widgets_do(new_offset, todo){
-    this.offset = new_offset;
-    for(let i = 0; i < this.items.length; i++) {
-        let item = this.items[i];
-        let orig_item_index = this.index_pool[i];
-        let item_index = this.index_pool[i+this.item_offset];
-        let item_index_offset = item_index - orig_item_index;
-        for(let widget of item) {
-            todo.call(widget, new_offset + item_index_offset);
-        }
-    }
-}
-
-function foreach_subscribe(new_offset=0){
-    foreach_widgets_do.call(this, new_offset, subscribe);
-}
-
 function widget_apply_cache() {
     for(let index of this.indexes){
         /* dispatch current cache in newly opened page widgets */
@@ -387,34 +359,6 @@
     }
 }
 
-function foreach_apply_cache() {
-    foreach_widgets_do.call(this, this.offset, widget_apply_cache);
-}
-
-function foreach_onclick(opstr, evt) {
-    new_item_offset = eval(String(this.item_offset)+opstr)
-    if(new_item_offset + this.items.length > this.index_pool.length) {
-        if(this.item_offset + this.items.length == this.index_pool.length)
-            new_item_offset = 0;
-        else
-            new_item_offset = this.index_pool.length - this.items.length;
-    } else if(new_item_offset < 0) {
-        if(this.item_offset == 0)
-            new_item_offset = this.index_pool.length - this.items.length;
-        else
-            new_item_offset = 0;
-    }
-    this.item_offset = new_item_offset;
-    off = this.offset;
-    foreach_unsubscribe.call(this);
-    foreach_subscribe.call(this,off);
-    update_subscriptions();
-    need_cache_apply.push(this);
-    jumps_need_update = true;
-    requestHMIAnimation();
-}
-
-
 function switch_visible_page(page_name) {
 
     let old_desc = page_desc[current_visible_page];
@@ -444,12 +388,6 @@
     current_visible_page = page_name;
 };
 
-function update_jumps() {
-    page_desc[current_visible_page].jumps.map(w=>w.notify_page_change(current_visible_page,current_page_index));
-    jumps_need_update = false;
-};
-
-
 // Once connection established
 ws.onopen = function (evt) {
     init_widgets();
--- a/svghmi/widget_foreach.ysl2	Sun Apr 19 22:01:12 2020 +0200
+++ b/svghmi/widget_foreach.ysl2	Sun Apr 19 22:44:41 2020 +0200
@@ -55,3 +55,59 @@
     |     apply_cache: foreach_apply_cache,
 }
 
+emit "epilogue:foreach"
+||
+function foreach_unsubscribe(){
+    for(let item of this.items){
+        for(let widget of item) {
+            unsubscribe.call(widget);
+        }
+    }
+    this.offset = 0;
+}
+
+function foreach_widgets_do(new_offset, todo){
+    this.offset = new_offset;
+    for(let i = 0; i < this.items.length; i++) {
+        let item = this.items[i];
+        let orig_item_index = this.index_pool[i];
+        let item_index = this.index_pool[i+this.item_offset];
+        let item_index_offset = item_index - orig_item_index;
+        for(let widget of item) {
+            todo.call(widget, new_offset + item_index_offset);
+        }
+    }
+}
+
+function foreach_subscribe(new_offset=0){
+    foreach_widgets_do.call(this, new_offset, subscribe);
+}
+
+function foreach_apply_cache() {
+    foreach_widgets_do.call(this, this.offset, widget_apply_cache);
+}
+
+function foreach_onclick(opstr, evt) {
+    new_item_offset = eval(String(this.item_offset)+opstr)
+    if(new_item_offset + this.items.length > this.index_pool.length) {
+        if(this.item_offset + this.items.length == this.index_pool.length)
+            new_item_offset = 0;
+        else
+            new_item_offset = this.index_pool.length - this.items.length;
+    } else if(new_item_offset < 0) {
+        if(this.item_offset == 0)
+            new_item_offset = this.index_pool.length - this.items.length;
+        else
+            new_item_offset = 0;
+    }
+    this.item_offset = new_item_offset;
+    off = this.offset;
+    foreach_unsubscribe.call(this);
+    foreach_subscribe.call(this,off);
+    update_subscriptions();
+    need_cache_apply.push(this);
+    jumps_need_update = true;
+    requestHMIAnimation();
+}
+
+||
--- a/svghmi/widget_jump.ysl2	Sun Apr 19 22:01:12 2020 +0200
+++ b/svghmi/widget_jump.ysl2	Sun Apr 19 22:44:41 2020 +0200
@@ -126,3 +126,16 @@
             error > Jump id="«@id»" to page "«$target_page_name»" with incompatible path "«path[1]/@value» (must be same class as "«$target_page_path»")
     }
 }
+
+emit "epilogue:jump"
+||
+var jumps_need_update = false;
+var jump_history = [[default_page, undefined]];
+
+function update_jumps() {
+    page_desc[current_visible_page].jumps.map(w=>w.notify_page_change(current_visible_page,current_page_index));
+    jumps_need_update = false;
+};
+
+||
+