# HG changeset patch
# User Edouard Tisserant
# Date 1613641390 -3600
# Node ID 5a1bb6ec48a0ab34f5561f60e7625cc1096f691f
# Parent d32e6246cd59f74aa784b52fd9180178263c872a
SVGHMI: JsonTable removed useless promise since fetch() already does one. Updated generated XSLT.
diff -r d32e6246cd59 -r 5a1bb6ec48a0 svghmi/gen_index_xhtml.xslt
--- a/svghmi/gen_index_xhtml.xslt Thu Feb 18 05:39:46 2021 +0100
+++ b/svghmi/gen_index_xhtml.xslt Thu Feb 18 10:43:10 2021 +0100
@@ -974,11 +974,13 @@
- var langs = [ "default",
+ var langs = [ ["Default", "C"],
- "
+ ["
- "
+ ","
+
+ "]
,
@@ -4502,6 +4504,12 @@
this.spread_json_data_bound = this.spread_json_data.bind(this);
+ this.handle_http_response_bound = this.handle_http_response.bind(this);
+
+ this.fetch_error_bound = this.fetch_error.bind(this);
+
+ this.promised = false;
+
}
@@ -4520,8 +4528,18 @@
+ fetch_error(e){
+
+ console.log("HTTP fetch error, message = " + e.message + "Widget:" + this.element_id);
+
+ }
+
+
+
do_http_request(...opt) {
+ this.abort_controller = new AbortController();
+
const query = {
args: this.args,
@@ -4546,27 +4564,61 @@
body: JSON.stringify(query),
- headers: {'Content-Type': 'application/json'}
+ headers: {'Content-Type': 'application/json'},
+
+ signal: this.abort_controller.signal
};
- fetch(this.args[0], options)
-
- .then(this.handle_http_response)
-
- .then(this.spread_json_data_bound);
-
-
-
- }
+ return fetch(this.args[0], options)
+
+ .then(this.handle_http_response_bound)
+
+ .then(this.spread_json_data_bound)
+
+ .catch(this.fetch_error_bound);
+
+
+
+ }
+
+ unsub(){
+
+ this.abort_controller.abort();
+
+ super.unsub();
+
+ }
+
+
dispatch(value, oldval, index) {
- this.cache[index] = value;
-
- this.do_http_request();
+
+
+ if(this.cache[index] != value)
+
+ this.cache[index] = value;
+
+ else
+
+ return;
+
+
+
+ if(!this.promised){
+
+ this.promised = true;
+
+ this.do_http_request().finally(() => {
+
+ this.promised = false;
+
+ });
+
+ }
}
@@ -4790,10 +4842,9 @@
-
obj_
-
+
_
try {
@@ -4811,13 +4862,6 @@
== undefined) {
- console.log("
-
-
- =
-
- ");
-
throw null;
}
@@ -4847,7 +4891,7 @@
} catch(err) {
id("
-
+
").style = "display:none";
}
@@ -4877,11 +4921,13 @@
let [range,position,jdata] = janswer;
- this.apply_hmi_value(1, range);
-
- this.apply_hmi_value(2, position);
-
- this.apply_hmi_value(3, this.visible);
+ [[1, range], [2, position], [3, this.visible]].map(([i,v]) => {
+
+ this.apply_hmi_value(i,v);
+
+ this.cache[i] = v;
+
+ });
@@ -7171,67 +7217,89 @@
function switch_langnum(langnum) {
- if(langnum == current_lang) {
-
- return;
+ langnum = Math.max(0, Math.min(langs.length - 1, langnum));
+
+
+
+ for (let translation of translations) {
+
+ let [objs, msgs] = translation;
+
+ let msg = msgs[langnum];
+
+ for (let obj of objs) {
+
+ multiline_to_svg_text(obj, msg);
+
+ obj.setAttribute("lang",langnum);
+
+ }
}
-
-
- for (let translation of translations) {
-
- let [objs, msgs, orig] = translation;
-
- let msg = langnum == 0 ? orig : msgs[langnum - 1];
-
- for (let obj of objs) {
-
- multiline_to_svg_text(obj, msg);
-
- obj.setAttribute("lang",langnum);
-
- }
+ return langnum;
+
+ }
+
+
+
+ // backup original texts
+
+ for (let translation of translations) {
+
+ let [objs, msgs] = translation;
+
+ msgs.unshift(svg_text_to_multiline(objs[0]));
+
+ }
+
+
+
+ var lang_local_index = hmi_local_index("lang");
+
+ var langcode_local_index = hmi_local_index("lang_code");
+
+ var langname_local_index = hmi_local_index("lang_name");
+
+ subscribers(lang_local_index).add({
+
+ indexes: [lang_local_index],
+
+ new_hmi_value: function(index, value, oldval) {
+
+ let current_lang = switch_langnum(value);
+
+ let [langname,langcode] = langs[current_lang];
+
+ apply_hmi_value(langcode_local_index, langcode);
+
+ apply_hmi_value(langname_local_index, langname);
+
+ switch_page();
}
- current_lang = langnum;
+ });
+
+
+
+ function setup_lang(){
+
+ let current_lang = cache[lang_local_index];
+
+ let new_lang = switch_langnum(current_lang);
+
+ if(current_lang != new_lang){
+
+ apply_hmi_value(lang_local_index, new_lang);
+
+ }
}
- // backup original texts
-
- for (let translation of translations) {
-
- let [objs] = translation;
-
- translation.push(svg_text_to_multiline(objs[0]));
-
- }
-
-
-
- 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);
-
- switch_page();
-
- }
-
- });
-
- var current_lang = 0;
-
- switch_langnum(cache[lang_local_index]);
+ setup_lang();
diff -r d32e6246cd59 -r 5a1bb6ec48a0 svghmi/widget_jsontable.ysl2
--- a/svghmi/widget_jsontable.ysl2 Thu Feb 18 05:39:46 2021 +0100
+++ b/svghmi/widget_jsontable.ysl2 Thu Feb 18 10:43:10 2021 +0100
@@ -61,11 +61,9 @@
if(!this.promised){
this.promised = true;
- Promise.resolve().then(() => {
- return this.do_http_request().finally(() => {
- this.promised = false;
- });
- })
+ this.do_http_request().finally(() => {
+ this.promised = false;
+ });
}
}
make_on_click(...options){