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@3513: current_value = undefined; Edouard@3513: Edouard@3513: init(){ Edouard@3516: this.animate(); Edouard@3513: } Edouard@3513: edouard@2962: dispatch(value) { Edouard@3513: this.current_value = value; Edouard@3513: this.request_animate(); Edouard@3513: } Edouard@3513: Edouard@3513: animate(){ edouard@2962: for(let choice of this.choices){ Edouard@3513: if(this.current_value != choice.value){ Edouard@3513: if(choice.parent == undefined){ Edouard@3513: choice.parent = choice.elt.parentElement; Edouard@3513: choice.parent.removeChild(choice.elt); Edouard@3513: } edouard@2962: } else { Edouard@3513: if(choice.parent != undefined){ Edouard@3518: choice.parent.insertBefore(choice.elt,choice.sibling); Edouard@3513: choice.parent = undefined; Edouard@3513: } 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@3513: // this prevents matching element in sub-widgets 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@3518: const "choices", "$accepted[regexp:test(@inkscape:label,$regex)]"; Edouard@3518: foreach "$choices" { edouard@2883: const "literal", "regexp:match(@inkscape:label,$regex)[2]"; Edouard@3518: const "sibling", "following-sibling::*[not(@id = $choices/@id)][position()=1]"; edouard@2883: | { edouard@2883: | elt:id("«@id»"), Edouard@3513: | parent:undefined, Edouard@3518: choose { Edouard@3518: when "count($sibling)=0" { Edouard@3518: | sibling:null, Edouard@3518: } Edouard@3518: otherwise { Edouard@3518: | sibling:id("«$sibling/@id»"), Edouard@3518: } Edouard@3518: } edouard@2883: | value:«$literal» edouard@2883: | }`if "position()!=last()" > ,` Edouard@2792: } edouard@2883: | ], Edouard@2753: }