button.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.Button and goog.ui.ToggleButton</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.Button');
  20. goog.require('goog.ui.ButtonRenderer');
  21. goog.require('goog.ui.ButtonSide');
  22. goog.require('goog.ui.CustomButton');
  23. goog.require('goog.ui.CustomButtonRenderer');
  24. goog.require('goog.ui.FlatButtonRenderer');
  25. goog.require('goog.ui.LinkButtonRenderer');
  26. goog.require('goog.ui.ToggleButton');
  27. goog.require('goog.ui.decorate');
  28. goog.require('goog.dom.TagName');
  29. </script>
  30. <link rel="stylesheet" href="css/demo.css">
  31. <link rel="stylesheet" href="../css/button.css">
  32. <link rel="stylesheet" href="../css/custombutton.css">
  33. <link rel="stylesheet" href="../css/flatbutton.css">
  34. <link rel="stylesheet" href="../css/linkbutton.css">
  35. <style>
  36. /* Base class for all icon elements. */
  37. .icon {
  38. height: 16px;
  39. width: 16px;
  40. margin: 0 1px;
  41. background-image: url(../images/toolbar_icons.gif);
  42. background-repeat: no-repeat;
  43. vertical-align: middle;
  44. }
  45. /* "Highlight" icon. */
  46. .highlight-icon{
  47. background-position: -64px;
  48. }
  49. /* "Insert Image" icon. */
  50. .insert-image-icon {
  51. background-position: -80px;
  52. }
  53. /* Custom style for the "default" button. */
  54. .default-button {
  55. font-weight: bold;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <h1>goog.ui.Button</h1>
  61. <fieldset>
  62. <legend>
  63. The first <strong>Button</strong> was created programmatically,
  64. the second by decorating an <strong>&lt;input&gt;</strong> element:&nbsp;
  65. </legend>
  66. <div id="b1"></div>
  67. <label>
  68. Enable button:<input type="checkbox" id="b1_enable" checked/>
  69. </label>
  70. <br/>
  71. <br/>
  72. <input type="button" id="b2" title="Tooltip extracted from title"
  73. value="My Button" disabled="true"/><br/>
  74. <label>Enable button:<input type="checkbox" id="b2_enable"/></label>
  75. <br/>
  76. </fieldset>
  77. <br/>
  78. <h2>goog.ui.FlatButtonRenderer</h2>
  79. <fieldset>
  80. <legend>
  81. Buttons made with <strong>&lt;div&gt;</strong>'s instead of
  82. <strong>&lt;input&gt;</strong>'s or <strong>&lt;button&gt;</strong>'s
  83. The first rendered, the second decorated:&nbsp;
  84. </legend>
  85. <div id="fb1"></div>
  86. <label>
  87. Enable button:<input type="checkbox" id="fb1_enable" checked/>
  88. </label>
  89. <br/>
  90. <br/>
  91. <div id="fb2" class="goog-flat-button goog-flat-button-disabled"
  92. title="Tooltip extracted from title">My Flat Button</div><br/>
  93. <label>Enable button:<input type="checkbox" id="fb2_enable"/></label>
  94. <br/>
  95. <br/>
  96. <div id="fb3" class="goog-flat-button goog-flat-button-collapse-right">
  97. Combined
  98. </div><div id="fb4" class="goog-flat-button goog-flat-button-collapse-left">
  99. Buttons
  100. </div>
  101. <br/>
  102. </fieldset>
  103. <br/>
  104. <h2>goog.ui.LinkButtonRenderer</h2>
  105. <fieldset>
  106. <legend>
  107. Like FlatButtonRenderer, except the style makes the button appear to be a
  108. link.
  109. </legend>
  110. <div id="lb"></div>
  111. <label>
  112. Enable button:<input type="checkbox" id="lb_enable" checked/>
  113. </label>
  114. </fieldset>
  115. <h2>goog.ui.CustomButton &amp goog.ui.ToggleButton</h2>
  116. <fieldset>
  117. <legend>
  118. These buttons were rendered using <strong>goog.ui.CustomButton</strong>:
  119. &nbsp;
  120. </legend>
  121. <br/>
  122. These buttons were created programmatically:<br/>
  123. <div id="cb1"></div>
  124. <br/>
  125. These buttons were created by decorating some DIVs, and they dispatch
  126. state transition events (watch the event log):<br/>
  127. <div id="cb2">
  128. <!-- On FF2, if you don't enclose the contents of the button in a SPAN,
  129. the bold element isn't rendered. Works on every other browser. -->
  130. <div id="foo" class="goog-custom-button" title="Title specified in HTML">
  131. <span>Decorated <b>Button</b>, yay!</span>
  132. </div>
  133. <div id="bar" class="goog-custom-button goog-custom-button-disabled"
  134. title="Initialized to DISABLED in HTML...">Decorated Disabled</div>
  135. <div id="fee" class="goog-custom-button">Another Button</div>
  136. <div id="btn1"
  137. class="goog-custom-button goog-custom-button-collapse-right">
  138. Archive
  139. </div><div id="btn2" class="goog-custom-button goog-custom-button-collapse-right goog-custom-button-collapse-left">
  140. Delete
  141. </div><div id="btn3"
  142. class="goog-custom-button goog-custom-button-collapse-left">
  143. Report Spam
  144. </div>
  145. </div>
  146. <br/>
  147. Use these <strong>ToggleButton</strong>s to hide/show and enable/disable
  148. the middle button:<br/>
  149. <div id="toggleEnable" class="goog-toggle-button"
  150. title="Click here to enable/disable the button above">Enable</div>
  151. &nbsp;
  152. <div id="hideShow"
  153. class="goog-toggle-button goog-custom-button-checked"
  154. title="Click here to hide/show the button above">Show</div>
  155. <br/><br/>
  156. Combined toggle buttons:<br/>
  157. <div id="btn4" class="goog-toggle-button goog-custom-button-collapse-right">
  158. Bold
  159. </div><div id="btn5" class="goog-toggle-button goog-custom-button-collapse-right goog-custom-button-collapse-left">
  160. Italics
  161. </div><div id="btn6" class="goog-toggle-button goog-custom-button-collapse-left goog-custom-button-checked">
  162. Underlined
  163. </div>
  164. <br/>
  165. <br/>
  166. These buttons have icons, and the second one has an extra CSS class:<br/>
  167. <div id="iconbuttons"></div>
  168. <br/>
  169. <span class="hint">
  170. The button with the <span style="font-weight:bold;color:orange;">orange
  171. outline</span> has keyboard focus. Hit Enter to activate focused buttons.
  172. </span>
  173. </fieldset>
  174. <br/>
  175. <div id="perf"></div>
  176. <!-- Event log. -->
  177. <fieldset class="goog-debug-panel">
  178. <legend>Event Log</legend>
  179. <div id="log"></div>
  180. </fieldset>
  181. <script type="text/javascript">
  182. var timer = goog.now();
  183. // Set up a logger.
  184. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  185. var logger = goog.log.getLogger('demo');
  186. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  187. logconsole.setCapturing(true);
  188. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  189. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  190. function logEvent(e) {
  191. goog.log.info(logger, '"' + e.target.getCaption() + '" dispatched: ' + e.type);
  192. }
  193. // Create the first button programmatically.
  194. var b1 = new goog.ui.Button('Hello!');
  195. b1.render(goog.dom.getElement('b1'));
  196. b1.setTooltip('I changed the tooltip using setTooltip() ' +
  197. 'after the button was rendered.');
  198. goog.events.listen(b1, EVENTS, logEvent);
  199. goog.events.listen(goog.dom.getElement('b1_enable'),
  200. goog.events.EventType.CLICK,
  201. function(e) {
  202. b1.setEnabled(e.target.checked);
  203. });
  204. goog.events.listen(b1, goog.ui.Component.EventType.ACTION,
  205. function(e) {
  206. var newCaption = window.prompt('Enter new caption for button:');
  207. b1.setCaption(newCaption || 'Empty');
  208. });
  209. // Create the second button by decorating an element.
  210. var b2 = new goog.ui.Button();
  211. b2.decorate(goog.dom.getElement('b2'));
  212. goog.events.listen(b2, EVENTS, logEvent);
  213. goog.events.listen(goog.dom.getElement('b2_enable'),
  214. goog.events.EventType.CLICK,
  215. function(e) {
  216. b2.setEnabled(e.target.checked);
  217. });
  218. goog.events.listen(b2, goog.ui.Component.EventType.ACTION,
  219. function(e) {
  220. alert('The value of the button is: ' + b2.getValue());
  221. });
  222. // Create flat buttons that use divs instead of button or input elements.
  223. // Render 1st flat button.
  224. var fb1 = new goog.ui.Button('Hello!',
  225. goog.ui.FlatButtonRenderer.getInstance());
  226. fb1.render(goog.dom.getElement('fb1'));
  227. fb1.setTooltip('I changed the tooltip using setTooltip() ' +
  228. 'after the button was rendered.');
  229. goog.events.listen(fb1, EVENTS, logEvent);
  230. goog.events.listen(goog.dom.getElement('fb1_enable'),
  231. goog.events.EventType.CLICK,
  232. function(e) {
  233. fb1.setEnabled(e.target.checked);
  234. });
  235. goog.events.listen(fb1, goog.ui.Component.EventType.ACTION,
  236. function(e) {
  237. var newCaption = window.prompt('Enter new caption for button:');
  238. fb1.setCaption(newCaption || 'Empty');
  239. });
  240. // Decorate 2nd flat button.
  241. var fb2 = goog.ui.decorate(goog.dom.getElement('fb2'));
  242. goog.events.listen(fb2, EVENTS, logEvent);
  243. goog.events.listen(goog.dom.getElement('fb2_enable'),
  244. goog.events.EventType.CLICK,
  245. function(e) {
  246. fb2.setEnabled(e.target.checked);
  247. });
  248. goog.events.listen(fb2, goog.ui.Component.EventType.ACTION,
  249. function(e) {
  250. alert('The caption of the button is: ' + fb2.getCaption());
  251. });
  252. // Decorate 3rd and 4th flat buttons.
  253. goog.ui.decorate(goog.dom.getElement('fb3'));
  254. goog.ui.decorate(goog.dom.getElement('fb4'));
  255. // Create buttons that look like links.
  256. var lb = new goog.ui.Button('Hello!',
  257. goog.ui.LinkButtonRenderer.getInstance());
  258. lb.render(goog.dom.getElement('lb'));
  259. lb.setTooltip('I changed the tooltip using setTooltip() ' +
  260. 'after the button was rendered.');
  261. goog.events.listen(lb, EVENTS, logEvent);
  262. goog.events.listen(goog.dom.getElement('lb_enable'),
  263. goog.events.EventType.CLICK,
  264. function(e) {
  265. lb.setEnabled(e.target.checked);
  266. });
  267. goog.events.listen(lb, goog.ui.Component.EventType.ACTION,
  268. function(e) {
  269. var newCaption = window.prompt('Enter new caption for button:');
  270. lb.setCaption(newCaption || 'Empty');
  271. });
  272. // Create some custom buttons.
  273. var disabledButton, leftButton, centerButton, rightButton;
  274. var customButtons = [
  275. new goog.ui.CustomButton('Button'),
  276. new goog.ui.CustomButton('Another Button'),
  277. disabledButton = new goog.ui.CustomButton('Disabled Button'),
  278. new goog.ui.CustomButton('Yet Another Button'),
  279. leftButton = new goog.ui.CustomButton('Left'),
  280. centerButton = new goog.ui.CustomButton('Center'),
  281. rightButton = new goog.ui.CustomButton('Right'),
  282. ];
  283. disabledButton.setEnabled(false);
  284. leftButton.setCollapsed(goog.ui.ButtonSide.END);
  285. centerButton.setCollapsed(goog.ui.ButtonSide.BOTH);
  286. rightButton.setCollapsed(goog.ui.ButtonSide.START);
  287. goog.array.forEach(customButtons, function(b) {
  288. b.render(goog.dom.getElement('cb1'));
  289. goog.events.listen(b, goog.ui.Component.EventType.ACTION,
  290. function(e) {
  291. var newCaption = window.prompt('Enter new caption for button:');
  292. b.setCaption(newCaption || 'Empty');
  293. });
  294. goog.events.listen(b, EVENTS, logEvent);
  295. });
  296. // Decorate some custom buttons.
  297. var cb2 = [];
  298. var decoratedButtons = goog.array.map(
  299. ['foo', 'bar', 'fee', 'btn1', 'btn2', 'btn3'],
  300. goog.dom.getElement);
  301. goog.array.forEach(decoratedButtons, function(element) {
  302. // Since the elements to be decorated each have the correct "marker" CSS
  303. // class ("goog-custom-button"), we can use the renderer registry to get
  304. // the appropriate control instance to decorate them.
  305. var button = goog.ui.decorate(element);
  306. button.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  307. cb2.push(button);
  308. goog.events.listen(button, EVENTS, logEvent);
  309. });
  310. // Decorate toggle buttons.
  311. var toggleEnableElem = goog.dom.getElement('toggleEnable');
  312. var toggleEnable = goog.ui.decorate(toggleEnableElem);
  313. toggleEnable.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  314. goog.events.listen(toggleEnable, EVENTS, logEvent);
  315. goog.events.listen(toggleEnable, goog.ui.Component.EventType.ACTION,
  316. function(e) {
  317. cb2[1].setEnabled(e.target.isChecked());
  318. });
  319. var hideShowElem = goog.dom.getElement('hideShow');
  320. var hideShow = new goog.ui.decorate(hideShowElem);
  321. hideShow.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  322. goog.events.listen(hideShow, EVENTS, logEvent);
  323. goog.events.listen(hideShow, goog.ui.Component.EventType.ACTION,
  324. function(e) {
  325. cb2[1].setVisible(e.target.isChecked());
  326. });
  327. // Decorate combined toggle buttons.
  328. var cb3 = [];
  329. var combinedButtons = goog.array.map(['btn4', 'btn5', 'btn6'],
  330. goog.dom.getElement);
  331. goog.array.forEach(combinedButtons, function(element) {
  332. var button = goog.ui.decorate(element);
  333. button.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  334. cb3.push(button);
  335. goog.events.listen(button, EVENTS, logEvent);
  336. });
  337. // Use a DIV with a background image as the icon, and a SPAN as the caption.
  338. var iconbutton1 = new goog.ui.ToggleButton([
  339. goog.dom.createDom(goog.dom.TagName.DIV, 'icon insert-image-icon goog-inline-block'),
  340. goog.dom.createDom(goog.dom.TagName.SPAN, {'style': 'vertical-align:middle'},
  341. 'Insert Image')
  342. ]);
  343. iconbutton1.render(goog.dom.getElement('iconbuttons'));
  344. goog.events.listen(iconbutton1, EVENTS, logEvent);
  345. // Add a custom style, too.
  346. var iconbutton2 = new goog.ui.ToggleButton([
  347. goog.dom.createDom(goog.dom.TagName.DIV, 'icon highlight-icon goog-inline-block'),
  348. goog.dom.createDom(goog.dom.TagName.SPAN, {'style': 'vertical-align:middle'},
  349. 'Highlight Text')
  350. ]);
  351. iconbutton2.addClassName('default-button');
  352. iconbutton2.render(goog.dom.getElement('iconbuttons'));
  353. goog.events.listen(iconbutton2, EVENTS, logEvent);
  354. goog.dom.setTextContent(goog.dom.getElement('perf'),
  355. (goog.now() - timer) + 'ms');
  356. </script>
  357. </body>
  358. </html>