css3buttonrenderer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview An alternative imageless button renderer that uses CSS3 rather
  16. * than voodoo to render custom buttons with rounded corners and dimensionality
  17. * (via a subtle flat shadow on the bottom half of the button) without the use
  18. * of images.
  19. *
  20. * Based on the Custom Buttons 3.1 visual specification, see
  21. * http://go/custombuttons
  22. *
  23. * Tested and verified to work in Gecko 1.9.2+ and WebKit 528+.
  24. *
  25. * @author eae@google.com (Emil A Eklund)
  26. * @see ../demos/css3button.html
  27. */
  28. goog.provide('goog.ui.Css3ButtonRenderer');
  29. goog.require('goog.asserts');
  30. goog.require('goog.dom.TagName');
  31. goog.require('goog.dom.classlist');
  32. goog.require('goog.ui.Button');
  33. goog.require('goog.ui.ButtonRenderer');
  34. goog.require('goog.ui.Component');
  35. goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');
  36. goog.require('goog.ui.registry');
  37. /**
  38. * Custom renderer for {@link goog.ui.Button}s. Css3 buttons can contain
  39. * almost arbitrary HTML content, will flow like inline elements, but can be
  40. * styled like block-level elements.
  41. *
  42. * @constructor
  43. * @extends {goog.ui.ButtonRenderer}
  44. * @final
  45. */
  46. goog.ui.Css3ButtonRenderer = function() {
  47. goog.ui.ButtonRenderer.call(this);
  48. };
  49. goog.inherits(goog.ui.Css3ButtonRenderer, goog.ui.ButtonRenderer);
  50. goog.addSingletonGetter(goog.ui.Css3ButtonRenderer);
  51. /**
  52. * Default CSS class to be applied to the root element of components rendered
  53. * by this renderer.
  54. * @type {string}
  55. */
  56. goog.ui.Css3ButtonRenderer.CSS_CLASS = goog.getCssName('goog-css3-button');
  57. /** @override */
  58. goog.ui.Css3ButtonRenderer.prototype.getContentElement = function(element) {
  59. return /** @type {Element} */ (element);
  60. };
  61. /**
  62. * Returns the button's contents wrapped in the following DOM structure:
  63. *
  64. * <div class="goog-inline-block goog-css3-button">
  65. * Contents...
  66. * </div>
  67. *
  68. * Overrides {@link goog.ui.ButtonRenderer#createDom}.
  69. * @param {goog.ui.Control} control goog.ui.Button to render.
  70. * @return {!Element} Root element for the button.
  71. * @override
  72. */
  73. goog.ui.Css3ButtonRenderer.prototype.createDom = function(control) {
  74. var button = /** @type {goog.ui.Button} */ (control);
  75. var classNames = this.getClassNames(button);
  76. return button.getDomHelper().createDom(
  77. goog.dom.TagName.DIV, {
  78. 'class': goog.ui.INLINE_BLOCK_CLASSNAME + ' ' + classNames.join(' '),
  79. 'title': button.getTooltip() || ''
  80. },
  81. button.getContent());
  82. };
  83. /**
  84. * Returns true if this renderer can decorate the element. Overrides
  85. * {@link goog.ui.ButtonRenderer#canDecorate} by returning true if the
  86. * element is a DIV, false otherwise.
  87. * @param {Element} element Element to decorate.
  88. * @return {boolean} Whether the renderer can decorate the element.
  89. * @override
  90. */
  91. goog.ui.Css3ButtonRenderer.prototype.canDecorate = function(element) {
  92. return element.tagName == goog.dom.TagName.DIV;
  93. };
  94. /** @override */
  95. goog.ui.Css3ButtonRenderer.prototype.decorate = function(button, element) {
  96. goog.asserts.assert(element);
  97. goog.dom.classlist.addAll(
  98. element, [goog.ui.INLINE_BLOCK_CLASSNAME, this.getCssClass()]);
  99. return goog.ui.Css3ButtonRenderer.superClass_.decorate.call(
  100. this, button, element);
  101. };
  102. /**
  103. * Returns the CSS class to be applied to the root element of components
  104. * rendered using this renderer.
  105. * @return {string} Renderer-specific CSS class.
  106. * @override
  107. */
  108. goog.ui.Css3ButtonRenderer.prototype.getCssClass = function() {
  109. return goog.ui.Css3ButtonRenderer.CSS_CLASS;
  110. };
  111. // Register a decorator factory function for goog.ui.Css3ButtonRenderer.
  112. goog.ui.registry.setDecoratorByClassName(
  113. goog.ui.Css3ButtonRenderer.CSS_CLASS, function() {
  114. return new goog.ui.Button(null, goog.ui.Css3ButtonRenderer.getInstance());
  115. });
  116. // Register a decorator factory function for toggle buttons using the
  117. // goog.ui.Css3ButtonRenderer.
  118. goog.ui.registry.setDecoratorByClassName(
  119. goog.getCssName('goog-css3-toggle-button'), function() {
  120. var button =
  121. new goog.ui.Button(null, goog.ui.Css3ButtonRenderer.getInstance());
  122. button.setSupportedState(goog.ui.Component.State.CHECKED, true);
  123. return button;
  124. });