UNPKG

15.1 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23import { __assign, __extends } from "tslib";
24import { MDCComponent } from '@material/base/component';
25import { applyPassive } from '@material/dom/events';
26import { matches } from '@material/dom/ponyfill';
27import { MDCRipple } from '@material/ripple/component';
28import { MDCRippleFoundation } from '@material/ripple/foundation';
29import { cssClasses, events } from './constants';
30import { MDCSliderFoundation } from './foundation';
31import { Thumb, TickMark } from './types';
32/** Vanilla JS implementation of slider component. */
33var MDCSlider = /** @class */ (function (_super) {
34 __extends(MDCSlider, _super);
35 function MDCSlider() {
36 var _this = _super !== null && _super.apply(this, arguments) || this;
37 _this.skipInitialUIUpdate = false;
38 // Function that maps a slider value to the value of the `aria-valuetext`
39 // attribute on the thumb element.
40 _this.valueToAriaValueTextFn = null;
41 return _this;
42 }
43 MDCSlider.attachTo = function (root, options) {
44 if (options === void 0) { options = {}; }
45 return new MDCSlider(root, undefined, options);
46 };
47 MDCSlider.prototype.getDefaultFoundation = function () {
48 var _this = this;
49 // tslint:disable:object-literal-sort-keys Methods should be in the same
50 // order as the adapter interface.
51 var adapter = {
52 hasClass: function (className) { return _this.root.classList.contains(className); },
53 addClass: function (className) {
54 _this.root.classList.add(className);
55 },
56 removeClass: function (className) {
57 _this.root.classList.remove(className);
58 },
59 addThumbClass: function (className, thumb) {
60 _this.getThumbEl(thumb).classList.add(className);
61 },
62 removeThumbClass: function (className, thumb) {
63 _this.getThumbEl(thumb).classList.remove(className);
64 },
65 getAttribute: function (attribute) { return _this.root.getAttribute(attribute); },
66 getInputValue: function (thumb) { return _this.getInput(thumb).value; },
67 setInputValue: function (value, thumb) {
68 _this.getInput(thumb).value = value;
69 },
70 getInputAttribute: function (attribute, thumb) {
71 return _this.getInput(thumb).getAttribute(attribute);
72 },
73 setInputAttribute: function (attribute, value, thumb) {
74 _this.getInput(thumb).setAttribute(attribute, value);
75 },
76 removeInputAttribute: function (attribute, thumb) {
77 _this.getInput(thumb).removeAttribute(attribute);
78 },
79 focusInput: function (thumb) {
80 _this.getInput(thumb).focus();
81 },
82 isInputFocused: function (thumb) {
83 return _this.getInput(thumb) === document.activeElement;
84 },
85 getThumbKnobWidth: function (thumb) {
86 return _this.getThumbEl(thumb)
87 .querySelector("." + cssClasses.THUMB_KNOB)
88 .getBoundingClientRect()
89 .width;
90 },
91 getThumbBoundingClientRect: function (thumb) {
92 return _this.getThumbEl(thumb).getBoundingClientRect();
93 },
94 getBoundingClientRect: function () { return _this.root.getBoundingClientRect(); },
95 isRTL: function () { return getComputedStyle(_this.root).direction === 'rtl'; },
96 setThumbStyleProperty: function (propertyName, value, thumb) {
97 _this.getThumbEl(thumb).style.setProperty(propertyName, value);
98 },
99 removeThumbStyleProperty: function (propertyName, thumb) {
100 _this.getThumbEl(thumb).style.removeProperty(propertyName);
101 },
102 setTrackActiveStyleProperty: function (propertyName, value) {
103 _this.trackActive.style.setProperty(propertyName, value);
104 },
105 removeTrackActiveStyleProperty: function (propertyName) {
106 _this.trackActive.style.removeProperty(propertyName);
107 },
108 setValueIndicatorText: function (value, thumb) {
109 var valueIndicatorEl = _this.getThumbEl(thumb).querySelector("." + cssClasses.VALUE_INDICATOR_TEXT);
110 valueIndicatorEl.textContent = String(value);
111 },
112 getValueToAriaValueTextFn: function () { return _this.valueToAriaValueTextFn; },
113 updateTickMarks: function (tickMarks) {
114 var tickMarksContainer = _this.root.querySelector("." + cssClasses.TICK_MARKS_CONTAINER);
115 if (!tickMarksContainer) {
116 tickMarksContainer = document.createElement('div');
117 tickMarksContainer.classList.add(cssClasses.TICK_MARKS_CONTAINER);
118 var track = _this.root.querySelector("." + cssClasses.TRACK);
119 track.appendChild(tickMarksContainer);
120 }
121 if (tickMarks.length !== tickMarksContainer.children.length) {
122 while (tickMarksContainer.firstChild) {
123 tickMarksContainer.removeChild(tickMarksContainer.firstChild);
124 }
125 _this.addTickMarks(tickMarksContainer, tickMarks);
126 }
127 else {
128 _this.updateTickMarks(tickMarksContainer, tickMarks);
129 }
130 },
131 setPointerCapture: function (pointerId) {
132 _this.root.setPointerCapture(pointerId);
133 },
134 emitChangeEvent: function (value, thumb) {
135 _this.emit(events.CHANGE, { value: value, thumb: thumb });
136 },
137 emitInputEvent: function (value, thumb) {
138 _this.emit(events.INPUT, { value: value, thumb: thumb });
139 },
140 emitDragStartEvent: function (_, thumb) {
141 // Emitting event is not yet implemented. See issue:
142 // https://github.com/material-components/material-components-web/issues/6448
143 _this.getRipple(thumb).activate();
144 },
145 emitDragEndEvent: function (_, thumb) {
146 // Emitting event is not yet implemented. See issue:
147 // https://github.com/material-components/material-components-web/issues/6448
148 _this.getRipple(thumb).deactivate();
149 },
150 registerEventHandler: function (evtType, handler) {
151 _this.listen(evtType, handler);
152 },
153 deregisterEventHandler: function (evtType, handler) {
154 _this.unlisten(evtType, handler);
155 },
156 registerThumbEventHandler: function (thumb, evtType, handler) {
157 _this.getThumbEl(thumb).addEventListener(evtType, handler);
158 },
159 deregisterThumbEventHandler: function (thumb, evtType, handler) {
160 _this.getThumbEl(thumb).removeEventListener(evtType, handler);
161 },
162 registerInputEventHandler: function (thumb, evtType, handler) {
163 _this.getInput(thumb).addEventListener(evtType, handler);
164 },
165 deregisterInputEventHandler: function (thumb, evtType, handler) {
166 _this.getInput(thumb).removeEventListener(evtType, handler);
167 },
168 registerBodyEventHandler: function (evtType, handler) {
169 document.body.addEventListener(evtType, handler);
170 },
171 deregisterBodyEventHandler: function (evtType, handler) {
172 document.body.removeEventListener(evtType, handler);
173 },
174 registerWindowEventHandler: function (evtType, handler) {
175 window.addEventListener(evtType, handler);
176 },
177 deregisterWindowEventHandler: function (evtType, handler) {
178 window.removeEventListener(evtType, handler);
179 },
180 // tslint:enable:object-literal-sort-keys
181 };
182 return new MDCSliderFoundation(adapter);
183 };
184 /**
185 * Initializes component, with the following options:
186 * - `skipInitialUIUpdate`: Whether to skip updating the UI when initially
187 * syncing with the DOM. This should be enabled when the slider position
188 * is set before component initialization.
189 */
190 MDCSlider.prototype.initialize = function (_a) {
191 var _b = _a === void 0 ? {} : _a, skipInitialUIUpdate = _b.skipInitialUIUpdate;
192 this.inputs =
193 [].slice.call(this.root.querySelectorAll("." + cssClasses.INPUT));
194 this.thumbs =
195 [].slice.call(this.root.querySelectorAll("." + cssClasses.THUMB));
196 this.trackActive =
197 this.root.querySelector("." + cssClasses.TRACK_ACTIVE);
198 this.ripples = this.createRipples();
199 if (skipInitialUIUpdate) {
200 this.skipInitialUIUpdate = true;
201 }
202 };
203 MDCSlider.prototype.initialSyncWithDOM = function () {
204 this.foundation.layout({ skipUpdateUI: this.skipInitialUIUpdate });
205 };
206 /** Redraws UI based on DOM (e.g. element dimensions, RTL). */
207 MDCSlider.prototype.layout = function () {
208 this.foundation.layout();
209 };
210 MDCSlider.prototype.getValueStart = function () {
211 return this.foundation.getValueStart();
212 };
213 MDCSlider.prototype.setValueStart = function (valueStart) {
214 this.foundation.setValueStart(valueStart);
215 };
216 MDCSlider.prototype.getValue = function () {
217 return this.foundation.getValue();
218 };
219 MDCSlider.prototype.setValue = function (value) {
220 this.foundation.setValue(value);
221 };
222 /** @return Slider disabled state. */
223 MDCSlider.prototype.getDisabled = function () {
224 return this.foundation.getDisabled();
225 };
226 /** Sets slider disabled state. */
227 MDCSlider.prototype.setDisabled = function (disabled) {
228 this.foundation.setDisabled(disabled);
229 };
230 /**
231 * Sets a function that maps the slider value to the value of the
232 * `aria-valuetext` attribute on the thumb element.
233 */
234 MDCSlider.prototype.setValueToAriaValueTextFn = function (mapFn) {
235 this.valueToAriaValueTextFn = mapFn;
236 };
237 MDCSlider.prototype.getThumbEl = function (thumb) {
238 return thumb === Thumb.END ? this.thumbs[this.thumbs.length - 1] :
239 this.thumbs[0];
240 };
241 MDCSlider.prototype.getInput = function (thumb) {
242 return thumb === Thumb.END ? this.inputs[this.inputs.length - 1] :
243 this.inputs[0];
244 };
245 MDCSlider.prototype.getRipple = function (thumb) {
246 return thumb === Thumb.END ? this.ripples[this.ripples.length - 1] :
247 this.ripples[0];
248 };
249 /** Adds tick mark elements to the given container. */
250 MDCSlider.prototype.addTickMarks = function (tickMarkContainer, tickMarks) {
251 var fragment = document.createDocumentFragment();
252 for (var i = 0; i < tickMarks.length; i++) {
253 var div = document.createElement('div');
254 var tickMarkClass = tickMarks[i] === TickMark.ACTIVE ?
255 cssClasses.TICK_MARK_ACTIVE :
256 cssClasses.TICK_MARK_INACTIVE;
257 div.classList.add(tickMarkClass);
258 fragment.appendChild(div);
259 }
260 tickMarkContainer.appendChild(fragment);
261 };
262 /** Updates tick mark elements' classes in the given container. */
263 MDCSlider.prototype.updateTickMarks = function (tickMarkContainer, tickMarks) {
264 var tickMarkEls = Array.from(tickMarkContainer.children);
265 for (var i = 0; i < tickMarkEls.length; i++) {
266 if (tickMarks[i] === TickMark.ACTIVE) {
267 tickMarkEls[i].classList.add(cssClasses.TICK_MARK_ACTIVE);
268 tickMarkEls[i].classList.remove(cssClasses.TICK_MARK_INACTIVE);
269 }
270 else {
271 tickMarkEls[i].classList.add(cssClasses.TICK_MARK_INACTIVE);
272 tickMarkEls[i].classList.remove(cssClasses.TICK_MARK_ACTIVE);
273 }
274 }
275 };
276 /** Initializes thumb ripples. */
277 MDCSlider.prototype.createRipples = function () {
278 var ripples = [];
279 var rippleSurfaces = [].slice.call(this.root.querySelectorAll("." + cssClasses.THUMB));
280 var _loop_1 = function (i) {
281 var rippleSurface = rippleSurfaces[i];
282 // Use the corresponding input as the focus source for the ripple (i.e.
283 // when the input is focused, the ripple is in the focused state).
284 var input = this_1.inputs[i];
285 var adapter = __assign(__assign({}, MDCRipple.createAdapter(this_1)), { addClass: function (className) {
286 rippleSurface.classList.add(className);
287 }, computeBoundingRect: function () { return rippleSurface.getBoundingClientRect(); }, deregisterInteractionHandler: function (evtType, handler) {
288 input.removeEventListener(evtType, handler);
289 }, isSurfaceActive: function () { return matches(input, ':active'); }, isUnbounded: function () { return true; }, registerInteractionHandler: function (evtType, handler) {
290 input.addEventListener(evtType, handler, applyPassive());
291 }, removeClass: function (className) {
292 rippleSurface.classList.remove(className);
293 }, updateCssVariable: function (varName, value) {
294 rippleSurface.style.setProperty(varName, value);
295 } });
296 var ripple = new MDCRipple(rippleSurface, new MDCRippleFoundation(adapter));
297 ripple.unbounded = true;
298 ripples.push(ripple);
299 };
300 var this_1 = this;
301 for (var i = 0; i < rippleSurfaces.length; i++) {
302 _loop_1(i);
303 }
304 return ripples;
305 };
306 return MDCSlider;
307}(MDCComponent));
308export { MDCSlider };
309//# sourceMappingURL=component.js.map
\No newline at end of file