# HG changeset patch # User Edouard Tisserant # Date 1584642236 -3600 # Node ID 4eeed820fd3a9f97c8f12f4e86399a877838820c # Parent 7beddc62a3889df782e45185bc55cd8e2298ba8a SVGHMI: reworked widget (un)subscription now a method of widgect object, allowing special subscription methods diff -r 7beddc62a388 -r 4eeed820fd3a svghmi/gen_index_xhtml.xslt --- a/svghmi/gen_index_xhtml.xslt Thu Mar 19 11:32:20 2020 +0100 +++ b/svghmi/gen_index_xhtml.xslt Thu Mar 19 19:23:56 2020 +0100 @@ -579,6 +579,8 @@ ], + offset: 0, + indexes: [ @@ -614,6 +616,9 @@ + + + } , @@ -621,6 +626,13 @@ + + sub: subscribe, + + unsub: unsubscribe, + + + @@ -1024,8 +1036,6 @@ let d = widget.dispatch; - console.log(index, idx, idxidx, value); - if(typeof(d) == "function" && idxidx == 0){ d.call(widget, value, oldval); @@ -1372,8 +1382,6 @@ dispatch: function(value) { - // console.log("Heartbeat" + value); - change_hmi_value(heartbeat_index, "+1"); } @@ -1566,6 +1574,46 @@ + function unsubscribe(){ + + widget = this; + + /* remove subsribers */ + + for(let index of widget.indexes){ + + let idx = index + widget.offset; + + subscribers[idx].delete(widget); + + } + + widget.offset = 0; + + } + + + + function subscribe(new_offset=0){ + + widget = this; + + /* set the offset because relative */ + + widget.offset = new_offset; + + /* add widget's subsribers */ + + for(let index of widget.indexes){ + + subscribers[index + new_offset].add(widget); + + } + + } + + + function switch_subscribed_page(page_name, page_index) { let old_desc = page_desc[current_subscribed_page]; @@ -1594,150 +1642,100 @@ if(old_desc){ - for(let widget of old_desc.absolute_widgets){ - - /* remove subsribers */ - - for(let index of widget.indexes){ - - subscribers[index].delete(widget); + old_desc.absolute_widgets.map(w=>w.unsub()); + + old_desc.relative_widgets.map(w=>w.unsub()); + + } + + new_desc.absolute_widgets.map(w=>w.sub()); + + var new_offset = page_index == undefined ? 0 : page_index - new_desc.page_index; + + new_desc.relative_widgets.map(w=>w.sub(new_offset)); + + + + update_subscriptions(); + + + + current_subscribed_page = page_name; + + + + 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 widget of old_desc.relative_widgets){ - - /* remove subsribers */ - - for(let index of widget.indexes){ - - let idx = widget.offset ? index + widget.offset : index; - - subscribers[idx].delete(widget); + 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); } - /* lose the offset */ - - delete widget.offset; - } - } - - for(let widget of new_desc.absolute_widgets){ - - /* add widget's subsribers */ + }else{ + + for(let eltid in new_desc.required_detachables){ + + let [element, parent] = new_desc.required_detachables[eltid]; + + parent.appendChild(element); + + } + + } + + + + for(let widget of chain(new_desc.absolute_widgets,new_desc.relative_widgets)){ for(let index of widget.indexes){ - subscribers[index].add(widget); + /* dispatch current cache in newly opened page widgets */ + + let cached_val = cache[index]; + + if(cached_val != undefined) + + dispatch_value_to_widget(widget, index, cached_val, cached_val); } } - var new_offset = page_index == undefined ? 0 : page_index - new_desc.page_index; - - for(let widget of new_desc.relative_widgets){ - - /* set the offset because relative */ - - widget.offset = new_offset; - - /* add widget's subsribers */ - - for(let index of widget.indexes){ - - subscribers[index + new_offset].add(widget); - - } - - } - - - - update_subscriptions(); - - - - current_subscribed_page = page_name; - - - - 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); - - } - - } - - - - for(let widget of chain(new_desc.absolute_widgets,new_desc.relative_widgets)){ - - for(let index of widget.indexes){ - - /* dispatch current cache in newly opened page widgets */ - - let cached_val = cache[index]; - - if(cached_val != undefined) - - dispatch_value_to_widget(widget, index, cached_val, cached_val); - - } - - } - svg_root.setAttribute('viewBox',new_desc.bbox.join(" ")); diff -r 7beddc62a388 -r 4eeed820fd3a svghmi/svghmi.js --- a/svghmi/svghmi.js Thu Mar 19 11:32:20 2020 +0100 +++ b/svghmi/svghmi.js Thu Mar 19 19:23:56 2020 +0100 @@ -8,7 +8,6 @@ let idx = widget.offset ? index - widget.offset : index; let idxidx = widget.indexes.indexOf(idx); let d = widget.dispatch; - console.log(index, idx, idxidx, value); if(typeof(d) == "function" && idxidx == 0){ d.call(widget, value, oldval); } @@ -182,7 +181,6 @@ frequency: 1, indexes: [heartbeat_index], dispatch: function(value) { - // console.log("Heartbeat" + value); change_hmi_value(heartbeat_index, "+1"); } }); @@ -279,6 +277,26 @@ yield* b; }; +function unsubscribe(){ + widget = this; + /* remove subsribers */ + for(let index of widget.indexes){ + let idx = index + widget.offset; + subscribers[idx].delete(widget); + } + widget.offset = 0; +} + +function subscribe(new_offset=0){ + widget = this; + /* set the offset because relative */ + widget.offset = new_offset; + /* add widget's subsribers */ + for(let index of widget.indexes){ + subscribers[index + new_offset].add(widget); + } +} + function switch_subscribed_page(page_name, page_index) { let old_desc = page_desc[current_subscribed_page]; let new_desc = page_desc[page_name]; @@ -293,37 +311,12 @@ } if(old_desc){ - for(let widget of old_desc.absolute_widgets){ - /* remove subsribers */ - for(let index of widget.indexes){ - subscribers[index].delete(widget); - } - } - for(let widget of old_desc.relative_widgets){ - /* remove subsribers */ - for(let index of widget.indexes){ - let idx = widget.offset ? index + widget.offset : index; - subscribers[idx].delete(widget); - } - /* lose the offset */ - delete widget.offset; - } - } - for(let widget of new_desc.absolute_widgets){ - /* add widget's subsribers */ - for(let index of widget.indexes){ - subscribers[index].add(widget); - } - } + old_desc.absolute_widgets.map(w=>w.unsub()); + old_desc.relative_widgets.map(w=>w.unsub()); + } + new_desc.absolute_widgets.map(w=>w.sub()); var new_offset = page_index == undefined ? 0 : page_index - new_desc.page_index; - for(let widget of new_desc.relative_widgets){ - /* set the offset because relative */ - widget.offset = new_offset; - /* add widget's subsribers */ - for(let index of widget.indexes){ - subscribers[index + new_offset].add(widget); - } - } + new_desc.relative_widgets.map(w=>w.sub(new_offset)); update_subscriptions(); diff -r 7beddc62a388 -r 4eeed820fd3a svghmi/widgets_common.ysl2 --- a/svghmi/widgets_common.ysl2 Thu Mar 19 11:32:20 2020 +0100 +++ b/svghmi/widgets_common.ysl2 Thu Mar 19 19:23:56 2020 +0100 @@ -17,9 +17,10 @@ | "«@id»": { | type: "«$widget/@type»", | args: [ - foreach "$widget/arg" + foreach "$widget/arg" | "«@value»"`if "position()!=last()" > ,` | ], + | offset: 0, | indexes: [ foreach "$widget/path" { choose { @@ -34,9 +35,17 @@ | ], | element: id("«@id»"), apply "$widget", mode="widget_defs" with "hmi_element","."; + apply "$widget", mode="widget_subscribe" with "hmi_element","."; | }`if "position()!=last()" > ,` } +// default : normal subscribing +template "widget", mode="widget_subscribe" { + | sub: subscribe, + | unsub: unsubscribe, +} +// page aren't subscribers +template "widget[@type='Page']", mode="widget_subscribe"; function "defs_by_labels" { param "labels","''";