Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

advanced-windows.asciidoc 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. <<dummy/../../../framework/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 <<dummy/../../../framework/advanced/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. <<dummy/../../../framework/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. <<dummy/../../../framework/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. TODO Re-enable We will go through them later in &lt;xref
  34. linkend="advanced.windows.caveats"/&gt;.
  35. ////
  36. [[advanced.windows.popup]]
  37. == Opening Popup Windows
  38. ((("popup windows")))
  39. ((("windows", "popup")))
  40. Popup windows are native browser windows or tabs opened by user interaction with
  41. an existing window. Due to browser security reasons, it is made incovenient for
  42. a web page to open popup windows using JavaScript commands. At the least, the
  43. browser will ask for a permission to open the popup, if it is possible at all.
  44. This limitation can be circumvented by letting the browser open the new window
  45. or tab directly by its URL when the user clicks some target. This is realized in
  46. Vaadin with the [classname]#BrowserWindowOpener# component extension, which
  47. causes the browser to open a window or tab when the component is clicked.
  48. [[advanced.windows.popup.ui]]
  49. === The Popup Window UI
  50. A popup Window displays an [classname]#UI#. The UI of a popup window is defined
  51. just like a main UI in a Vaadin application, and it can have a theme, title, and
  52. so forth.
  53. For example:
  54. [source, java]
  55. ----
  56. @Theme("book-examples")
  57. public static class MyPopupUI extends UI {
  58. @Override
  59. protected void init(VaadinRequest request) {
  60. getPage().setTitle("Popup Window");
  61. // Have some content for it
  62. VerticalLayout content = new VerticalLayout();
  63. Label label =
  64. new Label("I just popped up to say hi!");
  65. label.setSizeUndefined();
  66. content.addComponent(label);
  67. content.setComponentAlignment(label,
  68. Alignment.MIDDLE_CENTER);
  69. content.setSizeFull();
  70. setContent(content);
  71. }
  72. }
  73. ----
  74. [[advanced.windows.popup.popping]]
  75. === Popping It Up
  76. A popup window is opened using the [classname]#BrowserWindowOpener# extension,
  77. which you can attach to any component. The constructor of the extension takes
  78. the class object of the UI class to be opened as a parameter.
  79. You can configure the features of the popup window with
  80. [methodname]#setFeatures()#. It takes as its parameter a comma-separated list of
  81. window features, as defined in the HTML specification.
  82. status=[parameter]#0|1#:: Whether the status bar at the bottom of the window should be enabled.
  83. [parameter]##::
  84. scrollbars:: Enables scrollbars in the window if the document area is bigger than the view area of the window.
  85. resizable:: Allows the user to resize the browser window (no effect for tabs).
  86. menubar:: Enables the browser menu bar.
  87. location:: Enables the location bar.
  88. toolbar:: Enables the browser toolbar.
  89. height=[parameter]#value#:: Specifies the height of the window in pixels.
  90. width=[parameter]#value#:: Specifies the width of the window in pixels.
  91. For example:
  92. [source, java]
  93. ----
  94. // Create an opener extension
  95. BrowserWindowOpener opener =
  96. new BrowserWindowOpener(MyPopupUI.class);
  97. opener.setFeatures("height=200,width=300,resizable");
  98. // Attach it to a button
  99. Button button = new Button("Pop It Up");
  100. opener.extend(button);
  101. ----
  102. The resulting popup window, which appears when the button is clicked, is shown
  103. in <<figure.advanced.windows.popup.popping>>.
  104. [[figure.advanced.windows.popup.popping]]
  105. .A Popup Window
  106. image::img/windows-popup.png[]
  107. [[advanced.windows.popup.target]]
  108. === Popup Window Name (Target)
  109. The target name is one of the default HTML target names ( [parameter]#_new#,
  110. [parameter]#_blank#, [parameter]#_top#, etc.) or a custom target name. How the
  111. window is exactly opened depends on the browser. Browsers that support tabbed
  112. browsing can open the window in another tab, depending on the browser settings.
  113. [[advanced.windows.popup.url]]
  114. === URL and Session
  115. The URL path for a popup window UI is by default determined from the UI class
  116. name, by prefixig it with " [literal]#++popup/++#". For example, for the example
  117. UI giver earlier, the URL would be
  118. [literal]#++/book-examples/book/popup/MyPopupUI++#.
  119. [[advanced.windows.popup-closing]]
  120. == Closing Popup Windows
  121. Besides closing popup windows from a native window close button, you can close
  122. them programmatically by calling the JavaScript [methodname]#close()# method as
  123. follows.
  124. [source, java]
  125. ----
  126. public class MyPopup extends UI {
  127. @Override
  128. protected void init(VaadinRequest request) {
  129. setContent(new Button("Close Window", event -> {// Java 8
  130. // Close the popup
  131. JavaScript.eval("close()");
  132. // Detach the UI from the session
  133. getUI().close();
  134. }));
  135. }
  136. }
  137. ----