forked from Dogfalo/materialize
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathrange.ts
216 lines (193 loc) · 7.04 KB
/
range.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Component, BaseOptions, InitElements, MElement } from './component';
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface RangeOptions extends BaseOptions {}
const _defaults: RangeOptions = {};
// TODO: !!!!!
export class Range extends Component<RangeOptions> {
declare el: HTMLInputElement;
private _mousedown: boolean;
value: HTMLElement;
thumb: HTMLElement;
constructor(el: HTMLInputElement, options: Partial<RangeOptions>) {
super(el, options, Range);
this.el['M_Range'] = this;
this.options = {
...Range.defaults,
...options
};
this._mousedown = false;
this._setupThumb();
this._setupEventHandlers();
}
static get defaults(): RangeOptions {
return _defaults;
}
/**
* Initializes instance of Range.
* @param el HTML element.
* @param options Component options.
*/
static init(el: HTMLInputElement, options?: Partial<RangeOptions>): Range;
/**
* Initializes instances of Range.
* @param els HTML elements.
* @param options Component options.
*/
static init(
els: InitElements<HTMLInputElement | MElement>,
options?: Partial<RangeOptions>
): Range[];
/**
* Initializes instances of Range.
* @param els HTML elements.
* @param options Component options.
*/
static init(
els: HTMLInputElement | InitElements<HTMLInputElement | MElement>,
options: Partial<RangeOptions> = {}
): Range | Range[] {
return super.init(els, options, Range);
}
static getInstance(el: HTMLInputElement): Range {
return el['M_Range'];
}
destroy() {
this._removeEventHandlers();
this._removeThumb();
this.el['M_Range'] = undefined;
}
_setupEventHandlers() {
this.el.addEventListener('change', this._handleRangeChange);
this.el.addEventListener('mousedown', this._handleRangeMousedownTouchstart);
this.el.addEventListener('touchstart', this._handleRangeMousedownTouchstart);
this.el.addEventListener('input', this._handleRangeInputMousemoveTouchmove);
this.el.addEventListener('mousemove', this._handleRangeInputMousemoveTouchmove);
this.el.addEventListener('touchmove', this._handleRangeInputMousemoveTouchmove);
this.el.addEventListener('mouseup', this._handleRangeMouseupTouchend);
this.el.addEventListener('touchend', this._handleRangeMouseupTouchend);
this.el.addEventListener('blur', this._handleRangeBlurMouseoutTouchleave);
this.el.addEventListener('mouseout', this._handleRangeBlurMouseoutTouchleave);
this.el.addEventListener('touchleave', this._handleRangeBlurMouseoutTouchleave);
}
_removeEventHandlers() {
this.el.removeEventListener('change', this._handleRangeChange);
this.el.removeEventListener('mousedown', this._handleRangeMousedownTouchstart);
this.el.removeEventListener('touchstart', this._handleRangeMousedownTouchstart);
this.el.removeEventListener('input', this._handleRangeInputMousemoveTouchmove);
this.el.removeEventListener('mousemove', this._handleRangeInputMousemoveTouchmove);
this.el.removeEventListener('touchmove', this._handleRangeInputMousemoveTouchmove);
this.el.removeEventListener('mouseup', this._handleRangeMouseupTouchend);
this.el.removeEventListener('touchend', this._handleRangeMouseupTouchend);
this.el.removeEventListener('blur', this._handleRangeBlurMouseoutTouchleave);
this.el.removeEventListener('mouseout', this._handleRangeBlurMouseoutTouchleave);
this.el.removeEventListener('touchleave', this._handleRangeBlurMouseoutTouchleave);
}
_handleRangeChange = () => {
this.value.innerHTML = this.el.value;
if (!this.thumb.classList.contains('active')) {
this._showRangeBubble();
}
const offsetLeft = this._calcRangeOffset();
this.thumb.classList.add('active');
this.thumb.style.left = offsetLeft + 'px';
};
_handleRangeMousedownTouchstart = (e: MouseEvent | TouchEvent) => {
// Set indicator value
this.value.innerHTML = this.el.value;
this._mousedown = true;
this.el.classList.add('active');
if (!this.thumb.classList.contains('active')) {
this._showRangeBubble();
}
if (e.type !== 'input') {
const offsetLeft = this._calcRangeOffset();
this.thumb.classList.add('active');
this.thumb.style.left = offsetLeft + 'px';
}
};
_handleRangeInputMousemoveTouchmove = () => {
if (this._mousedown) {
if (!this.thumb.classList.contains('active')) {
this._showRangeBubble();
}
const offsetLeft = this._calcRangeOffset();
this.thumb.classList.add('active');
this.thumb.style.left = offsetLeft + 'px';
this.value.innerHTML = this.el.value;
}
};
_handleRangeMouseupTouchend = () => {
this._mousedown = false;
this.el.classList.remove('active');
};
_handleRangeBlurMouseoutTouchleave = () => {
if (!this._mousedown) {
const paddingLeft = parseInt(getComputedStyle(this.el).paddingLeft);
const marginLeftText = 7 + paddingLeft + 'px';
if (this.thumb.classList.contains('active')) {
const duration = 100;
// from
this.thumb.style.transition = 'none';
setTimeout(() => {
this.thumb.style.transition = `
height ${duration}ms ease,
width ${duration}ms ease,
top ${duration}ms ease,
margin ${duration}ms ease
`;
// to
this.thumb.style.height = '0';
this.thumb.style.width = '0';
this.thumb.style.top = '0';
this.thumb.style.marginLeft = marginLeftText;
}, 1);
}
this.thumb.classList.remove('active');
}
};
_setupThumb() {
this.thumb = document.createElement('span');
this.value = document.createElement('span');
this.thumb.classList.add('thumb');
this.value.classList.add('value');
this.thumb.append(this.value);
this.el.after(this.thumb);
}
_removeThumb() {
this.thumb.remove();
}
_showRangeBubble() {
const paddingLeft = parseInt(getComputedStyle(this.thumb.parentElement).paddingLeft);
const marginLeftText = -7 + paddingLeft + 'px'; // TODO: fix magic number?
const duration = 300;
// easeOutQuint
this.thumb.style.transition = `
height ${duration}ms ease,
width ${duration}ms ease,
top ${duration}ms ease,
margin ${duration}ms ease
`;
// to
this.thumb.style.height = '30px';
this.thumb.style.width = '30px';
this.thumb.style.top = '-30px';
this.thumb.style.marginLeft = marginLeftText;
}
_calcRangeOffset(): number {
const width = this.el.getBoundingClientRect().width - 15;
const max = parseFloat(this.el.getAttribute('max')) || 100; // Range default max
const min = parseFloat(this.el.getAttribute('min')) || 0; // Range default min
const percent = (parseFloat(this.el.value) - min) / (max - min);
return percent * width;
}
/**
* Initializes every range input in the current document.
*/
static Init() {
if (typeof document !== 'undefined')
Range.init(
document?.querySelectorAll('input[type=range]') as NodeListOf<HTMLInputElement>,
{}
);
}
}