Polymer elements added to third_party/polymer.
[chromium-blink-merge.git] / third_party / polymer / components / paper-slider / paper-slider.html
blobf2587bfb6d0a7e9450fef2ee0133fa3b6408cb4e
1 <!--
2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
8 -->
10 <!--
11 `paper-slider` allows user to select a value from a range of values by
12 moving the slider thumb. The interactive nature of the slider makes it a
13 great choice for settings that reflect intensity levels, such as volume,
14 brightness, or color saturation.
16 Example:
18 <paper-slider></paper-slider>
20 Use `min` and `max` to specify the slider range. Default is 0 to 100.
22 Example:
24 <paper-slider min="10" max="200" value="110"></paper-slider>
26 Styling slider:
28 To change the slider progress bar color:
30 paper-slider::shadow #sliderBar::shadow #activeProgress {
31 background-color: #0f9d58;
34 To change the slider knob color:
36 paper-slider::shadow #sliderKnobInner {
37 background-color: #0f9d58;
40 To change the slider pin color:
42 paper-slider::shadow #sliderKnobInner::before {
43 background-color: #0f9d58;
46 To change the slider pin's value:
48 paper-slider::shadow #sliderKnobInner::after {
49 color: #0f9d58
52 To change the slider secondary progress bar color:
54 paper-slider::shadow #sliderBar::shadow #secondaryProgress {
55 background-color: #0f9d58;
58 @group Paper Elements
59 @element paper-slider
60 @extends core-range
61 @homepage github.io
62 -->
64 <link rel="import" href="../paper-progress/paper-progress.html">
65 <link rel="import" href="../paper-input/paper-input.html">
67 <polymer-element name="paper-slider" extends="core-range" attributes="snaps pin disabled secondaryProgress editable immediateValue">
68 <template>
70 <link rel="stylesheet" href="paper-slider.css">
72 <div id="sliderContainer" on-keydown="{{keydown}}">
74 <paper-progress id="sliderBar" aria-hidden="true" min="{{min}}" max="{{max}}" value="{{immediateValue}}" secondaryProgress="{{secondaryProgress}}"
75 on-down="{{bardown}}" on-up="{{resetKnob}}"
76 on-trackstart="{{trackStart}}" on-trackx="{{trackx}}" on-trackend="{{trackEnd}}"></paper-progress>
78 <template if="{{snaps && !disabled}}">
79 <div class="slider-markers" horizontal layout>
80 <template repeat="{{markers}}">
81 <div flex class="slider-marker"></div>
82 </template>
83 </div>
84 </template>
86 <div id="sliderKnob" class="{{ {pin : pin, snaps : snaps} | tokenList }}"
87 on-down="{{expandKnob}}" on-up="{{resetKnob}}"
88 on-trackstart="{{trackStart}}" on-trackx="{{trackx}}" on-trackend="{{trackEnd}}"
89 on-transitionend="{{knobTransitionEnd}}"
90 role="slider" aria-valuenow="{{value}}" aria-valuemin="{{min}}" aria-valuemax="{{max}}"
91 aria-valuetext="{{value}}" tabindex="0"
92 center-justified center horizontal layout>
94 <div id="sliderKnobInner" value="{{immediateValue}}"></div>
96 </div>
98 </div>
100 <template if="{{editable}}">
101 <paper-input id="input" class="slider-input" value="{{immediateValue}}" validate="^[-+]?[0-9]*\.?[0-9]+$" disabled?="{{disabled}}" on-change="{{inputChange}}"></paper-input>
102 </template>
104 </template>
105 <script>
107 Polymer('paper-slider', {
110 * Fired when the slider's value changes.
112 * @event change
116 * Fired when the slider's value changes due to manual interaction.
118 * Changes to the slider's value due to changes in an underlying
119 * bound variable will not trigger this event.
121 * @event manual-change
125 * If true, the slider thumb snaps to tick marks evenly spaced based
126 * on the `step` property value.
128 * @attribute snaps
129 * @type boolean
130 * @default false
132 snaps: false,
135 * If true, a pin with numeric value label is shown when the slider thumb
136 * is pressed. Use for settings for which users need to know the exact
137 * value of the setting.
139 * @attribute pin
140 * @type boolean
141 * @default false
143 pin: false,
146 * If true, this slider is disabled. A disabled slider cannot be tapped
147 * or dragged to change the slider value.
149 * @attribute disabled
150 * @type boolean
151 * @default false
153 disabled: false,
156 * The number that represents the current secondary progress.
158 * @attribute secondaryProgress
159 * @type number
160 * @default 0
162 secondaryProgress: 0,
165 * If true, an input is shown and user can use it to set the slider value.
167 * @attribute editable
168 * @type boolean
169 * @default false
171 editable: false,
174 * The immediate value of the slider. This value is updated while the user
175 * is dragging the slider.
177 * @attribute immediateValue
178 * @type number
179 * @default 0
182 observe: {
183 'min max step snaps': 'update'
186 ready: function() {
187 this.update();
190 update: function() {
191 this.positionKnob(this.calcRatio(this.value));
192 this.updateMarkers();
195 valueChanged: function() {
196 this.update();
197 this.fire('change');
200 expandKnob: function() {
201 this.$.sliderKnob.classList.add('expand');
204 resetKnob: function() {
205 this.expandJob && this.expandJob.stop();
206 this.$.sliderKnob.classList.remove('expand');
209 positionKnob: function(ratio) {
210 this._ratio = ratio;
211 this.immediateValue = this.calcStep(this.calcKnobPosition()) || 0;
212 if (this.snaps) {
213 this._ratio = this.calcRatio(this.immediateValue);
215 this.$.sliderKnob.style.left = this._ratio * 100 + '%';
218 immediateValueChanged: function() {
219 this.$.sliderKnob.classList.toggle('ring', this.immediateValue <= this.min);
222 inputChange: function() {
223 this.value = this.$.input.value;
224 this.fire('manual-change');
227 calcKnobPosition: function() {
228 return (this.max - this.min) * this._ratio + this.min;
231 measureWidth: function() {
232 this._w = this.$.sliderBar.offsetWidth;
235 trackStart: function(e) {
236 this.measureWidth();
237 this._x = this._ratio * this._w;
238 this._startx = this._x || 0;
239 this._minx = - this._startx;
240 this._maxx = this._w - this._startx;
241 this.$.sliderKnob.classList.add('dragging');
242 e.preventTap();
245 trackx: function(e) {
246 var x = Math.min(this._maxx, Math.max(this._minx, e.dx));
247 this._x = this._startx + x;
248 this._ratio = this._x / this._w;
249 this.immediateValue = this.calcStep(this.calcKnobPosition()) || 0;
250 var s = this.$.sliderKnob.style;
251 s.transform = s.webkitTransform = 'translate3d(' + (this.snaps ?
252 (this.calcRatio(this.immediateValue) * this._w) - this._startx : x) + 'px, 0, 0)';
255 trackEnd: function() {
256 var s = this.$.sliderKnob.style;
257 s.transform = s.webkitTransform = '';
258 this.$.sliderKnob.classList.remove('dragging');
259 this.resetKnob();
260 this.value = this.immediateValue;
261 this.fire('manual-change');
264 bardown: function(e) {
265 this.measureWidth();
266 this.$.sliderKnob.classList.add('transiting');
267 var rect = this.$.sliderBar.getBoundingClientRect();
268 this.positionKnob((e.x - rect.left) / this._w);
269 this.value = this.calcStep(this.calcKnobPosition());
270 this.expandJob = this.job(this.expandJob, this.expandKnob, 60);
271 this.fire('manual-change');
274 knobTransitionEnd: function() {
275 this.$.sliderKnob.classList.remove('transiting');
278 updateMarkers: function() {
279 this.markers = [], l = (this.max - this.min) / this.step;
280 for (var i = 0; i < l; i++) {
281 this.markers.push('');
285 increment: function() {
286 this.value = this.clampValue(this.value + this.step);
289 decrement: function() {
290 this.value = this.clampValue(this.value - this.step);
293 keydown: function(e) {
294 if (this.disabled) {
295 return;
297 var c = e.keyCode;
298 if (c === 37) {
299 this.decrement();
300 this.fire('manual-change');
301 } else if (c === 39) {
302 this.increment();
303 this.fire('manual-change');
309 </script>
310 </polymer-element>