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