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 | 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 |
|
142 |
|
143 | _this.getRipple(thumb).activate();
|
144 | },
|
145 | emitDragEndEvent: function (_, thumb) {
|
146 |
|
147 |
|
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 |
|
181 | };
|
182 | return new MDCSliderFoundation(adapter);
|
183 | };
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
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 |
|
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 |
|
223 | MDCSlider.prototype.getDisabled = function () {
|
224 | return this.foundation.getDisabled();
|
225 | };
|
226 |
|
227 | MDCSlider.prototype.setDisabled = function (disabled) {
|
228 | this.foundation.setDisabled(disabled);
|
229 | };
|
230 | |
231 |
|
232 |
|
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 |
|
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 |
|
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 |
|
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 |
|
283 |
|
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));
|
308 | export { MDCSlider };
|
309 |
|
\ | No newline at end of file |