1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | import { __assign, __extends } from "tslib";
|
24 | import { MDCComponent } from '@material/base/component';
|
25 | import { applyPassive } from '@material/dom/events';
|
26 | import { matches } from '@material/dom/ponyfill';
|
27 | import { MDCRipple } from '@material/ripple/component';
|
28 | import { MDCRippleFoundation } from '@material/ripple/foundation';
|
29 | import { cssClasses, events } from './constants';
|
30 | import { MDCSliderFoundation } from './foundation';
|
31 | import { Thumb, TickMark } from './types';
|
32 |
|
33 | var MDCSlider = (function (_super) {
|
34 | __extends(MDCSlider, _super);
|
35 | function MDCSlider() {
|
36 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
37 | _this.skipInitialUIUpdate = false;
|
38 |
|
39 |
|
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 |
|
50 |
|
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 | shouldHideFocusStylesForPointerEvents: function () { return false; },
|
86 | getThumbKnobWidth: function (thumb) {
|
87 | return _this.getThumbEl(thumb)
|
88 | .querySelector("." + cssClasses.THUMB_KNOB)
|
89 | .getBoundingClientRect()
|
90 | .width;
|
91 | },
|
92 | getThumbBoundingClientRect: function (thumb) {
|
93 | return _this.getThumbEl(thumb).getBoundingClientRect();
|
94 | },
|
95 | getBoundingClientRect: function () { return _this.root.getBoundingClientRect(); },
|
96 | getValueIndicatorContainerWidth: function (thumb) {
|
97 | return _this.getThumbEl(thumb)
|
98 | .querySelector("." + cssClasses.VALUE_INDICATOR_CONTAINER)
|
99 | .getBoundingClientRect()
|
100 | .width;
|
101 | },
|
102 | isRTL: function () { return getComputedStyle(_this.root).direction === 'rtl'; },
|
103 | setThumbStyleProperty: function (propertyName, value, thumb) {
|
104 | _this.getThumbEl(thumb).style.setProperty(propertyName, value);
|
105 | },
|
106 | removeThumbStyleProperty: function (propertyName, thumb) {
|
107 | _this.getThumbEl(thumb).style.removeProperty(propertyName);
|
108 | },
|
109 | setTrackActiveStyleProperty: function (propertyName, value) {
|
110 | _this.trackActive.style.setProperty(propertyName, value);
|
111 | },
|
112 | removeTrackActiveStyleProperty: function (propertyName) {
|
113 | _this.trackActive.style.removeProperty(propertyName);
|
114 | },
|
115 | setValueIndicatorText: function (value, thumb) {
|
116 | var valueIndicatorEl = _this.getThumbEl(thumb).querySelector("." + cssClasses.VALUE_INDICATOR_TEXT);
|
117 | valueIndicatorEl.textContent = String(value);
|
118 | },
|
119 | getValueToAriaValueTextFn: function () { return _this.valueToAriaValueTextFn; },
|
120 | updateTickMarks: function (tickMarks) {
|
121 | var tickMarksContainer = _this.root.querySelector("." + cssClasses.TICK_MARKS_CONTAINER);
|
122 | if (!tickMarksContainer) {
|
123 | tickMarksContainer = document.createElement('div');
|
124 | tickMarksContainer.classList.add(cssClasses.TICK_MARKS_CONTAINER);
|
125 | var track = _this.root.querySelector("." + cssClasses.TRACK);
|
126 | track.appendChild(tickMarksContainer);
|
127 | }
|
128 | if (tickMarks.length !== tickMarksContainer.children.length) {
|
129 | while (tickMarksContainer.firstChild) {
|
130 | tickMarksContainer.removeChild(tickMarksContainer.firstChild);
|
131 | }
|
132 | _this.addTickMarks(tickMarksContainer, tickMarks);
|
133 | }
|
134 | else {
|
135 | _this.updateTickMarks(tickMarksContainer, tickMarks);
|
136 | }
|
137 | },
|
138 | setPointerCapture: function (pointerId) {
|
139 | _this.root.setPointerCapture(pointerId);
|
140 | },
|
141 | emitChangeEvent: function (value, thumb) {
|
142 | _this.emit(events.CHANGE, { value: value, thumb: thumb });
|
143 | },
|
144 | emitInputEvent: function (value, thumb) {
|
145 | _this.emit(events.INPUT, { value: value, thumb: thumb });
|
146 | },
|
147 | emitDragStartEvent: function (_, thumb) {
|
148 |
|
149 |
|
150 | _this.getRipple(thumb).activate();
|
151 | },
|
152 | emitDragEndEvent: function (_, thumb) {
|
153 |
|
154 |
|
155 | _this.getRipple(thumb).deactivate();
|
156 | },
|
157 | registerEventHandler: function (evtType, handler) {
|
158 | _this.listen(evtType, handler);
|
159 | },
|
160 | deregisterEventHandler: function (evtType, handler) {
|
161 | _this.unlisten(evtType, handler);
|
162 | },
|
163 | registerThumbEventHandler: function (thumb, evtType, handler) {
|
164 | _this.getThumbEl(thumb).addEventListener(evtType, handler);
|
165 | },
|
166 | deregisterThumbEventHandler: function (thumb, evtType, handler) {
|
167 | _this.getThumbEl(thumb).removeEventListener(evtType, handler);
|
168 | },
|
169 | registerInputEventHandler: function (thumb, evtType, handler) {
|
170 | _this.getInput(thumb).addEventListener(evtType, handler);
|
171 | },
|
172 | deregisterInputEventHandler: function (thumb, evtType, handler) {
|
173 | _this.getInput(thumb).removeEventListener(evtType, handler);
|
174 | },
|
175 | registerBodyEventHandler: function (evtType, handler) {
|
176 | document.body.addEventListener(evtType, handler);
|
177 | },
|
178 | deregisterBodyEventHandler: function (evtType, handler) {
|
179 | document.body.removeEventListener(evtType, handler);
|
180 | },
|
181 | registerWindowEventHandler: function (evtType, handler) {
|
182 | window.addEventListener(evtType, handler);
|
183 | },
|
184 | deregisterWindowEventHandler: function (evtType, handler) {
|
185 | window.removeEventListener(evtType, handler);
|
186 | },
|
187 |
|
188 | };
|
189 | return new MDCSliderFoundation(adapter);
|
190 | };
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | MDCSlider.prototype.initialize = function (_a) {
|
198 | var _b = _a === void 0 ? {} : _a, skipInitialUIUpdate = _b.skipInitialUIUpdate;
|
199 | this.inputs =
|
200 | [].slice.call(this.root.querySelectorAll("." + cssClasses.INPUT));
|
201 | this.thumbs =
|
202 | [].slice.call(this.root.querySelectorAll("." + cssClasses.THUMB));
|
203 | this.trackActive =
|
204 | this.root.querySelector("." + cssClasses.TRACK_ACTIVE);
|
205 | this.ripples = this.createRipples();
|
206 | if (skipInitialUIUpdate) {
|
207 | this.skipInitialUIUpdate = true;
|
208 | }
|
209 | };
|
210 | MDCSlider.prototype.initialSyncWithDOM = function () {
|
211 | this.foundation.layout({ skipUpdateUI: this.skipInitialUIUpdate });
|
212 | };
|
213 |
|
214 | MDCSlider.prototype.layout = function () {
|
215 | this.foundation.layout();
|
216 | };
|
217 | MDCSlider.prototype.getValueStart = function () {
|
218 | return this.foundation.getValueStart();
|
219 | };
|
220 | MDCSlider.prototype.setValueStart = function (valueStart) {
|
221 | this.foundation.setValueStart(valueStart);
|
222 | };
|
223 | MDCSlider.prototype.getValue = function () {
|
224 | return this.foundation.getValue();
|
225 | };
|
226 | MDCSlider.prototype.setValue = function (value) {
|
227 | this.foundation.setValue(value);
|
228 | };
|
229 |
|
230 | MDCSlider.prototype.getDisabled = function () {
|
231 | return this.foundation.getDisabled();
|
232 | };
|
233 |
|
234 | MDCSlider.prototype.setDisabled = function (disabled) {
|
235 | this.foundation.setDisabled(disabled);
|
236 | };
|
237 | |
238 |
|
239 |
|
240 |
|
241 | MDCSlider.prototype.setValueToAriaValueTextFn = function (mapFn) {
|
242 | this.valueToAriaValueTextFn = mapFn;
|
243 | };
|
244 | MDCSlider.prototype.getThumbEl = function (thumb) {
|
245 | return thumb === Thumb.END ? this.thumbs[this.thumbs.length - 1] :
|
246 | this.thumbs[0];
|
247 | };
|
248 | MDCSlider.prototype.getInput = function (thumb) {
|
249 | return thumb === Thumb.END ? this.inputs[this.inputs.length - 1] :
|
250 | this.inputs[0];
|
251 | };
|
252 | MDCSlider.prototype.getRipple = function (thumb) {
|
253 | return thumb === Thumb.END ? this.ripples[this.ripples.length - 1] :
|
254 | this.ripples[0];
|
255 | };
|
256 |
|
257 | MDCSlider.prototype.addTickMarks = function (tickMarkContainer, tickMarks) {
|
258 | var fragment = document.createDocumentFragment();
|
259 | for (var i = 0; i < tickMarks.length; i++) {
|
260 | var div = document.createElement('div');
|
261 | var tickMarkClass = tickMarks[i] === TickMark.ACTIVE ?
|
262 | cssClasses.TICK_MARK_ACTIVE :
|
263 | cssClasses.TICK_MARK_INACTIVE;
|
264 | div.classList.add(tickMarkClass);
|
265 | fragment.appendChild(div);
|
266 | }
|
267 | tickMarkContainer.appendChild(fragment);
|
268 | };
|
269 |
|
270 | MDCSlider.prototype.updateTickMarks = function (tickMarkContainer, tickMarks) {
|
271 | var tickMarkEls = Array.from(tickMarkContainer.children);
|
272 | for (var i = 0; i < tickMarkEls.length; i++) {
|
273 | if (tickMarks[i] === TickMark.ACTIVE) {
|
274 | tickMarkEls[i].classList.add(cssClasses.TICK_MARK_ACTIVE);
|
275 | tickMarkEls[i].classList.remove(cssClasses.TICK_MARK_INACTIVE);
|
276 | }
|
277 | else {
|
278 | tickMarkEls[i].classList.add(cssClasses.TICK_MARK_INACTIVE);
|
279 | tickMarkEls[i].classList.remove(cssClasses.TICK_MARK_ACTIVE);
|
280 | }
|
281 | }
|
282 | };
|
283 |
|
284 | MDCSlider.prototype.createRipples = function () {
|
285 | var ripples = [];
|
286 | var rippleSurfaces = [].slice.call(this.root.querySelectorAll("." + cssClasses.THUMB));
|
287 | var _loop_1 = function (i) {
|
288 | var rippleSurface = rippleSurfaces[i];
|
289 |
|
290 |
|
291 | var input = this_1.inputs[i];
|
292 | var adapter = __assign(__assign({}, MDCRipple.createAdapter(this_1)), { addClass: function (className) {
|
293 | rippleSurface.classList.add(className);
|
294 | }, computeBoundingRect: function () { return rippleSurface.getBoundingClientRect(); }, deregisterInteractionHandler: function (evtType, handler) {
|
295 | input.removeEventListener(evtType, handler);
|
296 | }, isSurfaceActive: function () { return matches(input, ':active'); }, isUnbounded: function () { return true; }, registerInteractionHandler: function (evtType, handler) {
|
297 | input.addEventListener(evtType, handler, applyPassive());
|
298 | }, removeClass: function (className) {
|
299 | rippleSurface.classList.remove(className);
|
300 | }, updateCssVariable: function (varName, value) {
|
301 | rippleSurface.style.setProperty(varName, value);
|
302 | } });
|
303 | var ripple = new MDCRipple(rippleSurface, new MDCRippleFoundation(adapter));
|
304 | ripple.unbounded = true;
|
305 | ripples.push(ripple);
|
306 | };
|
307 | var this_1 = this;
|
308 | for (var i = 0; i < rippleSurfaces.length; i++) {
|
309 | _loop_1(i);
|
310 | }
|
311 | return ripples;
|
312 | };
|
313 | return MDCSlider;
|
314 | }(MDCComponent));
|
315 | export { MDCSlider };
|
316 |
|
\ | No newline at end of file |