select.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.Select &amp; goog.ui.Option</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.Component.EventType');
  20. goog.require('goog.ui.FlatMenuButtonRenderer');
  21. goog.require('goog.ui.Option');
  22. goog.require('goog.ui.Select');
  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/menubutton.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/flatmenubutton.css">
  32. <style>
  33. .good {
  34. color: #080;
  35. vertical-align: middle;
  36. }
  37. .bad {
  38. color: #800;
  39. vertical-align: middle;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <h1>goog.ui.Select &amp; goog.ui.Option</h1>
  45. <fieldset>
  46. <legend>Demo of the <strong>goog.ui.Select</strong> component:</legend>
  47. <br>
  48. <label id="select1">The best movie of all time is </label>&nbsp;
  49. <span class="good" id="value1"></span>
  50. <br>
  51. <br>
  52. <label id="select2">The worst movie of all time is </label>&nbsp;
  53. <span class="bad" id="value2"></span>
  54. <br>
  55. <br>
  56. <label id="select3">The <strong>Select</strong> component for worst movie is
  57. </label>&nbsp;<i>(This control doesn't auto-highlight; it only dispatches
  58. ENTER and LEAVE events.)</i>
  59. <br>
  60. <br>
  61. <a id="add" href="#">Click here</a> to add a new option for the best movie,
  62. <a id="hide" href="#">here</a>
  63. to hide/show the select component for the best movie, or
  64. <a id="worst" href="#">here</a>
  65. to set the worst movie of all time to "Catwoman."
  66. <br>
  67. </fieldset>
  68. <br>
  69. <fieldset>
  70. <legend>This <strong>goog.ui.Select</strong> was decorated:</legend>
  71. <br>
  72. <label>
  73. My favorite Sergio Leone character is
  74. <div id="spaghetti" class="goog-select">
  75. Make your choice...
  76. <ul class="goog-menu">
  77. <li class="goog-menuitem">the Good</li>
  78. <li class="goog-menuitem">the Bad</li>
  79. <li class="goog-menuitem">the Ugly</li>
  80. <li class="goog-menuitem">the Man with the Harmonica</li>
  81. </ul>
  82. </div>
  83. </label>&nbsp;
  84. <span class="good" id="value4"></span>
  85. <br>
  86. <br>
  87. </fieldset>
  88. <br>
  89. <br>
  90. <fieldset>
  91. <legend>
  92. Demo of <strong>goog.ui.Select</strong> using
  93. <strong>goog.ui.FlatMenuButtonRenderer</strong>:
  94. </legend>
  95. <br>
  96. <label id="flat-select1">The best Arnold movie is </label>&nbsp;
  97. <span class="good" id="flat-value1"></span>
  98. <br>
  99. <br>
  100. <label id="flat-select2">The worst Arnold movie is </label>&nbsp;
  101. <span class="bad" id="flat-value2"></span>
  102. <br>
  103. <br>
  104. <label id="flat-select3">
  105. The <strong>Select</strong> component for worst movie is
  106. </label>&nbsp;<i>(This control doesn't auto-highlight; it only dispatches
  107. ENTER and LEAVE events.)</i>
  108. <br>
  109. <br>
  110. <a id="flat-add" href="#">Click here</a>
  111. to add a new option for the best Arnold movie,
  112. <a id="flat-hide" href="#">here</a>
  113. to hide/show the select component for the best Arnold movie, or
  114. <a id="flat-worst" href="#">here</a>
  115. to set the worst Arnold movie to "Jingle All the Way."
  116. <br>
  117. </fieldset>
  118. <br>
  119. <fieldset>
  120. <legend>This Flat <strong>goog.ui.Select</strong> was decorated:</legend>
  121. <br>
  122. <label>
  123. My favorite Will Ferrell character is
  124. <div id="flat-ferrell" class="goog-flat-menu-button">
  125. Make your choice...
  126. <ul class="goog-menu">
  127. <li class="goog-menuitem">Ron Burgundy</li>
  128. <li class="goog-menuitem">Chazz Reinhold</li>
  129. <li class="goog-menuitem">Chazz Michael Michaels</li>
  130. <li class="goog-menuitem">Ricky Bobby</li>
  131. </ul>
  132. </div>
  133. </label>&nbsp;
  134. <span class="good" id="flat-value4"></span>
  135. <br>
  136. <br>
  137. </fieldset>
  138. <br>
  139. <br>
  140. <!-- Event log. -->
  141. <fieldset class="goog-debug-panel">
  142. <legend>Event Log</legend>
  143. <div id="log"></div>
  144. </fieldset>
  145. <br>
  146. <div id="perf"></div>
  147. <script>
  148. var timer = goog.now();
  149. // Set up a logger.
  150. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  151. var logger = goog.log.getLogger('demo');
  152. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  153. logconsole.setCapturing(true);
  154. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  155. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  156. function logEvent(e) {
  157. var component = e.target;
  158. var caption = (typeof component.getCaption == 'function') ?
  159. component.getCaption() : component.getId();
  160. goog.log.info(logger, '"' + caption + '" dispatched: ' + e.type);
  161. }
  162. var select1 = new goog.ui.Select();
  163. select1.addItem(new goog.ui.MenuItem('Blade Runner'));
  164. select1.addItem(new goog.ui.MenuItem('Godfather Part II'));
  165. select1.addItem(new goog.ui.MenuItem('Citizen Kane'));
  166. select1.setSelectedIndex(0);
  167. select1.render(goog.dom.getElement('select1'));
  168. var select2 = new goog.ui.Select();
  169. var disabledItem;
  170. select2.addItem(new goog.ui.Option('Transformers'));
  171. select2.addItem(new goog.ui.Option('Spider-Man 3'));
  172. select2.addItem(disabledItem = new goog.ui.Option('Howard the Duck'));
  173. select2.addItem(new goog.ui.Option('Catwoman'));
  174. disabledItem.setEnabled(false);
  175. select2.setValue('Spider-Man 3');
  176. select2.render(goog.dom.getElement('select2'));
  177. var select3 = new goog.ui.Select('Click to select');
  178. // Turn off auto-highlighting, just for fun.
  179. select3.setAutoStates(goog.ui.Component.State.HOVER, false);
  180. select3.addItem(new goog.ui.Option('enabled', true));
  181. select3.addItem(new goog.ui.Option('disabled', false));
  182. select3.render(goog.dom.getElement('select3'));
  183. select3.setSelectedIndex(0);
  184. goog.events.listen(select1, goog.ui.Component.EventType.ACTION,
  185. function(e) {
  186. var select = e.target;
  187. var value = 'Yay ' + select.getValue() + '!';
  188. goog.dom.setTextContent(goog.dom.getElement('value1'), value);
  189. goog.dom.setTextContent(goog.dom.getElement('value2'), '');
  190. });
  191. goog.events.listen(select2, goog.ui.Component.EventType.ACTION,
  192. function(e) {
  193. var select = e.target;
  194. var value = 'Boo ' + select.getValue() + '...';
  195. goog.dom.setTextContent(goog.dom.getElement('value2'), value);
  196. goog.dom.setTextContent(goog.dom.getElement('value1'), '');
  197. });
  198. goog.events.listen(select3, goog.ui.Component.EventType.ACTION,
  199. function(e) {
  200. var select = e.target;
  201. select2.setEnabled(select.getValue());
  202. });
  203. goog.events.listen(goog.dom.getElement('add'), goog.events.EventType.CLICK,
  204. function(e) {
  205. var good = prompt('What\'s another good movie...?');
  206. if (select1.getItemCount() == 3) {
  207. select1.addItem(new goog.ui.Separator());
  208. }
  209. select1.addItem(new goog.ui.MenuItem(good));
  210. });
  211. goog.events.listen(goog.dom.getElement('hide'), goog.events.EventType.CLICK,
  212. function(e) {
  213. select1.setVisible(!select1.isVisible());
  214. });
  215. goog.events.listen(goog.dom.getElement('worst'),
  216. goog.events.EventType.CLICK,
  217. function(e) {
  218. select2.setValue('Catwoman');
  219. });
  220. // Decorate an element with a Select control.
  221. var select4 = goog.ui.decorate(goog.dom.getElement('spaghetti'));
  222. goog.events.listen(select1, EVENTS, logEvent);
  223. goog.events.listen(select2, EVENTS, logEvent);
  224. goog.events.listen(select3, EVENTS, logEvent);
  225. goog.events.listen(select4, EVENTS, logEvent);
  226. // goog.ui.Select using goog.ui.FlatMenuButtonRenderer
  227. var flatSelect1 = new goog.ui.Select(null, null,
  228. goog.ui.FlatMenuButtonRenderer.getInstance());
  229. flatSelect1.addItem(new goog.ui.MenuItem('Total Recall'));
  230. flatSelect1.addItem(new goog.ui.MenuItem('Kindergarten Cop'));
  231. flatSelect1.addItem(new goog.ui.MenuItem('Predator'));
  232. flatSelect1.setSelectedIndex(0);
  233. flatSelect1.render(goog.dom.getElement('flat-select1'));
  234. var flatSelect2 = new goog.ui.Select(null, null,
  235. goog.ui.FlatMenuButtonRenderer.getInstance());
  236. var flatDisabledItem;
  237. flatSelect2.addItem(new goog.ui.Option('Conan the Barbarian'));
  238. flatSelect2.addItem(new goog.ui.Option('Last Action Hero'));
  239. flatSelect2.addItem(
  240. flatDisabledItem = new goog.ui.Option('Eraser'));
  241. flatSelect2.addItem(new goog.ui.Option('Jingle All the Way'));
  242. flatDisabledItem.setEnabled(false);
  243. flatSelect2.setValue('Last Action Hero');
  244. flatSelect2.render(goog.dom.getElement('flat-select2'));
  245. var flatSelect3 = new goog.ui.Select('Click to select', null,
  246. goog.ui.FlatMenuButtonRenderer.getInstance());
  247. // Turn off auto-highlighting, just for fun.
  248. flatSelect3.setAutoStates(goog.ui.Component.State.HOVER, false);
  249. flatSelect3.addItem(new goog.ui.Option('enabled', true));
  250. flatSelect3.addItem(new goog.ui.Option('disabled', false));
  251. flatSelect3.render(goog.dom.getElement('flat-select3'));
  252. flatSelect3.setSelectedIndex(0);
  253. goog.events.listen(flatSelect1, goog.ui.Component.EventType.ACTION,
  254. function(e) {
  255. var select = e.target;
  256. var value = 'Yay ' + select.getValue() + '!';
  257. goog.dom.setTextContent(goog.dom.getElement('flat-value1'), value);
  258. goog.dom.setTextContent(goog.dom.getElement('flat-value2'), '');
  259. });
  260. goog.events.listen(flatSelect2, goog.ui.Component.EventType.ACTION,
  261. function(e) {
  262. var select = e.target;
  263. var value = 'Boo ' + select.getValue() + '...';
  264. goog.dom.setTextContent(goog.dom.getElement('flat-value2'), value);
  265. goog.dom.setTextContent(goog.dom.getElement('flat-value1'), '');
  266. });
  267. goog.events.listen(flatSelect3, goog.ui.Component.EventType.ACTION,
  268. function(e) {
  269. var select = e.target;
  270. flatSelect2.setEnabled(select.getValue());
  271. });
  272. goog.events.listen(goog.dom.getElement('flat-add'),
  273. goog.events.EventType.CLICK,
  274. function(e) {
  275. var good = prompt('What\'s another good Arnold movie...?');
  276. if (flatSelect1.getItemCount() == 3) {
  277. flatSelect1.addItem(new goog.ui.Separator());
  278. }
  279. flatSelect1.addItem(new goog.ui.MenuItem(good));
  280. });
  281. goog.events.listen(goog.dom.getElement('flat-hide'),
  282. goog.events.EventType.CLICK,
  283. function(e) {
  284. flatSelect1.setVisible(!flatSelect1.isVisible());
  285. });
  286. goog.events.listen(goog.dom.getElement('flat-worst'),
  287. goog.events.EventType.CLICK,
  288. function(e) {
  289. flatSelect2.setValue('Jingle All the Way');
  290. });
  291. // Decorate an element with a Select control using FlatMenuButtonRenderer.
  292. var flatSelect4 = new goog.ui.Select(null, null,
  293. goog.ui.FlatMenuButtonRenderer.getInstance());
  294. flatSelect4.decorate(goog.dom.getElement('flat-ferrell'));
  295. goog.events.listen(flatSelect1, EVENTS, logEvent);
  296. goog.events.listen(flatSelect2, EVENTS, logEvent);
  297. goog.events.listen(flatSelect3, EVENTS, logEvent);
  298. goog.events.listen(flatSelect4, EVENTS, logEvent);
  299. goog.dom.setTextContent(goog.dom.getElement('perf'),
  300. (goog.now() - timer) + 'ms');
  301. </script>
  302. </body>
  303. </html>