css3menubuttonrenderer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/css3menubutton.html
  27. */
  28. goog.provide('goog.ui.Css3MenuButtonRenderer');
  29. goog.require('goog.dom');
  30. goog.require('goog.dom.TagName');
  31. goog.require('goog.ui.INLINE_BLOCK_CLASSNAME');
  32. goog.require('goog.ui.MenuButton');
  33. goog.require('goog.ui.MenuButtonRenderer');
  34. goog.require('goog.ui.registry');
  35. /**
  36. * Custom renderer for {@link goog.ui.MenuButton}s. Css3 buttons can contain
  37. * almost arbitrary HTML content, will flow like inline elements, but can be
  38. * styled like block-level elements.
  39. *
  40. * @constructor
  41. * @extends {goog.ui.MenuButtonRenderer}
  42. * @final
  43. */
  44. goog.ui.Css3MenuButtonRenderer = function() {
  45. goog.ui.MenuButtonRenderer.call(this);
  46. };
  47. goog.inherits(goog.ui.Css3MenuButtonRenderer, goog.ui.MenuButtonRenderer);
  48. goog.addSingletonGetter(goog.ui.Css3MenuButtonRenderer);
  49. /**
  50. * Default CSS class to be applied to the root element of components rendered
  51. * by this renderer.
  52. * @type {string}
  53. */
  54. goog.ui.Css3MenuButtonRenderer.CSS_CLASS = goog.getCssName('goog-css3-button');
  55. /** @override */
  56. goog.ui.Css3MenuButtonRenderer.prototype.getContentElement = function(element) {
  57. if (element) {
  58. var captionElem = goog.dom.getElementsByTagNameAndClass(
  59. '*', goog.getCssName(this.getCssClass(), 'caption'), element)[0];
  60. return captionElem;
  61. }
  62. return null;
  63. };
  64. /**
  65. * Returns true if this renderer can decorate the element. Overrides
  66. * {@link goog.ui.MenuButtonRenderer#canDecorate} by returning true if the
  67. * element is a DIV, false otherwise.
  68. * @param {Element} element Element to decorate.
  69. * @return {boolean} Whether the renderer can decorate the element.
  70. * @override
  71. */
  72. goog.ui.Css3MenuButtonRenderer.prototype.canDecorate = function(element) {
  73. return element.tagName == goog.dom.TagName.DIV;
  74. };
  75. /**
  76. * Takes a text caption or existing DOM structure, and returns the content
  77. * wrapped in a pseudo-rounded-corner box. Creates the following DOM structure:
  78. *
  79. * <div class="goog-inline-block goog-css3-button goog-css3-menu-button">
  80. * <div class="goog-css3-button-caption">Contents...</div>
  81. * <div class="goog-css3-button-dropdown"></div>
  82. * </div>
  83. *
  84. * Used by both {@link #createDom} and {@link #decorate}. To be overridden
  85. * by subclasses.
  86. * @param {goog.ui.ControlContent} content Text caption or DOM structure to wrap
  87. * in a box.
  88. * @param {goog.dom.DomHelper} dom DOM helper, used for document interaction.
  89. * @return {!Element} Pseudo-rounded-corner box containing the content.
  90. * @override
  91. */
  92. goog.ui.Css3MenuButtonRenderer.prototype.createButton = function(content, dom) {
  93. var baseClass = this.getCssClass();
  94. var inlineBlock = goog.ui.INLINE_BLOCK_CLASSNAME + ' ';
  95. return dom.createDom(
  96. goog.dom.TagName.DIV, inlineBlock,
  97. dom.createDom(
  98. goog.dom.TagName.DIV,
  99. [
  100. goog.getCssName(baseClass, 'caption'),
  101. goog.getCssName('goog-inline-block')
  102. ],
  103. content),
  104. dom.createDom(goog.dom.TagName.DIV, [
  105. goog.getCssName(baseClass, 'dropdown'),
  106. goog.getCssName('goog-inline-block')
  107. ]));
  108. };
  109. /**
  110. * Returns the CSS class to be applied to the root element of components
  111. * rendered using this renderer.
  112. * @return {string} Renderer-specific CSS class.
  113. * @override
  114. */
  115. goog.ui.Css3MenuButtonRenderer.prototype.getCssClass = function() {
  116. return goog.ui.Css3MenuButtonRenderer.CSS_CLASS;
  117. };
  118. // Register a decorator factory function for goog.ui.Css3MenuButtonRenderer.
  119. // Since we're using goog-css3-button as the base class in order to get the
  120. // same styling as goog.ui.Css3ButtonRenderer, we need to be explicit about
  121. // giving goog-css3-menu-button here.
  122. goog.ui.registry.setDecoratorByClassName(
  123. goog.getCssName('goog-css3-menu-button'), function() {
  124. return new goog.ui.MenuButton(
  125. null, null, goog.ui.Css3MenuButtonRenderer.getInstance());
  126. });