svghmi/widget_dropdown.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 14 Apr 2020 17:17:35 +0200
branchsvghmi
changeset 2930 41edcb8e0a01
parent 2928 c73d0b042ca8
child 2931 f51c0e2e0bcb
permissions -rw-r--r--
SVGHMI: Connect HMI:DropDown to value in HMI Tree, in svghmi test as well.
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));
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    17
        this.content = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    18
                        "eleven", "twelve", "thirteen", "fourteen", "fifteen"];
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
    19
        //this.content = ["one", "two", "three", "four", "5", "6"];
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
    20
        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
    21
        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
    22
        this.opened = false;
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    23
        this.bound_inhibit_click_elsewhere = this.inhibit_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
    24
    },
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
    25
    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
    26
        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
    27
        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
    28
        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
    29
        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
    30
        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
    31
    },
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
    32
    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
    33
        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
    34
    },
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    35
    on_backward_click:function(){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    36
        this.move(false);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    37
    },
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    38
    on_forward_click:function(){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    39
        this.move(true);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
    40
    },
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
    41
    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
    42
        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
    43
          (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
    44
            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
    45
    },
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
    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
    47
        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
    48
        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
    49
        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
    50
        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
    51
        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
    52
        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
    53
            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
    54
            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
    55
            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
    56
            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
    57
            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
    58
            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
    59
            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
    60
                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
    61
                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
    62
                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
    63
                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
    64
                    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
    65
                } 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
    66
                    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
    67
                        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
    68
                    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
    69
                        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
    70
                    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
    71
                    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
    72
                }
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
    73
            }
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
    74
            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
    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
        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
    77
    },
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    78
    inhibit_click_elsewhere: function(e) {
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    79
        console.log("inhibit", e);
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    80
        console.log(e.target.parentNode, this.text_elt);
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    81
        if(e.target.parentNode !== this.text_elt)
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    82
            e.stopPropagation();
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    83
    },
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
    84
    close: 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
    85
        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
    86
        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
    87
        this.element.appendChild(this.button_elt);
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
    88
        this.apply_cache();
2927
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
    89
        document.removeEventListener("click", this.bound_inhibit_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
    90
        this.opened = false;
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
    },
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
    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
    93
        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
    94
        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
    95
        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
    96
            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
    97
            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
    98
            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
    99
            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
   100
        }
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
    },
2925
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   102
    move: function(forward){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   103
        let contentlength = this.content.length;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   104
        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
   105
        let spanslength = spans.length;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   106
        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
   107
        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
   108
        if(forward){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   109
            this.menu_offset = Math.min(
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   110
                contentlength - spans.length + 1, 
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   111
                this.menu_offset + spanslength);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   112
        }else{
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   113
            this.menu_offset = Math.max(
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   114
                0, 
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   115
                this.menu_offset - spanslength);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   116
        }
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   117
        console.log(this.menu_offset);
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   118
        this.set_partial_text();
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   119
    },
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
   120
    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
   121
        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
   122
        let contentlength = this.content.length;
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   123
        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
   124
        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
   125
        while(c < spanslength){
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   126
            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
   127
            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
   128
                span.textContent = "↑  ↑  ↑";
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   129
                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
   130
            }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
   131
                span.textContent = "↓  ↓  ↓";
220172cbdcff SVGHMI: HMI:DropDown now scrolls forward and backward
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2924
diff changeset
   132
                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
   133
            }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
   134
                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
   135
                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
   136
                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
   137
            }
69bb58eb0eac SVGHMI: progress on HMI:DropDown - now selects some value on click, and close
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2923
diff changeset
   138
            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
   139
        }
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
   140
    },
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
   141
    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
   142
        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
   143
        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
   144
        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
   145
        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
   146
            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
   147
        } 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
   148
            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
   149
        }
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
   150
        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
   151
        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
   152
        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
   153
        // disable interaction with background
23c35f3ba111 SVGHMI: HMI:DropDown now catches all clicks when open
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2926
diff changeset
   154
        document.addEventListener("click", this.bound_inhibit_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
   155
        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
   156
    },
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
   157
    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
   158
        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
   159
        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
   160
        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
   161
        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
   162
        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
   163
            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
   164
        }
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
   165
    },
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
   166
    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
   167
        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
   168
        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
   169
        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
   170
        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
   171
        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
   172
        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
   173
    },
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
    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
   175
        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
   176
        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
   177
        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
   178
        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
   179
        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
   180
        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
   181
        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
   182
    },
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
   183
||
2922
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   184
}
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   185
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
   186
    // |         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
   187
    // |         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
   188
    // |         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
   189
    // |         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
   190
    // |         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
   191
    // |         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
   192
    // |         this.box_elt.y.baseVal.value = nc.y;