imagelessbutton.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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>
  10. goog.ui.ImagelessButtonRenderer Demo
  11. </title>
  12. <script src="../base.js"></script>
  13. <script>
  14. goog.require('goog.array');
  15. goog.require('goog.debug.DivConsole');
  16. goog.require('goog.debug.LogManager');
  17. goog.require('goog.events');
  18. goog.require('goog.log');
  19. goog.require('goog.object');
  20. goog.require('goog.ui.CustomButton');
  21. goog.require('goog.ui.ImagelessButtonRenderer');
  22. goog.require('goog.ui.ToggleButton');
  23. goog.require('goog.ui.decorate');
  24. goog.require('goog.dom.TagName');
  25. </script>
  26. <link rel="stylesheet" href="css/demo.css">
  27. <link rel="stylesheet" href="../css/imagelessbutton.css">
  28. <style>
  29. /* Base class for all icon elements. */
  30. .icon {
  31. height: 16px;
  32. width: 16px;
  33. margin: 0 4px 0 0;
  34. background-image: url(../images/toolbar_icons.gif);
  35. background-repeat: no-repeat;
  36. vertical-align: middle;
  37. }
  38. /* "Highlight" icon. */
  39. .highlight-icon{
  40. background-position: -64px;
  41. }
  42. /* "Insert Image" icon. */
  43. .insert-image-icon {
  44. background-position: -80px;
  45. }
  46. /* Custom style for the "default" button. */
  47. .default-button {
  48. font-weight: bold;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <h1>goog.ui.ImagelessButtonRenderer</h1>
  54. <fieldset>
  55. <legend>
  56. These buttons were rendered using
  57. <strong>goog.ui.ImagelessButtonRenderer</strong>:
  58. </legend>
  59. <br/>
  60. These buttons were created programmatically:<br/>
  61. <div id="cb1"></div>
  62. <br/>
  63. These buttons were created by decorating some DIVs, and they dispatch
  64. state transition events (watch the event log):<br/>
  65. <div id="cb2">
  66. <!-- On FF2, if you don't enclose the contents of the button in a SPAN,
  67. the bold element isn't rendered. Works on every other browser. -->
  68. <div id="foo" class="goog-imageless-button"
  69. title="Title specified in HTML">
  70. <span>Decorated <b>Button</b>, yay!</span>
  71. </div>
  72. <div id="bar" class="goog-imageless-button goog-imageless-button-disabled"
  73. title="Initialized to DISABLED in HTML...">Decorated Disabled</div>
  74. <div id="fee" class="goog-imageless-button">Another Button</div>
  75. <div id="btn1"
  76. class="goog-imageless-button goog-imageless-button-collapse-right">
  77. Archive
  78. </div><div id="btn2"
  79. class="goog-imageless-button goog-imageless-button-collapse-right goog-imageless-button-collapse-left">
  80. Delete
  81. </div><div id="btn3"
  82. class="goog-imageless-button goog-imageless-button-collapse-left">
  83. Report Spam
  84. </div>
  85. </div>
  86. <br/>
  87. Use these <strong>ToggleButton</strong>s to hide/show and enable/disable
  88. the middle button:<br/>
  89. <div id="toggleEnable" class="goog-imageless-toggle-button"
  90. title="Click here to enable/disable the button above">Enable</div>
  91. <div id="hideShow"
  92. class="goog-imageless-toggle-button goog-imageless-button-checked"
  93. title="Click here to hide/show the button above">Show</div>
  94. <br/><br/>
  95. Combined toggle buttons<br/>
  96. <div id="btn4" class="goog-imageless-toggle-button goog-imageless-button-collapse-right">
  97. Bold
  98. </div><div id="btn5" class="goog-imageless-toggle-button goog-imageless-button-collapse-right goog-imageless-button-collapse-left">
  99. Italics
  100. </div><div id="btn6" class="goog-imageless-toggle-button goog-imageless-button-collapse-left goog-imageless-button-checked">
  101. Underlined
  102. </div>
  103. <br/><br/>
  104. These buttons have icons, and the second one has an extra CSS class:<br/>
  105. <div id="iconbuttons"></div>
  106. <br/>
  107. </fieldset>
  108. <br/>
  109. <div id="perf"></div>
  110. <!-- Event log. -->
  111. <fieldset class="goog-debug-panel">
  112. <legend>Event Log</legend>
  113. <div id="log"></div>
  114. </fieldset>
  115. <script type="text/javascript">
  116. var timer = goog.now();
  117. // Set up a logger.
  118. goog.debug.LogManager.getRoot().setLevel(goog.log.Level.ALL);
  119. var logger = goog.log.getLogger('demo');
  120. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  121. logconsole.setCapturing(true);
  122. var EVENTS = goog.object.getValues(goog.ui.Component.EventType);
  123. goog.log.fine(logger, 'Listening for: ' + EVENTS.join(', ') + '.');
  124. function logEvent(e) {
  125. goog.log.info(logger, '"' + e.target.getCaption() + '" dispatched: ' + e.type);
  126. }
  127. // Create some buttons using CustomButton with the
  128. // ImagelessButtonRenderer renderer.
  129. var disabledButton;
  130. var customButtons = [
  131. new goog.ui.CustomButton('Button',
  132. goog.ui.ImagelessButtonRenderer.getInstance()),
  133. new goog.ui.CustomButton('Another Button',
  134. goog.ui.ImagelessButtonRenderer.getInstance()),
  135. disabledButton = new goog.ui.CustomButton('Disabled Button',
  136. goog.ui.ImagelessButtonRenderer.getInstance()),
  137. new goog.ui.CustomButton('Yet Another Button',
  138. goog.ui.ImagelessButtonRenderer.getInstance())
  139. ];
  140. disabledButton.setEnabled(false);
  141. goog.array.forEach(customButtons, function(b) {
  142. b.render(goog.dom.getElement('cb1'));
  143. goog.events.listen(b, goog.ui.Component.EventType.ACTION,
  144. function(e) {
  145. var newCaption = window.prompt('Enter new caption for button:');
  146. b.setCaption(newCaption || 'Empty');
  147. });
  148. goog.events.listen(b, EVENTS, logEvent);
  149. });
  150. // Decorate some buttons.
  151. var cb2 = [];
  152. var decoratedButtons = goog.array.map([
  153. 'foo', 'bar', 'fee', 'btn1', 'btn2', 'btn3', 'btn4', 'btn5', 'btn6'
  154. ], goog.dom.getElement);
  155. goog.array.forEach(decoratedButtons, function(element) {
  156. // Since the elements to be decorated each have the correct "marker" CSS
  157. // class ("goog-imageless-button"), we can use the renderer
  158. // registry to get the appropriate control instance to decorate them.
  159. var button = goog.ui.decorate(element);
  160. button.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  161. cb2.push(button);
  162. goog.events.listen(button, EVENTS, logEvent);
  163. });
  164. // Decorate toggle buttons.
  165. var toggleEnableElem = goog.dom.getElement('toggleEnable');
  166. var toggleEnable = goog.ui.decorate(toggleEnableElem);
  167. toggleEnable.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  168. goog.events.listen(toggleEnable, EVENTS, logEvent);
  169. goog.events.listen(toggleEnable, goog.ui.Component.EventType.ACTION,
  170. function(e) {
  171. cb2[1].setEnabled(e.target.isChecked());
  172. });
  173. var hideShowElem = goog.dom.getElement('hideShow');
  174. var hideShow = new goog.ui.decorate(hideShowElem);
  175. hideShow.setDispatchTransitionEvents(goog.ui.Component.State.ALL, true);
  176. goog.events.listen(hideShow, EVENTS, logEvent);
  177. goog.events.listen(hideShow, goog.ui.Component.EventType.ACTION,
  178. function(e) {
  179. cb2[1].setVisible(e.target.isChecked());
  180. });
  181. // Use a DIV with a background image as the icon, and a SPAN as the caption.
  182. var iconbutton1 = new goog.ui.ToggleButton([
  183. goog.dom.createDom(goog.dom.TagName.DIV, 'icon insert-image-icon goog-inline-block'),
  184. 'Insert Image'
  185. ], goog.ui.ImagelessButtonRenderer.getInstance());
  186. iconbutton1.render(goog.dom.getElement('iconbuttons'));
  187. goog.events.listen(iconbutton1, EVENTS, logEvent);
  188. // Add a custom style, too.
  189. var iconbutton2 = new goog.ui.ToggleButton([
  190. goog.dom.createDom(goog.dom.TagName.DIV, 'icon highlight-icon goog-inline-block'),
  191. 'Highlight Text'
  192. ], goog.ui.ImagelessButtonRenderer.getInstance());
  193. iconbutton2.addClassName('default-button');
  194. iconbutton2.render(goog.dom.getElement('iconbuttons'));
  195. goog.events.listen(iconbutton2, EVENTS, logEvent);
  196. goog.dom.setTextContent(goog.dom.getElement('perf'),
  197. (goog.now() - timer) + 'ms');
  198. </script>
  199. </body>
  200. </html>