menubutton.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.MenuButton 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.decorate');
  25. </script>
  26. <link rel="stylesheet" href="css/demo.css">
  27. <link rel="stylesheet" href="../css/custombutton.css">
  28. <link rel="stylesheet" href="../css/menu.css">
  29. <link rel="stylesheet" href="../css/menuitem.css">
  30. <link rel="stylesheet" href="../css/menuseparator.css">
  31. <link rel="stylesheet" href="../css/menubutton.css">
  32. <style>
  33. /* Base class for all icon elements. */
  34. .icon {
  35. height: 16px;
  36. width: 16px;
  37. margin: 0 1px;
  38. background-image: url(../images/toolbar_icons.gif);
  39. background-repeat: no-repeat;
  40. vertical-align: middle;
  41. }
  42. /* "Format" icon. */
  43. .format-icon {
  44. background-position: -64px;
  45. }
  46. .positioning-frame {
  47. height: 250px;
  48. overflow: auto;
  49. width: 100%;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <h1>goog.ui.MenuButton</h1>
  55. <table border="0" cellpadding="0" cellspacing="4" width="100%">
  56. <tbody>
  57. <tr valign="top">
  58. <td width="67%">
  59. <fieldset>
  60. <legend>
  61. These <strong>MenuButton</strong>s were created programmatically:
  62. &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. <td>
  71. Enable first button:
  72. <input type="checkbox" id="b1_enable" checked>
  73. &nbsp;
  74. Show second button:
  75. <input type="checkbox" id="b2_show" checked>
  76. &nbsp;
  77. </td>
  78. </tr>
  79. </tbody>
  80. </table>
  81. <label>
  82. </label>
  83. <br>
  84. </fieldset>
  85. <fieldset>
  86. <legend>
  87. This <strong>MenuButton</strong> decorates an element:&nbsp;
  88. </legend>
  89. <table border="0" cellpadding="0" cellspacing="4">
  90. <tbody>
  91. <tr valign="middle">
  92. <td>
  93. <div id="formatButton" class="goog-menu-button"
  94. title="Format">
  95. <!-- These elements will become the button's caption. -->
  96. <div class="icon format-icon goog-inline-block"></div>
  97. <span style="vertical-align:middle">Format</span>
  98. <!-- This DIV will be auto-decorated with a menu. -->
  99. <div id="formatMenu" class="goog-menu">
  100. <div class="goog-menuitem">Bold</div>
  101. <div class="goog-menuitem">Italic</div>
  102. <div class="goog-menuitem">Underline</div>
  103. <div class="goog-menuseparator"></div>
  104. <div class="goog-menuitem goog-menuitem-disabled">
  105. Strikethrough
  106. </div>
  107. <div class="goog-menuseparator"></div>
  108. <div class="goog-menuitem">Font...</div>
  109. <div class="goog-menuitem">Color...</div>
  110. </div>
  111. </div>
  112. </td>
  113. <td>
  114. Enable button:
  115. <input type="checkbox" id="formatButton_enable" checked>
  116. &nbsp;
  117. Show button:
  118. <input type="checkbox" id="formatButton_show" checked>
  119. &nbsp;
  120. </td>
  121. </tr>
  122. </tbody>
  123. </table>
  124. <label>
  125. </label>
  126. <br>
  127. </fieldset>
  128. <fieldset>
  129. <legend>
  130. This <strong>MenuButton</strong> accompanies a
  131. <strong>CustomButton</strong> to form a combo button:
  132. &nbsp;
  133. </legend>
  134. <div id="comboButtons"></div>
  135. <label>
  136. </label>
  137. <br>
  138. </fieldset>
  139. <fieldset>
  140. <legend>
  141. These <strong>MenuButtons</strong> demonstrate
  142. menu positioning options:
  143. &nbsp;
  144. </legend>
  145. <div></div>
  146. <input type="checkbox" id="pos_option_scroll">
  147. <label for="pos_option_scroll">
  148. Scroll on overflow
  149. </label>
  150. <iframe class="positioning-frame" id="positioning-frame"
  151. src="menubutton_frame.html">
  152. </iframe>
  153. <label>
  154. </label>
  155. <br>
  156. </fieldset>
  157. </td>
  158. <td width="33%">
  159. <!-- Event log. -->
  160. <fieldset class="goog-debug-panel">
  161. <legend>Event Log</legend>
  162. <div id="log"></div>
  163. </fieldset>
  164. </td>
  165. </tr>
  166. </tbody>
  167. </table>
  168. <br>
  169. <div id="perf"></div>
  170. <script>
  171. var timer = goog.now();
  172. // Set up a logger.
  173. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  174. var logger = goog.log.getLogger('demo');
  175. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  176. logconsole.setCapturing(true);
  177. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  178. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  179. function logEvent(e) {
  180. var component = e.target;
  181. var caption = (typeof component.getCaption == 'function') ?
  182. component.getCaption() : component.getId();
  183. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  184. }
  185. // Create a sample menu.
  186. var m1 = new goog.ui.Menu();
  187. m1.setId('FileMenu');
  188. goog.array.forEach(['New', 'Open', 'Save', 'Save as', null, 'Change label'],
  189. function(label) {
  190. var item;
  191. if (label) {
  192. item = new goog.ui.MenuItem(label + '...');
  193. item.setId(label);
  194. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  195. if (label == 'Save as') {
  196. item.setEnabled(false);
  197. }
  198. } else {
  199. item = new goog.ui.MenuSeparator();
  200. }
  201. m1.addItem(item);
  202. });
  203. // Create the first button programmatically.
  204. var b1 = new goog.ui.MenuButton('File', m1);
  205. b1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  206. b1.setId('FileButton');
  207. b1.render(goog.dom.getElement('menuButtons'));
  208. b1.setTooltip('File menu demo');
  209. goog.events.listen(b1, EVENTS, logEvent);
  210. goog.events.listen(goog.dom.getElement('b1_enable'),
  211. goog.events.EventType.CLICK,
  212. function(e) {
  213. b1.setEnabled(e.target.checked);
  214. });
  215. goog.events.listen(b1, goog.ui.Component.EventType.ACTION,
  216. function(e) {
  217. if (e.target && e.target.getCaption() == 'Change label...') {
  218. var label = window.prompt('Enter new menu label:', b1.getCaption());
  219. b1.setCaption(label || 'Empty');
  220. }
  221. });
  222. // Create another sample menu.
  223. var m2 = new goog.ui.Menu();
  224. m2.setId('EditMenu');
  225. goog.array.forEach(['Cut', 'Copy', 'Paste', null, 'Paste special'],
  226. function(label) {
  227. var item;
  228. if (label) {
  229. item = new goog.ui.MenuItem(label + '...');
  230. item.setId(label);
  231. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  232. } else {
  233. item = new goog.ui.MenuSeparator();
  234. }
  235. m2.addItem(item);
  236. });
  237. // Create the second button programmatically.
  238. var b2 = new goog.ui.MenuButton('Edit', m2);
  239. b2.setId('EditButton');
  240. b2.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  241. b2.render(goog.dom.getElement('menuButtons'));
  242. b2.setTooltip('Edit menu demo');
  243. goog.events.listen(b2, EVENTS, logEvent);
  244. goog.events.listen(goog.dom.getElement('b2_show'),
  245. goog.events.EventType.CLICK,
  246. function(e) {
  247. b2.setVisible(e.target.checked);
  248. });
  249. // Create another sample menu.
  250. var m3 = new goog.ui.Menu();
  251. m3.setId('WindowMenu');
  252. goog.array.forEach(['Tile', 'Cascade', null, 'Zoom in', 'Zoom out'],
  253. function(label) {
  254. var item;
  255. if (label) {
  256. item = new goog.ui.MenuItem(label + '...');
  257. item.setId(label);
  258. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  259. } else {
  260. item = new goog.ui.MenuSeparator();
  261. }
  262. m3.addItem(item);
  263. });
  264. var unhighlightable = new goog.ui.MenuItem('Zoom control:');
  265. // Disabled implies unhighlightable, unless the menu is specifically set
  266. // to allow highlighting disabled items (but it doesn't by default).
  267. unhighlightable.setEnabled(false);
  268. unhighlightable.setId('unhighlightable');
  269. m3.addItemAt(unhighlightable, 3);
  270. // Create a third button programmatically.
  271. var b3 = new goog.ui.MenuButton('Window', m3);
  272. b3.setId('WindowButton');
  273. b3.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  274. b3.render(goog.dom.getElement('menuButtons'));
  275. b3.setTooltip('Windowm menu demo');
  276. goog.events.listen(b3, EVENTS, logEvent);
  277. // Decorate a menu button. Note that since one of the child nodes of the
  278. // menu button element can be decorated as a menu, it is auto-decorated and
  279. // attached to the button.
  280. var formatButton = goog.ui.decorate(goog.dom.getElement('formatButton'));
  281. goog.events.listen(goog.dom.getElement('formatButton_show'),
  282. goog.events.EventType.CLICK,
  283. function(e) {
  284. formatButton.setVisible(e.target.checked);
  285. });
  286. goog.events.listen(goog.dom.getElement('formatButton_enable'),
  287. goog.events.EventType.CLICK,
  288. function(e) {
  289. formatButton.setEnabled(e.target.checked);
  290. });
  291. goog.events.listen(formatButton, EVENTS, logEvent);
  292. // Create a combo button with a custom button and a menu button.
  293. // Create a sample menu.
  294. var cbb1 = new goog.ui.CustomButton('Add to Family');
  295. cbb1.render(goog.dom.getElement('comboButtons'));
  296. cbb1.addClassName('goog-custom-button-collapse-right');
  297. var cbm1 = new goog.ui.Menu();
  298. cbm1.setId('ComboMenu');
  299. goog.array.forEach(['Friends', 'Family', 'Coworkers'],
  300. function(label) {
  301. var item = new goog.ui.MenuItem(label);
  302. item.setId(label);
  303. item.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  304. cbm1.addItem(item);
  305. });
  306. var cb1 = new goog.ui.Button('Take action', cbm1);
  307. var cbmb1 = new goog.ui.MenuButton(null, cbm1);
  308. cbmb1.setPositionElement(cbb1.getElement());
  309. cbmb1.addClassName('goog-menu-button-collapse-left');
  310. cbmb1.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  311. cbmb1.setId('ComboMenuButton');
  312. cbmb1.render(goog.dom.getElement('comboButtons'));
  313. cbmb1.setTooltip('Combo button');
  314. goog.events.listen(cbmb1, EVENTS, logEvent);
  315. // Create two buttons with menus for the positioning demo inside the
  316. // positioning iframe.
  317. var positioningFrame = goog.dom.getElement('positioning-frame');
  318. var shortPosButton, longPosButton;
  319. goog.events.listen(positioningFrame, 'load', function() {
  320. var frameDocument = goog.dom.getFrameContentDocument(positioningFrame);
  321. var frameDomHelper = new goog.dom.DomHelper(frameDocument);
  322. var shortMenu = new goog.ui.Menu(frameDomHelper);
  323. shortMenu.setId('PositionMenuShort');
  324. for (var i = 1; i <= 5; i++) {
  325. shortMenu.addItem(new goog.ui.MenuItem('Item ' + i + '...'));
  326. }
  327. shortPosButton = new goog.ui.MenuButton('Short Menu', shortMenu);
  328. shortPosButton.setId('PositingButtonShort');
  329. shortPosButton.render(frameDocument.body);
  330. shortMenu.getElement().style.overflowY = 'auto';
  331. var longMenu = new goog.ui.Menu(frameDomHelper);
  332. longMenu.setId('PositionMenuLong');
  333. for (var i = 1; i <= 15; i++) {
  334. longMenu.addItem(new goog.ui.MenuItem('Item ' + i + '...'));
  335. }
  336. longPosButton = new goog.ui.MenuButton('Long Menu', longMenu);
  337. longPosButton.setId('PositingButtonLong');
  338. longPosButton.render(frameDocument.body);
  339. longMenu.getElement().style.overflowY = 'auto';
  340. });
  341. function updateScrollOption() {
  342. var checked = goog.dom.getElement('pos_option_scroll').checked;
  343. shortPosButton.setScrollOnOverflow(checked);
  344. longPosButton.setScrollOnOverflow(checked);
  345. }
  346. goog.events.listen(goog.dom.getElement('pos_option_scroll'), 'click',
  347. updateScrollOption);
  348. goog.dom.setTextContent(goog.dom.getElement('perf'),
  349. (goog.now() - timer) + 'ms');
  350. </script>
  351. </body>
  352. </html>