1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | (function() {
|
19 | 'use strict';
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var MaterialSlider = function MaterialSlider(element) {
|
30 | this.element_ = element;
|
31 |
|
32 | this.isIE_ = window.navigator.msPointerEnabled;
|
33 |
|
34 | this.init();
|
35 | };
|
36 | window['MaterialSlider'] = MaterialSlider;
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | MaterialSlider.prototype.Constant_ = {
|
45 |
|
46 | };
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | MaterialSlider.prototype.CssClasses_ = {
|
57 | IE_CONTAINER: 'mdl-slider__ie-container',
|
58 | SLIDER_CONTAINER: 'mdl-slider__container',
|
59 | BACKGROUND_FLEX: 'mdl-slider__background-flex',
|
60 | BACKGROUND_LOWER: 'mdl-slider__background-lower',
|
61 | BACKGROUND_UPPER: 'mdl-slider__background-upper',
|
62 | IS_LOWEST_VALUE: 'is-lowest-value',
|
63 | IS_UPGRADED: 'is-upgraded'
|
64 | };
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | MaterialSlider.prototype.onInput_ = function(event) {
|
73 | this.updateValueStyles_();
|
74 | };
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | MaterialSlider.prototype.onChange_ = function(event) {
|
83 | this.updateValueStyles_();
|
84 | };
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | MaterialSlider.prototype.onMouseUp_ = function(event) {
|
93 | event.target.blur();
|
94 | };
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | MaterialSlider.prototype.onContainerMouseDown_ = function(event) {
|
107 |
|
108 |
|
109 | if (event.target !== this.element_.parentElement) {
|
110 | return;
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | event.preventDefault();
|
116 | var newEvent = new MouseEvent('mousedown', {
|
117 | target: event.target,
|
118 | buttons: event.buttons,
|
119 | clientX: event.clientX,
|
120 | clientY: this.element_.getBoundingClientRect().y
|
121 | });
|
122 | this.element_.dispatchEvent(newEvent);
|
123 | };
|
124 |
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 | MaterialSlider.prototype.updateValueStyles_ = function() {
|
131 |
|
132 | var fraction = (this.element_.value - this.element_.min) /
|
133 | (this.element_.max - this.element_.min);
|
134 |
|
135 | if (fraction === 0) {
|
136 | this.element_.classList.add(this.CssClasses_.IS_LOWEST_VALUE);
|
137 | } else {
|
138 | this.element_.classList.remove(this.CssClasses_.IS_LOWEST_VALUE);
|
139 | }
|
140 |
|
141 | if (!this.isIE_) {
|
142 | this.backgroundLower_.style.flex = fraction;
|
143 | this.backgroundLower_.style.webkitFlex = fraction;
|
144 | this.backgroundUpper_.style.flex = 1 - fraction;
|
145 | this.backgroundUpper_.style.webkitFlex = 1 - fraction;
|
146 | }
|
147 | };
|
148 |
|
149 |
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | MaterialSlider.prototype.disable = function() {
|
157 | this.element_.disabled = true;
|
158 | };
|
159 | MaterialSlider.prototype['disable'] = MaterialSlider.prototype.disable;
|
160 |
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 | MaterialSlider.prototype.enable = function() {
|
167 |
|
168 | this.element_.disabled = false;
|
169 | };
|
170 | MaterialSlider.prototype['enable'] = MaterialSlider.prototype.enable;
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | MaterialSlider.prototype.change = function(value) {
|
179 |
|
180 | if (typeof value !== 'undefined') {
|
181 | this.element_.value = value;
|
182 | }
|
183 | this.updateValueStyles_();
|
184 | };
|
185 | MaterialSlider.prototype['change'] = MaterialSlider.prototype.change;
|
186 |
|
187 | |
188 |
|
189 |
|
190 | MaterialSlider.prototype.init = function() {
|
191 |
|
192 | if (this.element_) {
|
193 | if (this.isIE_) {
|
194 |
|
195 |
|
196 |
|
197 | var containerIE = document.createElement('div');
|
198 | containerIE.classList.add(this.CssClasses_.IE_CONTAINER);
|
199 | this.element_.parentElement.insertBefore(containerIE, this.element_);
|
200 | this.element_.parentElement.removeChild(this.element_);
|
201 | containerIE.appendChild(this.element_);
|
202 | } else {
|
203 |
|
204 |
|
205 |
|
206 | var container = document.createElement('div');
|
207 | container.classList.add(this.CssClasses_.SLIDER_CONTAINER);
|
208 | this.element_.parentElement.insertBefore(container, this.element_);
|
209 | this.element_.parentElement.removeChild(this.element_);
|
210 | container.appendChild(this.element_);
|
211 | var backgroundFlex = document.createElement('div');
|
212 | backgroundFlex.classList.add(this.CssClasses_.BACKGROUND_FLEX);
|
213 | container.appendChild(backgroundFlex);
|
214 | this.backgroundLower_ = document.createElement('div');
|
215 | this.backgroundLower_.classList.add(this.CssClasses_.BACKGROUND_LOWER);
|
216 | backgroundFlex.appendChild(this.backgroundLower_);
|
217 | this.backgroundUpper_ = document.createElement('div');
|
218 | this.backgroundUpper_.classList.add(this.CssClasses_.BACKGROUND_UPPER);
|
219 | backgroundFlex.appendChild(this.backgroundUpper_);
|
220 | }
|
221 |
|
222 | this.boundInputHandler = this.onInput_.bind(this);
|
223 | this.boundChangeHandler = this.onChange_.bind(this);
|
224 | this.boundMouseUpHandler = this.onMouseUp_.bind(this);
|
225 | this.boundContainerMouseDownHandler = this.onContainerMouseDown_.bind(this);
|
226 | this.element_.addEventListener('input', this.boundInputHandler);
|
227 | this.element_.addEventListener('change', this.boundChangeHandler);
|
228 | this.element_.addEventListener('mouseup', this.boundMouseUpHandler);
|
229 | this.element_.parentElement.addEventListener('mousedown', this.boundContainerMouseDownHandler);
|
230 |
|
231 | this.updateValueStyles_();
|
232 | this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
|
233 | }
|
234 | };
|
235 |
|
236 |
|
237 |
|
238 | componentHandler.register({
|
239 | constructor: MaterialSlider,
|
240 | classAsString: 'MaterialSlider',
|
241 | cssClass: 'mdl-js-slider',
|
242 | widget: true
|
243 | });
|
244 | })();
|