|
5 | 5 | * This file is copyright under the latest version of the EUPL. |
6 | 6 | * Please see LICENSE file for your rights under this license. */ |
7 | 7 |
|
8 | | -/* global moment:false, apiFailure: false, updateFtlInfo: false, NProgress:false, WaitMe:false, TomSelect: false */ |
| 8 | +/* global moment:false, apiFailure: false, updateFtlInfo: false, NProgress:false, WaitMe:false */ |
9 | 9 |
|
10 | 10 | "use strict"; |
11 | 11 |
|
@@ -358,49 +358,140 @@ function validateHostnameStrict(name) { |
358 | 358 | } |
359 | 359 |
|
360 | 360 | /** |
361 | | - * Create a Tom Select multi-select out of a <select multiple> element, with |
362 | | - * "Select all"/"Select none" actions injected into the dropdown (bootstrap- |
363 | | - * select used to provide this via its actionsBox option). |
| 361 | + * Create a bootstrap-multiselect out of a <select multiple> element, with |
| 362 | + * "Select all"/"Select none" actions injected above the options. |
364 | 363 | * @param {HTMLElement|jQuery} selectEl - The <select multiple> element (or a jQuery wrapper around it) |
365 | | - * @param {object} [options] - Extra options merged into the Tom Select config |
366 | | - * @returns {TomSelect} The created Tom Select instance |
| 364 | + * @param {object} [options] - Extra options merged into the bootstrap-multiselect config |
| 365 | + * @returns {object} A small wrapper exposing compatibility helpers used by callers |
367 | 366 | */ |
368 | 367 | function createGroupSelect(selectEl, options = {}) { |
369 | | - const el = selectEl instanceof HTMLElement ? selectEl : selectEl[0]; |
370 | | - const allValues = [...el.options].map(option => option.value); |
371 | | - |
372 | | - const ts = new TomSelect(el, { |
373 | | - plugins: ["remove_button"], |
374 | | - create: false, |
375 | | - placeholder: "none selected", |
376 | | - // Tom Select keeps the placeholder visible on multi-selects by default; |
377 | | - // hide it once at least one group is selected so the "none selected" |
378 | | - // hint does not sit below the selected item chips. |
379 | | - hidePlaceholder: true, |
380 | | - // Render the dropdown into <body> instead of nesting it in the table |
381 | | - // cell, since ancestors like .table-responsive/.card clip overflow and |
382 | | - // would otherwise cut it off (bootstrap-select used container: "body" |
383 | | - // for the same reason). |
384 | | - dropdownParent: "body", |
385 | | - ...options, |
| 368 | + const select = selectEl instanceof HTMLElement ? $(selectEl) : selectEl; |
| 369 | + const allValues = select.find("option").map((_, option) => option.value).get(); |
| 370 | + const { onChange, onDropdownClose, onInitialized, ...multiselectOptions } = options; |
| 371 | + |
| 372 | + select.multiselect({ |
| 373 | + nonSelectedText: "none selected", |
| 374 | + buttonContainer: '<div class="btn-group w-100" />', |
| 375 | + buttonWidth: "100%", |
| 376 | + onChange(option, checked) { |
| 377 | + if (typeof onChange === "function") { |
| 378 | + onChange(option, checked); |
| 379 | + } |
| 380 | + }, |
| 381 | + onDropdownHidden(event) { |
| 382 | + if (typeof onDropdownClose === "function") { |
| 383 | + onDropdownClose(event); |
| 384 | + } |
| 385 | + }, |
| 386 | + onInitialized(initializedSelect, initializedContainer) { |
| 387 | + if (typeof onInitialized === "function") { |
| 388 | + onInitialized(initializedSelect, initializedContainer); |
| 389 | + } |
| 390 | + }, |
| 391 | + ...multiselectOptions, |
386 | 392 | }); |
387 | 393 |
|
388 | | - const actionsBox = document.createElement("div"); |
389 | | - actionsBox.className = "ts-actions-box"; |
390 | | - actionsBox.innerHTML = |
391 | | - '<div class="btn-group btn-group-sm">' + |
392 | | - '<button type="button" class="btn btn-secondary btn-sm select-all">All</button>' + |
393 | | - '<button type="button" class="btn btn-secondary btn-sm select-none">None</button>' + |
394 | | - "</div>"; |
395 | | - actionsBox.querySelector(".select-all").addEventListener("click", () => { |
396 | | - ts.setValue(allValues); |
| 394 | + const multiselect = select.data("multiselect"); |
| 395 | + const container = multiselect ? multiselect.$container : $(); |
| 396 | + const button = multiselect ? multiselect.$button : $(); |
| 397 | + const dropdown = multiselect ? multiselect.$popupContainer : $(); |
| 398 | + const dropdownParent = dropdown.parent(); |
| 399 | + const updateDropdownPosition = () => { |
| 400 | + if (button.length === 0 || dropdown.length === 0) { |
| 401 | + return; |
| 402 | + } |
| 403 | + |
| 404 | + const rect = button[0].getBoundingClientRect(); |
| 405 | + dropdown[0].style.setProperty("--multiselect-trigger-left", `${rect.left}px`); |
| 406 | + dropdown[0].style.setProperty("--multiselect-trigger-bottom", `${rect.bottom}px`); |
| 407 | + dropdown[0].style.setProperty("--multiselect-trigger-width", `${rect.width}px`); |
| 408 | + }; |
| 409 | + |
| 410 | + const cleanupDropdown = () => { |
| 411 | + $(window).off("resize", updateDropdownPosition).off("scroll", updateDropdownPosition); |
| 412 | + |
| 413 | + if (dropdown.length === 0 || dropdownParent.length === 0) { |
| 414 | + return; |
| 415 | + } |
| 416 | + |
| 417 | + dropdown.removeClass("multiselect-floating-menu show"); |
| 418 | + dropdown[0].style.removeProperty("--multiselect-trigger-left"); |
| 419 | + dropdown[0].style.removeProperty("--multiselect-trigger-bottom"); |
| 420 | + dropdown[0].style.removeProperty("--multiselect-trigger-width"); |
| 421 | + dropdown.appendTo(dropdownParent); |
| 422 | + }; |
| 423 | + |
| 424 | + container |
| 425 | + .on("shown.bs.dropdown.multiselectPortal", () => { |
| 426 | + if (dropdown.length === 0 || dropdownParent.length === 0) { |
| 427 | + return; |
| 428 | + } |
| 429 | + |
| 430 | + dropdown.detach().appendTo(document.body); |
| 431 | + dropdown.addClass("multiselect-floating-menu"); |
| 432 | + updateDropdownPosition(); |
| 433 | + $(window) |
| 434 | + .on("resize", updateDropdownPosition) |
| 435 | + .on("scroll", updateDropdownPosition); |
| 436 | + }) |
| 437 | + .on("hidden.bs.dropdown.multiselectPortal", () => { |
| 438 | + cleanupDropdown(); |
| 439 | + }); |
| 440 | + |
| 441 | + const actionsItem = $( |
| 442 | + '<div class="multiselect-actions">' + |
| 443 | + '<div class="multiselect-actions-box">' + |
| 444 | + '<div class="btn-group btn-group-sm">' + |
| 445 | + '<button type="button" class="btn btn-secondary btn-sm select-all">All</button>' + |
| 446 | + '<button type="button" class="btn btn-secondary btn-sm select-none">None</button>' + |
| 447 | + "</div>" + |
| 448 | + "</div>" + |
| 449 | + "</div>" |
| 450 | + ); |
| 451 | + |
| 452 | + actionsItem.find(".select-all").on("click", event => { |
| 453 | + event.preventDefault(); |
| 454 | + select.multiselect("select", allValues, true); |
397 | 455 | }); |
398 | | - actionsBox.querySelector(".select-none").addEventListener("click", () => { |
399 | | - ts.clear(); |
| 456 | + |
| 457 | + actionsItem.find(".select-none").on("click", event => { |
| 458 | + event.preventDefault(); |
| 459 | + const selectedValues = select.find("option:selected").map((_, option) => option.value).get(); |
| 460 | + select.multiselect("deselect", selectedValues, true); |
400 | 461 | }); |
401 | | - ts.dropdown.prepend(actionsBox); |
402 | 462 |
|
403 | | - return ts; |
| 463 | + dropdown.prepend(actionsItem); |
| 464 | + |
| 465 | + return { |
| 466 | + setValue(values) { |
| 467 | + const selectedValues = select.find("option:selected").map((_, option) => option.value).get(); |
| 468 | + |
| 469 | + if (selectedValues.length > 0) { |
| 470 | + select.multiselect("deselect", selectedValues); |
| 471 | + } |
| 472 | + |
| 473 | + if (Array.isArray(values) && values.length > 0) { |
| 474 | + select.multiselect("select", values); |
| 475 | + } |
| 476 | + |
| 477 | + select.multiselect("updateButtonText"); |
| 478 | + }, |
| 479 | + clear() { |
| 480 | + const selectedValues = select.find("option:selected").map((_, option) => option.value).get(); |
| 481 | + |
| 482 | + if (selectedValues.length > 0) { |
| 483 | + select.multiselect("deselect", selectedValues); |
| 484 | + } |
| 485 | + |
| 486 | + select.multiselect("updateButtonText"); |
| 487 | + }, |
| 488 | + close() { |
| 489 | + button.removeClass("show").attr("aria-expanded", "false"); |
| 490 | + container.removeClass("show"); |
| 491 | + cleanupDropdown(); |
| 492 | + }, |
| 493 | + dropdown, |
| 494 | + }; |
404 | 495 | } |
405 | 496 |
|
406 | 497 | const backupStorage = {}; |
|
0 commit comments