svghmi/widget_dropdown.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 14 Apr 2020 22:08:52 +0200
branchsvghmi
changeset 2933 ed3f712a8eef
parent 2932 abb995a30088
child 2934 ee483e8346f5
permissions -rw-r--r--
SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
2922
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
     1
// widget_dropdown.ysl2
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
     2
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
     3
template "widget[@type='DropDown']", mode="widget_defs" {
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
     4
    param "hmi_element";
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
     5
    labels("text box button");
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
     6
||    
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
     7
    dispatch: function(value) {
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
     8
        if(!this.opened) this.set_selection(value);
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
     9
    },
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    10
    init: function() {
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    11
        this.button_elt.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_button_click()");
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    12
        this.text_bbox = this.text_elt.getBBox()
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    13
        this.box_bbox = this.box_elt.getBBox()
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    14
        lmargin = this.text_bbox.x - this.box_bbox.x;
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    15
        tmargin = this.text_bbox.y - this.box_bbox.y;
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    16
        this.margins = [lmargin, tmargin].map(x => Math.max(x,0));
2931
f51c0e2e0bcb SVGHMI: HMI:DropDown now temporarily takes content from widget's arguments, until HMI:List is implemented.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2930
diff changeset
    17
        //this.content = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
f51c0e2e0bcb SVGHMI: HMI:DropDown now temporarily takes content from widget's arguments, until HMI:List is implemented.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2930
diff changeset
    18
        //                "eleven", "twelve", "thirteen", "fourteen", "fifteen"];
f51c0e2e0bcb SVGHMI: HMI:DropDown now temporarily takes content from widget's arguments, until HMI:List is implemented.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2930
diff changeset
    19
        this.content = [
f51c0e2e0bcb SVGHMI: HMI:DropDown now temporarily takes content from widget's arguments, until HMI:List is implemented.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2930
diff changeset
    20
        ``foreach "arg" | "«@value»",
f51c0e2e0bcb SVGHMI: HMI:DropDown now temporarily takes content from widget's arguments, until HMI:List is implemented.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2930
diff changeset
    21
        ];
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    22
        this.menu_offset = 0;
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    23
        this.lift = 0;
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    24
        this.opened = false;
2933
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    25
        this.bound_close_on_click_elsewhere = this.close_on_click_elsewhere.bind(this);
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    26
    },
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    27
    on_selection_click: function(selection) {
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    28
        console.log("selected "+selection);
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    29
        this.close();
2930
41edcb8e0a01 SVGHMI: Connect HMI:DropDown to value in HMI Tree, in svghmi test as well.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2928
diff changeset
    30
        let orig = this.indexes[0];
41edcb8e0a01 SVGHMI: Connect HMI:DropDown to value in HMI Tree, in svghmi test as well.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2928
diff changeset
    31
        let idx = this.offset ? orig - this.offset : orig;
41edcb8e0a01 SVGHMI: Connect HMI:DropDown to value in HMI Tree, in svghmi test as well.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2928
diff changeset
    32
        apply_hmi_value(idx, selection);
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    33
    },
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    34
    on_button_click: function() {
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    35
        this.open();
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    36
    },
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    37
    on_backward_click:function(){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    38
        this.move(false);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    39
    },
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    40
    on_forward_click:function(){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    41
        this.move(true);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    42
    },
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    43
    set_selection: function(value) {
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    44
        this.text_elt.firstElementChild.textContent = 
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    45
          (value >= 0 && value < this.content.length) ?
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    46
            this.content[value] : "?"+String(value)+"?";
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    47
    },
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    48
    grow_text: function(up_to) {
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    49
        let count = 1;
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    50
        let txt = this.text_elt; 
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    51
        let first = txt.firstElementChild;
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    52
        let bounds = svg_root.getBoundingClientRect(); 
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    53
        this.lift = 0;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    54
        while(count < up_to) {
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    55
            let next = first.cloneNode();
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    56
            next.removeAttribute("y");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    57
            next.setAttribute("dy", "1.1em");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    58
            next.textContent = "...";
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    59
            txt.appendChild(next);
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    60
            let rect = txt.getBoundingClientRect();
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    61
            if(rect.bottom > bounds.bottom){
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    62
                let backup = first.getAttribute("dy");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    63
                first.setAttribute("dy", "-"+String((this.lift+1)*1.1)+"em");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    64
                rect = txt.getBoundingClientRect();
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    65
                if(rect.top > bounds.top){
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    66
                    this.lift += 1;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    67
                } else {
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    68
                    if(backup)
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    69
                        first.setAttribute("dy", backup);
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    70
                    else
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    71
                        first.removeAttribute("dy");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    72
                    txt.removeChild(next);
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    73
                    return count;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    74
                }
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    75
            }
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    76
            count++;
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
    77
        }
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    78
        return count;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    79
    },
2933
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    80
    close_on_click_elsewhere: function(e) {
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    81
        console.log("inhibit", e);
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    82
        console.log(e.target.parentNode, this.text_elt);
2933
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    83
        if(e.target.parentNode !== this.text_elt){
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    84
            e.stopPropagation();
2933
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    85
            if(e.target !== this.box_elt)
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    86
                this.close();
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    87
        }
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    88
    },
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    89
    close: function(){
2933
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
    90
        document.removeEventListener("click", this.bound_close_on_click_elsewhere, true);
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    91
        this.reset_text();
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    92
        this.reset_box();
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
    93
        this.element.appendChild(this.button_elt);
2932
abb995a30088 SVGHMI: Fix HMI:DropDown flicker on close.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2931
diff changeset
    94
        this.opened = false;
2930
41edcb8e0a01 SVGHMI: Connect HMI:DropDown to value in HMI Tree, in svghmi test as well.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2928
diff changeset
    95
        this.apply_cache();
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    96
    },
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    97
    set_complete_text: function(){
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    98
        let spans = this.text_elt.children; 
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    99
        let c = 0;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   100
        for(let item of this.content){
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   101
            let span=spans[c];
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   102
            span.textContent = item;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   103
            span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_selection_click("+c+")");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   104
            c++;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   105
        }
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   106
    },
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   107
    move: function(forward){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   108
        let contentlength = this.content.length;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   109
        let spans = this.text_elt.children; 
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   110
        let spanslength = spans.length;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   111
        if(this.menu_offset != 0) spanslength--;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   112
        if(this.menu_offset < contentlength - 1) spanslength--;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   113
        if(forward){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   114
            this.menu_offset = Math.min(
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   115
                contentlength - spans.length + 1, 
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   116
                this.menu_offset + spanslength);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   117
        }else{
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   118
            this.menu_offset = Math.max(
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   119
                0, 
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   120
                this.menu_offset - spanslength);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   121
        }
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   122
        console.log(this.menu_offset);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   123
        this.set_partial_text();
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   124
    },
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   125
    set_partial_text: function(){
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   126
        let spans = this.text_elt.children; 
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   127
        let contentlength = this.content.length;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   128
        let spanslength = spans.length;
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   129
        let i = this.menu_offset, c = 0;
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   130
        while(c < spanslength){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   131
            let span=spans[c];
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   132
            if(c == 0 && i != 0){
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   133
                span.textContent = "↑  ↑  ↑";
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   134
                span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_backward_click()");
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   135
            }else if(c == spanslength-1 && i < contentlength - 1){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   136
                span.textContent = "↓  ↓  ↓";
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   137
                span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_forward_click()");
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   138
            }else{
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   139
                span.textContent = this.content[i];
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   140
                span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_selection_click("+i+")");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   141
                i++;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   142
            }
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   143
            c++;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   144
        }
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   145
    },
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   146
    open: function(){
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   147
        let length = this.content.length;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   148
        this.reset_text();
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   149
        let slots = this.grow_text(length);
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   150
        if(slots == length) {
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   151
            this.set_complete_text();
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   152
        } else {
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   153
            this.set_partial_text();
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   154
        }
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   155
        this.adjust_box_to_text();
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
   156
        this.element.removeChild(this.button_elt);
2928
c73d0b042ca8 SVGHMI: HMI:DropDown element is brought in front when opened, so that it isn't occulted by other widgets at the same level.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2927
diff changeset
   157
        this.element.parentNode.appendChild(this.element.parentNode.removeChild(this.element));
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
   158
        // disable interaction with background
2933
ed3f712a8eef SVGHMI: When clicking outside of opened HMI:DropDown box, close it.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2932
diff changeset
   159
        document.addEventListener("click", this.bound_close_on_click_elsewhere, true);
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   160
        this.opened = true;
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   161
    },
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   162
    reset_text: function(){
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   163
        let txt = this.text_elt; 
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   164
        let first = txt.firstElementChild;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   165
        first.removeAttribute("onclick");
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   166
        first.removeAttribute("dy");
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   167
        for(let span of Array.from(txt.children).slice(1)){
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   168
            txt.removeChild(span)
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   169
        }
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   170
    },
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   171
    reset_box: function(){
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   172
        let m = this.box_bbox;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   173
        let b = this.box_elt;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   174
        b.x.baseVal.value = m.x;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   175
        b.y.baseVal.value = m.y;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   176
        b.width.baseVal.value = m.width;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   177
        b.height.baseVal.value = m.height;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   178
    },
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   179
    adjust_box_to_text: function(){
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
   180
        let [lmargin, tmargin] = this.margins;
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   181
        let m = this.text_elt.getBBox();
2924
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   182
        let b = this.box_elt;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   183
        b.x.baseVal.value = m.x - lmargin;
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   184
        b.y.baseVal.value = m.y - tmargin;
2926
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
   185
        b.width.baseVal.value = 2 * lmargin + m.width;
90f9d9782632 SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2925
diff changeset
   186
        b.height.baseVal.value = 2 * tmargin + m.height;
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   187
    },
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   188
||
2922
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   189
}
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   190
2923
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   191
    // |         let p = new DOMPoint(this.box_elt.x.baseVal.value, this.box_elt.y.baseVal.value);
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   192
    // |         let k = DOMMatrix.fromMatrix(this.box_elt.getCTM());
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   193
    // |         let new_corner = k.transformPoint(p);
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   194
    // |         new_corner.y = 0;
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   195
    // |         let nc = k.inverse().transformPoint(new_corner);
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   196
    // |         this.box_elt.x.baseVal.value = nc.x;
5ec1c07ce582 SVGHMI: HMI:DropDown work in progress, intermediate commit. Now menu's rectangle grows up to viewport borders.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2922
diff changeset
   197
    // |         this.box_elt.y.baseVal.value = nc.y;