popup.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.ui.Popup</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. if (typeof goog == 'undefined') {
  13. alert('Closure failed to load');
  14. } else {
  15. goog.require('goog.events');
  16. goog.require('goog.events.EventType');
  17. goog.require('goog.positioning.ClientPosition');
  18. goog.require('goog.positioning.Corner');
  19. goog.require('goog.positioning.AnchoredViewportPosition');
  20. goog.require('goog.ui.Popup');
  21. }
  22. </script>
  23. <link rel="stylesheet" href="css/demo.css">
  24. <style>
  25. .popup {
  26. position:absolute;
  27. background-color:#e0ecff;
  28. color:black;
  29. visibility:hidden;
  30. width:100px;
  31. height:100px;
  32. font-size: 80%;
  33. border:solid red 1px;
  34. -moz-outline:0;
  35. outline:0;
  36. }
  37. button {
  38. border: solid black 1px;
  39. margin-left: 50%;
  40. }
  41. label {
  42. display: block;
  43. width: 15em;
  44. }
  45. #abs-box {
  46. border: solid black 2px;
  47. height: 200px;
  48. width: 200px;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <h1>goog.ui.Popup</h1>
  54. <div id='popup' class='popup' tabindex="0"></div>
  55. <p>Positioning relative to an anchor element</p>
  56. <form>
  57. <strong>Button Corner</strong>
  58. <label for="button_corner_tl">
  59. <input type="radio" id="button_corner_tl" name="button_corner" value="tl">Top Left
  60. </label>
  61. <label for="button_corner_tr">
  62. <input type="radio" id="button_corner_tr" name="button_corner" value="tr">Top Right
  63. </label>
  64. <label for="button_corner_bl">
  65. <input type="radio" id="button_corner_bl" name="button_corner" value="bl" checked="checked">Bottom Left
  66. </label>
  67. <label for="button_corner_br">
  68. <input type="radio" id="button_corner_br" name="button_corner" value="br">Bottom Right
  69. </label>
  70. <br>
  71. <strong>Popup Corner</strong>
  72. <label for="menu_corner_tl">
  73. <input type="radio" id="menu_corner_tl" name="menu_corner" value="tl" checked/>Top Left
  74. </label>
  75. <label for="menu_corner_tr">
  76. <input type="radio" id="menu_corner_tr" name="menu_corner" value="tr">Top Right
  77. </label>
  78. <label for="menu_corner_bl">
  79. <input type="radio" id="menu_corner_bl" name="menu_corner" value="bl">Bottom Left
  80. </label>
  81. <label for="menu_corner_br">
  82. <input type="radio" id="menu_corner_br" name="menu_corner" value="br">Bottom Right
  83. </label>
  84. <br>
  85. <strong>Margin</strong>
  86. Top: <input id="margin_top" size=2 value="0">
  87. Right: <input id="margin_right" size=2 value="0">
  88. Bottom: <input id="margin_bottom" size=2 value="0">
  89. Left: <input id="margin_left" size=2 value="0">
  90. <br>
  91. <br>
  92. <br>
  93. </form>
  94. <button id="btn" onclick="showPopup()">
  95. Press me!
  96. </button>
  97. <br>
  98. <br>
  99. <h3>Iframe to test cross frame dismissal</h3>
  100. <iframe src="about:blank"></iframe>
  101. <br>
  102. <br>
  103. <hr>
  104. <h3>Positioning at coordinates</h3>
  105. <div id="abs-box"></div>
  106. <script>
  107. var popupElt = document.getElementById('popup');
  108. var popup = new goog.ui.Popup(popupElt);
  109. popup.setHideOnEscape(true);
  110. popup.setAutoHide(true);
  111. var showingBecauseOfBox = false;
  112. goog.events.listen(window, goog.events.EventType.RESIZE, onResize);
  113. goog.events.listen(document, goog.events.EventType.MOUSEMOVE, onMouseMove);
  114. // goog.events.listen(absBox, goog.events.EventType.MOUSEOUT,
  115. // onAbsBoxMouseOut);
  116. function showPopup() {
  117. var btn = document.getElementById('btn');
  118. var buttonCorner = toCorner(
  119. getCheckedValue(document.forms[0].elements['button_corner']));
  120. var menuCorner = toCorner(
  121. getCheckedValue(document.forms[0].elements['menu_corner']));
  122. var t = parseInt(document.getElementById('margin_top').value);
  123. var r = parseInt(document.getElementById('margin_right').value);
  124. var b = parseInt(document.getElementById('margin_bottom').value);
  125. var l = parseInt(document.getElementById('margin_left').value);
  126. var margin = new goog.math.Box(t, r, b, l);
  127. popup.setVisible(false);
  128. popup.setPinnedCorner(menuCorner);
  129. popup.setMargin(margin);
  130. popup.setPosition(new goog.positioning.AnchoredViewportPosition(btn,
  131. buttonCorner));
  132. popup.setVisible(true);
  133. }
  134. function onResize(e) {
  135. if (popup && popup.isVisible()) {
  136. popup.reposition();
  137. }
  138. }
  139. function onMouseMove(e) {
  140. var absBox = document.getElementById('abs-box');
  141. var offset = goog.style.getViewportPageOffset(
  142. goog.dom.getOwnerDocument(absBox));
  143. var size = goog.style.getSize(absBox);
  144. var boxPagePosition = goog.style.getPageOffset(absBox);
  145. var boxClientPosition = new goog.math.Coordinate(
  146. boxPagePosition.x - offset.x,
  147. boxPagePosition.y - offset.y);
  148. if (e.clientX >= boxClientPosition.x &&
  149. e.clientX < (boxClientPosition.x + size.width) &&
  150. e.clientY >= boxClientPosition.y &&
  151. e.clientY < (boxClientPosition.y + size.height)) {
  152. popup.setPinnedCorner(goog.positioning.Corner.TOP_LEFT);
  153. popup.setPosition(new goog.positioning.ClientPosition(
  154. e.clientX, e.clientY));
  155. popup.setVisible(true);
  156. showingBecauseOfBox = true;
  157. } else if (showingBecauseOfBox) {
  158. popup.setVisible(false);
  159. showingBecauseOfBox = false;
  160. }
  161. }
  162. function getCheckedValue(radioObj) {
  163. for (var i = 0; i < radioObj.length; i++) {
  164. if (radioObj[i].checked) {
  165. return radioObj[i].value;
  166. }
  167. }
  168. }
  169. function toCorner(val) {
  170. switch (val) {
  171. case "tl":
  172. return goog.positioning.Corner.TOP_LEFT;
  173. case "tr":
  174. return goog.positioning.Corner.TOP_RIGHT;
  175. case "bl":
  176. return goog.positioning.Corner.BOTTOM_LEFT;
  177. case "br":
  178. return goog.positioning.Corner.BOTTOM_RIGHT;
  179. }
  180. }
  181. </script>
  182. </body>
  183. </html>