-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
185 lines (149 loc) · 5.9 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@tmbr/utils</title>
</head>
<body>
<div id="traverse">
<header>
<h1>Hello</h1>
</header>
<main>
<p>Lorem <strong>ipsum</strong> dolor</p>
<ul>
<li>Minnesota</li>
<li>Colorado</li>
</ul>
</main>
</div>
<div id="on">
<button type="button" class="button"><span>a</span></button>
<button type="button" class="button"><span>b</span></button>
</div>
<div id="trap">
<button type="button">Menu</button>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
<input type="search" placeholder="Search" />
</nav>
</div>
<script type="module">
import { on } from './lib/on.js';
import { io } from './lib/io.js';
import { once } from './lib/once.js';
import { request } from './lib/request.js';
import { cookie } from './lib/cookie.js';
import { worker } from './lib/worker.js';
import { trap } from './lib/trap.js';
// 1. on | off : event with selector
// 2. on | off : event with selector and scope
// 3. on | off : event with element
// 4. on | off : event with array
// 5. on | off : multiple events with selector
// 6. on | off : multiple events with selector and scope
// 7. on | off : multiple events with element
// 8. on | off : multiple events with array
// 9. on | off : event object with selector
// 10. on | off : event object with selector and scope
// 11. on | off : event object with element
// 12. on | off : event object with array
const div = document.getElementById('on');
const buttons = [...div.querySelectorAll('button')];
const [ a, b ] = buttons;
let off;
// off = once('click ', '.button', e => console.log(e.target.matches('.button')));
// off = once('click ', '.button', e => console.log(e.currentTarget), div);
// off = once('click', a, e => console.log(e.type, e.target === a));
// off = once('click', buttons, e => console.log(buttons.includes(event.target)));
// off = on('click', '.button', e => console.log(e.target.matches('.button')));
// off = on('click', '.button', e => console.log(e.currentTarget), div);
// off = on('click', a, e => console.log(e.type, e.target === a));
// off = on('click', buttons, e => console.log(buttons.includes(event.target)));
// off = on('click mouseenter', '.button', e => console.log(e.target.matches('.button')));
// off = on('click mouseenter', '.button', e => console.log(e.currentTarget), div);
// off = on('click mouseenter', a, e => console.log(e.type, e.target === a));
// off = on('click mouseenter', buttons, e => console.log(e.type, e.target.matches('.button')));
// off = on(['click', 'mouseenter'], '.button', e => console.log(e.target.matches('.button')));
// off = on(['click', 'mouseenter'], '.button', e => console.log(e.currentTarget), div);
// off = on(['click', 'mouseenter'], a, e => console.log(e.type, e.target === a));
// off = on(['click', 'mouseenter'], buttons, e => console.log(e.type, e.target.matches('.button')));
// off = on(events, '.button', div);
// off = on(events, a);
// off = on(events, buttons);
off && setTimeout(() => (console.log('off'), off()), 5000);
// <div style="height:1000px;width:100px;background:red;margin:30px auto"></div>
// <div id="io" style="height:100px;width:100px;background:green;margin:30px auto"></div>
// <div style="height:1000px;width:100px;background:red;margin:30px auto"></div>
// const unobserve = io(document.getElementById('io'), {
// enter: () => console.log('enter'),
// leave: () => console.log('leave'),
// once: false,
// rootMargin: '30px'
// });
// setTimeout(unobserve, 5000);
// const controller = new AbortController();
// const signal = controller.signal;
// setTimeout(() => controller.abort(), 2000);
// request.get('https://reqres.in/api/users?page=2&v=123', {delay: 4}, {signal}).then(
// res => console.log(res),
// err => console.log(err)
// );
// request.headers['X-Message'] = 'Hello';
// request.get('https://jsonplaceholder.typicode.com/users', null).then(console.log);
// request('GET', 'https://jsonplaceholder.typicode.com/users').then(console.log);
// cookie('greeting', 'Hello');
// setTimeout(() => console.log(cookie('greeting')), 100);
// setTimeout(() => cookie('greeting', null), 200);
// setTimeout(() => console.log(cookie('greeting')), 300);
// cookie('example', 'Expire in 30 days', 30);
// cookie('example', 'Expire on this date', new Date('2024-04-15'));
// cookie('example', 'Custom cookie attributes', {SameSite: 'strict'});
// console.log('before worker');
// const instance = worker(/* js */`
// // comments work in worker code
// console.log('in worker');
// function calculate(n) {
// let value = 0;
// for (var i = 0; i < 1_000_000_000; i++) value += Math.pow(i, n);
// return n;
// }
// self.onmessage = function(e) {
// self.postMessage(calculate(e.data));
// };
// `);
// const instance = worker(function() {
// // comments work in worker code
// console.log('in worker');
// function calculate(n) {
// let value = 0;
// for (var i = 0; i < 1_000_000_000; i++) value += Math.pow(i, n);
// return value;
// }
// self.onmessage = function(e) {
// self.postMessage(calculate(e.data));
// };
// });
// console.log('after worker');
// instance.onmessage = e => console.log('got it!', e.data);
// instance.postMessage(1000);
/* const el = document.getElementById('trap');
const btn = el.querySelector('button');
const nav = el.querySelector('nav');
let untrap;
console.log(btn, nav);
document.addEventListener('keyup', event => {
event.key === 'Escape' && untrap?.();
});
btn.addEventListener('click', event => {
untrap = trap(nav, elements => {
elements.push(btn);
return elements;
});
}) */
</script>
</body>
</html>