# HG changeset patch # User Edouard Tisserant <edouard.tisserant@gmail.com> # Date 1585087674 -3600 # Node ID 99c5335ed59fc48c74492782ddadff189a70a6dd # Parent 89c02b452717ff9c6d847d3aea16c33702836e6e SVGHMI: Widget ForEach buttons now call corresponding methods. diff -r 89c02b452717 -r 99c5335ed59f svghmi/gen_index_xhtml.xslt --- a/svghmi/gen_index_xhtml.xslt Tue Mar 24 14:03:19 2020 +0100 +++ b/svghmi/gen_index_xhtml.xslt Tue Mar 24 23:07:54 2020 +0100 @@ -723,29 +723,23 @@ </xsl:for-each> <xsl:text> ], </xsl:text> - <xsl:text> buttons: [ + <xsl:text> init: function() { </xsl:text> <xsl:variable name="prefix" select="concat($class,':')"/> <xsl:variable name="buttons_regex" select="concat('^',$prefix,'[+\-][0-9]+')"/> - <xsl:for-each select="$hmi_element/*[regexp:test(@inkscape:label, $buttons_regex)]"> - <xsl:text> ["</xsl:text> - <xsl:value-of select="substring-after(@inkscape:label, concat(arg[1]/@value, ':'))"/> - <xsl:text>", id("</xsl:text> + <xsl:variable name="buttons" select="$hmi_element/*[regexp:test(@inkscape:label, $buttons_regex)]"/> + <xsl:for-each select="$buttons"> + <xsl:variable name="op" select="substring-after(@inkscape:label, $prefix)"/> + <xsl:text> id("</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>").setAttribute("onclick", "hmi_widgets['</xsl:text> + <xsl:value-of select="$hmi_element/@id"/> + <xsl:text>'].on_click('</xsl:text> + <xsl:value-of select="$op"/> + <xsl:text>', evt)"); </xsl:text> </xsl:for-each> - <xsl:text> ], -</xsl:text> - <xsl:text> init: function() { -</xsl:text> - <xsl:text> /* TODO elt.setAttribute("onclick", "hmi_widgets['</xsl:text> - <xsl:value-of select="$hmi_element/@id"/> - <xsl:text>'].on_click(evt)");*/ + <xsl:text> </xsl:text> <xsl:text> this.items = [ </xsl:text> @@ -779,9 +773,9 @@ <xsl:value-of select="@inkscape:label"/> <xsl:text>" is having wrong path. Accroding to ForEach widget ancestor id="</xsl:text> <xsl:value-of select="$hmi_element/@id"/> - <xsl:text>", path should be descendant of </xsl:text> + <xsl:text>", path should be descendant of "</xsl:text> <xsl:value-of select="$item_path"/> - <xsl:text>.</xsl:text> + <xsl:text>".</xsl:text> </xsl:message> </xsl:if> <xsl:text> hmi_widgets["</xsl:text> @@ -804,19 +798,25 @@ </xsl:text> <xsl:text> }, </xsl:text> + <xsl:text> on_click: function(opstr, evt) { +</xsl:text> + <xsl:text> console.log(opstr); +</xsl:text> + <xsl:text> }, +</xsl:text> <xsl:text> item_offset: 0, </xsl:text> </xsl:template> <xsl:template mode="widget_subscribe" match="widget[@type='ForEach']"> <xsl:text> sub: function(off){ </xsl:text> - <xsl:text> subscribe_foreach.call(this,off); + <xsl:text> foreach_subscribe.call(this,off); </xsl:text> <xsl:text> }, </xsl:text> <xsl:text> unsub: function(){ </xsl:text> - <xsl:text> unsubscribe_foreach.call(this); + <xsl:text> foreach_unsubscribe.call(this); </xsl:text> <xsl:text> }, </xsl:text> @@ -1751,7 +1751,7 @@ </xsl:text> <xsl:text> </xsl:text> - <xsl:text>function unsubscribe_foreach(){ + <xsl:text>function foreach_unsubscribe(){ </xsl:text> <xsl:text> for(let item of this.items){ </xsl:text> @@ -1767,7 +1767,7 @@ </xsl:text> <xsl:text> </xsl:text> - <xsl:text>function subscribe_foreach(new_offset=0){ + <xsl:text>function foreach_subscribe(new_offset=0){ </xsl:text> <xsl:text> for(let i = 0; i < this.items.length; i++) { </xsl:text> diff -r 89c02b452717 -r 99c5335ed59f svghmi/svghmi.js --- a/svghmi/svghmi.js Tue Mar 24 14:03:19 2020 +0100 +++ b/svghmi/svghmi.js Tue Mar 24 23:07:54 2020 +0100 @@ -303,7 +303,7 @@ } } -function unsubscribe_foreach(){ +function foreach_unsubscribe(){ for(let item of this.items){ for(let widget of item) { unsubscribe.call(widget); @@ -311,7 +311,7 @@ } } -function subscribe_foreach(new_offset=0){ +function foreach_subscribe(new_offset=0){ for(let i = 0; i < this.items.length; i++) { let item = this.items[i]; let orig_item_index = this.index_pool[i]; diff -r 89c02b452717 -r 99c5335ed59f svghmi/widget_foreach.ysl2 --- a/svghmi/widget_foreach.ysl2 Tue Mar 24 14:03:19 2020 +0100 +++ b/svghmi/widget_foreach.ysl2 Tue Mar 24 23:07:54 2020 +0100 @@ -16,16 +16,15 @@ | «@index»`if "position()!=last()" > ,` } | ], - | buttons: [ + | init: function() { const "prefix","concat($class,':')"; const "buttons_regex","concat('^',$prefix,'[+\-][0-9]+')"; - foreach "$hmi_element/*[regexp:test(@inkscape:label, $buttons_regex)]" { - | ["«substring-after(@inkscape:label, concat(arg[1]/@value, ':'))»", id("«@id»")]`if "position()!=last()" > ,` + const "buttons", "$hmi_element/*[regexp:test(@inkscape:label, $buttons_regex)]"; + foreach "$buttons" { + const "op","substring-after(@inkscape:label, $prefix)"; + | id("«@id»").setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_click('«$op»', evt)"); } - | ], - | init: function() { - | /* TODO elt.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_click(evt)");*/ - + | | this.items = [ const "items_regex","concat('^',$prefix,'[0-9]+')"; const "unordered_items","$hmi_element//*[regexp:test(@inkscape:label, $items_regex)]"; @@ -38,23 +37,26 @@ if "count($elt)=0" error > Missing item labeled «$elt_label» in ForEach widget «$hmi_element/@id» foreach "func:refered_elements($elt)[@id = $hmi_elements/@id][not(@id = $elt/@id)]" { if "not(func:is_descendant_path(func:widget(@id)/path/@value, $item_path))" - error > Widget id="«@id»" label="«@inkscape:label»" is having wrong path. Accroding to ForEach widget ancestor id="«$hmi_element/@id»", path should be descendant of «$item_path». + error > Widget id="«@id»" label="«@inkscape:label»" is having wrong path. Accroding to ForEach widget ancestor id="«$hmi_element/@id»", path should be descendant of "«$item_path»". | hmi_widgets["«@id»"]`if "position()!=last()" > ,` } | ]`if "position()!=last()" > ,` } | ] | }, + | on_click: function(opstr, evt) { + | console.log(opstr); + | }, | item_offset: 0, } template "widget[@type='ForEach']", mode="widget_subscribe"{ // param "hmi_element"; | sub: function(off){ - | subscribe_foreach.call(this,off); + | foreach_subscribe.call(this,off); | }, | unsub: function(){ - | unsubscribe_foreach.call(this); + | foreach_unsubscribe.call(this); | }, }