-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
200 lines (173 loc) Β· 5.05 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Listbox</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
/>
<style>
.list-group {
position: relative;
max-height: 293px;
visibility: hidden;
opacity: 0;
}
.list-group.is-active {
visibility: visible;
opacity: 1;
}
.list-group-item.is-active {
z-index: 2;
color: #fff;
background-color: #007bff;
border-color: #007bff;
}
.Alert {
margin: 1em;
position: fixed;
top: 0;
bottom: 0;
right: 0;
z-index: 10;
}
</style>
</head>
<body>
<div class="Alert js-alert"></div>
<div class="container">
<div class="my-5 row">
<div class="col-12">
<h1 class="m-0">
<a href="https://github.com/19h47/19h47-listbox">Listbox</a>
</h1>
</div>
</div>
<div class="my-5 row">
<div class="col-12">
<div id="monster" class="p-3 border rounded-lg js-list">
<h2 id="monster-label">Choose an monster <sup class="js-keys"></sup></h2>
<button
id="monster-button"
class="my-3 btn btn-outline-primary w-100 js-list-button"
aria-haspopup="listbox"
aria-labelledby="monster-label monster-button"
type="button"
>
<span class="js-list-label">Monsters</span>
</button>
<div class="overflow-hidden border rounded-lg" style="height: 293px">
<ul
class="overflow-auto list-group list-group-flush js-list-box"
tabindex="-1"
role="listbox"
aria-labelledby="monster-label"
></ul>
</div>
</div>
</div>
</div>
<div class="my-5 row">
<div class="col-12">
<div class="p-3 border rounded-lg js-list" id="spells">
<h2 id="monster-label">Choose an spells<sup class="js-keys"></sup></h2>
<p>(select on focus)</p>
<button
id="monster-button"
class="my-3 btn btn-outline-primary js-list-button w-100"
aria-haspopup="listbox"
aria-labelledby="monster-label monster-button"
type="button"
>
<span class="js-list-label">Spells</span>
</button>
<div class="overflow-hidden border rounded-lg">
<ul
class="overflow-auto list-group list-group-flush js-list-box"
tabindex="-1"
role="listbox"
aria-labelledby="monster-label"
aria-multiselectable="true"
></ul>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.rawgit.com/inexorabletash/polyfill/v0.1.42/polyfill.min.js"></script>
<script src="main.js"></script>
<script>
const lists = [...document.querySelectorAll('.js-list')];
const url = 'https://www.dnd5eapi.co/api/';
const getData = async url => {
let response = await fetch(url);
let data = await response.json();
return data;
};
const options = (index, name) => `
<li class="list-group-item list-group-item-action" id="${index}" role="option">
${name}
</li>
`;
const alert = content => {
const div = document.createElement('div');
div.className = 'toast fade show';
div.style = 'toast fade show';
div.innerHTML = `<div class="toast-body"><strong>${content}</strong></div>`;
div.setAttribute('role', 'alert');
div.setAttribute('aria-live', 'assertive');
div.setAttribute('aria-atomic', 'true');
return div;
};
const $alert = document.querySelector('.js-alert');
lists.forEach($list => {
const $listbox = $list.querySelector('[role="listbox"]');
const $button = $list.querySelector('.js-list-button');
const $keys = $list.querySelector('.js-keys');
const $search = $list.querySelector('.js-search');
const { id } = $list;
if ('monster' === id) {
const data = getData(`${url}monsters`);
data.then(monsters => {
monsters.results.forEach(monster => {
$listbox.innerHTML += options(monster.index, monster.name);
});
const box = new Listbox.Box($listbox, false);
box.init();
const button = new Listbox.Button($button, box);
button.init();
});
}
if ('spells' === id) {
const data = getData(`${url}spells`);
data.then(spells => {
spells.results.forEach(spell => {
$listbox.innerHTML += options(spell.index, spell.name);
});
const box = new Listbox.Box($listbox);
box.init();
const button = new Listbox.Button($button, box);
button.init();
});
}
$button.addEventListener('Button.change', ({ detail }) => {
const $el = alert(detail.item.innerHTML);
$alert.appendChild($el);
$el.addEventListener(
'click',
() => $el.style.setProperty('display', 'none'),
false,
);
setTimeout(() => $el.parentNode.removeChild($el), 2000);
});
$button.addEventListener('Button.keys', ({ detail }) => {
const { keys } = detail;
$keys.innerHTML = keys;
});
});
</script>
</body>
</html>