menubar.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2012 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.menuBar 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.log');
  17. goog.require('goog.object');
  18. goog.require('goog.ui.Menu');
  19. goog.require('goog.ui.MenuBarRenderer');
  20. goog.require('goog.ui.MenuButton');
  21. goog.require('goog.ui.MenuItem');
  22. goog.require('goog.ui.Separator');
  23. goog.require('goog.ui.decorate');
  24. goog.require('goog.ui.menuBar');
  25. goog.require('goog.ui.menuBarDecorator');
  26. </script>
  27. <link rel="stylesheet" href="css/demo.css">
  28. <link rel="stylesheet" href="../css/menu.css">
  29. <link rel="stylesheet" href="../css/menubar.css">
  30. <link rel="stylesheet" href="../css/menubutton.css">
  31. <link rel="stylesheet" href="../css/menuitem.css">
  32. <link rel="stylesheet" href="../css/menuseparator.css">
  33. <style>
  34. /* Base class for all icon elements. */
  35. .icon {
  36. height: 16px;
  37. width: 16px;
  38. margin: 0 1px;
  39. background-image: url(../images/toolbar_icons.gif);
  40. background-repeat: no-repeat;
  41. vertical-align: middle;
  42. }
  43. /* "Format" icon. */
  44. .format-icon {
  45. background-position: -64px;
  46. }
  47. .positioning-frame {
  48. height: 250px;
  49. overflow: auto;
  50. width: 100%;
  51. }
  52. .format-lightborder {
  53. border-style: solid;
  54. border-width: 1px;
  55. border-color: #ddd;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <h1>goog.ui.menuBar example</h1>
  61. <table border="0" cellpadding="0" cellspacing="4" width="100%">
  62. <tbody>
  63. <tr valign="top">
  64. <td width="67%">
  65. <fieldset>
  66. <legend>
  67. This <strong>menu bar</strong> was created programmatically:
  68. &nbsp;
  69. </legend>
  70. <table border="0" cellpadding="0" cellspacing="4">
  71. <tbody>
  72. <tr>
  73. <td>
  74. <div id="menuBarProgrammatic"
  75. class="format-lightborder"></div>
  76. </td>
  77. </tr>
  78. </tbody>
  79. </table>
  80. <br>
  81. </fieldset>
  82. </td>
  83. <td width="67%">
  84. <fieldset>
  85. <legend>
  86. This <strong>menu bar</strong> is decorated:
  87. &nbsp;
  88. </legend>
  89. <!-- Wrap the menubar in a table to prevent it from stretching -->
  90. <table border="0" cellpadding="0" cellspacing="4"><tbody>
  91. <tr align="middle"><td>
  92. <!-- Decorated menu bar definition begins here -->
  93. <div id="menubar" class="goog-menubar format-lightborder">
  94. <div id="FileButton" class="goog-menu-button" title="File">
  95. <span style="vertical-align:middle">File</span>
  96. <!-- This DIV will be auto-decorated with a menu. -->
  97. <div id="fileMenu" class="goog-menu">
  98. <div class="goog-menuitem">Open</div>
  99. <div class="goog-menuitem">Save</div>
  100. <div class="goog-menuseparator"></div>
  101. <div class="goog-menuitem">Close</div>
  102. </div>
  103. </div>
  104. <div id="formatButton" class="goog-menu-button"
  105. title="Format">
  106. <!-- These elements will become the button's caption. -->
  107. <div class="icon format-icon goog-inline-block"></div>
  108. <span style="vertical-align:middle">Format</span>
  109. <!-- This DIV will be auto-decorated with a menu. -->
  110. <div id="formatMenu" class="goog-menu">
  111. <div class="goog-menuitem">Bold</div>
  112. <div class="goog-menuitem">Italic</div>
  113. <div class="goog-menuitem">Underline</div>
  114. <div class="goog-menuseparator"></div>
  115. <div class="goog-menuitem goog-menuitem-disabled">
  116. Strikethrough
  117. </div>
  118. <div class="goog-menuseparator"></div>
  119. <div class="goog-menuitem">Font...</div>
  120. <div class="goog-menuitem">Color...</div>
  121. </div>
  122. </div>
  123. </div>
  124. </td></tr></tbody></table>
  125. <br>
  126. </fieldset>
  127. </td>
  128. </tr>
  129. <tr>
  130. <td width="33%">
  131. <!-- Event log. -->
  132. <fieldset class="goog-debug-panel">
  133. <legend>Event Log</legend>
  134. <div id="log"></div>
  135. </fieldset>
  136. </td>
  137. </tr>
  138. </tbody>
  139. </table>
  140. <br>
  141. <div id="perf"></div>
  142. <script>
  143. // Create programmatic menu bar with menus.
  144. buildMenu = function(rootNode) {
  145. var menubar = goog.ui.menuBar.create();
  146. var menuNames = ["File","Edit","About"];
  147. var menuOptions = [];
  148. menuOptions[0] = ['New Files', 'Open File', null, 'Exit'];
  149. menuOptions[1] = ['Copy', 'Paste'];
  150. menuOptions[2] = ['Zerg Rush!', null, 'Exit'];
  151. for (i in menuNames) {
  152. // Create the drop down menu with a few suboptions.
  153. var menu = new goog.ui.Menu();
  154. goog.array.forEach(menuOptions[i],
  155. function(label) {
  156. var item;
  157. if (label) {
  158. item = new goog.ui.MenuItem(label + '...');
  159. item.setId(label);
  160. } else {
  161. item = new goog.ui.MenuSeparator();
  162. }
  163. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  164. menu.addItem(item);
  165. });
  166. // Create a button inside menubar.
  167. var btn = new goog.ui.MenuButton(menuNames[i], menu);
  168. btn.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  169. menubar.addChild(btn, true);
  170. goog.events.listen(btn, EVENTS, logEvent);
  171. }
  172. menubar.render(goog.dom.getElement('menuBarProgrammatic'));
  173. }
  174. // Set up a logger.
  175. var timer = goog.now();
  176. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  177. var logger = goog.log.getLogger('demo');
  178. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  179. logconsole.setCapturing(true);
  180. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  181. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  182. function logEvent(e) {
  183. var component = e.target;
  184. var caption = (typeof component.getCaption == 'function') ?
  185. component.getCaption() : component.getId();
  186. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  187. }
  188. buildMenu(goog.dom.getElement('menuBarProgrammatic'));
  189. // Decorate a pre-baked menubar.
  190. var menubar = goog.ui.decorate(goog.dom.getElement('menubar'));
  191. // Compute time to process entire page
  192. goog.dom.setTextContent(goog.dom.getElement('perf'),
  193. (goog.now() - timer) + 'ms');
  194. </script>
  195. </body>
  196. </html>