svghmi/widget_dropdown.ysl2
author Edouard Tisserant
Tue, 15 Dec 2020 13:43:21 +0100
branchsvghmi
changeset 3090 9e172e4e50c7
parent 3035 d1fc8c55c1d3
child 3091 f475f39713aa
permissions -rw-r--r--
SVGHMI: DropDown widget now using new class based style
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
3090
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     3
template "widget[@type='DropDown']", mode="widget_class"{
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     4
||
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     5
    class DropDownWidget extends Widget{
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     6
        dispatch(value) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     7
            if(!this.opened) this.set_selection(value);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     8
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
     9
        init() {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    10
            this.button_elt.onclick = this.on_button_click.bind(this);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    11
            // Save original size of rectangle
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    12
            this.box_bbox = this.box_elt.getBBox()
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    13
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    14
            // Compute margins
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    15
            let text_bbox = this.text_elt.getBBox();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    16
            let lmargin = text_bbox.x - this.box_bbox.x;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    17
            let tmargin = text_bbox.y - this.box_bbox.y;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    18
            this.margins = [lmargin, tmargin].map(x => Math.max(x,0));
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    19
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    20
            // Index of first visible element in the menu, when opened
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    21
            this.menu_offset = 0;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    22
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    23
            // How mutch to lift the menu vertically so that it does not cross bottom border
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    24
            this.lift = 0;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    25
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    26
            // Event handlers cannot be object method ('this' is unknown)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    27
            // as a workaround, handler given to addEventListener is bound in advance.
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    28
            this.bound_close_on_click_elsewhere = this.close_on_click_elsewhere.bind(this);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    29
            this.bound_on_selection_click = this.on_selection_click.bind(this);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    30
            this.bound_on_backward_click = this.on_backward_click.bind(this);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    31
            this.bound_on_forward_click = this.on_forward_click.bind(this);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    32
            this.opened = false;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    33
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    34
        on_button_click() {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    35
            this.open();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    36
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    37
        // Called when a menu entry is clicked
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    38
        on_selection_click(selection) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    39
            this.close();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    40
            this.apply_hmi_value(0, selection);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    41
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    42
        on_backward_click(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    43
            this.scroll(false);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    44
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    45
        on_forward_click(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    46
            this.scroll(true);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    47
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    48
        set_selection(value) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    49
            let display_str;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    50
            if(value >= 0 && value < this.content.length){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    51
                // if valid selection resolve content
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    52
                display_str = this.content[value];
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    53
                this.last_selection = value;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    54
            } else {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    55
                // otherwise show problem
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    56
                display_str = "?"+String(value)+"?";
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    57
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    58
            // It is assumed that first span always stays,
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    59
            // and contains selection when menu is closed
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    60
            this.text_elt.firstElementChild.textContent = display_str;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    61
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    62
        grow_text(up_to) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    63
            let count = 1;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    64
            let txt = this.text_elt; 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    65
            let first = txt.firstElementChild;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    66
            // Real world (pixels) boundaries of current page
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    67
            let bounds = svg_root.getBoundingClientRect(); 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    68
            this.lift = 0;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    69
            while(count < up_to) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    70
                let next = first.cloneNode();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    71
                // relative line by line text flow instead of absolute y coordinate
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    72
                next.removeAttribute("y");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    73
                next.setAttribute("dy", "1.1em");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    74
                // default content to allow computing text element bbox
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    75
                next.textContent = "...";
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    76
                // append new span to text element
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    77
                txt.appendChild(next);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    78
                // now check if text extended by one row fits to page
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    79
                // FIXME : exclude margins to be more accurate on box size
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    80
                let rect = txt.getBoundingClientRect();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    81
                if(rect.bottom > bounds.bottom){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    82
                    // in case of overflow at the bottom, lift up one row
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    83
                    let backup = first.getAttribute("dy");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    84
                    // apply lift asr a dy added too first span (y attrib stays)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    85
                    first.setAttribute("dy", "-"+String((this.lift+1)*1.1)+"em");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    86
                    rect = txt.getBoundingClientRect();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    87
                    if(rect.top > bounds.top){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    88
                        this.lift += 1;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    89
                    } else {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    90
                        // if it goes over the top, then backtrack
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    91
                        // restore dy attribute on first span
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    92
                        if(backup)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    93
                            first.setAttribute("dy", backup);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    94
                        else
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    95
                            first.removeAttribute("dy");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    96
                        // remove unwanted child
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    97
                        txt.removeChild(next);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    98
                        return count;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
    99
                    }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   100
                }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   101
                count++;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   102
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   103
            return count;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   104
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   105
        close_on_click_elsewhere(e) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   106
            // inhibit events not targetting spans (menu items)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   107
            if(e.target.parentNode !== this.text_elt){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   108
                e.stopPropagation();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   109
                // close menu in case click is outside box
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   110
                if(e.target !== this.box_elt)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   111
                    this.close();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   112
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   113
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   114
        close(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   115
            // Stop hogging all click events
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   116
            svg_root.removeEventListener("click", this.bound_close_on_click_elsewhere, true);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   117
            // Restore position and sixe of widget elements
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   118
            this.reset_text();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   119
            this.reset_box();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   120
            // Put the button back in place
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   121
            this.element.appendChild(this.button_elt);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   122
            // Mark as closed (to allow dispatch)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   123
            this.opened = false;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   124
            // Dispatch last cached value
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   125
            this.apply_cache();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   126
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   127
        // Set text content when content is smaller than menu (no scrolling)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   128
        set_complete_text(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   129
            let spans = this.text_elt.children; 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   130
            let c = 0;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   131
            for(let item of this.content){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   132
                let span=spans[c];
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   133
                span.textContent = item;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   134
                let sel = c;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   135
                span.onclick = (evt) => this.bound_on_selection_click(sel);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   136
                c++;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   137
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   138
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   139
        // Move partial view :
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   140
        // false : upward, lower value
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   141
        // true  : downward, higher value
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   142
        scroll(forward){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   143
            let contentlength = this.content.length;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   144
            let spans = this.text_elt.children; 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   145
            let spanslength = spans.length;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   146
            // reduce accounted menu size according to jumps
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   147
            if(this.menu_offset != 0) spanslength--;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   148
            if(this.menu_offset < contentlength - 1) spanslength--;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   149
            if(forward){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   150
                this.menu_offset = Math.min(
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   151
                    contentlength - spans.length + 1, 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   152
                    this.menu_offset + spanslength);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   153
            }else{
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   154
                this.menu_offset = Math.max(
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   155
                    0, 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   156
                    this.menu_offset - spanslength);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   157
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   158
            this.set_partial_text();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   159
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   160
        // Setup partial view text content
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   161
        // with jumps at first and last entry when appropriate
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   162
        set_partial_text(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   163
            let spans = this.text_elt.children; 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   164
            let contentlength = this.content.length;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   165
            let spanslength = spans.length;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   166
            let i = this.menu_offset, c = 0;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   167
            while(c < spanslength){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   168
                let span=spans[c];
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   169
                // backward jump only present if not exactly at start
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   170
                if(c == 0 && i != 0){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   171
                    span.textContent = "↑  ↑  ↑";
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   172
                    span.onclick = this.bound_on_backward_click;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   173
                // presence of forward jump when not right at the end
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   174
                }else if(c == spanslength-1 && i < contentlength - 1){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   175
                    span.textContent = "↓  ↓  ↓";
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   176
                    span.onclick = this.bound_on_forward_click;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   177
                // otherwise normal content
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   178
                }else{
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   179
                    span.textContent = this.content[i];
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   180
                    let sel = i;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   181
                    span.onclick = (evt) => this.bound_on_selection_click(sel);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   182
                    i++;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   183
                }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   184
                c++;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   185
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   186
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   187
        open(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   188
            let length = this.content.length;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   189
            // systematically reset text, to strip eventual whitespace spans
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   190
            this.reset_text();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   191
            // grow as much as needed or possible
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   192
            let slots = this.grow_text(length);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   193
            // Depending on final size
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   194
            if(slots == length) {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   195
                // show all at once
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   196
                this.set_complete_text();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   197
            } else {
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   198
                // eventualy align menu to current selection, compensating for lift
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   199
                let offset = this.last_selection - this.lift;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   200
                if(offset > 0)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   201
                    this.menu_offset = Math.min(offset + 1, length - slots + 1);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   202
                else
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   203
                    this.menu_offset = 0;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   204
                // show surrounding values
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   205
                this.set_partial_text();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   206
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   207
            // Now that text size is known, we can set the box around it
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   208
            this.adjust_box_to_text();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   209
            // Take button out until menu closed
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   210
            this.element.removeChild(this.button_elt);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   211
            // Rise widget to top by moving it to last position among siblings
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   212
            this.element.parentNode.appendChild(this.element.parentNode.removeChild(this.element));
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   213
            // disable interaction with background
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   214
            svg_root.addEventListener("click", this.bound_close_on_click_elsewhere, true);
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   215
            // mark as open
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   216
            this.opened = true;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   217
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   218
        // Put text element in normalized state
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   219
        reset_text(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   220
            let txt = this.text_elt; 
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   221
            let first = txt.firstElementChild;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   222
            // remove attribute eventually added to first text line while opening
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   223
            first.removeAttribute("onclick");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   224
            first.removeAttribute("dy");
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   225
            // keep only the first line of text
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   226
            for(let span of Array.from(txt.children).slice(1)){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   227
                txt.removeChild(span)
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   228
            }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   229
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   230
        // Put rectangle element in saved original state
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   231
        reset_box(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   232
            let m = this.box_bbox;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   233
            let b = this.box_elt;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   234
            b.x.baseVal.value = m.x;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   235
            b.y.baseVal.value = m.y;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   236
            b.width.baseVal.value = m.width;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   237
            b.height.baseVal.value = m.height;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   238
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   239
        // Use margin and text size to compute box size
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   240
        adjust_box_to_text(){
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   241
            let [lmargin, tmargin] = this.margins;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   242
            let m = this.text_elt.getBBox();
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   243
            let b = this.box_elt;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   244
            b.x.baseVal.value = m.x - lmargin;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   245
            b.y.baseVal.value = m.y - tmargin;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   246
            b.width.baseVal.value = 2 * lmargin + m.width;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   247
            b.height.baseVal.value = 2 * tmargin + m.height;
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   248
        }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   249
    }
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   250
||
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   251
}
2922
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   252
template "widget[@type='DropDown']", mode="widget_defs" {
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   253
    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
   254
    labels("text box button");
2935
83d83aa0f085 SVGHMI: Comments in HMI:DropDown source code
Edouard Tisserant
parents: 2934
diff changeset
   255
||
3090
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   256
    // It is assumed that list content conforms to Array interface.
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   257
    content: [
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   258
    ``foreach "arg" | "«@value»",
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   259
    ],
9e172e4e50c7 SVGHMI: DropDown widget now using new class based style
Edouard Tisserant
parents: 3035
diff changeset
   260
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
   261
||
2922
ddce4ebdf010 SVGHMI: intermediate commit while working on dropdown widget.
Edouard Tisserant
parents:
diff changeset
   262
}