Debian Codegraph @

closure-library/closure/goog/ui/slider.js

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