1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import onsElements from '../ons/elements.js';
|
19 | import util from '../ons/util.js';
|
20 | import autoStyle from '../ons/autostyle.js';
|
21 | import ModifierUtil from '../ons/internal/modifier-util.js';
|
22 | import BaseCheckboxElement from './base/base-checkbox.js';
|
23 | import contentReady from '../ons/content-ready.js';
|
24 | import GestureDetector from '../ons/gesture-detector.js';
|
25 |
|
26 | const scheme = {
|
27 | '': 'switch--*',
|
28 | '.switch__input': 'switch--*__input',
|
29 | '.switch__handle': 'switch--*__handle',
|
30 | '.switch__toggle': 'switch--*__toggle'
|
31 | };
|
32 |
|
33 | const locations = {
|
34 | ios: [1, 21],
|
35 | material: [0, 16]
|
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 | export default class SwitchElement extends BaseCheckboxElement {
|
61 |
|
62 | constructor() {
|
63 | super();
|
64 |
|
65 | contentReady(this, () => {
|
66 | this.attributeChangedCallback('modifier', null, this.getAttribute('modifier'));
|
67 | });
|
68 |
|
69 | this._onChange = this._onChange.bind(this);
|
70 | this._onRelease = this._onRelease.bind(this);
|
71 | this._lastTimeStamp = 0;
|
72 | }
|
73 |
|
74 | get _scheme() {
|
75 | return scheme;
|
76 | }
|
77 |
|
78 | get _defaultClassName() {
|
79 | return 'switch';
|
80 | }
|
81 |
|
82 | get _template() {
|
83 | return `
|
84 | <input type="${this.type}" class="${this._defaultClassName}__input">
|
85 | <div class="${this._defaultClassName}__toggle">
|
86 | <div class="${this._defaultClassName}__handle">
|
87 | <div class="${this._defaultClassName}__touch"></div>
|
88 | </div>
|
89 | </div>
|
90 | `;
|
91 | }
|
92 |
|
93 | get type() {
|
94 | return 'checkbox';
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 | _getPosition(e) {
|
100 | const l = this._locations;
|
101 | return Math.min(l[1], Math.max(l[0], this._startX + e.gesture.deltaX));
|
102 | }
|
103 |
|
104 | _emitChangeEvent() {
|
105 | util.triggerElementEvent(this, 'change', {
|
106 | value: this.checked,
|
107 | switch: this,
|
108 | isInteractive: true
|
109 | });
|
110 | }
|
111 |
|
112 | _onChange(event) {
|
113 | if (event && event.stopPropagation) {
|
114 | event.stopPropagation();
|
115 | }
|
116 |
|
117 | this._emitChangeEvent();
|
118 | }
|
119 |
|
120 | _onClick(ev) {
|
121 | if (ev.target.classList.contains(`${this.defaultElementClass}__touch`)
|
122 | || (ev.timeStamp - this._lastTimeStamp < 50)
|
123 | ) {
|
124 | ev.preventDefault();
|
125 | }
|
126 | this._lastTimeStamp = ev.timeStamp;
|
127 | }
|
128 |
|
129 | _onHold(e) {
|
130 | if (!this.disabled) {
|
131 | ModifierUtil.addModifier(this, 'active');
|
132 | document.addEventListener('release', this._onRelease);
|
133 | }
|
134 | }
|
135 |
|
136 | _onDragStart(e) {
|
137 | if (this.disabled || ['left', 'right'].indexOf(e.gesture.direction) === -1) {
|
138 | ModifierUtil.removeModifier(this, 'active');
|
139 | return;
|
140 | }
|
141 |
|
142 | e.consumed = true;
|
143 |
|
144 | ModifierUtil.addModifier(this, 'active');
|
145 | this._startX = this._locations[this.checked ? 1 : 0];
|
146 |
|
147 | this.addEventListener('drag', this._onDrag);
|
148 | document.addEventListener('release', this._onRelease);
|
149 | }
|
150 |
|
151 | _onDrag(e) {
|
152 | e.stopPropagation();
|
153 | this._handle.style.left = this._getPosition(e) + 'px';
|
154 | }
|
155 |
|
156 | _onRelease(e) {
|
157 | const l = this._locations;
|
158 | const position = this._getPosition(e);
|
159 | const previousValue = this.checked;
|
160 |
|
161 | this.checked = position >= (l[0] + l[1]) / 2;
|
162 |
|
163 | if (this.checked !== previousValue) {
|
164 | this._emitChangeEvent();
|
165 | }
|
166 |
|
167 | this.removeEventListener('drag', this._onDrag);
|
168 | document.removeEventListener('release', this._onRelease);
|
169 |
|
170 | this._handle.style.left = '';
|
171 | ModifierUtil.removeModifier(this, 'active');
|
172 | }
|
173 |
|
174 | click(ev = {}) {
|
175 | if (!this.disabled) {
|
176 | this.checked = !this.checked;
|
177 | this._emitChangeEvent();
|
178 | this._lastTimeStamp = ev.timeStamp || 0;
|
179 | }
|
180 | }
|
181 |
|
182 | get _handle() {
|
183 | return this.querySelector(`.${this._defaultClassName}__handle`);
|
184 | }
|
185 |
|
186 | get checkbox() {
|
187 | return this._input;
|
188 | }
|
189 |
|
190 | connectedCallback() {
|
191 | contentReady(this, () => {
|
192 | this._input.addEventListener('change', this._onChange);
|
193 | });
|
194 |
|
195 | this.addEventListener('dragstart', this._onDragStart);
|
196 | this.addEventListener('hold', this._onHold);
|
197 | this.addEventListener('tap', this.click);
|
198 | this.addEventListener('click', this._onClick);
|
199 | this._gestureDetector = new GestureDetector(this, { dragMinDistance: 1, holdTimeout: 251, passive: true });
|
200 | }
|
201 |
|
202 | disconnectedCallback() {
|
203 | contentReady(this, () => {
|
204 | this._input.removeEventListener('change', this._onChange);
|
205 | });
|
206 |
|
207 | this.removeEventListener('dragstart', this._onDragStart);
|
208 | this.removeEventListener('hold', this._onHold);
|
209 | this.removeEventListener('tap', this.click);
|
210 | this.removeEventListener('click', this._onClick);
|
211 | if (this._gestureDetector) {
|
212 | this._gestureDetector.dispose();
|
213 | }
|
214 | }
|
215 |
|
216 | static get observedAttributes() {
|
217 | return [...super.observedAttributes, 'modifier'];
|
218 | }
|
219 |
|
220 | attributeChangedCallback(name, last, current) {
|
221 | if (name === 'modifier') {
|
222 | const md = (current || '').indexOf('material') !== -1;
|
223 | this._locations = locations[md ? 'material' : 'ios'];
|
224 | }
|
225 |
|
226 | super.attributeChangedCallback(name, last, current);
|
227 | }
|
228 |
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | |
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | |
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | |
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 | |
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | |
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | |
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | |
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | }
|
327 |
|
328 | onsElements.Switch = SwitchElement;
|
329 | customElements.define('ons-switch', SwitchElement);
|