svghmi/widgets_common.ysl2
author Edouard Tisserant
Wed, 25 Mar 2020 13:36:04 +0100
branchsvghmi
changeset 2897 bf8a39cc65e4
parent 2893 d57a12b8f5db
child 2920 3ee337c8c769
permissions -rw-r--r--
SVGHMI: Generalize applying cache to widgets, be it from page or ForEach widgets. Now ForEach widget buttons work as expected.
// widgets_common.ysl2

in xsl decl labels(*ptr, name="defs_by_labels") alias call-template {
    with "hmi_element", "$hmi_element";
    with "labels"{text *ptr};
};

in xsl decl optional_labels(*ptr, name="defs_by_labels") alias call-template {
    with "hmi_element", "$hmi_element";
    with "labels"{text *ptr};
    with "mandatory","'no'";
};

template "svg:*", mode="hmi_elements" {
    const "widget", "func:widget(@id)";
    const "eltid","@id";
    |   "«@id»": {
    |     type: "«$widget/@type»",
    |     args: [
    foreach "$widget/arg"
    |         "«@value»"`if "position()!=last()" > ,`
    |     ],
    |     offset: 0,
    |     indexes: [
    foreach "$widget/path" {
        choose {
            when "not(@index)" {
                warning > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree
            }
            otherwise {
    |         «@index» /* «@value» */ `if "position()!=last()" > ,`
            }
        }
    }
    |     ],
    |     element: id("«@id»"),
    apply "$widget", mode="widget_defs" with "hmi_element",".";
    apply "$widget", mode="widget_subscribe" with "hmi_element",".";
    |   }`if "position()!=last()" > ,`
}

// default : normal subscribing
template "widget", mode="widget_subscribe" {
    |     sub: subscribe,
    |     unsub: unsubscribe,
    |     apply_cache: widget_apply_cache,
}
// page aren't subscribers
template "widget[@type='Page']", mode="widget_subscribe";

function "defs_by_labels" {
    param "labels","''";
    param "mandatory","'yes'";
    param "hmi_element";
    const "widget_type","@type";
    foreach "str:split($labels)" {
        const "name",".";
        const "elt_id","$result_svg_ns//*[@id = $hmi_element/@id]//*[@inkscape:label=$name][1]/@id";
        choose {
            when "not($elt_id)" {
                if "$mandatory='yes'" {
                    // TODO FIXME error > «$widget_type» widget must have a «$name» element
                    warning > «$widget_type» widget must have a «$name» element
                }
                // otherwise produce nothing
            }
            otherwise {
                |     «$name»_elt: id("«$elt_id»"),
            }
        }
    }
}

def "func:escape_quotes" {
    param "txt";
    // have to use a python string to enter escaped quote
    const "frst", !"substring-before($txt,'\"')"!;
    const "frstln", "string-length($frst)";
    choose {
        when "$frstln > 0 and string-length($txt) > $frstln" {
            result !"concat($frst,'\\\"',func:escape_quotes(substring-after($txt,'\"')))"!;
        }
        otherwise {
            result "$txt";
        }
    }
}