twothumbslider.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2007 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 Twothumbslider is a slider that allows to select a subrange
  16. * within a range by dragging two thumbs. The selected sub-range is exposed
  17. * through getValue() and getExtent().
  18. *
  19. * To decorate, the twothumbslider should be bound to an element with the class
  20. * name 'goog-twothumbslider-[vertical / horizontal]' containing children with
  21. * the classname 'goog-twothumbslider-value-thumb' and
  22. * 'goog-twothumbslider-extent-thumb', respectively.
  23. *
  24. * Decorate Example:
  25. * <div id="twothumbslider" class="goog-twothumbslider-horizontal">
  26. * <div class="goog-twothumbslider-value-thumb">
  27. * <div class="goog-twothumbslider-extent-thumb">
  28. * </div>
  29. * <script>
  30. *
  31. * var slider = new goog.ui.TwoThumbSlider;
  32. * slider.decorate(document.getElementById('twothumbslider'));
  33. *
  34. * TODO(user): add a11y once we know what this element is
  35. *
  36. * @see ../demos/twothumbslider.html
  37. */
  38. goog.provide('goog.ui.TwoThumbSlider');
  39. goog.require('goog.a11y.aria');
  40. goog.require('goog.a11y.aria.Role');
  41. goog.require('goog.dom');
  42. goog.require('goog.dom.TagName');
  43. goog.require('goog.ui.SliderBase');
  44. /**
  45. * This creates a TwoThumbSlider object.
  46. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  47. * @constructor
  48. * @extends {goog.ui.SliderBase}
  49. */
  50. goog.ui.TwoThumbSlider = function(opt_domHelper) {
  51. goog.ui.SliderBase.call(this, opt_domHelper);
  52. this.rangeModel.setValue(this.getMinimum());
  53. this.rangeModel.setExtent(this.getMaximum() - this.getMinimum());
  54. };
  55. goog.inherits(goog.ui.TwoThumbSlider, goog.ui.SliderBase);
  56. goog.tagUnsealableClass(goog.ui.TwoThumbSlider);
  57. /**
  58. * The prefix we use for the CSS class names for the slider and its elements.
  59. * @type {string}
  60. */
  61. goog.ui.TwoThumbSlider.CSS_CLASS_PREFIX =
  62. goog.getCssName('goog-twothumbslider');
  63. /**
  64. * CSS class name for the value thumb element.
  65. * @type {string}
  66. */
  67. goog.ui.TwoThumbSlider.VALUE_THUMB_CSS_CLASS =
  68. goog.getCssName(goog.ui.TwoThumbSlider.CSS_CLASS_PREFIX, 'value-thumb');
  69. /**
  70. * CSS class name for the extent thumb element.
  71. * @type {string}
  72. */
  73. goog.ui.TwoThumbSlider.EXTENT_THUMB_CSS_CLASS =
  74. goog.getCssName(goog.ui.TwoThumbSlider.CSS_CLASS_PREFIX, 'extent-thumb');
  75. /**
  76. * CSS class name for the range highlight element.
  77. * @type {string}
  78. */
  79. goog.ui.TwoThumbSlider.RANGE_HIGHLIGHT_CSS_CLASS =
  80. goog.getCssName(goog.ui.TwoThumbSlider.CSS_CLASS_PREFIX, 'rangehighlight');
  81. /**
  82. * @param {goog.ui.SliderBase.Orientation} orient orientation of the slider.
  83. * @return {string} The CSS class applied to the twothumbslider element.
  84. * @protected
  85. * @override
  86. */
  87. goog.ui.TwoThumbSlider.prototype.getCssClass = function(orient) {
  88. return orient == goog.ui.SliderBase.Orientation.VERTICAL ?
  89. goog.getCssName(goog.ui.TwoThumbSlider.CSS_CLASS_PREFIX, 'vertical') :
  90. goog.getCssName(goog.ui.TwoThumbSlider.CSS_CLASS_PREFIX, 'horizontal');
  91. };
  92. /**
  93. * This creates a thumb element with the specified CSS class name.
  94. * @param {string} cs CSS class name of the thumb to be created.
  95. * @return {!HTMLDivElement} The created thumb element.
  96. * @private
  97. */
  98. goog.ui.TwoThumbSlider.prototype.createThumb_ = function(cs) {
  99. var thumb = this.getDomHelper().createDom(goog.dom.TagName.DIV, cs);
  100. goog.a11y.aria.setRole(thumb, goog.a11y.aria.Role.BUTTON);
  101. return /** @type {!HTMLDivElement} */ (thumb);
  102. };
  103. /**
  104. * Creates the thumb members for a twothumbslider. If the
  105. * element contains a child with a class name 'goog-twothumbslider-value-thumb'
  106. * (or 'goog-twothumbslider-extent-thumb', respectively), then that will be used
  107. * as the valueThumb (or as the extentThumb, respectively). If the element
  108. * contains a child with a class name 'goog-twothumbslider-rangehighlight',
  109. * then that will be used as the range highlight.
  110. * @override
  111. */
  112. goog.ui.TwoThumbSlider.prototype.createThumbs = function() {
  113. // find range highlight and thumbs
  114. var valueThumb = goog.dom.getElementsByTagNameAndClass(
  115. null, goog.ui.TwoThumbSlider.VALUE_THUMB_CSS_CLASS, this.getElement())[0];
  116. var extentThumb = goog.dom.getElementsByTagNameAndClass(
  117. null, goog.ui.TwoThumbSlider.EXTENT_THUMB_CSS_CLASS,
  118. this.getElement())[0];
  119. var rangeHighlight = goog.dom.getElementsByTagNameAndClass(
  120. null, goog.ui.TwoThumbSlider.RANGE_HIGHLIGHT_CSS_CLASS,
  121. this.getElement())[0];
  122. if (!valueThumb) {
  123. valueThumb =
  124. this.createThumb_(goog.ui.TwoThumbSlider.VALUE_THUMB_CSS_CLASS);
  125. this.getElement().appendChild(valueThumb);
  126. }
  127. if (!extentThumb) {
  128. extentThumb =
  129. this.createThumb_(goog.ui.TwoThumbSlider.EXTENT_THUMB_CSS_CLASS);
  130. this.getElement().appendChild(extentThumb);
  131. }
  132. if (!rangeHighlight) {
  133. rangeHighlight = this.getDomHelper().createDom(
  134. goog.dom.TagName.DIV, goog.ui.TwoThumbSlider.RANGE_HIGHLIGHT_CSS_CLASS);
  135. // Insert highlight before value thumb so that it renders under the thumbs.
  136. this.getDomHelper().insertSiblingBefore(rangeHighlight, valueThumb);
  137. }
  138. this.valueThumb = /** @type {!HTMLDivElement} */ (valueThumb);
  139. this.extentThumb = /** @type {!HTMLDivElement} */ (extentThumb);
  140. this.rangeHighlight = /** @type {!HTMLDivElement} */ (rangeHighlight);
  141. };