html - How to enable javascript window.close() in vaadin dialog? -


i have html page has close button on click, should close window. html page being loaded on vaadin dialog. understand vaadin takes care of closing dialog window.setclosable(true). but, button in html page should same. how enable ?
below code :

myhelp.html:

<html> <head> </head> <body> . . <!-- @ footer right corner --> <p class="myclass" align="right"><img src="images/iconclose.gif" width="50" height="10" border="0" onclick="window.close()" title="close"></p> . . </body> </html> 

java code:

. . string link = "test/myhelp.html"; menuitem menuitem = null; if (link.contains("/test")) {  menuitem = menubar.additem("", new externalresource(stringutil.append("/images/", images.get(i))), new command() {  @override public void menuselected(menuitem selecteditem) {      final window window = new window(this.caption);     window.setclosable(true);     window.setwindowmode(windowmode.normal);     window.setmodal(true);     window.setdraggable(true);     window.setresizable(false);     window.center();     window.addstylename("abcdailog");     verticallayout layout = new verticallayout();     layout.setsizefull();     layout.setspacing(true);     layout.setmargin(true);      if (!commonutil.isempty(this.stylename)) {         window.addstylename("abcstyle");         layout.setmargin(false);     }     if (!commonutil.isempty(link)) {         browserframe browser = new browserframe(null, new externalresource(this.link));         browser.setsizefull();         layout.addcomponent(browser);     } else {         verticallayout.setsizefull();         layout.addcomponent(verticallayout);     }      window.setcontent(layout);     ui.getcurrent().addwindow(window);     }     }     }); . . . 

what happens myhtml gets loaded in new window. vaadin window, fine since html has window.close on image, suppose work not working. hope code helps in better understanding.

the simplest 1 add vaadin button @ bottom of window. if that's not choice, there few ways of doing this, first 1 comes mind (and possibly next simplest one) adding a callback js function close window.

1) custom window implementation:

import com.vaadin.shared.ui.label.contentmode; import com.vaadin.ui.javascript; import com.vaadin.ui.label; import com.vaadin.ui.ui; import com.vaadin.ui.window;      public class myhtmlwindow extends window {     private static final string close_window_function = "closewindow";      public myhtmlwindow() {         // window configuration         setmodal(true);         setclosable(true);         setresizable(false);     }      public void show(ui ui) {         // add html content in label including call closewindow() js function         setcontent(new label("<html>\n" +                 "<button type=\"button\" onclick=\"" + close_window_function + "();\">close</button>\n" +                 "<script>\n", contentmode.html));          // add js function close window         javascript.getcurrent().addfunction(close_window_function, arguments -> this.close());          // show window         ui.addwindow(this);     }      @override     public void close() {         // call super gets cleaned nicely         super.close();         // remove added function         javascript.getcurrent().removefunction(close_window_function);     } } 

2) usage:

public class myvaadinui extends ui {     @override     protected void init(vaadinrequest request) {         new myhtmlwindow().show(this);     } } 

3) result:

js callback window

p.s.: suggestions improvements welcome


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -