# HG changeset patch # User Edouard Tisserant # Date 1597311018 -7200 # Node ID 48e7e336c0528b77ac394221f4269fecf804ba53 # Parent 0a9f6f29b7ddaaef4a0bee5601ac8c601b46f262 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. diff -r 0a9f6f29b7dd -r 48e7e336c052 svghmi/gen_index_xhtml.xslt --- 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 @@ " - + " , @@ -976,9 +976,10 @@ only applies to HMI variable. - - : + " + ": + , @@ -1021,6 +1022,14 @@ } + let defaultval = local_defaults[varname]; + + console.log("page_local_index creat local", varname, pagename, new_index, defaultval); + + if(defaultval != undefined) + + cache[new_index] = defaultval; + return new_index; } @@ -1132,7 +1141,11 @@ 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 */ @@ -1142,7 +1155,15 @@ 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); + + } } @@ -1190,50 +1211,34 @@ 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); } + break; + } - } catch(err) { - - console.log(err); - } } diff -r 0a9f6f29b7dd -r 48e7e336c052 svghmi/widgets_common.ysl2 --- 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; + } } } diff -r 0a9f6f29b7dd -r 48e7e336c052 tests/svghmi/svghmi_0@svghmi/svghmi.svg --- 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 + inkscape:label="HMI:VarInit:42@level" /> +