menu.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Menu</title>
  10. <link rel="stylesheet" href="css/demo.css">
  11. <link rel="stylesheet" href="../css/menu.css">
  12. <link rel="stylesheet" href="../css/menuitem.css">
  13. <link rel="stylesheet" href="../css/menuseparator.css">
  14. <script src="../base.js"></script>
  15. <script>
  16. goog.require('goog.array');
  17. goog.require('goog.debug.DivConsole');
  18. goog.require('goog.debug.LogManager');
  19. goog.require('goog.events');
  20. goog.require('goog.log');
  21. goog.require('goog.object');
  22. goog.require('goog.ui.CheckBoxMenuItem');
  23. goog.require('goog.ui.Menu');
  24. goog.require('goog.ui.MenuItem');
  25. goog.require('goog.dom.TagName');
  26. </script>
  27. </head>
  28. <body>
  29. <h1>goog.ui.Menu</h1>
  30. <div>
  31. This is a very basic menu class, it doesn't handle its display or
  32. dismissal. It just exists, listens to keys and mouse events and can fire
  33. events for selections or highlights.
  34. </div>
  35. <div>
  36. <a href="javascript:void(menu1.setVisible(true));">Show Menu</a> |
  37. <a href="javascript:void(menu1.setVisible(false));">Hide Menu</a> |
  38. <a href="javascript:void(menu1.setPosition(0, 0));">Move(0, 0)</a> |
  39. <a href="javascript:void(menu1.setPosition(50, 50));">Move(50, 50)</a> |
  40. <a href="javascript:void(menu1.setPosition(50, 100));">Move(50, 100)</a> |
  41. <a href="javascript:void(menu1.setPosition(200, 300));">Move(200, 300)</a>
  42. </div>
  43. <br>
  44. <br>
  45. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  46. <tbody>
  47. <tr valign="top" height="180">
  48. <td width="33%">
  49. <div id="menu1"></div>
  50. </td>
  51. <td width="33%">
  52. <div id="menu2" class="goog-menu">
  53. <div class="goog-menuitem">Google</div>
  54. <div class="goog-menuitem">Yahoo</div>
  55. <div class="goog-menuitem">MSN</div>
  56. <hr>
  57. <div id="enableNewItems"
  58. class="goog-menuitem goog-option goog-option-selected">
  59. <div class="goog-menuitem-accel">Ctrl+E</div>
  60. Enable new items
  61. </div>
  62. <hr>
  63. <div id="addNewItem" class="goog-menuitem">
  64. <div class="goog-menuitem-accel">Ctrl+Shift+N</div>
  65. New...
  66. </div>
  67. </div>
  68. </td>
  69. <td width="33%">
  70. <ul id="menu3" class="goog-menu">
  71. <li class="goog-menuitem">I am a <b>menu</b></li>
  72. <li class="goog-menuitem">based on an</li>
  73. <li class="goog-menuitem goog-menuitem-disabled">unordered list</li>
  74. <li class="goog-menuitem">Woop!</li>
  75. </ul>
  76. </td>
  77. </tr>
  78. <tr valign="top" height="180">
  79. <td width="33%">
  80. Here's a menu with checkbox items.<br>You checked:&nbsp;
  81. <span id="checkedItems" style="color: #800">Bold</span><br>
  82. <div id="menu4"></div>
  83. </td>
  84. <td width="33%">
  85. Here's a BiDi menu with checkbox items.<br>
  86. <div id="menu5" dir="rtl"></div>
  87. </td>
  88. <td width="33%">
  89. Here's a menu with an explicit content container.<br>
  90. <div id="menu6" class="goog-menu">
  91. <div style="background: #ccc; border-right: 15px solid #888;">
  92. <div class="goog-menu-content">
  93. <div class="goog-menuitem">Red</div>
  94. <div class="goog-menuitem">Black</div>
  95. <div class="goog-menuitem">Blue</div>
  96. </div>
  97. </div>
  98. <hr>
  99. <div class="goog-menuitem">White</div>
  100. </div>
  101. </td>
  102. </tr>
  103. </tbody>
  104. </table>
  105. <br>
  106. <br>
  107. <!-- Event log. -->
  108. <fieldset class="goog-debug-panel">
  109. <legend>Event Log</legend>
  110. <div id="log"></div>
  111. </fieldset>
  112. <div id="perf"></div>
  113. <script type="text/javascript">
  114. var timer = goog.now();
  115. // Set up a logger.
  116. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  117. var logger = goog.log.getLogger('demo');
  118. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  119. logconsole.setCapturing(true);
  120. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  121. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  122. function logEvent(e) {
  123. var name = e.target.getCaption ? e.target.getCaption() : 'Menu';
  124. goog.log.info(logger, '"' + name + '" dispatched: ' + e.type);
  125. }
  126. var el = goog.dom.getElement('menu1');
  127. var menu1 = new goog.ui.Menu();
  128. var m1, m2, m3, m4, m5, m6;
  129. menu1.addItem(m1 = new goog.ui.MenuItem('Inbox'));
  130. menu1.addItem(m2 = new goog.ui.MenuItem('Starred'));
  131. menu1.addItem(m3 = new goog.ui.MenuItem('Chats'));
  132. menu1.addItem(m4 = new goog.ui.MenuItem('Sent'));
  133. menu1.addItem(new goog.ui.MenuSeparator());
  134. menu1.addItem(m5 = new goog.ui.MenuItem('New Folder...'));
  135. menu1.addItemAt(m6 = new goog.ui.MenuItem('All Mail'), 1);
  136. menu1.render(el);
  137. goog.events.listen(menu1, EVENTS, logEvent);
  138. m1.setEnabled(false);
  139. m6.setEnabled(false);
  140. var el2 = goog.dom.getElement('menu2');
  141. var menu2 = new goog.ui.Menu();
  142. menu2.decorate(el2);
  143. goog.events.listen(menu2, EVENTS, logEvent);
  144. goog.events.listen(menu2, 'action', function(e) {
  145. if (e.target.getId() == 'addNewItem') {
  146. var n = prompt('Enter a new item...');
  147. if (n) {
  148. menu2.addItemAt(new goog.ui.MenuItem(n), menu2.getItemCount() - 4);
  149. }
  150. } else if (e.target.getId() == 'enableNewItems') {
  151. menu2.getItemAt(menu2.getItemCount() - 1).setEnabled(
  152. e.target.isChecked());
  153. } else {
  154. alert(e.target.getCaption());
  155. }
  156. });
  157. var el3 = goog.dom.getElement('menu3');
  158. var menu3 = new goog.ui.Menu();
  159. menu3.decorate(el3);
  160. goog.events.listen(menu3, EVENTS, logEvent);
  161. var menu4 = new goog.ui.Menu();
  162. var foo, bar;
  163. menu4.addItem(foo = new goog.ui.CheckBoxMenuItem('Bold'));
  164. menu4.addItem(new goog.ui.CheckBoxMenuItem('Italic'));
  165. menu4.addItem(bar = new goog.ui.CheckBoxMenuItem('3D'));
  166. menu4.addItem(new goog.ui.CheckBoxMenuItem('Underline'));
  167. foo.setChecked(true);
  168. bar.setEnabled(false);
  169. menu4.render(goog.dom.getElement('menu4'));
  170. goog.events.listen(menu4, EVENTS, logEvent);
  171. goog.events.listen(menu4, 'action', function(e) {
  172. var items = [];
  173. menu4.forEachChild(function(child) {
  174. if (child.isChecked()) {
  175. items.push(child.getCaption());
  176. }
  177. });
  178. goog.dom.setTextContent(goog.dom.getElement('checkedItems'),
  179. items.length > 0 ? items.join(', ') : 'nothing');
  180. });
  181. function createCheckBoxItem(label, shortcut) {
  182. return new goog.ui.CheckBoxMenuItem([
  183. goog.dom.createDom(goog.dom.TagName.DIV, 'goog-menuitem-accel', shortcut),
  184. goog.dom.createTextNode(label)
  185. ]);
  186. }
  187. var menu5 = new goog.ui.Menu();
  188. menu5.setRightToLeft(true);
  189. var fee, baz;
  190. menu5.addItem(fee = createCheckBoxItem('Bold', 'Ctrl+B'));
  191. menu5.addItem(createCheckBoxItem('Italic', 'Ctrl+I'));
  192. menu5.addItem(baz = createCheckBoxItem('3D', 'Ctrl+Shift+3'));
  193. menu5.addItem(createCheckBoxItem('Underline', 'Ctrl+U'));
  194. fee.setChecked(true);
  195. baz.setEnabled(false);
  196. menu5.render(goog.dom.getElement('menu5'));
  197. goog.events.listen(menu5, EVENTS, logEvent);
  198. var menu6 = new goog.ui.Menu();
  199. menu6.decorate(goog.dom.getElement('menu6'));
  200. goog.events.listen(menu6, EVENTS, logEvent);
  201. goog.dom.setTextContent(goog.dom.getElement('perf'),
  202. (goog.now() - timer) + 'ms');
  203. </script>
  204. </body>
  205. </html>