SVGHMI: add get_idx(index) to widget objects to factorize a bit code repeated accross widgets. To be continued. svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Mon, 03 Aug 2020 16:34:44 +0200
branchsvghmi
changeset 3001 003fd80ff0b8
parent 3000 a9a45977bac0
child 3002 30b0635fe92e
SVGHMI: add get_idx(index) to widget objects to factorize a bit code repeated accross widgets. To be continued.
svghmi/widget_dropdown.ysl2
svghmi/widget_input.ysl2
svghmi/widgets_common.ysl2
--- a/svghmi/widget_dropdown.ysl2	Mon Aug 03 16:32:23 2020 +0200
+++ b/svghmi/widget_dropdown.ysl2	Mon Aug 03 16:34:44 2020 +0200
@@ -38,9 +38,7 @@
     // Called when a menu entry is clicked
     on_selection_click: function(selection) {
         this.close();
-        let orig = this.indexes[0];
-        let idx = this.offset ? orig - this.offset : orig;
-        apply_hmi_value(idx, selection);
+        apply_hmi_value(this.get_idx(0), selection);
     },
     on_button_click: function() {
         this.open();
--- a/svghmi/widget_input.ysl2	Mon Aug 03 16:32:23 2020 +0200
+++ b/svghmi/widget_input.ysl2	Mon Aug 03 16:34:44 2020 +0200
@@ -26,9 +26,7 @@
     }
     |     },
     |     on_op_click: function(opstr) {
-    |         let orig = this.indexes[0];
-    |         let idx = this.offset ? orig - this.offset : orig;
-    |         let new_val = change_hmi_value(idx, opstr);
+    |         let new_val = change_hmi_value(this.get_idx(0), opstr);
     //     if "$have_value"{
     // |         this.value_elt.textContent = String(new_val);
     //           /* TODO gray out value until refreshed */
@@ -39,9 +37,7 @@
     |     },
 
     |     edit_callback: function(new_val) {
-    |         let orig = this.indexes[0];
-    |         let idx = this.offset ? orig - this.offset : orig;
-    |         apply_hmi_value(idx, new_val);
+    |         apply_hmi_value(this.get_idx(0), new_val);
     //     if "$have_value"{
     // |         this.value_elt.textContent = String(new_val);
     //           /* TODO gray out value until refreshed */
--- a/svghmi/widgets_common.ysl2	Mon Aug 03 16:32:23 2020 +0200
+++ b/svghmi/widgets_common.ysl2	Mon Aug 03 16:34:44 2020 +0200
@@ -105,6 +105,10 @@
             }
         }
 
+        get_idx(index) {
+             let orig = this.indexes[index];
+             return this.offset ? orig + this.offset : orig;
+        }
     }
     ||
 }