menuitem.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2011 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.MenuItem 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.CustomButton');
  20. goog.require('goog.ui.Menu');
  21. goog.require('goog.ui.MenuButton');
  22. goog.require('goog.ui.MenuItem');
  23. goog.require('goog.ui.Separator');
  24. goog.require('goog.ui.SubMenu');
  25. goog.require('goog.ui.decorate');
  26. </script>
  27. <link rel="stylesheet" href="css/demo.css">
  28. <link rel="stylesheet" href="../css/custombutton.css">
  29. <link rel="stylesheet" href="../css/menu.css">
  30. <link rel="stylesheet" href="../css/menuitem.css">
  31. <link rel="stylesheet" href="../css/menuseparator.css">
  32. <link rel="stylesheet" href="../css/menubutton.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. </style>
  53. </head>
  54. <body>
  55. <h1>goog.ui.MenuItem</h1>
  56. <table border="0" cellpadding="0" cellspacing="4" width="100%">
  57. <tbody>
  58. <tr valign="top">
  59. <td width="67%">
  60. <fieldset>
  61. <legend>
  62. Use the first letter of each menuitem to activate: &nbsp;
  63. </legend>
  64. <table border="0" cellpadding="0" cellspacing="4">
  65. <tbody>
  66. <tr valign="middle">
  67. <td>
  68. <div id="menuButtons"></div>
  69. </td>
  70. </tr>
  71. </tbody>
  72. </table>
  73. <label>
  74. </label>
  75. <br>
  76. </fieldset>
  77. <td width="33%">
  78. <!-- Event log. -->
  79. <fieldset class="goog-debug-panel">
  80. <legend>Event Log</legend>
  81. <div id="log"></div>
  82. </fieldset>
  83. </td>
  84. </tr>
  85. </tbody>
  86. </table>
  87. <br>
  88. <div id="perf"></div>
  89. <script>
  90. var timer = goog.now();
  91. // Set up a logger.
  92. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  93. var logger = goog.log.getLogger('demo');
  94. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  95. logconsole.setCapturing(true);
  96. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  97. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  98. function logEvent(e) {
  99. var component = e.target;
  100. var caption = (typeof component.getCaption == 'function') ?
  101. component.getCaption() : component.getId();
  102. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  103. }
  104. // Create a sample menu.
  105. var m1 = new goog.ui.Menu();
  106. m1.setId('FileMenu');
  107. goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'],
  108. function(label) {
  109. var item;
  110. if (label) {
  111. item = new goog.ui.MenuItem(label + '...');
  112. item.setId(label);
  113. item.setMnemonic(goog.events.KeyCodes[label.charAt(0)]);
  114. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  115. if (label == 'Save as') {
  116. item.setMnemonic(goog.events.KeyCodes.A);
  117. item.setEnabled(false);
  118. }
  119. } else {
  120. item = new goog.ui.MenuSeparator();
  121. }
  122. m1.addItem(item);
  123. });
  124. // Add a submenu.
  125. var s1 = new goog.ui.SubMenu('More');
  126. s1.setId('PrintSubmenu');
  127. s1.setMnemonic(goog.events.KeyCodes.M);
  128. goog.array.forEach(['Print', 'Send'], function(label) {
  129. var item = new goog.ui.MenuItem(label + '...');
  130. item.setId(label);
  131. item.setMnemonic(goog.events.KeyCodes[label.charAt(0)]);
  132. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  133. s1.addItem(item);
  134. });
  135. m1.addItem(s1);
  136. // Create the first button programmatically.
  137. var b1 = new goog.ui.MenuButton('File', m1);
  138. b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  139. b1.setId('FileButton');
  140. b1.render(goog.dom.getElement('menuButtons'));
  141. b1.setTooltip('File menu demo');
  142. goog.events.listen(b1, EVENTS, logEvent);
  143. goog.events.listen(b1, goog.ui.Component.EventType.ACTION,
  144. function(e) {
  145. if (e.target && e.target.getCaption() == 'Change label...') {
  146. var label = window.prompt('Enter new menu label:', b1.getCaption());
  147. b1.setCaption(label || 'Empty');
  148. }
  149. });
  150. goog.dom.setTextContent(goog.dom.getElement('perf'),
  151. (goog.now() - timer) + 'ms');
  152. </script>
  153. </body>
  154. </html>