tristatemenuitemrenderer.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.TriStateMenuItem}s.
  16. *
  17. * @author eae@google.com (Emil A Eklund)
  18. */
  19. goog.provide('goog.ui.TriStateMenuItemRenderer');
  20. goog.require('goog.asserts');
  21. goog.require('goog.dom.classlist');
  22. goog.require('goog.ui.MenuItemRenderer');
  23. /**
  24. * Default renderer for {@link goog.ui.TriStateMenuItemRenderer}s. Each item has
  25. * the following structure:
  26. *
  27. * <div class="goog-tristatemenuitem">
  28. * <div class="goog-tristatemenuitem-checkbox"></div>
  29. * <div>...(content)...</div>
  30. * </div>
  31. *
  32. * @constructor
  33. * @extends {goog.ui.MenuItemRenderer}
  34. * @final
  35. */
  36. goog.ui.TriStateMenuItemRenderer = function() {
  37. goog.ui.MenuItemRenderer.call(this);
  38. };
  39. goog.inherits(goog.ui.TriStateMenuItemRenderer, goog.ui.MenuItemRenderer);
  40. goog.addSingletonGetter(goog.ui.TriStateMenuItemRenderer);
  41. /**
  42. * CSS class name the renderer applies to menu item elements.
  43. * @type {string}
  44. */
  45. goog.ui.TriStateMenuItemRenderer.CSS_CLASS =
  46. goog.getCssName('goog-tristatemenuitem');
  47. /**
  48. * Overrides {@link goog.ui.ControlRenderer#decorate} by initializing the
  49. * menu item to checkable based on whether the element to be decorated has
  50. * extra styling indicating that it should be.
  51. * @param {goog.ui.Control} item goog.ui.TriStateMenuItem to decorate
  52. * the element.
  53. * @param {Element} element Element to decorate.
  54. * @return {!Element} Decorated element.
  55. * @override
  56. */
  57. goog.ui.TriStateMenuItemRenderer.prototype.decorate = function(item, element) {
  58. element = goog.ui.TriStateMenuItemRenderer.superClass_.decorate.call(
  59. this, item, element);
  60. this.setCheckable(item, element, true);
  61. goog.asserts.assert(element);
  62. if (goog.dom.classlist.contains(
  63. element, goog.getCssName(this.getCssClass(), 'fully-checked'))) {
  64. item.setCheckedState(/** @suppress {missingRequire} */
  65. goog.ui.TriStateMenuItem.State.FULLY_CHECKED);
  66. } else if (
  67. goog.dom.classlist.contains(
  68. element, goog.getCssName(this.getCssClass(), 'partially-checked'))) {
  69. /** @suppress {missingRequire} */
  70. item.setCheckedState(goog.ui.TriStateMenuItem.State.PARTIALLY_CHECKED);
  71. } else {
  72. /** @suppress {missingRequire} */
  73. item.setCheckedState(goog.ui.TriStateMenuItem.State.NOT_CHECKED);
  74. }
  75. return element;
  76. };
  77. /** @override */
  78. goog.ui.TriStateMenuItemRenderer.prototype.getCssClass = function() {
  79. return goog.ui.TriStateMenuItemRenderer.CSS_CLASS;
  80. };