You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-windows.asciidoc 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. title: Handling Browser Windows
  3. order: 1
  4. layout: page
  5. ---
  6. [[advanced.windows]]
  7. = Handling Browser Windows
  8. The UI of a Vaadin application runs in a web page displayed in a browser window
  9. or tab. An application can be used from multiple UIs in different windows or
  10. tabs, either opened by the user using an URL or by the Vaadin application.
  11. In addition to native browser windows, Vaadin has a [classname]#Window#
  12. component, which is a floating panel or __sub-window__ inside a page, as
  13. described in
  14. <<../layout/layout-sub-window#layout.sub-window,"Sub-Windows">>.
  15. * __Native popup windows__. An application can open popup windows for sub-tasks.
  16. * __Page-based browsing__. The application can allow the user to open certain content to different windows. For example, in a messaging application, it can be useful to open different messages to different windows so that the user can browse through them while writing a new message.
  17. * __Bookmarking__. Bookmarks in the web browser can provide an entry-point to some content provided by an application.
  18. * __Embedding UIs__. UIs can be embedded in web pages, thus making it possible to provide different views to an application from different pages or even from the same page, while keeping the same session. See <<advanced-embedding#advanced.embedding,"Embedding UIs in Web Pages">>.
  19. Use of multiple windows in an application may require defining and providing
  20. different UIs for the different windows. The UIs of an application share the
  21. same user session, that is, the [classname]#VaadinSession# object, as described
  22. in
  23. <<../application/application-lifecycle#application.lifecycle.session,"User
  24. Session">>. Each UI is identified by a URL that is used to access it, which
  25. makes it possible to bookmark application UIs. UI instances can even be created
  26. dynamically based on the URLs or other request parameters, such as browser
  27. information, as described in
  28. <<../application/application-lifecycle#application.lifecycle.ui,"Loading
  29. a UI">>.
  30. Because of the special nature of AJAX applications, use of multiple windows uses
  31. require some
  32. caveats.
  33. ////
  34. TODO Re-enable We will go through them later in &lt;xref
  35. linkend="advanced.windows.caveats"/&gt;.
  36. ////
  37. [[advanced.windows.popup]]
  38. == Opening Pop-up Windows
  39. ((("popup windows")))
  40. ((("windows", "popup")))
  41. Pop-up windows are native browser windows or tabs opened by user interaction with
  42. an existing window. Due to browser security reasons, it is made inconvenient for
  43. a web page to open popup windows using JavaScript commands. At the least, the
  44. browser will ask for a permission to open the popup, if it is possible at all.
  45. This limitation can be circumvented by letting the browser open the new window
  46. or tab directly by its URL when the user clicks some target. This is realized in
  47. Vaadin with the [classname]#BrowserWindowOpener# component extension, which
  48. causes the browser to open a window or tab when the component is clicked.
  49. [[advanced.windows.popup.ui]]
  50. === The Pop-up Window UI
  51. A popup Window displays an [classname]#UI#. The UI of a popup window is defined
  52. just like a main UI in a Vaadin application, and it can have a theme, title, and
  53. so forth.
  54. For example:
  55. [source, java]
  56. ----
  57. @Theme("mytheme")
  58. public static class MyPopupUI extends UI {
  59. @Override
  60. protected void init(VaadinRequest request) {
  61. getPage().setTitle("Popup Window");
  62. // Have some content for it
  63. VerticalLayout content = new VerticalLayout();
  64. Label label =
  65. new Label("I just popped up to say hi!");
  66. label.setSizeUndefined();
  67. content.addComponent(label);
  68. content.setComponentAlignment(label,
  69. Alignment.MIDDLE_CENTER);
  70. content.setSizeFull();
  71. setContent(content);
  72. }
  73. }
  74. ----
  75. [[advanced.windows.popup.popping]]
  76. === Popping It Up
  77. A popup window is opened using the [classname]#BrowserWindowOpener# extension,
  78. which you can attach to any component or `MenuItem`. The constructor of the extension takes
  79. the class object of the UI class to be opened as a parameter.
  80. You can configure the features of the popup window with
  81. [methodname]#setFeatures()#. It takes as its parameter a comma-separated list of
  82. window features, as defined in the HTML specification.
  83. status=[parameter]#0|1#:: Whether the status bar at the bottom of the window should be enabled.
  84. [parameter]##::
  85. scrollbars:: Enables scrollbars in the window if the document area is bigger than the view area of the window.
  86. resizable:: Allows the user to resize the browser window (no effect for tabs).
  87. menubar:: Enables the browser menu bar.
  88. location:: Enables the location bar.
  89. toolbar:: Enables the browser toolbar.
  90. height=[parameter]#value#:: Specifies the height of the window in pixels.
  91. width=[parameter]#value#:: Specifies the width of the window in pixels.
  92. For example:
  93. [source, java]
  94. ----
  95. // Create an opener extension
  96. BrowserWindowOpener opener =
  97. new BrowserWindowOpener(MyPopupUI.class);
  98. opener.setFeatures("height=200,width=300,resizable");
  99. // Attach it to a button
  100. Button button = new Button("Pop It Up");
  101. opener.extend(button);
  102. ----
  103. The resulting popup window, which appears when the button is clicked, is shown
  104. in <<figure.advanced.windows.popup.popping>>.
  105. [[figure.advanced.windows.popup.popping]]
  106. .A Popup Window
  107. image::img/windows-popup.png[]
  108. [[advanced.windows.popup.target]]
  109. === Popup Window Name (Target)
  110. The target name is one of the default HTML target names ( [parameter]#_new#,
  111. [parameter]#_blank#, [parameter]#_top#, etc.) or a custom target name. How the
  112. window is exactly opened depends on the browser. Browsers that support tabbed
  113. browsing can open the window in another tab, depending on the browser settings.
  114. [[advanced.windows.popup.url]]
  115. === URL and Session
  116. The URL path for a popup window UI is by default determined from the UI class
  117. name, by prefixing it with "[literal]#++popup/++#". For example, for the
  118. UI given earlier, the URL would be
  119. [literal]#++/example/base/popup/MyPopupUI++#.
  120. [[advanced.windows.popup-closing]]
  121. == Closing Popup Windows
  122. Besides closing popup windows from a native window close button, you can close
  123. them programmatically by calling the JavaScript [methodname]#close()# method as
  124. follows.
  125. [source, java]
  126. ----
  127. public class MyPopup extends UI {
  128. @Override
  129. protected void init(VaadinRequest request) {
  130. setContent(new Button("Close Window", event -> {// Java 8
  131. // Close the popup
  132. JavaScript.eval("close()");
  133. // Detach the UI from the session
  134. getUI().close();
  135. }));
  136. }
  137. }
  138. ----