template "widget[@type='DropDown']", mode="widget_defs" {
dispatch: function(value) {
if(!this.opened) this.set_selection(value);
this.element.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_click()");
this.text_bbox = this.text_elt.getBBox()
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;
rmargin = this.box_bbox.width - this.text_bbox.width - lmargin;
bmargin = this.box_bbox.height - this.text_bbox.height - tmargin;
this.margins = [lmargin, tmargin, rmargin, bmargin].map(x => Math.max(x,0));
this.content = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen"];
//this.content = ["one", "two", "three", "four", "5", "6"];
on_selection_click: function(selection) {
this.set_selection(selection);
on_backward_click:function(){
on_forward_click:function(){
set_selection: function(value) {
this.text_elt.firstElementChild.textContent =
(value >= 0 && value < this.content.length) ?
this.content[value] : "?"+String(value)+"?";
grow_text: function(up_to) {
let first = txt.firstElementChild;
let bounds = svg_root.getBoundingClientRect();
let next = first.cloneNode();
next.removeAttribute("y");
next.setAttribute("dy", "1.1em");
next.textContent = "...";
let rect = txt.getBoundingClientRect();
if(rect.bottom > bounds.bottom){
let backup = first.getAttribute("dy");
first.setAttribute("dy", "-"+String((this.lift+1)*1.1)+"em");
rect = txt.getBoundingClientRect();
if(rect.top > bounds.top){
first.setAttribute("dy", backup);
first.removeAttribute("dy");
set_complete_text: function(){
let spans = this.text_elt.children;
for(let item of this.content){
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_selection_click("+c+")");
let contentlength = this.content.length;
let spans = this.text_elt.children;
let spanslength = spans.length;
if(this.menu_offset != 0) spanslength--;
if(this.menu_offset < contentlength - 1) spanslength--;
this.menu_offset = Math.min(
contentlength - spans.length + 1,
this.menu_offset + spanslength);
this.menu_offset = Math.max(
this.menu_offset - spanslength);
console.log(this.menu_offset);
set_partial_text: function(){
let spans = this.text_elt.children;
let contentlength = this.content.length;
let spanslength = spans.length;
let i = this.menu_offset, c = 0;
span.textContent = "↑ ↑ ↑";
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_backward_click()");
}else if(c == spanslength-1 && i < contentlength - 1){
span.textContent = "↓ ↓ ↓";
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_forward_click()");
span.textContent = this.content[i];
span.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_selection_click("+i+")");
let length = this.content.length;
let slots = this.grow_text(length);
this.set_complete_text();
this.adjust_box_to_text();
/* TODO disable interaction with background */
let first = txt.firstElementChild;
first.removeAttribute("onclick");
first.removeAttribute("dy");
for(let span of Array.from(txt.children).slice(1)){
b.width.baseVal.value = m.width;
b.height.baseVal.value = m.height;
adjust_box_to_text: function(){
let [lmargin, tmargin, rmargin, bmargin] = this.margins;
let m = this.text_elt.getBBox();
b.x.baseVal.value = m.x - lmargin;
b.y.baseVal.value = m.y - tmargin;
b.width.baseVal.value = lmargin + m.width + rmargin;
b.height.baseVal.value = tmargin + m.height + bmargin;
// | 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;