svghmi/widget_tooglebutton.ysl2
author Edouard Tisserant
Mon, 17 May 2021 08:52:38 +0200
branchsvghmi
changeset 3240 5f756332ada1
parent 3219 cc0ecc5e918f
child 3232 7bdb766c2a4d
permissions -rw-r--r--
SVGHMI: Fix behaviour of Jump widget, that wasn't displaying "disabled" state in some cases.
2977
82f062408e70 SVGHMI: Added widget toggleButton.
dgaberscek
parents:
diff changeset
     1
// widget_tooglebutton.ysl2
82f062408e70 SVGHMI: Added widget toggleButton.
dgaberscek
parents:
diff changeset
     2
3011
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
     3
3024
Edouard Tisserant
parents: 3018
diff changeset
     4
template "widget[@type='ToggleButton']", mode="widget_class"{
3011
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
     5
    ||
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
     6
    class ToggleButtonWidget extends Widget{
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
     7
        frequency = 5;
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
     8
        state = 0;
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
     9
        active_style = undefined;
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    10
        inactive_style = undefined;
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    11
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    12
        dispatch(value) {
3059
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    13
            this.state = value;
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    14
            //redraw toggle button
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    15
            this.request_animate();
3011
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    16
        }
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    17
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    18
        on_click(evt) {
3059
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    19
            //toggle state and apply
3219
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    20
            this.state = this.state ? false : true;
3018
Edouard Tisserant
parents: 3011
diff changeset
    21
            this.apply_hmi_value(0, this.state);
3059
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    22
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    23
            //redraw toggle button
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    24
            this.request_animate();
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    25
        }
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    26
3219
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    27
        activate(val) {
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    28
            let [active, inactive] = val ? ["none",""] : ["", "none"];
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    29
            if (this.active_elt)
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    30
                this.active_elt.style.display = active;
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    31
            if (this.inactive_elt)
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    32
                this.inactive_elt.style.display = inactive;
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    33
        }
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    34
3059
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    35
        animate(){
3219
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    36
            // redraw toggle button on screen refresh
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    37
            this.activate(this.state);
3011
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    38
        }
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    39
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    40
        init() {
3219
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    41
            this.activate(false);
cc0ecc5e918f SVGHMI: Code cleanup for ToggleButton.
Edouard Tisserant
parents: 3059
diff changeset
    42
            this.element.onclick = (evt) => this.on_click(evt);
3011
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    43
        }
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    44
    }
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    45
    ||
3024
Edouard Tisserant
parents: 3018
diff changeset
    46
}
3011
601c6dbc1da7 Reworked togglebutton widget to extand class widget
usveticic
parents: 3004
diff changeset
    47
2977
82f062408e70 SVGHMI: Added widget toggleButton.
dgaberscek
parents:
diff changeset
    48
template "widget[@type='ToggleButton']", mode="widget_defs" {
82f062408e70 SVGHMI: Added widget toggleButton.
dgaberscek
parents:
diff changeset
    49
    param "hmi_element";
3059
e0db3f6a5f39 Button and toggle reworked to use animate and dispatch
usveticic
parents: 3056
diff changeset
    50
    optional_labels("active inactive");
3004
705e34c6fe93 SVGHMI: More JS code refactoring : change_hmi_value and apply_hmi_value now methods of widget class.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2977
diff changeset
    51
}