svghmi/widget_multistate.ysl2
branchsvghmi
changeset 3018 22b969b409b0
parent 3014 1a3fd83d9136
child 3232 7bdb766c2a4d
equal deleted inserted replaced
3008:dabad70db1bf 3018:22b969b409b0
       
     1 // widget_multistate.ysl2
       
     2 
       
     3 template "widget[@type='MultiState']", mode="widget_class"
       
     4     ||
       
     5     class MultiStateWidget extends Widget{
       
     6         frequency = 5;
       
     7         state = 0;
       
     8         dispatch(value) {
       
     9             this.state = value;
       
    10             for(let choice of this.choices){
       
    11                 if(this.state != choice.value){
       
    12                     choice.elt.setAttribute("style", "display:none");
       
    13                 } else {
       
    14                     choice.elt.setAttribute("style", choice.style);
       
    15                 }
       
    16             }
       
    17         }
       
    18 
       
    19         on_click(evt) {
       
    20             //get current selected value
       
    21             let next_ind;
       
    22             for(next_ind=0; next_ind<this.choices.length; next_ind++){
       
    23                 if(this.state == this.choices[next_ind].value){
       
    24                    next_ind = next_ind + 1;
       
    25                    break;
       
    26                 }
       
    27             }
       
    28 
       
    29             //get next selected value
       
    30             if(this.choices.length > next_ind){
       
    31                 this.state = this.choices[next_ind].value;
       
    32             }
       
    33             else{
       
    34                 this.state = this.choices[0].value;
       
    35             }
       
    36 
       
    37             //post value to plc
       
    38             this.apply_hmi_value(0, this.state);
       
    39         }
       
    40 
       
    41         init() {
       
    42             this.element.setAttribute("onclick", "hmi_widgets['"+this.element_id+"'].on_click(evt)");
       
    43         }
       
    44     }
       
    45     ||
       
    46 
       
    47 template "widget[@type='MultiState']", mode="widget_defs" {
       
    48     param "hmi_element";
       
    49     |     choices: [
       
    50     const "regex",!"'^(\"[^\"].*\"|\-?[0-9]+|false|true)(#.*)?$'"!;
       
    51     foreach "$result_svg_ns//*[@id = $hmi_element/@id]//*[regexp:test(@inkscape:label,$regex)]" {
       
    52         const "literal", "regexp:match(@inkscape:label,$regex)[2]";
       
    53     |         {
       
    54     |             elt:id("«@id»"),
       
    55     |             style:"«@style»",
       
    56     |             value:«$literal»
       
    57     |         }`if "position()!=last()" > ,`
       
    58     }
       
    59     |     ],
       
    60 }