Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

components-link.asciidoc 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ---
  2. title: Link
  3. order: 8
  4. layout: page
  5. ---
  6. [[components.link]]
  7. = [classname]#Link#
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/interaction/link"]
  11. endif::web[]
  12. The [classname]#Link# component allows making hyperlinks. References to
  13. locations are represented as resource objects, explained in
  14. <<dummy/../../../framework/application/application-resources#application.resources,"Images
  15. and Other Resources">>. The [classname]#Link# is a regular HTML hyperlink, that
  16. is, an [literal]#++<a href>++# anchor element that is handled natively by the
  17. browser. Unlike when clicking a [classname]#Button#, clicking a
  18. [classname]#Link# does not cause an event on the server-side.
  19. Links to an arbitrary URL can be made by using an [classname]#ExternalResource#
  20. as follows:
  21. [source, java]
  22. ----
  23. // Textual link
  24. Link link = new Link("Click Me!",
  25. new ExternalResource("http://vaadin.com/"));
  26. ----
  27. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.link.basic[on-line example, window="_blank"].
  28. You can use [methodname]#setIcon()# to make image links as follows:
  29. [source, java]
  30. ----
  31. // Image link
  32. Link iconic = new Link(null,
  33. new ExternalResource("http://vaadin.com/"));
  34. iconic.setIcon(new ThemeResource("img/nicubunu_Chain.png"));
  35. // Image + caption
  36. Link combo = new Link("To appease both literal and visual",
  37. new ExternalResource("http://vaadin.com/"));
  38. combo.setIcon(new ThemeResource("img/nicubunu_Chain.png"));
  39. ----
  40. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.link.basic[on-line example, window="_blank"].
  41. The resulting links are shown in <<figure.components.link.basic>>. You could add
  42. a " [literal]#++display: block++#" style for the icon element to place the
  43. caption below it.
  44. [[figure.components.link.basic]]
  45. .[classname]#Link# Example
  46. image::img/link.png[width=30%, scaledwidth=70%]
  47. [[components.link.new-window]]
  48. == Opening a New Window
  49. With the simple constructor used in the above example, the resource is opened in
  50. the current window. Using the constructor that takes the target window as a
  51. parameter, or by setting the target window with [methodname]#setTargetName()#,
  52. you can open the resource in another window, such as a popup browser window/tab.
  53. As the target name is an HTML [literal]#++target++# string managed by the
  54. browser, the target can be any window, including windows not managed by the
  55. application itself. You can use the special underscored target names, such as
  56. [literal]#++_blank++# to open the link to a new browser window or tab.
  57. [source, java]
  58. ----
  59. // Hyperlink to a given URL
  60. Link link = new Link("Take me a away to a faraway land",
  61. new ExternalResource("http://vaadin.com/"));
  62. // Open the URL in a new window/tab
  63. link.setTargetName("_blank");
  64. // Indicate visually that it opens in a new window/tab
  65. link.setIcon(new ThemeResource("icons/external-link.png"));
  66. link.addStyleName("icon-after-caption");
  67. ----
  68. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.link.target[on-line example, window="_blank"].
  69. [[components.link.pop-up]]
  70. == Opening as a Pop-Up Window
  71. With the [literal]#++_blank++# target, a normal new browser window is opened. If
  72. you wish to open it in a popup window (or tab), you need to give a size for the
  73. window with [methodname]#setTargetWidth()# and [methodname]#setTargetHeight()#.
  74. You can control the window border style with [methodname]#setTargetBorder()#,
  75. which takes any of the defined border styles [parameter]#TARGET_BORDER_DEFAULT#,
  76. [parameter]#TARGET_BORDER_MINIMAL#, and [parameter]#TARGET_BORDER_NONE#. The
  77. exact result depends on the browser.
  78. [source, java]
  79. ----
  80. // Open the URL in a popup
  81. link.setTargetName("_blank");
  82. link.setTargetBorder(Link.TARGET_BORDER_NONE);
  83. link.setTargetHeight(300);
  84. link.setTargetWidth(400);
  85. ----
  86. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.link.target[on-line example, window="_blank"].
  87. == Alternatives
  88. In addition to the [classname]#Link# component, Vaadin allows alternative ways to make hyperlinks.
  89. Also, you can make hyperlinks (or any other HTML) in a [classname]#Label# in HTML content mode.
  90. The [classname]#Button# component has a [parameter]#Reindeer.BUTTON_LINK# style name that makes it look like a hyperlink, while handling clicks in a server-side click listener instead of in the browser.
  91. However, browsers do not generally allow opening new windows from with browser code, so for such tasks you need to use the [classname]#BrowserWindowOpener# extension described in <<dummy/../../../framework/advanced/advanced-windows#advanced.windows.popup, "Opening Pop-up Windows">>
  92. == CSS Style Rules
  93. [source, css]
  94. ----
  95. .v-link { }
  96. a { }
  97. .v-icon {}
  98. span {}
  99. ----
  100. The overall style for the [classname]#Link# component is [literal]#++v-link++#.
  101. The root element contains the [literal]#++<a href>++# hyperlink anchor. Inside
  102. the anchor are the icon, with [literal]#++v-icon++# style, and the caption in a
  103. text span.
  104. Hyperlink anchors have a number of __pseudo-classes__ that are active at
  105. different times. An unvisited link has [literal]#++a:link++# class and a visited
  106. link [literal]#++a:visited++#. When the mouse pointer hovers over the link, it
  107. will have a:hover, and when the mouse button is being pressed over the link, the
  108. [literal]#++a:active++# class. When combining the pseudo-classes in a selector,
  109. please notice that [literal]#++a:hover++# must come after an
  110. [literal]#++a:link++# and [literal]#++a:visited++#, and [literal]#++a:active++#
  111. after the [literal]#++a:hover++#.
  112. ifdef::web[]
  113. === Icon Position
  114. Normally, the link icon is before the caption.
  115. You can have it right of the caption by reversing the text direction in the containing element.
  116. [source, css]
  117. ----
  118. /* Position icon right of the link caption. */
  119. .icon-after-caption {
  120. direction: rtl;
  121. }
  122. /* Add some padding around the icon. */
  123. .icon-after-caption .v-icon {
  124. padding: 0 3px;
  125. }
  126. ----
  127. See the http://demo.vaadin.com/book-examples-vaadin7/book#component.link.target[on-line example, window="_blank"].
  128. The resulting link is shown in <<figure.components.link.new-window>>.
  129. [[figure.components.link.new-window]]
  130. .Link That Opens a New Window
  131. image::img/link-new.png[width=25%, scaledwidth=50%]
  132. endif::web[]