svghmi/widget_switch.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 28 Sep 2023 18:39:19 +0200
changeset 3848 91da73c3df61
parent 3518 c663d1f9f03b
permissions -rw-r--r--
Addapt to Py3.11.5 in Stunnel.py
b2a_hqx is now deprecated, base64 should bring comparable results
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
     1
// widget_switch.ysl2
2779
75c6a31caca6 SVGHMI: Work In Progress : fixed pointer types in ctypes interface, cleaned up server startup and cleanup code, changed document type to XHTML, cleaner JS script : encapsulated in a function and in CDATA.
Edouard Tisserant
parents: 2763
diff changeset
     2
3241
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     3
widget_desc("Switch") {
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     4
    longdesc
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     5
    ||
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     6
    Switch widget hides all subelements whose label do not match given
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     7
    variable current value representation. For exemple if given variable type
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     8
    is HMI_INT and value is 1, then elements with label '1' will be displayed.
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
     9
    Label can have comments, so '1#some comment' would also match. If matching
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    10
    variable of type HMI_STRING, then double quotes must be used. For exemple,
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    11
    '"hello"' or '"hello"#another comment' match HMI_STRING 'hello'.
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    12
    ||
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    13
3386
608f48ad3dfc SVGHMI: multiple non significant changes: doc fixes, modern style access to attributes, enforce double quotes around member name in object literals.
Edouard Tisserant
parents: 3241
diff changeset
    14
    shortdesc > Show elements whose label matches value.
3241
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    15
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    16
    // TODO: add optional format/precision argument to support floating points
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    17
    // TODO: support (in)equations and ranges
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    18
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    19
    path name="value" accepts="HMI_INT,HMI_STRING" > value to compare to labels
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    20
    
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    21
}
fe945f1f48b7 SVGHMI: WIP on Widget DnD UI : Added documentation to widgets, that is injected in widget parse tree during widget analysis
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3232
diff changeset
    22
3232
7bdb766c2a4d SVGHMI: In order to allow widget signature and description to coexist in same ysl2 file, introduced widget_class, widget_defs to declare widget codegen templates and gen_index_xhtml to mark templates that are only usefull in gen_index_xhtml.xslt.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3220
diff changeset
    23
widget_class("Switch")
2962
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    24
    ||
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    25
        frequency = 5;
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    26
        current_value = undefined;
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    27
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    28
        init(){
3516
d3cf85a3c282 SVGHMI: nasty scope shadowing bug... was making funny recursion, leading to really slow interaction.
Edouard Tisserant
parents: 3513
diff changeset
    29
            this.animate();
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    30
        }
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    31
2962
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    32
        dispatch(value) {
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    33
            this.current_value = value;
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    34
            this.request_animate();
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    35
        }
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    36
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    37
        animate(){
2962
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    38
            for(let choice of this.choices){
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    39
                if(this.current_value != choice.value){
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    40
                    if(choice.parent == undefined){
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    41
                        choice.parent = choice.elt.parentElement;
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    42
                        choice.parent.removeChild(choice.elt);
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    43
                    }
2962
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    44
                } else {
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    45
                    if(choice.parent != undefined){
3518
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    46
                        choice.parent.insertBefore(choice.elt,choice.sibling);
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    47
                        choice.parent = undefined;
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    48
                    }
2962
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    49
                }
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    50
            }
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    51
        }
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    52
    ||
02ea529fd08c SVGHMI: Switch widget now use class declaration
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2907
diff changeset
    53
3232
7bdb766c2a4d SVGHMI: In order to allow widget signature and description to coexist in same ysl2 file, introduced widget_class, widget_defs to declare widget codegen templates and gen_index_xhtml to mark templates that are only usefull in gen_index_xhtml.xslt.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3220
diff changeset
    54
widget_defs("Switch") {
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    55
    |     choices: [
2907
1695122a5e16 SVGHMI: HMI:Switch now accepts boolean values 'true' and 'false'
Edouard Tisserant
parents: 2883
diff changeset
    56
    const "regex",!"'^(\"[^\"].*\"|\-?[0-9]+|false|true)(#.*)?$'"!;
3220
ec365ef396b1 SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax.
Edouard Tisserant
parents: 2969
diff changeset
    57
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    58
    // this prevents matching element in sub-widgets
3220
ec365ef396b1 SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax.
Edouard Tisserant
parents: 2969
diff changeset
    59
    const "subelts", "$result_widgets[@id = $hmi_element/@id]//*";
ec365ef396b1 SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax.
Edouard Tisserant
parents: 2969
diff changeset
    60
    const "subwidgets", "$subelts//*[@id = $hmi_widgets/@id]";
ec365ef396b1 SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax.
Edouard Tisserant
parents: 2969
diff changeset
    61
    const "accepted", "$subelts[not(ancestor-or-self::*/@id = $subwidgets/@id)]";
ec365ef396b1 SVGHMI: Ensure that when widgets are nested in HMI:Switch widget, their sub-elements are taken as switch's choice in case their label match the syntax.
Edouard Tisserant
parents: 2969
diff changeset
    62
3518
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    63
    const "choices", "$accepted[regexp:test(@inkscape:label,$regex)]";
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    64
    foreach "$choices" {
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    65
        const "literal", "regexp:match(@inkscape:label,$regex)[2]";
3518
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    66
        const "sibling", "following-sibling::*[not(@id = $choices/@id)][position()=1]";
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    67
    |         {
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    68
    |             elt:id("«@id»"),
3513
7d4a16e59337 SVGHMI: rework Switch widget : detach/attach elements instead of hiding with style.display, detach all possible choices at init.
Edouard Tisserant
parents: 3386
diff changeset
    69
    |             parent:undefined,
3518
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    70
    choose {
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    71
        when "count($sibling)=0" {
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    72
    |             sibling:null,
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    73
        }
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    74
        otherwise {
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    75
    |             sibling:id("«$sibling/@id»"),
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    76
        }
c663d1f9f03b SVGHMI: fix Switch widget so that order of choices elements is preserved across detaching and re-attaching.
Edouard Tisserant
parents: 3516
diff changeset
    77
    }
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    78
    |             value:«$literal»
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    79
    |         }`if "position()!=last()" > ,`
2792
0c0d3895b036 SVGHMI: moved/fixed some templates, avoided namespace problems, added parsing of HMI:* inkscape labels
Edouard Tisserant
parents: 2791
diff changeset
    80
    }
2883
8e3d130399b0 SVGHMI: created widget_*.ysl2. Renamed widget_common in widgets_common, so that it doesn't match globing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2882
diff changeset
    81
    |     ],
2753
9a7e12e96399 SVGHMI: Added XSLT transformation, Makefile to get XSLT from ysl2 (copy of plcopen/Makefile) and a minimal stylesheet to start with.
Edouard Tisserant
parents:
diff changeset
    82
}