| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class ITable { |
| 22 |
constructor(params) { |
| 23 |
this._element = null; |
| 24 |
this._class = null; |
| 25 |
this._header = null; |
| 26 |
this._status = null; |
| 27 |
this._frames = []; |
| 28 |
this._columns = []; |
| 29 |
this._body = null; |
| 30 |
this._onsort = null; |
| 31 |
this._onclick = null; |
| 32 |
this._onfocus = null; |
| 33 |
this.column_set = 4095; |
| 34 |
if (params) { |
| 35 |
this._class = params.class || null; |
| 36 |
this._onsort = params.onsort || null; |
| 37 |
this._onclick = params.onclick || null; |
| 38 |
this._onfocus = params.onfocus || null; |
| 39 |
this._nodata_text = params.nodata_text || null; |
| 40 |
} |
| 41 |
this._focused = false; |
| 42 |
this._focused_row = null; |
| 43 |
this._selected_rows = []; |
| 44 |
} |
| 45 |
|
| 46 |
element() { |
| 47 |
if (!this._element) { |
| 48 |
this._element = document.createElement("div"); |
| 49 |
if (this._class) this._element.setAttribute("class", this._class); |
| 50 |
this._element.classList.add("table"); |
| 51 |
this._element.setAttribute("tabindex", -1); |
| 52 |
this._element.addEventListener("focus", event => { |
| 53 |
this._focused = true; |
| 54 |
this._update_focus(); |
| 55 |
}, true); |
| 56 |
this._element.addEventListener("blur", event => { |
| 57 |
this._focused = false; |
| 58 |
this._update_focus(); |
| 59 |
}, true); |
| 60 |
let th = this._element.appendChild(document.createElement("div")); |
| 61 |
th.setAttribute("class", "table-header"); |
| 62 |
this._header = th.appendChild(document.createElement("div")); |
| 63 |
this._header.setAttribute("class", "table-row"); |
| 64 |
this._header.addEventListener("click", event => { |
| 65 |
const col = this.get_column_by_element(event.target); |
| 66 |
if (col && col.is_sortable()) { |
| 67 |
if (this._onsort) this._onsort(col); |
| 68 |
} |
| 69 |
}); |
| 70 |
this._fill_columns(); |
| 71 |
this._body = this._element.appendChild(document.createElement("div")); |
| 72 |
this._body.setAttribute("class", "table-body"); |
| 73 |
this._body.addEventListener("click", event => { |
| 74 |
let row = this._get_row_by_element(event.target); |
| 75 |
if (row) { |
| 76 |
this._set_selected_rows([ row ]); |
| 77 |
if (this._onclick) this._onclick(row); |
| 78 |
} |
| 79 |
}); |
| 80 |
this._body.addEventListener("focus", event => { |
| 81 |
let row = this._get_row_by_element(event.target); |
| 82 |
if (row) { |
| 83 |
this._update_focused_row(row); |
| 84 |
if (this._onfocus) this._onfocus(row.element()); |
| 85 |
} |
| 86 |
}, true); |
| 87 |
this._body.addEventListener("blur", event => { |
| 88 |
let row = this._get_row_by_element(event.target); |
| 89 |
if (row) row.onfocus(false); |
| 90 |
}, true); |
| 91 |
this._body.addEventListener("keydown", event => { |
| 92 |
let row = null; |
| 93 |
switch (event.code) { |
| 94 |
case "ArrowDown": |
| 95 |
row = this._get_row(this._focused_row !== null && (this._focused_row.id() + 1) || 0); |
| 96 |
break; |
| 97 |
case "ArrowUp": |
| 98 |
if (this._focused_row) { |
| 99 |
let id = this._focused_row.id(); |
| 100 |
if (id >= 0) row = this._get_row(id - 1); |
| 101 |
} |
| 102 |
else { |
| 103 |
row = this._get_row(0); |
| 104 |
} |
| 105 |
break; |
| 106 |
case "PageUp": |
| 107 |
if (this._focused_row && this._frames.length > 0) { |
| 108 |
let c_id = this._focused_row.id(); |
| 109 |
let f_fr = this._frames[0]; |
| 110 |
let f_id = f_fr.first_index(); |
| 111 |
if (c_id == f_id) break; |
| 112 |
let s_el = this._get_scroll_element(); |
| 113 |
if (s_el) { |
| 114 |
let r_ht = this._focused_row.element().getBoundingClientRect().height; |
| 115 |
let s_ht = s_el.getBoundingClientRect().height; |
| 116 |
let n_id = Math.max(c_id - Math.floor(s_ht / r_ht) - 1, f_id); |
| 117 |
row = this._get_row(n_id); |
| 118 |
} |
| 119 |
else { |
| 120 |
row = f_fr.row(f_id); |
| 121 |
} |
| 122 |
} |
| 123 |
break; |
| 124 |
case "PageDown": |
| 125 |
if (this._focused_row && this._frames.length > 0) { |
| 126 |
let c_id = this._focused_row.id(); |
| 127 |
let l_fr = this._frames[this._frames.length - 1]; |
| 128 |
let l_id = l_fr.last_index(); |
| 129 |
if (c_id == l_id) break; |
| 130 |
let s_el = this._get_scroll_element(); |
| 131 |
if (s_el) { |
| 132 |
let r_ht = this._focused_row.element().getBoundingClientRect().height; |
| 133 |
let s_ht = s_el.getBoundingClientRect().height; |
| 134 |
let n_id = Math.min(c_id + Math.floor(s_ht / r_ht) - 1, l_id); |
| 135 |
row = this._get_row(n_id); |
| 136 |
} |
| 137 |
else { |
| 138 |
row = l_fr.row(l_id); |
| 139 |
} |
| 140 |
} |
| 141 |
break; |
| 142 |
case "Home": |
| 143 |
if (this._frames.length > 0) { |
| 144 |
let first_frame = this._frames[0]; |
| 145 |
row = first_frame.row(first_frame.first_index()); |
| 146 |
} |
| 147 |
break; |
| 148 |
case "End": |
| 149 |
if (this._frames.length > 0) { |
| 150 |
let last_frame = this._frames[this._frames.length - 1]; |
| 151 |
row = last_frame.row(last_frame.last_index()); |
| 152 |
} |
| 153 |
break; |
| 154 |
case "Enter": |
| 155 |
case "NumpadEnter": |
| 156 |
if (this._onclick && this._focused_row) this._onclick(this._focused_row); |
| 157 |
event.preventDefault(); |
| 158 |
return; |
| 159 |
} |
| 160 |
if (row) { |
| 161 |
row.element().focus(); |
| 162 |
this._set_selected_rows([ row ]); |
| 163 |
event.preventDefault(); |
| 164 |
} |
| 165 |
}); |
| 166 |
this._fill_frames(); |
| 167 |
} |
| 168 |
return this._element; |
| 169 |
} |
| 170 |
|
| 171 |
more() { |
| 172 |
return this._frames.length > 0 && this._frames[this._frames.length - 1].more(); |
| 173 |
} |
| 174 |
|
| 175 |
frames_count() { |
| 176 |
return this._frames.length; |
| 177 |
} |
| 178 |
|
| 179 |
add_column(data) { |
| 180 |
let col = new ITableColumn(data.content, { |
| 181 |
name: data.name, |
| 182 |
class: data.class, |
| 183 |
sortable: data.sortable, |
| 184 |
sorted: data.sorted |
| 185 |
}); |
| 186 |
this._columns.push(col); |
| 187 |
if (this._header) { |
| 188 |
const c_idx = this._columns.length - 1; |
| 189 |
if (this.column_set & (1 << c_idx)) this._header.appendChild(col.element()); |
| 190 |
} |
| 191 |
return col; |
| 192 |
} |
| 193 |
|
| 194 |
set_columns_visible(indexes) { |
| 195 |
let col_set = indexes.reduce((res, idx) => { |
| 196 |
res += (1 << idx); |
| 197 |
return res; |
| 198 |
}, 0); |
| 199 |
if (this.column_set !== col_set) { |
| 200 |
this.column_set = col_set; |
| 201 |
if (this._header) this._fill_columns(); |
| 202 |
this._frames.forEach(fr => fr.update()); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
get_column_by_element(el) { |
| 207 |
el = el && el.closest("div.table-cell"); |
| 208 |
if (!el) return null; |
| 209 |
|
| 210 |
let bmask = 1; |
| 211 |
for (const col of this._columns) { |
| 212 |
if ((this.column_set & bmask) && el === col.element()) return col; |
| 213 |
bmask <<= 1; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
display_status(status, text) { |
| 218 |
if (this._status) { |
| 219 |
this._status.remove(); |
| 220 |
if (!status) { |
| 221 |
this._status = null; |
| 222 |
return; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
this.element(); |
| 227 |
this._status = document.createElement("div"); |
| 228 |
this._status.classList.add("table-row", "colspanned", "noninteractive"); |
| 229 |
const tc1 = this._status.appendChild(document.createElement("div")); |
| 230 |
tc1.classList.add("table-cell"); |
| 231 |
const tc2 = this._status.appendChild(document.createElement("div")); |
| 232 |
tc2.classList.add("table-cell"); |
| 233 |
tc2.textContent = "\u00A0"; |
| 234 |
if (status === "wait") { |
| 235 |
set_wait_status(tc1); |
| 236 |
} else { |
| 237 |
this._body.replaceChildren(); |
| 238 |
if (status === "nodata") { |
| 239 |
tc1.append(text || "No data"); |
| 240 |
} else { |
| 241 |
set_error_status(tc1, text); |
| 242 |
} |
| 243 |
} |
| 244 |
this._status.classList.add("nodata"); |
| 245 |
this._body.append(this._status); |
| 246 |
} |
| 247 |
|
| 248 |
last_row_index() { |
| 249 |
let idx = -1; |
| 250 |
if (this._frames.length > 0) { |
| 251 |
idx = this._frames[this._frames.length - 1].last_index(); |
| 252 |
} |
| 253 |
return idx; |
| 254 |
} |
| 255 |
|
| 256 |
add_frame(frame) { |
| 257 |
if (frame.count() === 0) { |
| 258 |
if (this._frames.length === 0) |
| 259 |
this.display_status("nodata", this._nodata_text); |
| 260 |
return |
| 261 |
} |
| 262 |
|
| 263 |
frame.table = this; |
| 264 |
if (this._frames.length > 0 && this._frames[0].first_index() > frame.last_index()) { |
| 265 |
this._frames.unshift(frame); |
| 266 |
if (this._body) |
| 267 |
this._body.insertBefore(frame.element(), this._body.firstChild); |
| 268 |
} |
| 269 |
else { |
| 270 |
this._frames.push(frame); |
| 271 |
if (this._body) |
| 272 |
this._body.appendChild(frame.element()); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
clear() { |
| 277 |
this._frames = []; |
| 278 |
if (this._body) this._body.replaceChildren(); |
| 279 |
this._focused_row = null; |
| 280 |
this._selected_rows = []; |
| 281 |
} |
| 282 |
|
| 283 |
focus() { |
| 284 |
if (!this._focused_row) { |
| 285 |
if (this._frames.length > 0) { |
| 286 |
let fr = this._frames[0]; |
| 287 |
this._focused_row = fr.row(fr.first_index()); |
| 288 |
} |
| 289 |
} |
| 290 |
if (this._focused_row) |
| 291 |
this._focused_row.element().focus(); |
| 292 |
} |
| 293 |
|
| 294 |
sort(col_name, direction) { |
| 295 |
if (this._frames.length != 1) return; |
| 296 |
|
| 297 |
for (let i = 0; i < this._columns.length; ++i) { |
| 298 |
const col = this._columns[i]; |
| 299 |
if (col.is_sortable() && col.name() === col_name) { |
| 300 |
const fr = this._frames[0]; |
| 301 |
fr.sort(i, direction); |
| 302 |
if (this._body) this._body.replaceChildren(fr.element()); |
| 303 |
return; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
set_sorted(col_name, direction) { |
| 309 |
for (const col of this._columns) { |
| 310 |
if (!col.is_sortable()) continue; |
| 311 |
if (col.name() !== col_name) { |
| 312 |
col.sort(null); |
| 313 |
continue; |
| 314 |
} |
| 315 |
|
| 316 |
if (direction === "toggle") { |
| 317 |
switch (col.sorted()) { |
| 318 |
case "ascent": |
| 319 |
direction = "descent"; |
| 320 |
break; |
| 321 |
case "descent": |
| 322 |
direction = "ascent"; |
| 323 |
break; |
| 324 |
default: |
| 325 |
direction = null; |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
col.sort(direction); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
_fill_columns() { |
| 334 |
let bmask = 1; |
| 335 |
this._header.replaceChildren(...this._columns.reduce((res, col) => { |
| 336 |
if (this.column_set & bmask) { |
| 337 |
res.push(col.element()); |
| 338 |
} else { |
| 339 |
col.remove(); |
| 340 |
} |
| 341 |
bmask <<= 1; |
| 342 |
return res; |
| 343 |
}, [])); |
| 344 |
} |
| 345 |
|
| 346 |
_fill_frames() { |
| 347 |
this._frames.forEach(fr => this._body.append(fr.element())); |
| 348 |
} |
| 349 |
|
| 350 |
_get_row(row_id) { |
| 351 |
const fr = this._frames.find(fr => { |
| 352 |
return fr.last_index() >= row_id && fr.first_index() <= row_id; |
| 353 |
}); |
| 354 |
return fr && fr.row(row_id) || null; |
| 355 |
} |
| 356 |
|
| 357 |
_get_row_by_element(el) { |
| 358 |
if (el) { |
| 359 |
const r_el = el.closest("div.table-row"); |
| 360 |
if (r_el) { |
| 361 |
const id = parseInt(r_el.dataset.id); |
| 362 |
if (id !== NaN) return this._get_row(id); |
| 363 |
} |
| 364 |
} |
| 365 |
return null; |
| 366 |
} |
| 367 |
|
| 368 |
_update_focus() { |
| 369 |
if (this._focused) |
| 370 |
this._element.classList.add("focused"); |
| 371 |
else |
| 372 |
this._element.classList.remove("focused"); |
| 373 |
} |
| 374 |
|
| 375 |
_update_focused_row(row) { |
| 376 |
if (this._focused_row && row !== this._focused_row) { |
| 377 |
this._focused_row.tabindex(-1); |
| 378 |
} |
| 379 |
this._focused_row = row; |
| 380 |
this._focused_row.tabindex(0); |
| 381 |
this._focused_row.onfocus(true); |
| 382 |
} |
| 383 |
|
| 384 |
_set_selected_rows(rows) { |
| 385 |
this._selected_rows.forEach(row => row.select(false)); |
| 386 |
rows.forEach(row => row.select(true)); |
| 387 |
this._selected_rows = rows; |
| 388 |
} |
| 389 |
|
| 390 |
_get_scroll_element() { |
| 391 |
let t_rect = this._element.getBoundingClientRect(); |
| 392 |
let p_elem = this._element.parentElement; |
| 393 |
while (p_elem) { |
| 394 |
let p_rect = p_elem.getBoundingClientRect(); |
| 395 |
if (t_rect.top < p_rect.top || t_rect.bottom > p_rect.bottom) { |
| 396 |
return p_elem; |
| 397 |
} |
| 398 |
p_elem = p_elem.parentElement; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
class ITableFrame { |
| 404 |
constructor(data, pos) { |
| 405 |
this._pos = pos; |
| 406 |
this._more = !!data.more; |
| 407 |
let id = pos; |
| 408 |
this._rows = data.rows.map(function(rd) { |
| 409 |
if (!(rd instanceof ITableRow)) { |
| 410 |
rd = new ITableRow(rd); |
| 411 |
} |
| 412 |
rd.id(id++); |
| 413 |
return rd; |
| 414 |
}); |
| 415 |
this.table = null; |
| 416 |
} |
| 417 |
|
| 418 |
count() { |
| 419 |
return this._rows.length; |
| 420 |
} |
| 421 |
|
| 422 |
first_index() { |
| 423 |
return this._pos; |
| 424 |
} |
| 425 |
|
| 426 |
last_index() { |
| 427 |
let cnt = this._rows.length; |
| 428 |
return cnt > 0 ? this._pos + cnt - 1 : null; |
| 429 |
} |
| 430 |
|
| 431 |
row(id) { |
| 432 |
let idx = id - this._pos; |
| 433 |
if (idx < 0 || idx >= this._rows.length) return null; |
| 434 |
return this._rows[idx]; |
| 435 |
} |
| 436 |
|
| 437 |
more() { |
| 438 |
return this._more; |
| 439 |
} |
| 440 |
|
| 441 |
element() { |
| 442 |
const fr = document.createDocumentFragment(); |
| 443 |
this._rows.forEach(row => { |
| 444 |
row.table = this.table; |
| 445 |
fr.appendChild(row.element()); |
| 446 |
}); |
| 447 |
return fr; |
| 448 |
} |
| 449 |
|
| 450 |
update() { |
| 451 |
this._rows.forEach(row => row.update()); |
| 452 |
} |
| 453 |
|
| 454 |
sort(col_idx, direction) { |
| 455 |
let dir = (direction === "ascent" && 1) || (direction === "descent" && 2) || 0; |
| 456 |
if (!dir) return; |
| 457 |
|
| 458 |
this._rows.sort((a, b) => { |
| 459 |
const c1 = a.cell(col_idx); |
| 460 |
const c2 = b.cell(col_idx); |
| 461 |
return dir === 1 ? this._compare_cells(c2, c1) : this._compare_cells(c1, c2); |
| 462 |
}); |
| 463 |
let id = this._pos; |
| 464 |
this._rows.forEach(row => row.id(id++)); |
| 465 |
} |
| 466 |
|
| 467 |
_compare_cells(c1, c2) { |
| 468 |
return c1.value("sort") < c2.value("sort"); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
class ITableRow { |
| 473 |
constructor(data) { |
| 474 |
this._id = -1; |
| 475 |
this._focused = false; |
| 476 |
this._tabindex = -1; |
| 477 |
this._selected = false; |
| 478 |
this._element = null; |
| 479 |
this._class = data.class || null; |
| 480 |
this._userdata = data.userdata || null; |
| 481 |
this._cells = data.cells.map(function(col) { |
| 482 |
if (col instanceof ITableCell) { |
| 483 |
return col; |
| 484 |
} |
| 485 |
let props = null; |
| 486 |
if (col.title || col.class || col.label) { |
| 487 |
props = { |
| 488 |
title: col.title || null, |
| 489 |
class: col.class || null, |
| 490 |
label: col.label || null |
| 491 |
}; |
| 492 |
} |
| 493 |
return new ITableCell(col.content, props); |
| 494 |
}); |
| 495 |
} |
| 496 |
|
| 497 |
userdata() { |
| 498 |
return this._userdata; |
| 499 |
} |
| 500 |
|
| 501 |
element() { |
| 502 |
let row_el = this._element; |
| 503 |
if (!row_el) { |
| 504 |
row_el = document.createElement("div"); |
| 505 |
row_el.dataset.id = this._id; |
| 506 |
if (this._class) row_el.setAttribute("class", this._class); |
| 507 |
row_el.classList.add("table-row"); |
| 508 |
this._element = row_el; |
| 509 |
|
| 510 |
row_el.append(...this._get_cell_elements()); |
| 511 |
this._update_focus(); |
| 512 |
this._update_tabindex(); |
| 513 |
this._update_select(); |
| 514 |
} |
| 515 |
return row_el; |
| 516 |
} |
| 517 |
|
| 518 |
update() { |
| 519 |
if (this._element) { |
| 520 |
this._element.replaceChildren(...this._get_cell_elements()); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
onfocus(flag) { |
| 525 |
this._focused = flag; |
| 526 |
if (this._element) this._update_focus(); |
| 527 |
} |
| 528 |
|
| 529 |
tabindex(index) { |
| 530 |
if (this._tabindex !== index) { |
| 531 |
this._tabindex = index; |
| 532 |
this._update_tabindex(); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
select(flag) { |
| 537 |
this._selected = flag; |
| 538 |
if (this._element) this._update_select(); |
| 539 |
} |
| 540 |
|
| 541 |
id(new_id) { |
| 542 |
if (new_id !== undefined && new_id !== this._id) { |
| 543 |
this._id = new_id; |
| 544 |
if (this._element) this._element.dataset.id = new_id; |
| 545 |
} |
| 546 |
return this._id; |
| 547 |
} |
| 548 |
|
| 549 |
cell(index) { |
| 550 |
return this._cells[index] || null; |
| 551 |
} |
| 552 |
|
| 553 |
_update_focus() { |
| 554 |
if (this._focused) |
| 555 |
this._element.classList.add("focused"); |
| 556 |
else |
| 557 |
this._element.classList.remove("focused"); |
| 558 |
} |
| 559 |
|
| 560 |
_update_tabindex() { |
| 561 |
this._element.setAttribute("tabindex", this._tabindex); |
| 562 |
} |
| 563 |
|
| 564 |
_update_select() { |
| 565 |
if (this._selected) { |
| 566 |
this._element.classList.add("selected"); |
| 567 |
} |
| 568 |
else { |
| 569 |
this._element.classList.remove("selected"); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
_get_cell_elements() { |
| 574 |
const col_set = this.table && this.table.column_set || 4095; |
| 575 |
const res = []; |
| 576 |
let bmask = 1; |
| 577 |
for (const col of this._cells) { |
| 578 |
if (col_set & bmask) { |
| 579 |
res.push(col.element()); |
| 580 |
} else { |
| 581 |
col.remove(); |
| 582 |
} |
| 583 |
bmask <<= 1; |
| 584 |
} |
| 585 |
return res; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
class ITableCell { |
| 590 |
constructor(content, props) { |
| 591 |
this._element = null; |
| 592 |
this._content = content; |
| 593 |
if (props) { |
| 594 |
this._title = props.title || null; |
| 595 |
this._class = props.class || null; |
| 596 |
this._label = props.label || null; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
element() { |
| 601 |
if (!this._element) { |
| 602 |
this._element = document.createElement("div"); |
| 603 |
if (this._title) this._element.title = this._title; |
| 604 |
if (this._class) this._element.setAttribute("class", this._class); |
| 605 |
if (this._label) this._element.dataset.label = this._label; |
| 606 |
this._element.classList.add("table-cell"); |
| 607 |
const content = this.value("dom"); |
| 608 |
if (content !== null) this._element.append(content); |
| 609 |
} |
| 610 |
return this._element; |
| 611 |
} |
| 612 |
|
| 613 |
remove() { |
| 614 |
if (this._element) { |
| 615 |
this._element.remove(); |
| 616 |
this._element = null; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
value(target) { |
| 621 |
if (target === "dom" || typeof(this._content) !== "object") { |
| 622 |
return this._content; |
| 623 |
} |
| 624 |
return null; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
class ITableColumn extends ITableCell { |
| 629 |
constructor(content, props) { |
| 630 |
super(content, props); |
| 631 |
this._name = props.name; |
| 632 |
this._sortable = !!props.sortable; |
| 633 |
this._sorted = props.sorted || null; |
| 634 |
} |
| 635 |
|
| 636 |
element() { |
| 637 |
if (this._element !== super.element()) { |
| 638 |
this._update_sorted(); |
| 639 |
} |
| 640 |
return this._element; |
| 641 |
} |
| 642 |
|
| 643 |
is_sortable() { |
| 644 |
return this._sortable; |
| 645 |
} |
| 646 |
|
| 647 |
sort(dir) { |
| 648 |
if (this._sorted !== dir) { |
| 649 |
this._sorted = dir || null; |
| 650 |
if (this._element) { |
| 651 |
this._update_sorted(); |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
sorted() { |
| 657 |
return this._sorted; |
| 658 |
} |
| 659 |
|
| 660 |
name() { |
| 661 |
return this._name; |
| 662 |
} |
| 663 |
|
| 664 |
_update_sorted() { |
| 665 |
if (this._sortable) { |
| 666 |
this._element.classList.add("sortable"); |
| 667 |
let c_act = { |
| 668 |
asc: "remove", |
| 669 |
des: "remove" |
| 670 |
}; |
| 671 |
if (this._sorted) { |
| 672 |
this._element.classList.add("arrows"); |
| 673 |
if (this._sorted === "ascent") { |
| 674 |
c_act["asc"] = "add"; |
| 675 |
} |
| 676 |
else if (this._sorted === "descent") { |
| 677 |
c_act["des"] = "add"; |
| 678 |
} |
| 679 |
} |
| 680 |
else { |
| 681 |
this._element.classList.remove("arrows"); |
| 682 |
} |
| 683 |
for (let key in c_act) { |
| 684 |
this._element.classList[c_act[key]]("sorted-" + key); |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
class Toolbar { |
| 691 |
constructor(label) { |
| 692 |
this._label = label; |
| 693 |
this._element = null; |
| 694 |
this._items = []; |
| 695 |
this._spacer = {}; |
| 696 |
} |
| 697 |
|
| 698 |
element() { |
| 699 |
if (!this._element) { |
| 700 |
const el = document.createElement("div"); |
| 701 |
el.role = "toolbar"; |
| 702 |
el.ariaLabel = this._label; |
| 703 |
let spacer = false |
| 704 |
let first = true; |
| 705 |
for (const it of this._items) { |
| 706 |
if (it === this._spacer) { |
| 707 |
spacer = true; |
| 708 |
continue; |
| 709 |
} |
| 710 |
if (spacer) { |
| 711 |
it.classList.add("spacer-left"); |
| 712 |
spacer = false; |
| 713 |
} |
| 714 |
let te = (it instanceof ToolbarButton) ? it.element() : it; |
| 715 |
el.append(te); |
| 716 |
if (first && te.tabIndex === -1) { |
| 717 |
first = false; |
| 718 |
te.tabIndex = 0; |
| 719 |
} |
| 720 |
} |
| 721 |
el.addEventListener("keydown", event => { |
| 722 |
const target = event.target; |
| 723 |
switch (event.code) { |
| 724 |
case "ArrowLeft": |
| 725 |
this._focusPreviousItem(event.target); |
| 726 |
break; |
| 727 |
case "ArrowRight": |
| 728 |
this._focusNextItem(event.target); |
| 729 |
break; |
| 730 |
case "Home": |
| 731 |
this._focusFirstItem(); |
| 732 |
break; |
| 733 |
case "End": |
| 734 |
this._focusLastItem(); |
| 735 |
break; |
| 736 |
} |
| 737 |
}); |
| 738 |
this._element = el; |
| 739 |
} |
| 740 |
return this._element; |
| 741 |
} |
| 742 |
|
| 743 |
appendItem(item) { |
| 744 |
this._items.push(item); |
| 745 |
return this; |
| 746 |
} |
| 747 |
|
| 748 |
appendSpacer() { |
| 749 |
this._items.push(this._spacer); |
| 750 |
return this; |
| 751 |
} |
| 752 |
|
| 753 |
_focusFirstItem() { |
| 754 |
const first = this._element.firstElementChild; |
| 755 |
if (!first.hasAttribute("tabindex")) { |
| 756 |
this._focusNextItem(first); |
| 757 |
return; |
| 758 |
} |
| 759 |
this._resetTabindexValues(); |
| 760 |
first.tabIndex = 0; |
| 761 |
first.focus(); |
| 762 |
} |
| 763 |
|
| 764 |
_focusLastItem() { |
| 765 |
const last = this._element.lastElementChild; |
| 766 |
if (!last.hasAttribute("tabindex")) { |
| 767 |
this._focusPreviousItem(last); |
| 768 |
return; |
| 769 |
} |
| 770 |
this._resetTabindexValues(); |
| 771 |
last.tabIndex = 0; |
| 772 |
last.focus(); |
| 773 |
} |
| 774 |
|
| 775 |
_focusNextItem(cItem) { |
| 776 |
let next = cItem; |
| 777 |
while ((next = next.nextElementSibling)) { |
| 778 |
if (next.hasAttribute("tabindex")) { |
| 779 |
this._resetTabindexValues(); |
| 780 |
next.tabIndex = 0; |
| 781 |
next.focus(); |
| 782 |
break; |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
_focusPreviousItem(cItem) { |
| 788 |
let prev = cItem; |
| 789 |
while ((prev = prev.previousElementSibling)) { |
| 790 |
if (prev.hasAttribute("tabindex")) { |
| 791 |
this._resetTabindexValues(); |
| 792 |
prev.tabIndex = 0; |
| 793 |
prev.focus(); |
| 794 |
break; |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
} |
| 799 |
|
| 800 |
_resetTabindexValues() { |
| 801 |
this._element.querySelectorAll('[tabindex="0"]').forEach(el => { |
| 802 |
el.tabIndex = -1; |
| 803 |
}); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
class ToolbarButton { |
| 808 |
constructor(params) { |
| 809 |
this._element = null; |
| 810 |
this._title = params.title || null; |
| 811 |
this._content = params.content || null; |
| 812 |
this._onclick = params.onclick || null; |
| 813 |
} |
| 814 |
|
| 815 |
element() { |
| 816 |
if (!this._element) { |
| 817 |
const el = document.createElement("button"); |
| 818 |
el.type = "button"; |
| 819 |
el.tabIndex = -1; |
| 820 |
let ce = null |
| 821 |
if (this._content) { |
| 822 |
ce = el.appendChild((typeof(this._content) === "string") && this._getSVG() || this._content); |
| 823 |
} |
| 824 |
if (this._title) { |
| 825 |
const popup = document.createElement("span"); |
| 826 |
popup.classList.add("popup-label"); |
| 827 |
el.appendChild(popup).textContent = this._title; |
| 828 |
if (ce && ce.nodeName.toUpperCase() === "SVG") ce.setAttribute("aria-hidden", true); |
| 829 |
} |
| 830 |
if (this._onclick) el.addEventListener("click", this._onclick); |
| 831 |
this._element = el; |
| 832 |
} |
| 833 |
return this._element; |
| 834 |
} |
| 835 |
|
| 836 |
_getSVG() { |
| 837 |
switch (this._content) { |
| 838 |
case "info_icon": |
| 839 |
return this._svgIcon( |
| 840 |
'0 0 16 16', |
| 841 |
'<path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.808 1.319.545 0 1.178-.252 1.465-.598l.088-.416c-.2.176-.492.246-.686.246-.275 0-.375-.193-.304-.533L8.93 6.588zM9 4.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0z"/>' |
| 842 |
); |
| 843 |
case "filter_icon": |
| 844 |
return this._svgIcon( |
| 845 |
'0 1 15 15', |
| 846 |
'<path d="M1.5 1.5A.5.5 0 0 1 2 1h12a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.128.334L10 8.692V13.5a.5.5 0 0 1-.342.474l-3 1A.5.5 0 0 1 6 14.5V8.692L1.628 3.834A.5.5 0 0 1 1.5 3.5zm1 .5v1.308l4.372 4.858A.5.5 0 0 1 7 8.5v5.306l2-.666V8.5a.5.5 0 0 1 .128-.334L13.5 3.308V2z"/>' |
| 847 |
); |
| 848 |
case "columns_icon": |
| 849 |
return this._svgIcon( |
| 850 |
'0 0 17 17', |
| 851 |
'<path d="M0 1.5A1.5 1.5 0 0 1 1.5 0h13A1.5 1.5 0 0 1 16 1.5v13a1.5 1.5 0 0 1-1.5 1.5h-13A1.5 1.5 0 0 1 0 14.5zM1.5 1a.5.5 0 0 0-.5.5v13a.5.5 0 0 0 .5.5H5V1zM10 15V1H6v14zm1 0h3.5a.5.5 0 0 0 .5-.5v-13a.5.5 0 0 0-.5-.5H11z"/>' |
| 852 |
); |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
_svgIcon(view_box, html) { |
| 857 |
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 858 |
svg.setAttribute("focusable", "false"); |
| 859 |
svg.setAttribute("fill", "currentColor"); |
| 860 |
svg.setAttribute("viewBox", view_box); |
| 861 |
svg.innerHTML = html; |
| 862 |
return svg; |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
class ModalDialog { |
| 867 |
constructor(params) { |
| 868 |
this._params = params; |
| 869 |
this._element = null; |
| 870 |
this._title = null; |
| 871 |
this._messages = null; |
| 872 |
this._alert = null; |
| 873 |
this._wait = null; |
| 874 |
this._buttons = []; |
| 875 |
this._content = null; |
| 876 |
this._result = null; |
| 877 |
this._callback = null; |
| 878 |
} |
| 879 |
|
| 880 |
element() { |
| 881 |
if (!this._element) { |
| 882 |
const ovl = document.createElement("div"); |
| 883 |
ovl.classList.add("dialog-overlay", "hidden"); |
| 884 |
this._element = ovl; |
| 885 |
const dlg = ovl.appendChild(document.createElement("div")); |
| 886 |
dlg.role = "dialog"; |
| 887 |
dlg.ariaModal = true; |
| 888 |
dlg.tabIndex = -1; |
| 889 |
dlg.classList.add("dialog"); |
| 890 |
const con = dlg.appendChild(document.createElement("div")); |
| 891 |
con.classList.add("container"); |
| 892 |
this._title = con.appendChild(document.createElement("div")); |
| 893 |
this._title.classList.add("title"); |
| 894 |
{ |
| 895 |
const tt = this._title.appendChild(document.createElement("div")); |
| 896 |
tt.classList.add("title-text"); |
| 897 |
tt.textContent = this._params.title || ""; |
| 898 |
if (this._params.title) dlg.setAriaLabelledBy(tt); |
| 899 |
} |
| 900 |
{ |
| 901 |
const cbt = this._title.appendChild(document.createElement("button")); |
| 902 |
cbt.type = "button"; |
| 903 |
cbt.ariaLabel = "Close"; |
| 904 |
cbt.classList.add("close-btn"); |
| 905 |
cbt.textContent = "x"; |
| 906 |
this._buttons = [ cbt ]; |
| 907 |
cbt.addEventListener("click", event => this.hide()); |
| 908 |
} |
| 909 |
const frm = con.appendChild(document.createElement("form")); |
| 910 |
frm.classList.add("vertical-content"); |
| 911 |
this._content = frm.appendChild(document.createElement("div")); |
| 912 |
const bdv = frm.appendChild(document.createElement("div")); |
| 913 |
bdv.classList.add("dialog-buttons"); |
| 914 |
this._add_buttons(bdv); |
| 915 |
this._gen_content(); |
| 916 |
this._element.addEventListener("click", event => { |
| 917 |
if (event.target === event.currentTarget && this._params.overlay_click !== "ignore") { |
| 918 |
this.hide(); |
| 919 |
} |
| 920 |
}); |
| 921 |
this._element.addEventListener("keydown", event => { |
| 922 |
switch (event.code) { |
| 923 |
case "Tab": |
| 924 |
{ |
| 925 |
const els = this._get_focusable_elements(); |
| 926 |
const lfe = [ els[0], els[els.length - 1] ]; |
| 927 |
switch (lfe.indexOf(event.target)) { |
| 928 |
case -1: |
| 929 |
return; |
| 930 |
case 0: |
| 931 |
if (!event.shiftKey) return; |
| 932 |
lfe[1].focus(); |
| 933 |
break; |
| 934 |
case 1: |
| 935 |
if (event.shiftKey) return; |
| 936 |
lfe[0].focus(); |
| 937 |
break; |
| 938 |
} |
| 939 |
event.preventDefault(); |
| 940 |
} |
| 941 |
break; |
| 942 |
case "Esc": |
| 943 |
case "Escape": |
| 944 |
event.preventDefault(); |
| 945 |
this.hide(); |
| 946 |
break; |
| 947 |
} |
| 948 |
}); |
| 949 |
frm.addEventListener("submit", event => { |
| 950 |
event.preventDefault(); |
| 951 |
this._submit(); |
| 952 |
}); |
| 953 |
frm.addEventListener("reset", event => this._reset()); |
| 954 |
} |
| 955 |
return this._element; |
| 956 |
} |
| 957 |
|
| 958 |
show() { |
| 959 |
this.element(); |
| 960 |
this._result = null; |
| 961 |
this._element.classList.remove("hidden"); |
| 962 |
this.focus(); |
| 963 |
|
| 964 |
return new Promise((resolve, reject) => { |
| 965 |
this._callback = resolve; |
| 966 |
}); |
| 967 |
} |
| 968 |
|
| 969 |
hide() { |
| 970 |
if (this._element) this._element.classList.add("hidden"); |
| 971 |
this._callback && this._callback(this._result); |
| 972 |
} |
| 973 |
|
| 974 |
focus() { |
| 975 |
this.element(); |
| 976 |
const els = this._get_focusable_elements(2); |
| 977 |
switch (els.length) { |
| 978 |
case 2: |
| 979 |
if (els[0].classList.contains("close-btn")) { |
| 980 |
els[1].focus(); |
| 981 |
break; |
| 982 |
} |
| 983 |
case 1: |
| 984 |
els[0].focus(); |
| 985 |
break; |
| 986 |
default: |
| 987 |
this._element.querySelector('[role="dialog"]').focus(); |
| 988 |
break; |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
display_status(type, text) { |
| 993 |
if (type && !text) { |
| 994 |
type == "error" && this._alert && this._alert.replaceChildren(); |
| 995 |
type == "wait" && this._wait && this._wait.replaceChildren(); |
| 996 |
} else { |
| 997 |
this._alert && this._alert.replaceChildren(); |
| 998 |
this._wait && this._wait.replaceChildren(); |
| 999 |
} |
| 1000 |
if (!text) return; |
| 1001 |
|
| 1002 |
const t_el = document.createElement("p"); |
| 1003 |
t_el.textContent = text; |
| 1004 |
if (type == "error") { |
| 1005 |
if (!this._alert) { |
| 1006 |
this._alert = this._make_msg_container(); |
| 1007 |
this._alert.role = "alert"; |
| 1008 |
} |
| 1009 |
t_el.classList.add("error-message"); |
| 1010 |
this._alert.append(t_el); |
| 1011 |
} else if (type == "wait") { |
| 1012 |
if (!this._wait) { |
| 1013 |
this._wait = this._make_msg_container(); |
| 1014 |
} |
| 1015 |
t_el.classList.add("wait-message"); |
| 1016 |
this._wait.append(t_el); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
_make_msg_container() { |
| 1021 |
if (!this._messages) { |
| 1022 |
this._messages = document.createElement("div"); |
| 1023 |
const btns = this.element().querySelector("form .dialog-buttons"); |
| 1024 |
btns.parentElement.insertBefore(this._messages, btns); |
| 1025 |
} |
| 1026 |
return this._messages.appendChild(document.createElement("div")); |
| 1027 |
} |
| 1028 |
|
| 1029 |
_add_buttons(container) { |
| 1030 |
let bl = this._params.buttons || []; |
| 1031 |
bl.forEach(function(bt) { |
| 1032 |
let name = null; |
| 1033 |
let type = null; |
| 1034 |
if (bt == "ok") { |
| 1035 |
name = "Ok"; |
| 1036 |
type = "submit"; |
| 1037 |
} |
| 1038 |
else if (bt == "apply") { |
| 1039 |
name = "Apply"; |
| 1040 |
type = "submit"; |
| 1041 |
} |
| 1042 |
else if (bt == "reset") { |
| 1043 |
name = "Reset"; |
| 1044 |
type = "reset"; |
| 1045 |
} |
| 1046 |
else if (bt == "login") { |
| 1047 |
name = "Log in"; |
| 1048 |
type = "submit"; |
| 1049 |
} |
| 1050 |
else if (bt == "cancel") { |
| 1051 |
name = "Cancel"; |
| 1052 |
type = "close"; |
| 1053 |
} |
| 1054 |
else if (bt == "close") { |
| 1055 |
name = "Close"; |
| 1056 |
type = "close"; |
| 1057 |
} |
| 1058 |
else { |
| 1059 |
name = bt; |
| 1060 |
type = bt; |
| 1061 |
} |
| 1062 |
this._add_button(container, name, type); |
| 1063 |
}, this); |
| 1064 |
} |
| 1065 |
|
| 1066 |
_add_button(container, text, type) { |
| 1067 |
let btn = document.createElement("button"); |
| 1068 |
if (type == "close") { |
| 1069 |
btn.setAttribute("type", "button"); |
| 1070 |
btn.addEventListener("click", this.hide.bind(this)); |
| 1071 |
} |
| 1072 |
else { |
| 1073 |
btn.setAttribute("type", type); |
| 1074 |
} |
| 1075 |
btn.appendChild(document.createTextNode(text)); |
| 1076 |
container.appendChild(btn); |
| 1077 |
this._buttons.push(btn); |
| 1078 |
} |
| 1079 |
|
| 1080 |
_gen_content() { |
| 1081 |
} |
| 1082 |
|
| 1083 |
_get_focusable_elements(max) { |
| 1084 |
const res = []; |
| 1085 |
if (!max) max = -1; |
| 1086 |
for (const el of this._element.querySelectorAll("input, select, button, a[href], [tabindex]")) { |
| 1087 |
const ti = el.tabIndex; |
| 1088 |
if (!isNaN(ti) && ti >= 0 && !el.disabled) { |
| 1089 |
if (window.getComputedStyle(el, null).display !== "none") { |
| 1090 |
res.push(el); |
| 1091 |
if (!(--max)) break; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
return res; |
| 1096 |
} |
| 1097 |
|
| 1098 |
_submit() { |
| 1099 |
} |
| 1100 |
|
| 1101 |
_reset() { |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
class VerticalDialog extends ModalDialog { |
| 1106 |
constructor(params) { |
| 1107 |
super(params); |
| 1108 |
this._inputs = null; |
| 1109 |
} |
| 1110 |
|
| 1111 |
_insert_input_row(text, v_el) { |
| 1112 |
if (!this._inputs) { |
| 1113 |
this._inputs = document.createElement("div"); |
| 1114 |
this._inputs.classList.add("titled-input"); |
| 1115 |
this._content.appendChild(this._inputs); |
| 1116 |
this._content.classList.add("vertical-content"); |
| 1117 |
} |
| 1118 |
const l_el = document.createElement("label"); |
| 1119 |
const t_el = document.createElement("span"); |
| 1120 |
t_el.textContent = text + ": "; |
| 1121 |
l_el.appendChild(t_el); |
| 1122 |
l_el.appendChild(v_el); |
| 1123 |
this._inputs.appendChild(l_el); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
class AboutDialog extends ModalDialog { |
| 1128 |
constructor(params) { |
| 1129 |
super({ |
| 1130 |
title: "About", |
| 1131 |
buttons: [ "ok" ] |
| 1132 |
}); |
| 1133 |
this._authors = params.authors; |
| 1134 |
this._documentation = params.documentation; |
| 1135 |
this._source_code = params.source_code; |
| 1136 |
} |
| 1137 |
|
| 1138 |
element() { |
| 1139 |
if (!this._element) { |
| 1140 |
super.element(); |
| 1141 |
this._element.children[0].classList.add("about"); |
| 1142 |
this._content.classList.add("vertical-content"); |
| 1143 |
this._content.parentElement.classList.add("vertical-content"); |
| 1144 |
} |
| 1145 |
return this._element; |
| 1146 |
} |
| 1147 |
|
| 1148 |
_gen_content() { |
| 1149 |
let header = document.createElement("h2"); |
| 1150 |
header.appendChild(document.createTextNode(Router.app_name(true))); |
| 1151 |
this._content.appendChild(header); |
| 1152 |
|
| 1153 |
let cblock = document.createElement("div"); |
| 1154 |
this._authors.forEach(function(author) { |
| 1155 |
let ablock = document.createElement("div"); |
| 1156 |
ablock.appendChild(document.createTextNode("Copyright © " + author.years + ", ")); |
| 1157 |
cblock.appendChild(ablock); |
| 1158 |
let alink = document.createElement("a"); |
| 1159 |
alink.setAttribute("href", author.url); |
| 1160 |
alink.setAttribute("title", "The author's page"); |
| 1161 |
alink.setAttribute("target", "_blank"); |
| 1162 |
alink.appendChild(document.createTextNode(author.name)); |
| 1163 |
ablock.appendChild(alink); |
| 1164 |
}); |
| 1165 |
this._content.appendChild(cblock); |
| 1166 |
|
| 1167 |
let oblock = document.createElement("div"); |
| 1168 |
oblock.setAttribute("class", "left-titled"); |
| 1169 |
let add_row = function(title, value) { |
| 1170 |
let t_el = document.createElement("span"); |
| 1171 |
t_el.appendChild(document.createTextNode(title + ": ")); |
| 1172 |
oblock.appendChild(t_el); |
| 1173 |
let v_el = document.createElement("div"); |
| 1174 |
value.forEach(function(v) { |
| 1175 |
if (v_el.children.length > 0) { |
| 1176 |
v_el.appendChild(document.createTextNode(", ")); |
| 1177 |
} |
| 1178 |
let a_el = document.createElement("a"); |
| 1179 |
a_el.setAttribute("href", v.url); |
| 1180 |
a_el.setAttribute("title", v.title || v.ancor); |
| 1181 |
a_el.setAttribute("target", "_blank"); |
| 1182 |
a_el.appendChild(document.createTextNode(v.ancor)); |
| 1183 |
v_el.appendChild(a_el); |
| 1184 |
}); |
| 1185 |
oblock.appendChild(v_el); |
| 1186 |
}; |
| 1187 |
this._content.appendChild(oblock); |
| 1188 |
add_row("Documentation", this._documentation); |
| 1189 |
add_row("Source code", this._source_code); |
| 1190 |
{ |
| 1191 |
let tl = document.createElement("span"); |
| 1192 |
tl.appendChild(document.createTextNode("PHP version: ")); |
| 1193 |
oblock.appendChild(tl); |
| 1194 |
let vl = document.createElement("span"); |
| 1195 |
vl.appendChild(document.createTextNode(Router.php_version || "n/a")); |
| 1196 |
oblock.appendChild(vl); |
| 1197 |
} |
| 1198 |
|
| 1199 |
const lblock = this._content.appendChild(document.createElement("div")); |
| 1200 |
lblock.classList.add("text"); |
| 1201 |
lblock.appendChild(document.createTextNode( |
| 1202 |
"This program is free software: you can redistribute it and/or modify it \ |
| 1203 |
under the terms of the GNU General Public License as published by the Free \ |
| 1204 |
Software Foundation, either version 3 of the License." |
| 1205 |
)); |
| 1206 |
} |
| 1207 |
|
| 1208 |
_submit() { |
| 1209 |
this.hide(); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
class ReportFilterDialog extends ModalDialog { |
| 1214 |
constructor(params) { |
| 1215 |
params ||= {}; |
| 1216 |
super({ title: params.title || "Filter settings", buttons: [ "apply", "reset" ] }); |
| 1217 |
this._data = params; |
| 1218 |
this._content = null; |
| 1219 |
let item_list = params.item_list || []; |
| 1220 |
this._ui_data = [ |
| 1221 |
{ name: "domain", title: "Domain" }, |
| 1222 |
{ name: "month", title: "Month" }, |
| 1223 |
{ name: "organization", title: "Organization" }, |
| 1224 |
{ name: "dkim", title: "DKIM result" }, |
| 1225 |
{ name: "spf", title: "SPF result" }, |
| 1226 |
{ name: "disposition", title: "Disposition" }, |
| 1227 |
{ name: "status", title: "Status" } |
| 1228 |
].reduce(function(res, item) { |
| 1229 |
if (item_list.includes(item.name)) |
| 1230 |
res.push(item); |
| 1231 |
return res; |
| 1232 |
}, []); |
| 1233 |
} |
| 1234 |
|
| 1235 |
show() { |
| 1236 |
this._update_ui(); |
| 1237 |
return super.show(); |
| 1238 |
} |
| 1239 |
|
| 1240 |
_gen_content() { |
| 1241 |
let fs = document.createElement("fieldset"); |
| 1242 |
fs.setAttribute("class", "round-border titled-input"); |
| 1243 |
let lg = document.createElement("legend"); |
| 1244 |
lg.appendChild(document.createTextNode("Filter by")); |
| 1245 |
fs.appendChild(lg); |
| 1246 |
this._ui_data.forEach(function(ud) { |
| 1247 |
let el = this._create_select_label(ud.title, fs); |
| 1248 |
ud.element = el; |
| 1249 |
}, this); |
| 1250 |
this._content.appendChild(fs); |
| 1251 |
this._content.classList.add("vertical-content"); |
| 1252 |
if (!this._data.loaded_filters) |
| 1253 |
this._fetch_data(); |
| 1254 |
} |
| 1255 |
|
| 1256 |
_create_select_label(text, c_el) { |
| 1257 |
let lb = document.createElement("label"); |
| 1258 |
let sp = document.createElement("span"); |
| 1259 |
sp.appendChild(document.createTextNode(text + ": ")); |
| 1260 |
lb.appendChild(sp); |
| 1261 |
let sl = document.createElement("select"); |
| 1262 |
lb.appendChild(sl); |
| 1263 |
c_el.appendChild(lb); |
| 1264 |
return sl; |
| 1265 |
} |
| 1266 |
|
| 1267 |
_enable_ui(enable) { |
| 1268 |
let list = this._element.querySelector("form").elements; |
| 1269 |
for (let i = 0; i < list.length; ++i) { |
| 1270 |
list[i].disabled = !enable; |
| 1271 |
} |
| 1272 |
this.focus(); |
| 1273 |
} |
| 1274 |
|
| 1275 |
_update_ui() { |
| 1276 |
this._update_filters(); |
| 1277 |
} |
| 1278 |
|
| 1279 |
_update_filters() { |
| 1280 |
let data = this._data.loaded_filters || {}; |
| 1281 |
let vals = this._data.filter || {}; |
| 1282 |
this._ui_data.forEach(function(ud) { |
| 1283 |
this._update_select_element(ud.element, data[ud.name], vals[ud.name]); |
| 1284 |
}, this); |
| 1285 |
} |
| 1286 |
|
| 1287 |
_update_select_element(sl, d, v) { |
| 1288 |
let ao = document.createElement("option"); |
| 1289 |
ao.setAttribute("value", ""); |
| 1290 |
ao.setAttribute("selected", "selected"); |
| 1291 |
ao.appendChild(document.createTextNode("Any")); |
| 1292 |
sl.replaceChildren(ao); |
| 1293 |
let v2 = ""; |
| 1294 |
if (d) { |
| 1295 |
let op = null; |
| 1296 |
d.forEach(function(fs) { |
| 1297 |
op = document.createElement("option"); |
| 1298 |
op.setAttribute("value", fs); |
| 1299 |
op.appendChild(document.createTextNode(fs)); |
| 1300 |
if (fs === v) { |
| 1301 |
v2 = v; |
| 1302 |
} |
| 1303 |
sl.appendChild(op); |
| 1304 |
}, this); |
| 1305 |
} |
| 1306 |
sl.value = v2; |
| 1307 |
} |
| 1308 |
|
| 1309 |
_submit() { |
| 1310 |
let res = {}; |
| 1311 |
let fdata = {}; |
| 1312 |
this._ui_data.forEach(function(ud) { |
| 1313 |
let el = ud.element; |
| 1314 |
let val = el.options[el.selectedIndex].value; |
| 1315 |
res[ud.name] = val; |
| 1316 |
fdata[ud.name] = val; |
| 1317 |
}); |
| 1318 |
this._data.filter = fdata; |
| 1319 |
this._result = res; |
| 1320 |
this.hide(); |
| 1321 |
} |
| 1322 |
|
| 1323 |
_fetch_data() { |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
class Multiselect extends HTMLElement { |
| 1328 |
constructor() { |
| 1329 |
super(); |
| 1330 |
this._items = []; |
| 1331 |
this._aitems = []; |
| 1332 |
this._values = new Set(); |
| 1333 |
this._label = null; |
| 1334 |
this._tags = null; |
| 1335 |
this._more = null; |
| 1336 |
this._search = null; |
| 1337 |
this._select = null; |
| 1338 |
this._listBox = null; |
| 1339 |
this._active = false; |
| 1340 |
this._disabled = false; |
| 1341 |
this._focused = { index: -1, item: null }; |
| 1342 |
} |
| 1343 |
|
| 1344 |
connectedCallback() { |
| 1345 |
this._makeSelectButton(); |
| 1346 |
const iw = document.createElement("div"); |
| 1347 |
iw.classList.add("multiselect-wrapper"); |
| 1348 |
this.appendChild(iw).append(this._makeInputElement(), this._select); |
| 1349 |
this._makeListbox(); |
| 1350 |
|
| 1351 |
this._select.setAriaControls(this._listBox); |
| 1352 |
this._search.setAriaControls(this._listBox); |
| 1353 |
this._select.setAriaLabelledBy(this._listBox); |
| 1354 |
|
| 1355 |
this.addEventListener("focusin", event => { |
| 1356 |
if (event.target === this._search) { |
| 1357 |
this.activate(); |
| 1358 |
} |
| 1359 |
}); |
| 1360 |
this.addEventListener("focusout", event => { |
| 1361 |
if (!this.contains(event.relatedTarget)) { |
| 1362 |
this.deactivate(); |
| 1363 |
} |
| 1364 |
}); |
| 1365 |
this.tabIndex = -1; |
| 1366 |
this._disableChanged(); |
| 1367 |
this._activateSearch(); |
| 1368 |
} |
| 1369 |
|
| 1370 |
static get observedAttributes() { |
| 1371 |
return [ "placeholder", "disabled" ]; |
| 1372 |
} |
| 1373 |
|
| 1374 |
attributeChangedCallback(name, oldValue, newValue) { |
| 1375 |
switch (name) { |
| 1376 |
case "placeholder": |
| 1377 |
if (this._search) this._search.placeholder = newValue; |
| 1378 |
break; |
| 1379 |
case "disabled": |
| 1380 |
if (this._disabled !== (newValue !== null)) { |
| 1381 |
this._disabled = !this._disabled; |
| 1382 |
this._disableChanged(); |
| 1383 |
} |
| 1384 |
break; |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
activate() { |
| 1389 |
if (!this._active && !this._disabled) { |
| 1390 |
this.classList.add("active"); |
| 1391 |
this._active = true; |
| 1392 |
this._select.ariaExpanded = true; |
| 1393 |
this._search.ariaExpanded = true; |
| 1394 |
this._search.classList.add("active"); |
| 1395 |
this._displayList(true); |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
deactivate() { |
| 1400 |
if (this._active) { |
| 1401 |
this.classList.remove("active"); |
| 1402 |
this._active = false; |
| 1403 |
this._select.ariaExpanded = false; |
| 1404 |
this._search.ariaExpanded = false; |
| 1405 |
if (this._values.size) this._search.classList.remove("active"); |
| 1406 |
this._search.value = ""; |
| 1407 |
this._displayList(false); |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
clear() { |
| 1412 |
this._items = []; |
| 1413 |
if (this._active) this._updateList(); |
| 1414 |
} |
| 1415 |
|
| 1416 |
appendItem(value, text) { |
| 1417 |
this._items.push({ value: value, text: text || ("" + value) }); |
| 1418 |
if (this._active) this._updateList(); |
| 1419 |
} |
| 1420 |
|
| 1421 |
setValues(data) { |
| 1422 |
this._clearResults(); |
| 1423 |
if (data.length) { |
| 1424 |
const items = data.reduce((res, val) => { |
| 1425 |
const item = this._items.find(it => it.value === val); |
| 1426 |
if (item && !this._values.has(item)) res.push(item); |
| 1427 |
return res; |
| 1428 |
}, []); |
| 1429 |
this._updateResult(items); |
| 1430 |
} else if (!this._active) { |
| 1431 |
this._activateSearch(); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
setLabel(str) { |
| 1436 |
this._label = str; |
| 1437 |
if (this._listBox) this._listBox.ariaLabel = str; |
| 1438 |
} |
| 1439 |
|
| 1440 |
getValues() { |
| 1441 |
const res = []; |
| 1442 |
for (const item of this._values) { |
| 1443 |
res.push(item.value); |
| 1444 |
} |
| 1445 |
return res; |
| 1446 |
} |
| 1447 |
|
| 1448 |
isEmpty() { |
| 1449 |
return !this._values.size; |
| 1450 |
} |
| 1451 |
|
| 1452 |
get disabled() { |
| 1453 |
return this._disabled; |
| 1454 |
} |
| 1455 |
|
| 1456 |
set disabled(val) { |
| 1457 |
if (val) { |
| 1458 |
this.setAttribute("disabled", ""); |
| 1459 |
} else { |
| 1460 |
this.removeAttribute("disabled"); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
_makeInputElement() { |
| 1465 |
const inputEl = document.createElement("div"); |
| 1466 |
inputEl.classList.add("multiselect-input"); |
| 1467 |
this._tags = document.createElement("div"); |
| 1468 |
this._tags.ariaHidden = true; |
| 1469 |
this._tags.classList.add("multiselect-tags"); |
| 1470 |
this._makeSearchBar() |
| 1471 |
inputEl.append(this._tags, this._search); |
| 1472 |
return inputEl; |
| 1473 |
} |
| 1474 |
|
| 1475 |
_makeSearchBar() { |
| 1476 |
this._search = document.createElement("input"); |
| 1477 |
this._search.type = "text"; |
| 1478 |
this._search.role = "textbox"; |
| 1479 |
this._search.tabIndex = 0; |
| 1480 |
this._search.ariaHasPopup = "listbox"; |
| 1481 |
this._search.ariaExpanded = false; |
| 1482 |
this._search.disabled = this._disabled; |
| 1483 |
this._search.classList.add("multiselect-search"); |
| 1484 |
this._search.setAttribute("spellcheck", "false"); |
| 1485 |
this._search.placeholder = this.getAttribute("placeholder") || "" |
| 1486 |
this._search.addEventListener("input", event => { |
| 1487 |
this._updateList(); |
| 1488 |
}); |
| 1489 |
this._search.addEventListener("keydown", event => { |
| 1490 |
let idx = this._focused.index; |
| 1491 |
switch (event.code) { |
| 1492 |
case "Down": |
| 1493 |
case "ArrowDown": |
| 1494 |
event.preventDefault(); |
| 1495 |
if (++idx < this._aitems.length) this._focusItem(idx, true); |
| 1496 |
break; |
| 1497 |
case "Up": |
| 1498 |
case "ArrowUp": |
| 1499 |
event.preventDefault(); |
| 1500 |
if (--idx >= 0) this._focusItem(idx, true); |
| 1501 |
break; |
| 1502 |
case "Home": |
| 1503 |
event.preventDefault(); |
| 1504 |
if (idx > 0) this._focusItem(0, true); |
| 1505 |
break; |
| 1506 |
case "End": |
| 1507 |
event.preventDefault(); |
| 1508 |
if (++idx > 0 && idx < this._aitems.length) this._focusItem(this._aitems.length - 1, true); |
| 1509 |
break; |
| 1510 |
case "Enter": |
| 1511 |
case "NumpadEnter": |
| 1512 |
if (this._active) { |
| 1513 |
event.preventDefault(); |
| 1514 |
if (this._focused.item) this._updateResult(this._focused.item); |
| 1515 |
} |
| 1516 |
break; |
| 1517 |
case "Esc": |
| 1518 |
case "Escape": |
| 1519 |
if (this._active) { |
| 1520 |
event.preventDefault(); |
| 1521 |
event.stopPropagation(); |
| 1522 |
this.deactivate(); |
| 1523 |
} |
| 1524 |
break; |
| 1525 |
} |
| 1526 |
}); |
| 1527 |
} |
| 1528 |
|
| 1529 |
_makeSelectButton() { |
| 1530 |
this._select = document.createElement("div"); |
| 1531 |
this._select.role = "button"; |
| 1532 |
this._select.tabIndex = -1; |
| 1533 |
this._select.ariaExpanded = false; |
| 1534 |
this._select.ariaHasPopup = "listbox"; |
| 1535 |
if (this._disabled) this._select.ariaDisabled = true; |
| 1536 |
this._select.classList.add("multiselect-select"); |
| 1537 |
this._select.addEventListener("click", event => { |
| 1538 |
if (this._active) { |
| 1539 |
event.preventDefault(); |
| 1540 |
this.deactivate(); |
| 1541 |
} |
| 1542 |
}); |
| 1543 |
} |
| 1544 |
|
| 1545 |
_makeListbox() { |
| 1546 |
this._listBox = document.createElement("ul"); |
| 1547 |
this._listBox.role = "listbox"; |
| 1548 |
this._listBox.tabIndex = -1; |
| 1549 |
if (this._label) this._listBox.ariaLabel = this._label; |
| 1550 |
this._listBox.ariaMultiSelectable = true; |
| 1551 |
this._listBox.classList.add("multiselect-options", "hidden"); |
| 1552 |
this.append(this._listBox); |
| 1553 |
this._listBox.addEventListener("click", event => { |
| 1554 |
if (event.target.role === "option") { |
| 1555 |
event.preventDefault(); |
| 1556 |
const item = this._aitems.find(item => event.target === item.element); |
| 1557 |
if (item) this._updateResult(item); |
| 1558 |
this.deactivate(); |
| 1559 |
} |
| 1560 |
}); |
| 1561 |
} |
| 1562 |
|
| 1563 |
_displayList(visible) { |
| 1564 |
if (!visible) { |
| 1565 |
this._listBox && this._listBox.classList.add("hidden"); |
| 1566 |
return; |
| 1567 |
} |
| 1568 |
|
| 1569 |
this._listBox.classList.remove("hidden"); |
| 1570 |
this._listBox.scrollTop = 0; |
| 1571 |
this._updateList(); |
| 1572 |
} |
| 1573 |
|
| 1574 |
_updateList() { |
| 1575 |
this._listBox.replaceChildren(); |
| 1576 |
this._aitems = []; |
| 1577 |
|
| 1578 |
let cnt = 0; |
| 1579 |
let txt = this._search.value; |
| 1580 |
this._items.forEach(item => { |
| 1581 |
if (!item.element) item.element = this._makeOption(item.text, this._values.has(item), false); |
| 1582 |
if (item.text.includes(txt)) { |
| 1583 |
this._listBox.appendChild(item.element); |
| 1584 |
this._aitems.push(item); |
| 1585 |
++cnt; |
| 1586 |
} |
| 1587 |
}); |
| 1588 |
|
| 1589 |
if (cnt) { |
| 1590 |
this._focusItem(0); |
| 1591 |
} else { |
| 1592 |
this._focused.index = -1; |
| 1593 |
this._focused.item = null; |
| 1594 |
this._listBox.append(this._makeOption("No items found", null, true)); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
_makeTag(item) { |
| 1599 |
const tb = document.createElement("i"); |
| 1600 |
tb.addEventListener("click", event => { |
| 1601 |
event.preventDefault(); |
| 1602 |
if (!this._disabled) { |
| 1603 |
this._updateResult(item) |
| 1604 |
this._search.focus(); |
| 1605 |
} |
| 1606 |
}); |
| 1607 |
const el = document.createElement("div"); |
| 1608 |
el.classList.add("multiselect-tag"); |
| 1609 |
const sp = el.appendChild(document.createElement("span")); |
| 1610 |
sp.tabIndex = -1; |
| 1611 |
sp.textContent = item.text |
| 1612 |
el.append(tb); |
| 1613 |
item.tag = el; |
| 1614 |
return el; |
| 1615 |
} |
| 1616 |
|
| 1617 |
_makeOption(text, selected, nodata) { |
| 1618 |
const el = document.createElement("li"); |
| 1619 |
el.role = "option"; |
| 1620 |
el.textContent = text; |
| 1621 |
if (nodata) { |
| 1622 |
el.classList.add("nodata"); |
| 1623 |
} else { |
| 1624 |
el.ariaSelected = selected; |
| 1625 |
el.addEventListener("pointerenter", event => this._focusItem(event.target)); |
| 1626 |
} |
| 1627 |
return el; |
| 1628 |
} |
| 1629 |
|
| 1630 |
_activateSearch() { |
| 1631 |
this._search.classList[this._values.size ? "remove" : "add"]("active"); |
| 1632 |
} |
| 1633 |
|
| 1634 |
_focusItem(item, scroll) { |
| 1635 |
let idx = -1; |
| 1636 |
if (typeof(item) === "number") { |
| 1637 |
idx = item; |
| 1638 |
item = this._aitems[idx]; |
| 1639 |
} else { |
| 1640 |
idx = this._aitems.findIndex(it => item === it.element); |
| 1641 |
item = (idx >= 0) ? this._aitems[idx] : null; |
| 1642 |
} |
| 1643 |
this._focused.item && this._focused.item.element.classList.remove("focused"); |
| 1644 |
item.element.classList.add("focused"); |
| 1645 |
this._focused = { index: idx, item: item }; |
| 1646 |
this._search.setAttribute("aria-activedescendant", item.element.getId()); |
| 1647 |
if (scroll) scroll_to_element(item.element, this._listBox); |
| 1648 |
} |
| 1649 |
|
| 1650 |
_clearResults() { |
| 1651 |
this._values.clear(); |
| 1652 |
this._items.forEach(item => { |
| 1653 |
item.tag && item.tag.remove(); |
| 1654 |
if (item.element) item.element.ariaSelected = false; |
| 1655 |
}); |
| 1656 |
this._more && this._more.remove(); |
| 1657 |
this.dispatchEvent(new Event("change")); |
| 1658 |
} |
| 1659 |
|
| 1660 |
_updateResult(items) { |
| 1661 |
const MAX_ITEMS = 3; |
| 1662 |
if (!Array.isArray(items)) items = [ items ]; |
| 1663 |
items.forEach(item => { |
| 1664 |
if (this._values.delete(item)) { |
| 1665 |
if (item.element) item.element.ariaSelected = false; |
| 1666 |
} else { |
| 1667 |
this._values.add(item); |
| 1668 |
if (item.element) item.element.ariaSelected = true; |
| 1669 |
} |
| 1670 |
}); |
| 1671 |
this._tags.replaceChildren(); |
| 1672 |
let cnt = 0; |
| 1673 |
for (const vi of this._values) { |
| 1674 |
this._tags.append(vi.tag || this._makeTag(vi)); |
| 1675 |
if (++cnt >= MAX_ITEMS) break; |
| 1676 |
} |
| 1677 |
if (this._values.size > MAX_ITEMS) { |
| 1678 |
if (!this._more) { |
| 1679 |
this._more = document.createElement("span"); |
| 1680 |
this._more.append("and ", "", " more"); |
| 1681 |
} |
| 1682 |
this._more.childNodes[1].textContent = this._values.size - MAX_ITEMS; |
| 1683 |
this._tags.append(this._more); |
| 1684 |
} |
| 1685 |
if (!this._active) this._activateSearch(); |
| 1686 |
this.dispatchEvent(new Event("change")); |
| 1687 |
} |
| 1688 |
|
| 1689 |
_disableChanged() { |
| 1690 |
if (this._disabled) this.deactivate(); |
| 1691 |
if (this._search) this._search.disabled = this._disabled; |
| 1692 |
if (this._select) this._select.ariaDisabled = this._disabled; |
| 1693 |
} |
| 1694 |
} |
| 1695 |
customElements.define("multi-select", Multiselect); |
| 1696 |
|
| 1697 |
class HintButton { |
| 1698 |
constructor(params) { |
| 1699 |
this._params = params || {}; |
| 1700 |
this._element = null; |
| 1701 |
this._content = null; |
| 1702 |
} |
| 1703 |
|
| 1704 |
element() { |
| 1705 |
if (!this._element) { |
| 1706 |
const el = document.createElement("div"); |
| 1707 |
el.classList.add("hint-block"); |
| 1708 |
const bt = el.appendChild((new ToolbarButton({ content: "info_icon", title: "Details" })).element()); |
| 1709 |
bt.tabIndex = 0; |
| 1710 |
const ct = el.appendChild(document.createElement("div")); |
| 1711 |
ct.tabIndex = -1; |
| 1712 |
ct.classList.add("hint-content", "hidden"); |
| 1713 |
bt.setAriaControls(ct); |
| 1714 |
bt.addEventListener("click", event => { |
| 1715 |
if (!this._content) { |
| 1716 |
switch (typeof(this._params.content)) { |
| 1717 |
case "function": |
| 1718 |
this._content = this._params.content(this._params.data); |
| 1719 |
break; |
| 1720 |
case "object": |
| 1721 |
case "string": |
| 1722 |
this._content = this._params.content; |
| 1723 |
break; |
| 1724 |
} |
| 1725 |
if (this._content) ct.append(this._content); |
| 1726 |
} |
| 1727 |
if (this._content) { |
| 1728 |
ct.classList.remove("hidden"); |
| 1729 |
ct.focus(); |
| 1730 |
} |
| 1731 |
}); |
| 1732 |
ct.addEventListener("focusout", event => { |
| 1733 |
if (!event.relatedTarget || !ct.contains(event.relatedTarget)) { |
| 1734 |
ct.classList.add("hidden"); |
| 1735 |
} |
| 1736 |
}); |
| 1737 |
ct.addEventListener("keydown", event => { |
| 1738 |
switch (event.code) { |
| 1739 |
case "Esc": |
| 1740 |
case "Escape": |
| 1741 |
event.preventDefault(); |
| 1742 |
bt.focus(); |
| 1743 |
break; |
| 1744 |
} |
| 1745 |
}); |
| 1746 |
this._element = el; |
| 1747 |
} |
| 1748 |
return this._element; |
| 1749 |
} |
| 1750 |
|
| 1751 |
reset() { |
| 1752 |
if (this._content) { |
| 1753 |
this._content.remove(); |
| 1754 |
this._content = null; |
| 1755 |
} |
| 1756 |
} |
| 1757 |
} |
| 1758 |
|
| 1759 |
class MenuBar { |
| 1760 |
constructor(element) { |
| 1761 |
this._element = element; |
| 1762 |
this._tbtn = null; |
| 1763 |
this._mbar = null; |
| 1764 |
this._focused = false; |
| 1765 |
} |
| 1766 |
|
| 1767 |
static instance() { |
| 1768 |
if (!this._instance) this._instance = new MenuBar(document.getElementById("main-menu-block")); |
| 1769 |
return this._instance; |
| 1770 |
} |
| 1771 |
|
| 1772 |
init() { |
| 1773 |
this._tbtn = this._element.querySelector(".toggle-button"); |
| 1774 |
this._tggl = this._element.querySelector('#main-menu-toggle'); |
| 1775 |
this._mbar = this._element.querySelector('ul[role="menubar"]'); |
| 1776 |
this._updateMenu(); |
| 1777 |
|
| 1778 |
function delayedFocus() { |
| 1779 |
this._mbar.addEventListener("transitionend", event => this.focus(), { once: true }); |
| 1780 |
} |
| 1781 |
|
| 1782 |
this._element.addEventListener("focusin", event => { |
| 1783 |
this._focused = true; |
| 1784 |
}); |
| 1785 |
this._element.addEventListener("focusout", event => { |
| 1786 |
this._focused = false; |
| 1787 |
setTimeout(() => { |
| 1788 |
if (!this._focused) this._tggl.checked = false; |
| 1789 |
}, 0); |
| 1790 |
}); |
| 1791 |
this._tbtn.addEventListener("keydown", event => { |
| 1792 |
switch (event.key) { |
| 1793 |
case " ": |
| 1794 |
case "Enter": |
| 1795 |
this._tggl.checked = !this._tggl.checked; |
| 1796 |
if (this._tggl.checked) delayedFocus.call(this); |
| 1797 |
break; |
| 1798 |
} |
| 1799 |
}); |
| 1800 |
this._tbtn.addEventListener("click", delayedFocus.bind(this)); |
| 1801 |
this._mbar.addEventListener("click", event => { |
| 1802 |
const target = event.target; |
| 1803 |
switch (target.role) { |
| 1804 |
case "menuitem": |
| 1805 |
if (target.ariaHasPopup === "true") { |
| 1806 |
this._toggleMenu(target); |
| 1807 |
this._focusItem(target); |
| 1808 |
} else { |
| 1809 |
this._tggl.checked = false; |
| 1810 |
} |
| 1811 |
break; |
| 1812 |
} |
| 1813 |
}); |
| 1814 |
this._mbar.addEventListener("keydown", this._onKeydown.bind(this)); |
| 1815 |
return this; |
| 1816 |
} |
| 1817 |
|
| 1818 |
element(selector) { |
| 1819 |
if (typeof(selector) !== "string") return this._element; |
| 1820 |
return this._element.querySelector(selector); |
| 1821 |
} |
| 1822 |
|
| 1823 |
focus() { |
| 1824 |
const that = this; |
| 1825 |
function findOldFocus(ul) { |
| 1826 |
for (const li of ul.children) { |
| 1827 |
if (window.getComputedStyle(li).display !== "none" && !li.classList.contains("disabled")) { |
| 1828 |
let item = that._getMenuItem(li); |
| 1829 |
if (item.tabIndex === 0) return item; |
| 1830 |
if (item.ariaHasPopup) { |
| 1831 |
item = findOldFocus(li.children[1]); |
| 1832 |
if (item) return item; |
| 1833 |
} |
| 1834 |
} |
| 1835 |
} |
| 1836 |
} |
| 1837 |
let m = findOldFocus(this._mbar); |
| 1838 |
if (!m) m = this._getFirstMenuItem(); |
| 1839 |
this._focusItem(m); |
| 1840 |
} |
| 1841 |
|
| 1842 |
updateCurrent() { |
| 1843 |
this._mbar.querySelectorAll('[role="menuitem"][aria-current]').forEach(el => { |
| 1844 |
el.ariaCurrent = null; |
| 1845 |
}); |
| 1846 |
if (!this._focused) { |
| 1847 |
this._mbar.querySelectorAll('[role="menuitem"][aria-expanded="true"]').forEach(el => { |
| 1848 |
el.ariaExpanded = false; |
| 1849 |
}); |
| 1850 |
} |
| 1851 |
|
| 1852 |
const url = new URL(document.location); |
| 1853 |
url.search = ""; |
| 1854 |
const href = url.toString(); |
| 1855 |
for (const el of this._mbar.querySelectorAll('a[role="menuitem"]')) { |
| 1856 |
if (el.href === href) { |
| 1857 |
el.ariaCurrent = "page"; |
| 1858 |
const pi = this._getMenuItem(el.parentNode.parentNode.parentNode); |
| 1859 |
if (pi) pi.ariaExpanded = true; |
| 1860 |
} |
| 1861 |
} |
| 1862 |
this._mbar.tabIndex = -1; |
| 1863 |
} |
| 1864 |
|
| 1865 |
insertItem(title, href, position) { |
| 1866 |
const li = document.createElement("li"); |
| 1867 |
li.role = "none"; |
| 1868 |
const ae = li.appendChild(document.createElement("a")); |
| 1869 |
ae.role = "menuitem"; |
| 1870 |
ae.tabIndex = -1; |
| 1871 |
ae.href = href; |
| 1872 |
ae.textContent = title; |
| 1873 |
if (position < 0 || position >= this._mbar.children.length) { |
| 1874 |
this._mbar.append(li); |
| 1875 |
} else { |
| 1876 |
this._mbar.insertBefore(li, this._mbar.children[position]); |
| 1877 |
} |
| 1878 |
return li; |
| 1879 |
} |
| 1880 |
|
| 1881 |
_focusItem(item) { |
| 1882 |
if (item && item.role !== "menuitem") item = this._getMenuItem(item); |
| 1883 |
if (!item) return; |
| 1884 |
|
| 1885 |
this._mbar.querySelectorAll('[role="menuitem"][tabindex="0"]').forEach(el => { |
| 1886 |
if (el !== item) el.tabIndex = -1; |
| 1887 |
}); |
| 1888 |
item.tabIndex = 0; |
| 1889 |
item.focus(); |
| 1890 |
} |
| 1891 |
|
| 1892 |
_focusNextItem(cItem) { |
| 1893 |
this._focusItem(this._getNextMenuItem(cItem.parentNode)); |
| 1894 |
} |
| 1895 |
|
| 1896 |
_focusPreviousItem(cItem) { |
| 1897 |
this._focusItem(this._getPreviousMenuItem(cItem.parentNode)); |
| 1898 |
} |
| 1899 |
|
| 1900 |
_getMenuItem(li) { |
| 1901 |
return li.querySelector('[role="menuitem"]'); |
| 1902 |
} |
| 1903 |
|
| 1904 |
_isItemAvailable(li) { |
| 1905 |
return (window.getComputedStyle(li).display !== "none" && !li.classList.contains("disabled")); |
| 1906 |
} |
| 1907 |
|
| 1908 |
_getFirstMenuItem() { |
| 1909 |
for (const li of this._mbar.children) { |
| 1910 |
if (this._isItemAvailable(li)) return li; |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
_getLastMenuItem() { |
| 1915 |
let li = this._mbar.lastElementChild; |
| 1916 |
while (li) { |
| 1917 |
if (!this._isItemAvailable(li)) return this._getPreviousMenuItem(li); |
| 1918 |
if (this._getMenuItem(li).ariaExpanded !== "true") return li; |
| 1919 |
li = li.children[1].lastElementChild; |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
_getNextMenuItem(li) { |
| 1924 |
const ci = this._getMenuItem(li); |
| 1925 |
if (ci.ariaExpanded === "true") { |
| 1926 |
const first = ci.nextElementSibling.firstElementChild; |
| 1927 |
if (this._isItemAvailable(first)) return first; |
| 1928 |
return this._getNextMenuItem(first); |
| 1929 |
} |
| 1930 |
|
| 1931 |
while (true) { |
| 1932 |
let next = li.nextElementSibling; |
| 1933 |
while (next) { |
| 1934 |
if (this._isItemAvailable(next)) return next; |
| 1935 |
next = next.nextElementSibling; |
| 1936 |
} |
| 1937 |
|
| 1938 |
if (li.parentNode === this._mbar) return this._getFirstMenuItem(); |
| 1939 |
li = li.parentNode.parentNode; |
| 1940 |
} |
| 1941 |
} |
| 1942 |
|
| 1943 |
_getPreviousMenuItem(li) { |
| 1944 |
let prev = li.previousElementSibling; |
| 1945 |
while (prev) { |
| 1946 |
if (!this._isItemAvailable(prev)) return this._getPreviousMenuItem(prev); |
| 1947 |
if (this._getMenuItem(prev).ariaExpanded !== "true") return prev; |
| 1948 |
prev = prev.children[1].lastElementChild; |
| 1949 |
} |
| 1950 |
if (li.parentNode === this._mbar) return this._getLastMenuItem(); |
| 1951 |
return li.parentNode.parentNode; |
| 1952 |
} |
| 1953 |
|
| 1954 |
_updateMenu() { |
| 1955 |
this._mbar.querySelectorAll('[role="menuitem"], [role="menu"]').forEach(el => { |
| 1956 |
el.tabIndex = -1; |
| 1957 |
}); |
| 1958 |
} |
| 1959 |
|
| 1960 |
_toggleMenu(item) { |
| 1961 |
item.ariaExpanded = (item.ariaExpanded !== "true"); |
| 1962 |
} |
| 1963 |
|
| 1964 |
_onKeydown(event) { |
| 1965 |
const tg = event.target; |
| 1966 |
switch (event.key) { |
| 1967 |
case " ": |
| 1968 |
case "Enter": |
| 1969 |
if (tg.ariaHasPopup === "true") this._toggleMenu(tg); |
| 1970 |
break; |
| 1971 |
case "Esc": |
| 1972 |
case "Escape": |
| 1973 |
this._tggl.checked = false; |
| 1974 |
this._tbtn.focus(); |
| 1975 |
break; |
| 1976 |
case "Up": |
| 1977 |
case "ArrowUp": |
| 1978 |
this._focusPreviousItem(tg); |
| 1979 |
break; |
| 1980 |
case "Down": |
| 1981 |
case "ArrowDown": |
| 1982 |
this._focusNextItem(tg); |
| 1983 |
break; |
| 1984 |
case "Home": |
| 1985 |
this._focusItem(this._getFirstMenuItem()); |
| 1986 |
break; |
| 1987 |
case "End": |
| 1988 |
this._focusItem(this._getLastMenuItem()); |
| 1989 |
break; |
| 1990 |
} |
| 1991 |
} |
| 1992 |
} |