edouard@2883: // widget_switch.ysl2
Edouard@2779: 
edouard@3241: widget_desc("Switch") {
edouard@3241:     longdesc
edouard@3241:     ||
edouard@3241:     Switch widget hides all subelements whose label do not match given
edouard@3241:     variable current value representation. For exemple if given variable type
edouard@3241:     is HMI_INT and value is 1, then elements with label '1' will be displayed.
edouard@3241:     Label can have comments, so '1#some comment' would also match. If matching
edouard@3241:     variable of type HMI_STRING, then double quotes must be used. For exemple,
edouard@3241:     '"hello"' or '"hello"#another comment' match HMI_STRING 'hello'.
edouard@3241:     ||
edouard@3241: 
Edouard@3386:     shortdesc > Show elements whose label matches value.
edouard@3241: 
edouard@3241:     // TODO: add optional format/precision argument to support floating points
edouard@3241:     // TODO: support (in)equations and ranges
edouard@3241: 
edouard@3241:     path name="value" accepts="HMI_INT,HMI_STRING" > value to compare to labels
edouard@3241:     
edouard@3241: }
edouard@3241: 
edouard@3232: widget_class("Switch")
edouard@2962:     ||
edouard@2962:         frequency = 5;
edouard@2962:         dispatch(value) {
edouard@2962:             for(let choice of this.choices){
edouard@2962:                 if(value != choice.value){
edouard@2962:                     choice.elt.setAttribute("style", "display:none");
edouard@2962:                 } else {
edouard@2962:                     choice.elt.setAttribute("style", choice.style);
edouard@2962:                 }
edouard@2962:             }
edouard@2962:         }
edouard@2962:     ||
edouard@2962: 
edouard@3232: widget_defs("Switch") {
edouard@2883:     |     choices: [
Edouard@2907:     const "regex",!"'^(\"[^\"].*\"|\-?[0-9]+|false|true)(#.*)?$'"!;
Edouard@3220: 
Edouard@3220:     const "subelts", "$result_widgets[@id = $hmi_element/@id]//*";
Edouard@3220:     const "subwidgets", "$subelts//*[@id = $hmi_widgets/@id]";
Edouard@3220:     const "accepted", "$subelts[not(ancestor-or-self::*/@id = $subwidgets/@id)]";
Edouard@3220: 
Edouard@3220:     foreach "$accepted[regexp:test(@inkscape:label,$regex)]" {
edouard@2883:         const "literal", "regexp:match(@inkscape:label,$regex)[2]";
edouard@2883:     |         {
edouard@2883:     |             elt:id("«@id»"),
edouard@3241:                   // TODO : use style.display = "none" to hide element
edouard@2883:     |             style:"«@style»",
edouard@2883:     |             value:«$literal»
edouard@2883:     |         }`if "position()!=last()" > ,`
Edouard@2792:     }
edouard@2883:     |     ],
Edouard@2753: }