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