slider.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2006 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 A slider implementation that allows to select a value within a
  16. * range by dragging a thumb. The selected value is exposed through getValue().
  17. *
  18. * To decorate, the slider should be bound to an element with the class name
  19. * 'goog-slider' containing a child with the class name 'goog-slider-thumb',
  20. * whose position is set to relative.
  21. * Note that you won't be able to see these elements unless they are styled.
  22. *
  23. * Slider orientation is horizontal by default.
  24. * Use setOrientation(goog.ui.Slider.Orientation.VERTICAL) for a vertical
  25. * slider.
  26. *
  27. * Decorate Example:
  28. * <div id="slider" class="goog-slider">
  29. * <div class="goog-slider-thumb"></div>
  30. * </div>
  31. *
  32. * JavaScript code:
  33. * <code>
  34. * var slider = new goog.ui.Slider;
  35. * slider.decorate(document.getElementById('slider'));
  36. * </code>
  37. *
  38. * @author arv@google.com (Erik Arvidsson)
  39. * @see ../demos/slider.html
  40. */
  41. // Implementation note: We implement slider by inheriting from baseslider,
  42. // which allows to select sub-ranges within a range using two thumbs. All we do
  43. // is we co-locate the two thumbs into one.
  44. goog.provide('goog.ui.Slider');
  45. goog.provide('goog.ui.Slider.Orientation');
  46. goog.require('goog.a11y.aria');
  47. goog.require('goog.a11y.aria.Role');
  48. goog.require('goog.dom');
  49. goog.require('goog.dom.TagName');
  50. goog.require('goog.ui.SliderBase');
  51. /**
  52. * This creates a slider object.
  53. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  54. * @param {(function(number):?string)=} opt_labelFn An optional function mapping
  55. * slider values to a description of the value.
  56. * @constructor
  57. * @extends {goog.ui.SliderBase}
  58. */
  59. goog.ui.Slider = function(opt_domHelper, opt_labelFn) {
  60. goog.ui.SliderBase.call(this, opt_domHelper, opt_labelFn);
  61. this.rangeModel.setExtent(0);
  62. };
  63. goog.inherits(goog.ui.Slider, goog.ui.SliderBase);
  64. goog.tagUnsealableClass(goog.ui.Slider);
  65. /**
  66. * Expose Enum of superclass (representing the orientation of the slider) within
  67. * Slider namespace.
  68. *
  69. * @enum {string}
  70. */
  71. goog.ui.Slider.Orientation = goog.ui.SliderBase.Orientation;
  72. /**
  73. * The prefix we use for the CSS class names for the slider and its elements.
  74. * @type {string}
  75. */
  76. goog.ui.Slider.CSS_CLASS_PREFIX = goog.getCssName('goog-slider');
  77. /**
  78. * CSS class name for the single thumb element.
  79. * @type {string}
  80. */
  81. goog.ui.Slider.THUMB_CSS_CLASS =
  82. goog.getCssName(goog.ui.Slider.CSS_CLASS_PREFIX, 'thumb');
  83. /**
  84. * Returns CSS class applied to the slider element.
  85. * @param {goog.ui.SliderBase.Orientation} orient Orientation of the slider.
  86. * @return {string} The CSS class applied to the slider element.
  87. * @protected
  88. * @override
  89. */
  90. goog.ui.Slider.prototype.getCssClass = function(orient) {
  91. return orient == goog.ui.SliderBase.Orientation.VERTICAL ?
  92. goog.getCssName(goog.ui.Slider.CSS_CLASS_PREFIX, 'vertical') :
  93. goog.getCssName(goog.ui.Slider.CSS_CLASS_PREFIX, 'horizontal');
  94. };
  95. /** @override */
  96. goog.ui.Slider.prototype.createThumbs = function() {
  97. // find thumb
  98. var element = this.getElement();
  99. var thumb = goog.dom.getElementsByTagNameAndClass(
  100. null, goog.ui.Slider.THUMB_CSS_CLASS, element)[0];
  101. if (!thumb) {
  102. thumb = this.createThumb_();
  103. element.appendChild(thumb);
  104. }
  105. this.valueThumb = this.extentThumb = /** @type {!HTMLDivElement} */ (thumb);
  106. };
  107. /**
  108. * Creates the thumb element.
  109. * @return {!HTMLDivElement} The created thumb element.
  110. * @private
  111. */
  112. goog.ui.Slider.prototype.createThumb_ = function() {
  113. var thumb = this.getDomHelper().createDom(
  114. goog.dom.TagName.DIV, goog.ui.Slider.THUMB_CSS_CLASS);
  115. goog.a11y.aria.setRole(thumb, goog.a11y.aria.Role.BUTTON);
  116. return /** @type {!HTMLDivElement} */ (thumb);
  117. };