selectionmenubutton.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.SelectionMenuButton Demo</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.array');
  13. goog.require('goog.debug.DivConsole');
  14. goog.require('goog.debug.LogManager');
  15. goog.require('goog.events');
  16. goog.require('goog.events.EventType');
  17. goog.require('goog.log');
  18. goog.require('goog.object');
  19. goog.require('goog.ui.MenuItem');
  20. goog.require('goog.ui.SelectionMenuButton');
  21. goog.require('goog.ui.Separator');
  22. goog.require('goog.ui.decorate');
  23. </script>
  24. <link rel="stylesheet" href="css/demo.css">
  25. <link rel="stylesheet" href="../css/menubutton.css">
  26. <style>
  27. /* Base class for all icon elements. */
  28. .icon {
  29. height: 16px;
  30. width: 16px;
  31. margin: 0 1px;
  32. background-image: url(../images/toolbar_icons.gif);
  33. background-repeat: no-repeat;
  34. vertical-align: middle;
  35. }
  36. /* "Format" icon. */
  37. .format-icon{
  38. background-position: -64px;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h1>goog.ui.SelectionMenuButton</h1>
  44. <table border="0" cellpadding="0" cellspacing="4" width="100%">
  45. <tbody>
  46. <tr valign="top">
  47. <td width="67%">
  48. <fieldset>
  49. <legend>
  50. This <strong>SelectionMenuButton</strong> was created programmatically:
  51. &nbsp;
  52. </legend>
  53. <table border="0" cellpadding="0" cellspacing="4">
  54. <tbody>
  55. <tr valign="middle">
  56. <td>
  57. <div id="menuButtons"></div>
  58. </td>
  59. <td>
  60. Enable button:
  61. <input type="checkbox" id="b1_enable" checked>
  62. &nbsp;
  63. </td>
  64. </tr>
  65. </tbody>
  66. </table>
  67. <label>
  68. </label>
  69. <br>
  70. </fieldset>
  71. <fieldset>
  72. <legend>
  73. This <strong>SelectionMenuButton</strong> decorates an element:&nbsp;
  74. </legend>
  75. <table border="0" cellpadding="0" cellspacing="4">
  76. <tbody>
  77. <tr valign="middle">
  78. <td>
  79. <div id="selectButton" class="goog-selectionmenubutton-button"
  80. title="Select">
  81. <!-- These elements will become the button's caption. -->
  82. <input type="checkbox" class="goog-selectionmenubutton-checkbox"></input>
  83. <!-- This DIV will be auto-decorated with a menu. -->
  84. <div id="selectMenu" class="goog-menu">
  85. <div class="goog-menuitem">All</div>
  86. <div class="goog-menuitem">None</div>
  87. <div class="goog-menuseparator"></div>
  88. <div class="goog-menuitem">Starred</div>
  89. <div class="goog-menuitem goog-menuitem-disabled">
  90. Unstarred
  91. </div>
  92. <div class="goog-menuseparator"></div>
  93. <div class="goog-menuitem">Read</div>
  94. <div class="goog-menuitem">Unread</div>
  95. </div>
  96. </div>
  97. </td>
  98. <td>
  99. Enable button:
  100. <input type="checkbox" id="selectButton_enable" checked>
  101. &nbsp;
  102. Show button:
  103. <input type="checkbox" id="selectButton_show" checked>
  104. &nbsp;
  105. </td>
  106. </tr>
  107. </tbody>
  108. </table>
  109. <label>
  110. </label>
  111. <br>
  112. </fieldset>
  113. </td>
  114. <td width="33%">
  115. <!-- Event log. -->
  116. <fieldset class="goog-debug-panel">
  117. <legend>Event Log</legend>
  118. <div id="log"></div>
  119. </fieldset>
  120. </td>
  121. </tr>
  122. </tbody>
  123. </table>
  124. <br>
  125. <div id="perf"></div>
  126. <script>
  127. var timer = goog.now();
  128. // Set up a logger.
  129. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  130. var logger = goog.log.getLogger('demo');
  131. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  132. logconsole.setCapturing(true);
  133. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  134. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  135. function logEvent(e) {
  136. var component = e.target;
  137. var caption = (typeof component.getCaption == 'function') ?
  138. component.getCaption() : component.getId();
  139. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  140. }
  141. // Create the first button programmatically.
  142. var b1 = new goog.ui.SelectionMenuButton();
  143. b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  144. b1.setId('progSelectButton');
  145. b1.render(goog.dom.getElement('menuButtons'));
  146. b1.setTooltip('Select menu demo');
  147. goog.events.listen(b1, EVENTS, logEvent);
  148. b1.addItem(new goog.ui.MenuItem('Important'));
  149. b1.addItem(new goog.ui.MenuItem('Unimportant'));
  150. goog.events.listen(goog.dom.getElement('b1_enable'),
  151. goog.events.EventType.CLICK,
  152. function(e) {
  153. b1.setEnabled(e.target.checked);
  154. });
  155. // Decorate a menu button. Note that since one of the child nodes of the
  156. // menu button element can be decorated as a menu, it is auto-decorated and
  157. // attached to the button.
  158. var selectButton = goog.ui.decorate(goog.dom.getElement('selectButton'));
  159. goog.events.listen(goog.dom.getElement('selectButton_show'),
  160. goog.events.EventType.CLICK,
  161. function(e) {
  162. selectButton.setVisible(e.target.checked);
  163. });
  164. goog.events.listen(goog.dom.getElement('selectButton_enable'),
  165. goog.events.EventType.CLICK,
  166. function(e) {
  167. selectButton.setEnabled(e.target.checked);
  168. });
  169. goog.events.listen(selectButton, EVENTS, logEvent);
  170. goog.dom.setTextContent(goog.dom.getElement('perf'),
  171. (goog.now() - timer) + 'ms');
  172. </script>
  173. </body>
  174. </html>