css3menubutton.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.Css3MenuButtonRenderer 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.Css3MenuButtonRenderer');
  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.decorate');
  25. </script>
  26. <link rel="stylesheet" href="css/demo.css">
  27. <link rel="stylesheet" href="../css/menu.css">
  28. <link rel="stylesheet" href="../css/menuitem.css">
  29. <link rel="stylesheet" href="../css/menuseparator.css">
  30. <link rel="stylesheet" href="../css/menubutton.css">
  31. <link rel="stylesheet" href="../css/css3button.css">
  32. <link rel="stylesheet" href="../css/css3menubutton.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. </style>
  48. </head>
  49. <body>
  50. <h1>goog.ui.Css3MenuButtonRenderer</h1>
  51. <table border="0" cellpadding="0" cellspacing="4" width="100%">
  52. <tbody>
  53. <tr valign="top">
  54. <td width="67%">
  55. <fieldset>
  56. <legend>
  57. These <strong>MenuButton</strong>s were created programmatically:
  58. &nbsp;
  59. </legend>
  60. <table border="0" cellpadding="0" cellspacing="4">
  61. <tbody>
  62. <tr valign="middle">
  63. <td>
  64. <div id="menuButtons"></div>
  65. </td>
  66. <td>
  67. Enable first button:
  68. <input type="checkbox" id="b1_enable" checked>
  69. &nbsp;
  70. Show second button:
  71. <input type="checkbox" id="b2_show" checked>
  72. &nbsp;
  73. </td>
  74. </tr>
  75. </tbody>
  76. </table>
  77. <label>
  78. </label>
  79. <br>
  80. </fieldset>
  81. <fieldset>
  82. <legend>
  83. This <strong>MenuButton</strong> decorates an element:&nbsp;
  84. </legend>
  85. <table border="0" cellpadding="0" cellspacing="4">
  86. <tbody>
  87. <tr valign="middle">
  88. <td>
  89. <div id="formatButton" class="goog-css3-menu-button"
  90. title="Format">
  91. <!-- These elements will become the button's caption. -->
  92. <div class="icon format-icon goog-inline-block"></div>
  93. <span style="vertical-align:middle">Format</span>
  94. <!-- This DIV will be auto-decorated with a menu. -->
  95. <div id="formatMenu" class="goog-menu">
  96. <div class="goog-menuitem">Bold</div>
  97. <div class="goog-menuitem">Italic</div>
  98. <div class="goog-menuitem">Underline</div>
  99. <div class="goog-menuseparator"></div>
  100. <div class="goog-menuitem goog-menuitem-disabled">
  101. Strikethrough
  102. </div>
  103. <div class="goog-menuseparator"></div>
  104. <div class="goog-menuitem">Font...</div>
  105. <div class="goog-menuitem">Color...</div>
  106. </div>
  107. </div>
  108. </td>
  109. <td>
  110. Enable button:
  111. <input type="checkbox" id="formatButton_enable" checked>
  112. &nbsp;
  113. Show button:
  114. <input type="checkbox" id="formatButton_show" checked>
  115. &nbsp;
  116. </td>
  117. </tr>
  118. </tbody>
  119. </table>
  120. <label>
  121. </label>
  122. <br>
  123. </fieldset>
  124. </td>
  125. <td width="33%">
  126. <!-- Event log. -->
  127. <fieldset class="goog-debug-panel">
  128. <legend>Event Log</legend>
  129. <div id="log"></div>
  130. </fieldset>
  131. </td>
  132. </tr>
  133. </tbody>
  134. </table>
  135. <br>
  136. <div id="perf"></div>
  137. <script>
  138. var timer = goog.now();
  139. // Set up a logger.
  140. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  141. var logger = goog.log.getLogger('demo');
  142. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  143. logconsole.setCapturing(true);
  144. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  145. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  146. function logEvent(e) {
  147. var component = e.target;
  148. var caption = (typeof component.getCaption == 'function') ?
  149. component.getCaption() : component.getId();
  150. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  151. }
  152. // Create a sample menu.
  153. var m1 = new goog.ui.Menu();
  154. m1.setId('FileMenu');
  155. goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'],
  156. function(label) {
  157. var item;
  158. if (label) {
  159. item = new goog.ui.MenuItem(label + '...');
  160. item.setId(label);
  161. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  162. if (label == 'Save as') {
  163. item.setEnabled(false);
  164. }
  165. } else {
  166. item = new goog.ui.MenuSeparator();
  167. }
  168. m1.addItem(item);
  169. });
  170. // Create the first button programmatically.
  171. var b1 = new goog.ui.MenuButton('File', m1,
  172. new goog.ui.Css3MenuButtonRenderer());
  173. b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  174. b1.setId('FileButton');
  175. b1.render(goog.dom.getElement('menuButtons'));
  176. b1.setTooltip('File menu demo');
  177. goog.events.listen(b1, EVENTS, logEvent);
  178. goog.events.listen(goog.dom.getElement('b1_enable'),
  179. goog.events.EventType.CLICK,
  180. function(e) {
  181. b1.setEnabled(e.target.checked);
  182. });
  183. goog.events.listen(b1, goog.ui.Component.EventType.ACTION,
  184. function(e) {
  185. if (e.target && e.target.getCaption() == 'Change label...') {
  186. var label = window.prompt('Enter new menu label:', b1.getCaption());
  187. b1.setCaption(label || 'Empty');
  188. }
  189. });
  190. // Create another sample menu.
  191. var m2 = new goog.ui.Menu();
  192. m2.setId('EditMenu');
  193. goog.array.forEach(['Cut', 'Copy', 'Paste', null, 'Paste special'],
  194. function(label) {
  195. var item;
  196. if (label) {
  197. item = new goog.ui.MenuItem(label + '...');
  198. item.setId(label);
  199. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  200. } else {
  201. item = new goog.ui.MenuSeparator();
  202. }
  203. m2.addItem(item);
  204. });
  205. // Create the second button programmatically.
  206. var b2 = new goog.ui.MenuButton('Edit', m2,
  207. new goog.ui.Css3MenuButtonRenderer());
  208. b2.setId('EditButton');
  209. b2.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  210. b2.render(goog.dom.getElement('menuButtons'));
  211. b2.setTooltip('Edit menu demo');
  212. goog.events.listen(b2, EVENTS, logEvent);
  213. goog.events.listen(goog.dom.getElement('b2_show'),
  214. goog.events.EventType.CLICK,
  215. function(e) {
  216. b2.setVisible(e.target.checked);
  217. });
  218. // Create another sample menu.
  219. var m3 = new goog.ui.Menu();
  220. m3.setId('WindowMenu');
  221. goog.array.forEach(['Tile', 'Cascade', null, 'Zoom in', 'Zoom out'],
  222. function(label) {
  223. var item;
  224. if (label) {
  225. item = new goog.ui.MenuItem(label + '...');
  226. item.setId(label);
  227. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  228. } else {
  229. item = new goog.ui.MenuSeparator();
  230. }
  231. m3.addItem(item);
  232. });
  233. var unhighlightable = new goog.ui.MenuItem('Zoom control:');
  234. // Disabled implies unhighlightable, unless the menu is specifically set
  235. // to allow highlighting disabled items (but it doesn't by default).
  236. unhighlightable.setEnabled(false);
  237. unhighlightable.setId('unhighlightable');
  238. m3.addItemAt(unhighlightable, 3);
  239. // Create a third button programmatically.
  240. var b3 = new goog.ui.MenuButton('Window', m3,
  241. new goog.ui.Css3MenuButtonRenderer());
  242. b3.setId('WindowButton');
  243. b3.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  244. b3.render(goog.dom.getElement('menuButtons'));
  245. b3.setTooltip('Windowm menu demo');
  246. goog.events.listen(b3, EVENTS, logEvent);
  247. // Decorate a menu button. Note that since one of the child nodes of the
  248. // menu button element can be decorated as a menu, it is auto-decorated and
  249. // attached to the button.
  250. var formatButton = goog.ui.decorate(goog.dom.getElement('formatButton'));
  251. goog.events.listen(goog.dom.getElement('formatButton_show'),
  252. goog.events.EventType.CLICK,
  253. function(e) {
  254. formatButton.setVisible(e.target.checked);
  255. });
  256. goog.events.listen(goog.dom.getElement('formatButton_enable'),
  257. goog.events.EventType.CLICK,
  258. function(e) {
  259. formatButton.setEnabled(e.target.checked);
  260. });
  261. goog.events.listen(formatButton, EVENTS, logEvent);
  262. goog.dom.setTextContent(goog.dom.getElement('perf'),
  263. (goog.now() - timer) + 'ms');
  264. </script>
  265. </body>
  266. </html>