forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider.js
102 lines (84 loc) · 2.62 KB
/
slider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview A Slider control. Based on Chromium's MediaControls.Slider.
*/
/**
* Creates a slider control.
*
* @param {HTMLElement} container The containing div element.
* @param {number} value Initial value
* @param {number} min Minimum value
* @param {number} max Maximum value
* @param {?function(number)=} opt_onChange Value change handler
* @constructor
*/
function Slider(container, value, min, max, opt_onChange) {
this.container_ = container;
this.onChange_ = opt_onChange;
var containerDocument = this.container_.ownerDocument;
this.container_.classList.add('slider');
this.input_ = containerDocument.createElement('input');
this.input_.type = 'range';
this.input_.min = min;
this.input_.max = max;
this.input_.value = value;
this.container_.appendChild(this.input_);
this.input_.addEventListener(
'change', this.onInputChange_.bind(this));
this.input_.addEventListener(
'input', this.onInputChange_.bind(this));
this.bar_ = containerDocument.createElement('div');
this.bar_.className = 'bar';
this.container_.appendChild(this.bar_);
this.filled_ = containerDocument.createElement('div');
this.filled_.className = 'filled';
this.bar_.appendChild(this.filled_);
var leftCap = containerDocument.createElement('div');
leftCap.className = 'cap left';
this.bar_.appendChild(leftCap);
var rightCap = containerDocument.createElement('div');
rightCap.className = 'cap right';
this.bar_.appendChild(rightCap);
this.updateFilledWidth_();
};
/**
* @return {number} The value of the input control.
*/
Slider.prototype.getValue = function() {
return this.input_.value;
};
/**
* @param{number} value The value to set the input control to.
*/
Slider.prototype.setValue = function(value) {
this.input_.value = value;
this.updateFilledWidth_();
};
/**
* @return {HTMLInputElement} The underlying input control.
*/
Slider.prototype.getInput = function() {
return this.input_;
}
/**
* Updates the filled portion of the slider to reflect the slider's current
* value.
* @private
*/
Slider.prototype.updateFilledWidth_ = function() {
var proportion = (this.input_.value - this.input_.min) /
(this.input_.max - this.input_.min);
this.filled_.style.width = proportion * 100 + '%';
};
/**
* Called when the slider's value changes.
* @private
*/
Slider.prototype.onInputChange_ = function() {
this.updateFilledWidth_();
if (this.onChange_)
this.onChange_(this.input_.value);
};