# HG changeset patch # User Edouard Tisserant # Date 1612853162 -3600 # Node ID 32a4675af377e6603428e40f4dc6c5c7c2d29005 # Parent d4dfd47f81562d4e4ce6a4bbd8c4b45e94d030f6 SVGHMI: Added HMI:VarInitPersistent to initialize persistent HMI_LOCAL and PAGE_LOCAL variables, stored as cookies in browser. diff -r d4dfd47f8156 -r 32a4675af377 svghmi/gen_index_xhtml.xslt --- a/svghmi/gen_index_xhtml.xslt Tue Feb 09 07:41:24 2021 +0100 +++ b/svghmi/gen_index_xhtml.xslt Tue Feb 09 07:46:02 2021 +0100 @@ -192,12 +192,12 @@ - + PAGE_LOCAL - + HMI_LOCAL @@ -233,6 +233,10 @@ + + + + @@ -972,7 +976,9 @@ var langs = [ + " + " , @@ -983,9 +989,18 @@ - [" - - ",[ + + + [[ + + id(" + + ") + + , + + + ],[ " @@ -1126,11 +1141,13 @@ var next_available_index = hmitree_types.length; + let cookies = new Map(document.cookie.split("; ").map(s=>s.split("="))); + const local_defaults = { - + VarInit @@ -1145,17 +1162,47 @@ only applies to HMI variable. - " + " ": - + + + cookies.has(" + + ")?cookies.get(" + + "): + + + + + + + + + + , + + + }; + + + + const persistent_locals = new Set([ + + + " + + " , - }; + ]); + + var persistent_indexes = new Map(); var cache = hmitree_types.map(_ignored => undefined); @@ -1193,10 +1240,16 @@ let defaultval = local_defaults[varname]; - if(defaultval != undefined) + if(defaultval != undefined) { cache[new_index] = defaultval; + if(persistent_locals.has(varname)) + + persistent_indexes.set(new_index, varname); + + } + return new_index; } @@ -1506,12 +1559,32 @@ } + + + activate_activable(eltsub) { + + eltsub.inactive.style.display = "none"; + + eltsub.active.style.display = ""; + + } + + + + inactivate_activable(eltsub) { + + eltsub.active.style.display = "none"; + + eltsub.inactive.style.display = ""; + + } + } - + @@ -3825,6 +3898,8 @@ highlight_selection(){ + if(this.last_selection == undefined) return; + let highlighted_row = this.last_selection - this.menu_offset; if(highlighted_row < 0) return; @@ -5123,7 +5198,7 @@ this._shift = this.shift; - (this.shift?widget_active_activable:widget_inactive_activable)(this.Shift_sub); + (this.shift?this.activate_activable:this.inactivate_activable)(this.Shift_sub); } @@ -5131,7 +5206,7 @@ this._caps = this.caps; - (this.caps?widget_active_activable:widget_inactive_activable)(this.CapsLock_sub); + (this.caps?this.activate_activable:this.inactivate_activable)(this.CapsLock_sub); } @@ -6734,6 +6809,72 @@ + var translated = false; + + + + function switch_langnum(langnum) { + + if(langnum == current_lang) { + + return; + + } + + + + if (!translated) { + + translated = true; + + for (let translation of translations) { + + let [objs] = translation; + + translation.push(Array.prototype.map.call(objs[0].children, x=>x.textContent).join("\n")); + + } + + } + + + + for (let translation of translations) { + + let [objs, msgs, orig] = translation; + + let msg = langnum == 0 ? orig : msgs[langnum - 1]; + + for (let obj of objs) { + + msg.split('\n').map((line,i) => {obj.children[i].textContent = line;}); + + } + + } + + current_lang = langnum; + + } + + var lang_local_index = hmi_local_index("lang"); + + subscribers(lang_local_index).add({ + + indexes: [lang_local_index], + + new_hmi_value: function(index, value, oldval) { + + switch_langnum(value); + + } + + }); + + var current_lang = 0; + + switch_langnum(cache[lang_local_index]); + function update_subscriptions() { @@ -6812,6 +6953,22 @@ updates[index] = value; + + + if(persistent_indexes.has(index)){ + + let varname = persistent_indexes.get(index); + + console.log(varname+"="+value+"; max-age=3153600000"); + + document.cookie = varname+"="+value+"; max-age=3153600000"; + + } + + + + + requestHMIAnimation(); return; @@ -6930,28 +7087,6 @@ - /* - - function change_hmi_value(index, opstr) { - - let old_val = cache[index]; - - let new_val = eval_operation_string(old_val, opstr); - - if(new_val != undefined && old_val != new_val) - - send_hmi_value(index, new_val); - - // TODO else raise - - return new_val; - - } - - */ - - - var current_visible_page; var current_subscribed_page; @@ -7256,34 +7391,6 @@ - function widget_active_activable(eltsub) { - - if(eltsub.inactive_style === undefined) - - eltsub.inactive_style = eltsub.inactive.getAttribute("style"); - - eltsub.inactive.setAttribute("style", "display:none"); - - if(eltsub.active_style !== undefined) - - eltsub.active.setAttribute("style", eltsub.active_style); - - }; - - function widget_inactive_activable(eltsub) { - - if(eltsub.active_style === undefined) - - eltsub.active_style = eltsub.active.getAttribute("style"); - - eltsub.active.setAttribute("style", "display:none"); - - if(eltsub.inactive_style !== undefined) - - eltsub.inactive.setAttribute("style", eltsub.inactive_style); - - }; - diff -r d4dfd47f8156 -r 32a4675af377 svghmi/svghmi.js --- a/svghmi/svghmi.js Tue Feb 09 07:41:24 2021 +0100 +++ b/svghmi/svghmi.js Tue Feb 09 07:46:02 2021 +0100 @@ -243,6 +243,13 @@ function send_hmi_value(index, value) { if(index > last_remote_index){ updates[index] = value; + + if(persistent_indexes.has(index)){ + let varname = persistent_indexes.get(index); + console.log(varname+"="+value+"; max-age=3153600000"); + document.cookie = varname+"="+value+"; max-age=3153600000"; + } + requestHMIAnimation(); return; } diff -r d4dfd47f8156 -r 32a4675af377 svghmi/widgets_common.ysl2 --- a/svghmi/widgets_common.ysl2 Tue Feb 09 07:41:24 2021 +0100 +++ b/svghmi/widgets_common.ysl2 Tue Feb 09 07:46:02 2021 +0100 @@ -68,16 +68,32 @@ let hmi_locals = {}; var last_remote_index = hmitree_types.length - 1; var next_available_index = hmitree_types.length; + let cookies = new Map(document.cookie.split("; ").map(s=>s.split("="))); const local_defaults = { || - foreach "$parsed_widgets/widget[@type = 'VarInit']"{ + foreach "$parsed_widgets/widget[starts-with(@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()" > ,` + > "«path/@value»": + choose { + when "@type = 'VarInitPersistent'" > cookies.has("«path/@value»")?cookies.get("«path/@value»"):«arg[1]/@value» + otherwise > «arg[1]/@value» + } + > \n + if "position()!=last()" > , } || }; + + const persistent_locals = new Set([ + || + foreach "$parsed_widgets/widget[@type='VarInitPersistent']"{ + | "«path/@value»"`if "position()!=last()" > ,` + } + || + ]); + var persistent_indexes = new Map(); var cache = hmitree_types.map(_ignored => undefined); function page_local_index(varname, pagename){ @@ -96,8 +112,11 @@ pagevars[varname] = new_index; } let defaultval = local_defaults[varname]; - if(defaultval != undefined) + if(defaultval != undefined) { cache[new_index] = defaultval; + if(persistent_locals.has(varname)) + persistent_indexes.set(new_index, varname); + } return new_index; } @@ -265,7 +284,7 @@ || } -const "excluded_types", "str:split('Page VarInit')"; +const "excluded_types", "str:split('Page VarInit VarInitPersistent')"; // Key to filter unique types key "TypesKey", "widget", "@type";