menuseparatorrenderer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2008 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 Renderer for {@link goog.ui.MenuSeparator}s.
  16. *
  17. * @author attila@google.com (Attila Bodis)
  18. */
  19. goog.provide('goog.ui.MenuSeparatorRenderer');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.dom.classlist');
  23. goog.require('goog.ui.ControlRenderer');
  24. /**
  25. * Renderer for menu separators.
  26. * @constructor
  27. * @extends {goog.ui.ControlRenderer}
  28. */
  29. goog.ui.MenuSeparatorRenderer = function() {
  30. goog.ui.ControlRenderer.call(this);
  31. };
  32. goog.inherits(goog.ui.MenuSeparatorRenderer, goog.ui.ControlRenderer);
  33. goog.addSingletonGetter(goog.ui.MenuSeparatorRenderer);
  34. /**
  35. * Default CSS class to be applied to the root element of components rendered
  36. * by this renderer.
  37. * @type {string}
  38. */
  39. goog.ui.MenuSeparatorRenderer.CSS_CLASS = goog.getCssName('goog-menuseparator');
  40. /**
  41. * Returns an empty, styled menu separator DIV. Overrides {@link
  42. * goog.ui.ControlRenderer#createDom}.
  43. * @param {goog.ui.Control} separator goog.ui.Separator to render.
  44. * @return {!Element} Root element for the separator.
  45. * @override
  46. */
  47. goog.ui.MenuSeparatorRenderer.prototype.createDom = function(separator) {
  48. return separator.getDomHelper().createDom(
  49. goog.dom.TagName.DIV, this.getCssClass());
  50. };
  51. /**
  52. * Takes an existing element, and decorates it with the separator. Overrides
  53. * {@link goog.ui.ControlRenderer#decorate}.
  54. * @param {goog.ui.Control} separator goog.ui.MenuSeparator to decorate the
  55. * element.
  56. * @param {Element} element Element to decorate.
  57. * @return {!Element} Decorated element.
  58. * @override
  59. */
  60. goog.ui.MenuSeparatorRenderer.prototype.decorate = function(
  61. separator, element) {
  62. // Normally handled in the superclass. But we don't call the superclass.
  63. if (element.id) {
  64. separator.setId(element.id);
  65. }
  66. if (element.tagName == goog.dom.TagName.HR) {
  67. // Replace HR with separator.
  68. var hr = element;
  69. element = this.createDom(separator);
  70. goog.dom.insertSiblingBefore(element, hr);
  71. goog.dom.removeNode(hr);
  72. } else {
  73. goog.dom.classlist.add(element, this.getCssClass());
  74. }
  75. return element;
  76. };
  77. /**
  78. * Overrides {@link goog.ui.ControlRenderer#setContent} to do nothing, since
  79. * separators are empty.
  80. * @param {Element} separator The separator's root element.
  81. * @param {goog.ui.ControlContent} content Text caption or DOM structure to be
  82. * set as the separators's content (ignored).
  83. * @override
  84. */
  85. goog.ui.MenuSeparatorRenderer.prototype.setContent = function(
  86. separator, content) {
  87. // Do nothing. Separators are empty.
  88. };
  89. /**
  90. * Returns the CSS class to be applied to the root element of components
  91. * rendered using this renderer.
  92. * @return {string} Renderer-specific CSS class.
  93. * @override
  94. */
  95. goog.ui.MenuSeparatorRenderer.prototype.getCssClass = function() {
  96. return goog.ui.MenuSeparatorRenderer.CSS_CLASS;
  97. };