--- a/svghmi/widget_dropdown.ysl2 Tue Apr 14 23:04:54 2020 +0200
+++ b/svghmi/widget_dropdown.ysl2 Wed Apr 15 14:29:19 2020 +0200
@@ -3,7 +3,7 @@
template "widget[@type='DropDown']", mode="widget_defs" {
labels("text box button");
dispatch: function(value) {
if(!this.opened) this.set_selection(value);
@@ -14,18 +14,26 @@
lmargin = this.text_bbox.x - this.box_bbox.x;
tmargin = this.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. ``foreach "arg" | "«@value»",
+ // Index of first visible element in the menu, when opened + // How mutch to lift the menu vertically so that it does not cross bottom border
+ // 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);
+ // Called when a menu entry is clicked on_selection_click: function(selection) {
- console.log("selected "+selection);
let orig = this.indexes[0];
let idx = this.offset ? orig - this.offset : orig;
@@ -34,46 +42,61 @@
on_button_click: function() {
- on_backward_click:function(){
+ on_backward_click: function(){ on_forward_click:function(){
set_selection: function(value) {
if(value >= 0 && value < this.content.length){
+ // if valid selection resolve content display_str = this.content[value];
this.last_selection = value;
+ // 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;
grow_text: function(up_to) {
let first = txt.firstElementChild;
+ // Real world (pixels) boundaries of current page let bounds = svg_root.getBoundingClientRect();
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 + // 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();
if(rect.top > bounds.top){
+ // if it goes over the top, then backtrack + // restore dy attribute on first span first.setAttribute("dy", backup);
first.removeAttribute("dy");
+ // remove unwanted child @@ -83,22 +106,28 @@
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){
+ // close menu in case click is outside box if(e.target !== this.box_elt)
- 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 + // Put the button back in place this.element.appendChild(this.button_elt);
+ // Mark as closed (to allow dispatch) + // Dispatch last cached value + // Set text content when content is smaller than menu (no scrolling) set_complete_text: function(){
let spans = this.text_elt.children;
@@ -109,7 +138,10 @@
- move: function(forward){
+ // false : upward, lower value + // true : downward, higher value + scroll: function(forward){ let contentlength = this.content.length;
let spans = this.text_elt.children;
let spanslength = spans.length;
@@ -127,6 +159,8 @@
console.log(this.menu_offset);
+ // Setup partial view text content + // with jumps at first and last entry when appropriate set_partial_text: function(){
let spans = this.text_elt.children;
let contentlength = this.content.length;
@@ -150,24 +184,32 @@
let length = this.content.length;
+ // systematically reset text, to strip eventual whitespace spans + // grow as much as needed or possible let slots = this.grow_text(length);
+ // Depending on final size this.set_complete_text();
+ // eventualy align menu to current selection, compensating for lift let offset = this.last_selection - this.lift;
this.menu_offset = Math.min(offset + 1, length - slots + 1);
+ // show surrounding values + // 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);
+ // Place widget in front 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); @@ -198,11 +240,3 @@
- // | let p = new DOMPoint(this.box_elt.x.baseVal.value, this.box_elt.y.baseVal.value);
- // | let k = DOMMatrix.fromMatrix(this.box_elt.getCTM());
- // | let new_corner = k.transformPoint(p);
- // | let nc = k.inverse().transformPoint(new_corner);
- // | this.box_elt.x.baseVal.value = nc.x;
- // | this.box_elt.y.baseVal.value = nc.y;