--- a/svghmi/gen_index_xhtml.xslt Wed Apr 15 14:29:19 2020 +0200
+++ b/svghmi/gen_index_xhtml.xslt Thu Apr 16 10:21:25 2020 +0200
@@ -837,19 +837,25 @@
<xsl:value-of select="$hmi_element/@id"/>
<xsl:text>'].on_button_click()");
- <xsl:text> this.text_bbox = this.text_elt.getBBox()
+ <xsl:text> // Save original size of rectangle <xsl:text> this.box_bbox = this.box_elt.getBBox()
- <xsl:text> lmargin = this.text_bbox.x - this.box_bbox.x;
- <xsl:text> tmargin = this.text_bbox.y - this.box_bbox.y;
+ <xsl:text> // Compute margins + <xsl:text> text_bbox = this.text_elt.getBBox() + <xsl:text> lmargin = text_bbox.x - this.box_bbox.x; + <xsl:text> tmargin = text_bbox.y - this.box_bbox.y; <xsl:text> this.margins = [lmargin, tmargin].map(x => Math.max(x,0));
- <xsl:text> //this.content = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
- <xsl:text> // "eleven", "twelve", "thirteen", "fourteen", "fifteen"];
+ <xsl:text> // It is assumed that list content conforms to Array interface. <xsl:text> this.content = [
@@ -861,20 +867,36 @@
+ <xsl:text> // Index of first visible element in the menu, when opened <xsl:text> this.menu_offset = 0;
+ <xsl:text> // How mutch to lift the menu vertically so that it does not cross bottom border <xsl:text> this.lift = 0;
- <xsl:text> this.opened = false;
+ <xsl:text> // Event handlers cannot be object method ('this' is unknown) + <xsl:text> // as a workaround, handler given to addEventListener is bound in advance. <xsl:text> this.bound_close_on_click_elsewhere = this.close_on_click_elsewhere.bind(this);
+ <xsl:text> this.opened = false; + <xsl:text> // Called when a menu entry is clicked <xsl:text> on_selection_click: function(selection) {
- <xsl:text> console.log("selected "+selection);
<xsl:text> let orig = this.indexes[0];
@@ -891,15 +913,15 @@
- <xsl:text> on_backward_click:function(){
- <xsl:text> this.move(false);
+ <xsl:text> on_backward_click: function(){ + <xsl:text> this.scroll(false); <xsl:text> on_forward_click:function(){
- <xsl:text> this.move(true);
+ <xsl:text> this.scroll(true); @@ -909,16 +931,24 @@
<xsl:text> if(value >= 0 && value < this.content.length){
+ <xsl:text> // if valid selection resolve content <xsl:text> display_str = this.content[value];
<xsl:text> this.last_selection = value;
+ <xsl:text> // otherwise show problem <xsl:text> display_str = "?"+String(value)+"?";
+ <xsl:text> // It is assumed that first span always stays, + <xsl:text> // and contains selection when menu is closed <xsl:text> this.text_elt.firstElementChild.textContent = display_str;
@@ -931,6 +961,8 @@
<xsl:text> let first = txt.firstElementChild;
+ <xsl:text> // Real world (pixels) boundaries of current page <xsl:text> let bounds = svg_root.getBoundingClientRect();
<xsl:text> this.lift = 0;
@@ -939,20 +971,34 @@
<xsl:text> let next = first.cloneNode();
+ <xsl:text> // relative line by line text flow instead of absolute y coordinate <xsl:text> next.removeAttribute("y");
<xsl:text> next.setAttribute("dy", "1.1em");
+ <xsl:text> // default content to allow computing text element bbox <xsl:text> next.textContent = "...";
+ <xsl:text> // append new span to text element <xsl:text> txt.appendChild(next);
+ <xsl:text> // now check if text extended by one row fits to page + <xsl:text> // FIXME : exclude margins to be more accurate on box size <xsl:text> let rect = txt.getBoundingClientRect();
<xsl:text> if(rect.bottom > bounds.bottom){
+ <xsl:text> // in case of overflow at the bottom, lift up one row <xsl:text> let backup = first.getAttribute("dy");
+ <xsl:text> // apply lift asr a dy added too first span (y attrib stays) <xsl:text> first.setAttribute("dy", "-"+String((this.lift+1)*1.1)+"em");
<xsl:text> rect = txt.getBoundingClientRect();
@@ -963,6 +1009,10 @@
+ <xsl:text> // if it goes over the top, then backtrack + <xsl:text> // restore dy attribute on first span <xsl:text> first.setAttribute("dy", backup);
@@ -971,6 +1021,8 @@
<xsl:text> first.removeAttribute("dy");
+ <xsl:text> // remove unwanted child <xsl:text> txt.removeChild(next);
@@ -989,14 +1041,14 @@
<xsl:text> close_on_click_elsewhere: function(e) {
- <xsl:text> console.log("inhibit", e);
- <xsl:text> console.log(e.target.parentNode, this.text_elt);
+ <xsl:text> // inhibit events not targetting spans (menu items) <xsl:text> if(e.target.parentNode !== this.text_elt){
<xsl:text> e.stopPropagation();
+ <xsl:text> // close menu in case click is outside box <xsl:text> if(e.target !== this.box_elt)
@@ -1007,20 +1059,32 @@
<xsl:text> close: function(){
- <xsl:text> document.removeEventListener("click", this.bound_close_on_click_elsewhere, true);
+ <xsl:text> // Stop hogging all click events + <xsl:text> svg_root.removeEventListener("click", this.bound_close_on_click_elsewhere, true); + <xsl:text> // Restore position and sixe of widget elements <xsl:text> this.reset_text();
<xsl:text> this.reset_box();
+ <xsl:text> // Put the button back in place <xsl:text> this.element.appendChild(this.button_elt);
+ <xsl:text> // Mark as closed (to allow dispatch) <xsl:text> this.opened = false;
+ <xsl:text> // Dispatch last cached value <xsl:text> this.apply_cache();
+ <xsl:text> // Set text content when content is smaller than menu (no scrolling) <xsl:text> set_complete_text: function(){
<xsl:text> let spans = this.text_elt.children;
@@ -1043,7 +1107,13 @@
- <xsl:text> move: function(forward){
+ <xsl:text> // Move partial view : + <xsl:text> // false : upward, lower value + <xsl:text> // true : downward, higher value + <xsl:text> scroll: function(forward){ <xsl:text> let contentlength = this.content.length;
@@ -1051,6 +1121,8 @@
<xsl:text> let spanslength = spans.length;
+ <xsl:text> // reduce accounted menu size according to jumps <xsl:text> if(this.menu_offset != 0) spanslength--;
<xsl:text> if(this.menu_offset < contentlength - 1) spanslength--;
@@ -1079,6 +1151,10 @@
+ <xsl:text> // Setup partial view text content + <xsl:text> // with jumps at first and last entry when appropriate <xsl:text> set_partial_text: function(){
<xsl:text> let spans = this.text_elt.children;
@@ -1093,6 +1169,8 @@
<xsl:text> let span=spans[c];
+ <xsl:text> // backward jump only present if not exactly at start <xsl:text> if(c == 0 && i != 0){
<xsl:text> span.textContent = "↑ ↑ ↑";
@@ -1101,6 +1179,8 @@
<xsl:value-of select="$hmi_element/@id"/>
<xsl:text>'].on_backward_click()");
+ <xsl:text> // presence of forward jump when not right at the end <xsl:text> }else if(c == spanslength-1 && i < contentlength - 1){
<xsl:text> span.textContent = "↓ ↓ ↓";
@@ -1109,6 +1189,8 @@
<xsl:value-of select="$hmi_element/@id"/>
<xsl:text>'].on_forward_click()");
+ <xsl:text> // otherwise normal content <xsl:text> span.textContent = this.content[i];
@@ -1131,17 +1213,25 @@
<xsl:text> let length = this.content.length;
+ <xsl:text> // systematically reset text, to strip eventual whitespace spans <xsl:text> this.reset_text();
+ <xsl:text> // grow as much as needed or possible <xsl:text> let slots = this.grow_text(length);
+ <xsl:text> // Depending on final size <xsl:text> if(slots == length) {
+ <xsl:text> // show all at once <xsl:text> this.set_complete_text();
- <xsl:text> // align to selection
+ <xsl:text> // eventualy align menu to current selection, compensating for lift <xsl:text> let offset = this.last_selection - this.lift;
@@ -1153,34 +1243,50 @@
<xsl:text> this.menu_offset = 0;
+ <xsl:text> // show surrounding values <xsl:text> this.set_partial_text();
+ <xsl:text> // Now that text size is known, we can set the box around it <xsl:text> this.adjust_box_to_text();
+ <xsl:text> // Take button out until menu closed <xsl:text> this.element.removeChild(this.button_elt);
+ <xsl:text> // Rise widget to top by moving it to last position among siblings <xsl:text> this.element.parentNode.appendChild(this.element.parentNode.removeChild(this.element));
<xsl:text> // disable interaction with background
- <xsl:text> document.addEventListener("click", this.bound_close_on_click_elsewhere, true);
+ <xsl:text> svg_root.addEventListener("click", this.bound_close_on_click_elsewhere, true); + <xsl:text> // mark as open <xsl:text> this.opened = true;
+ <xsl:text> // Put text element in normalized state <xsl:text> reset_text: function(){
<xsl:text> let txt = this.text_elt;
<xsl:text> let first = txt.firstElementChild;
+ <xsl:text> // remove attribute eventually added to first text line while opening <xsl:text> first.removeAttribute("onclick");
<xsl:text> first.removeAttribute("dy");
+ <xsl:text> // keep only the first line of text <xsl:text> for(let span of Array.from(txt.children).slice(1)){
<xsl:text> txt.removeChild(span)
@@ -1189,6 +1295,8 @@
+ <xsl:text> // Put rectangle element in saved original state <xsl:text> reset_box: function(){
<xsl:text> let m = this.box_bbox;
@@ -1205,6 +1313,8 @@
+ <xsl:text> // Use margin and text size to compute box size <xsl:text> adjust_box_to_text: function(){
<xsl:text> let [lmargin, tmargin] = this.margins;
--- a/svghmi/widget_dropdown.ysl2 Wed Apr 15 14:29:19 2020 +0200
+++ b/svghmi/widget_dropdown.ysl2 Thu Apr 16 10:21:25 2020 +0200
@@ -9,10 +9,13 @@
this.button_elt.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_button_click()");
- this.text_bbox = this.text_elt.getBBox()
+ // Save original size of rectangle this.box_bbox = this.box_elt.getBBox()
- lmargin = this.text_bbox.x - this.box_bbox.x;
- tmargin = this.text_bbox.y - this.box_bbox.y;
+ text_bbox = this.text_elt.getBBox() + lmargin = text_bbox.x - this.box_bbox.x; + tmargin = text_bbox.y - this.box_bbox.y; this.margins = [lmargin, tmargin].map(x => Math.max(x,0));
// It is assumed that list content conforms to Array interface.
@@ -145,6 +148,7 @@
let contentlength = this.content.length;
let spans = this.text_elt.children;
let spanslength = spans.length;
+ // reduce accounted menu size according to jumps if(this.menu_offset != 0) spanslength--;
if(this.menu_offset < contentlength - 1) spanslength--;
@@ -168,12 +172,15 @@
let i = this.menu_offset, c = 0;
+ // backward jump only present if not exactly at start span.textContent = "↑ ↑ ↑";
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_backward_click()");
+ // presence of forward jump when not right at the end }else if(c == spanslength-1 && i < contentlength - 1){
span.textContent = "↓ ↓ ↓";
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_forward_click()");
+ // otherwise normal content span.textContent = this.content[i];
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_selection_click("+i+")");
@@ -206,21 +213,26 @@
this.adjust_box_to_text();
// Take button out until menu closed
this.element.removeChild(this.button_elt);
- // Place widget in front by moving it to last position among siblings
+ // Rise widget to top by moving it to last position among siblings this.element.parentNode.appendChild(this.element.parentNode.removeChild(this.element));
// disable interaction with background
svg_root.addEventListener("click", this.bound_close_on_click_elsewhere, true);
+ // Put text element in normalized state let first = txt.firstElementChild;
+ // remove attribute eventually added to first text line while opening first.removeAttribute("onclick");
first.removeAttribute("dy");
+ // keep only the first line of text for(let span of Array.from(txt.children).slice(1)){
+ // Put rectangle element in saved original state @@ -229,6 +241,7 @@
b.width.baseVal.value = m.width;
b.height.baseVal.value = m.height;
+ // Use margin and text size to compute box size adjust_box_to_text: function(){
let [lmargin, tmargin] = this.margins;
let m = this.text_elt.getBBox();