# HG changeset patch # User Edouard Tisserant # Date 1587329081 -7200 # Node ID b07ad97e6019ee6d241754b0a0f43b72ff03a210 # Parent ef13a4007538228269c70eb167d82beaff7a8620 SVGHMI: moved JS code of foreach and jump widget in corresponding widget_*.ysl2. To be continued. diff -r ef13a4007538 -r b07ad97e6019 svghmi/gen_index_xhtml.xslt --- 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 @@ apply_cache: foreach_apply_cache, + + + 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(); + + } + + + + @@ -1847,6 +1956,25 @@ + + + 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; + + }; + + + + @@ -2225,10 +2353,6 @@ var need_cache_apply = []; - var jumps_need_update = false; - - var jump_history = [[default_page, undefined]]; - function dispatch_value_to_widget(widget, index, value, oldval) { @@ -2921,182 +3045,74 @@ - function foreach_unsubscribe(){ - - for(let item of this.items){ - - for(let widget of item) { - - unsubscribe.call(widget); + function widget_apply_cache() { + + for(let index of this.indexes){ + + /* dispatch current cache in newly opened page widgets */ + + let realindex = index+this.offset; + + let cached_val = cache[realindex]; + + if(cached_val != undefined) + + dispatch_value_to_widget(this, realindex, cached_val, cached_val); + + } + + } + + + + function switch_visible_page(page_name) { + + + + let old_desc = page_desc[current_visible_page]; + + let new_desc = page_desc[page_name]; + + + + if(old_desc){ + + for(let eltid in old_desc.required_detachables){ + + if(!(eltid in new_desc.required_detachables)){ + + let [element, parent] = old_desc.required_detachables[eltid]; + + parent.removeChild(element); + + } } + for(let eltid in new_desc.required_detachables){ + + if(!(eltid in old_desc.required_detachables)){ + + let [element, parent] = new_desc.required_detachables[eltid]; + + parent.appendChild(element); + + } + + } + + }else{ + + for(let eltid in new_desc.required_detachables){ + + let [element, parent] = new_desc.required_detachables[eltid]; + + parent.appendChild(element); + + } + } - 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 */ - - let realindex = index+this.offset; - - let cached_val = cache[realindex]; - - if(cached_val != undefined) - - dispatch_value_to_widget(this, realindex, cached_val, cached_val); - - } - - } - - - - 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]; - - let new_desc = page_desc[page_name]; - - - - if(old_desc){ - - for(let eltid in old_desc.required_detachables){ - - if(!(eltid in new_desc.required_detachables)){ - - let [element, parent] = old_desc.required_detachables[eltid]; - - parent.removeChild(element); - - } - - } - - for(let eltid in new_desc.required_detachables){ - - if(!(eltid in old_desc.required_detachables)){ - - let [element, parent] = new_desc.required_detachables[eltid]; - - parent.appendChild(element); - - } - - } - - }else{ - - for(let eltid in new_desc.required_detachables){ - - let [element, parent] = new_desc.required_detachables[eltid]; - - parent.appendChild(element); - - } - - } - svg_root.setAttribute('viewBox',new_desc.bbox.join(" ")); @@ -3107,18 +3123,6 @@ - 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) { diff -r ef13a4007538 -r b07ad97e6019 svghmi/svghmi.js --- 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(); diff -r ef13a4007538 -r b07ad97e6019 svghmi/widget_foreach.ysl2 --- 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(); +} + +|| diff -r ef13a4007538 -r b07ad97e6019 svghmi/widget_jump.ysl2 --- 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; +}; + +|| +