svghmi/widget_assign.ysl2
changeset 3627 1b627c2c743c
child 3922 ffc8da83fdc2
equal deleted inserted replaced
3626:dfcd13683362 3627:1b627c2c743c
       
     1 // widget_assign.ysl2
       
     2 
       
     3 widget_desc("Assign") {
       
     4     longdesc
       
     5     ||
       
     6 
       
     7     Arguments are either:
       
     8 
       
     9     - name=value: setting variable with literal value.
       
    10     - name=other_name: copy variable content into another
       
    11 
       
    12     "active"+"inactive" labeled elements can be provided to show feedback when pressed
       
    13 
       
    14     Exemples:
       
    15 
       
    16     HMI:Assign:notify=1@notify=/PLCVAR
       
    17     HMI:Assign:ack=2:notify=1@ack=.local_var@notify=/PLCVAR
       
    18 
       
    19     ||
       
    20 
       
    21     shortdesc > Assign variables on click
       
    22 
       
    23 }
       
    24 
       
    25 widget_class("Assign") {
       
    26 ||
       
    27         frequency = 2;
       
    28 
       
    29         onmouseup(evt) {
       
    30             svg_root.removeEventListener("pointerup", this.bound_onmouseup, true);
       
    31             if(this.enable_state) {
       
    32                 this.activity_state = false
       
    33                 this.request_animate();
       
    34                 this.assign();
       
    35             }
       
    36         }
       
    37 
       
    38         onmousedown(){
       
    39             if(this.enable_state) {
       
    40                 svg_root.addEventListener("pointerup", this.bound_onmouseup, true);
       
    41                 this.activity_state = true;
       
    42                 this.request_animate();
       
    43             }
       
    44         }
       
    45 
       
    46 ||
       
    47 }
       
    48 
       
    49 widget_defs("Assign") {
       
    50     optional_activable();
       
    51 
       
    52     |     init: function() {
       
    53     |         this.bound_onmouseup = this.onmouseup.bind(this);
       
    54     |         this.element.addEventListener("pointerdown", this.onmousedown.bind(this));
       
    55     |     },
       
    56 
       
    57     |     assignments: {},
       
    58     |     dispatch: function(value, oldval, varnum) {
       
    59     const "widget", ".";
       
    60     foreach "path" {
       
    61         const "varid","generate-id()";
       
    62         const "varnum","position()-1";
       
    63         if "@assign" foreach "$widget/path[@assign]" if "$varid = generate-id()" {
       
    64     |         if(varnum == «$varnum») this.assignments["«@assign»"] = value;
       
    65         }
       
    66     }
       
    67     |     },
       
    68     |     assign: function() {
       
    69     const "paths","path";
       
    70     foreach "arg[contains(@value,'=')]"{
       
    71         const "name","substring-before(@value,'=')";
       
    72         const "value","substring-after(@value,'=')";
       
    73         const "index" foreach "$paths" if "@assign = $name" value "position()-1";
       
    74         const "isVarName", "regexp:test($value,'^[a-zA-Z_][a-zA-Z0-9_]+$')";
       
    75         choose {
       
    76             when "$isVarName"{
       
    77     |         const «$value» = this.assignments["«$value»"];
       
    78     |         if(«$value» != undefined)
       
    79     |             this.apply_hmi_value(«$index», «$value»);
       
    80             }
       
    81             otherwise {
       
    82     |         this.apply_hmi_value(«$index», «$value»);
       
    83             }
       
    84         }
       
    85     }
       
    86     |     },
       
    87 }
       
    88