| 1 |
/** |
| 2 |
* WPSubscription admin UI components. |
| 3 |
* |
| 4 |
* WPSubsAdvSelect — styled dropdown replacing native <select>. |
| 5 |
* |
| 6 |
* Usage: |
| 7 |
* PHP: wpsubs_render_adv_select( $args ) — renders the HTML |
| 8 |
* JS: WPSubsAdvSelect.init() — auto-inits all .wpsubs-adv-select elements |
| 9 |
* |
| 10 |
* Events fired on the root element (bubbles): |
| 11 |
* wpsubs:select — { value, label } when user picks an item |
| 12 |
*/ |
| 13 |
(function () { |
| 14 |
"use strict"; |
| 15 |
|
| 16 |
var instances = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* @param {HTMLElement} el Root .wpsubs-adv-select element. |
| 20 |
*/ |
| 21 |
function WPSubsAdvSelect(el) { |
| 22 |
this.el = el; |
| 23 |
this.trigger = el.querySelector(".wpsubs-adv-select__trigger"); |
| 24 |
this.label = el.querySelector(".wpsubs-adv-select__label"); |
| 25 |
this.menu = el.querySelector(".wpsubs-adv-select__menu"); |
| 26 |
this.input = el.querySelector('input[type="hidden"]'); |
| 27 |
this._bind(); |
| 28 |
instances.push(this); |
| 29 |
} |
| 30 |
|
| 31 |
WPSubsAdvSelect.prototype.open = function () { |
| 32 |
closeAll(this); |
| 33 |
this.el.classList.add("wpsubs-adv-select--open"); |
| 34 |
if (this.trigger) this.trigger.setAttribute("aria-expanded", "true"); |
| 35 |
}; |
| 36 |
|
| 37 |
WPSubsAdvSelect.prototype.close = function () { |
| 38 |
this.el.classList.remove("wpsubs-adv-select--open"); |
| 39 |
if (this.trigger) this.trigger.setAttribute("aria-expanded", "false"); |
| 40 |
}; |
| 41 |
|
| 42 |
WPSubsAdvSelect.prototype.isOpen = function () { |
| 43 |
return this.el.classList.contains("wpsubs-adv-select--open"); |
| 44 |
}; |
| 45 |
|
| 46 |
/** |
| 47 |
* Programmatically select a value. |
| 48 |
* |
| 49 |
* @param {string} value |
| 50 |
* @param {string} label Display text shown in trigger. Defaults to value. |
| 51 |
*/ |
| 52 |
WPSubsAdvSelect.prototype.select = function (value, label) { |
| 53 |
if (this.input) this.input.value = value; |
| 54 |
if (this.label) this.label.textContent = label || value; |
| 55 |
this.el.dispatchEvent( |
| 56 |
new CustomEvent("wpsubs:select", { |
| 57 |
bubbles: true, |
| 58 |
detail: { value: value, label: label || value }, |
| 59 |
}), |
| 60 |
); |
| 61 |
}; |
| 62 |
|
| 63 |
/** Reset trigger label back to placeholder. */ |
| 64 |
WPSubsAdvSelect.prototype.reset = function () { |
| 65 |
var placeholder = this.el.dataset.placeholder || ""; |
| 66 |
if (this.input) this.input.value = this.el.dataset.defaultValue || ""; |
| 67 |
if (this.label && placeholder) this.label.textContent = placeholder; |
| 68 |
}; |
| 69 |
|
| 70 |
WPSubsAdvSelect.prototype._bind = function () { |
| 71 |
var self = this; |
| 72 |
|
| 73 |
if (self.trigger) { |
| 74 |
self.trigger.addEventListener("click", function (e) { |
| 75 |
e.stopPropagation(); |
| 76 |
self.isOpen() ? self.close() : self.open(); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
if (self.menu) { |
| 81 |
self.menu.addEventListener("click", function (e) { |
| 82 |
var item = e.target.closest(".wpsubs-adv-select__item"); |
| 83 |
if (!item || item.hasAttribute("data-disabled")) return; |
| 84 |
|
| 85 |
var value = item.dataset.value !== undefined ? item.dataset.value : ""; |
| 86 |
var labelEl = item.querySelector(".wpsubs-adv-select__item-label"); |
| 87 |
var label = labelEl ? labelEl.textContent.trim() : item.textContent.trim(); |
| 88 |
var confirmMsg = item.dataset.confirm || ""; |
| 89 |
|
| 90 |
if (confirmMsg && !window.confirm(confirmMsg)) return; |
| 91 |
|
| 92 |
self.close(); |
| 93 |
self.select(value, label); |
| 94 |
}); |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
function closeAll(except) { |
| 99 |
instances.forEach(function (inst) { |
| 100 |
if (inst !== except) inst.close(); |
| 101 |
}); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Initialise all un-initialised .wpsubs-adv-select elements under root. |
| 106 |
* |
| 107 |
* @param {Document|HTMLElement} [root] |
| 108 |
*/ |
| 109 |
function init(root) { |
| 110 |
(root || document).querySelectorAll(".wpsubs-adv-select:not([data-adv-init])").forEach(function (el) { |
| 111 |
el.setAttribute("data-adv-init", "1"); |
| 112 |
new WPSubsAdvSelect(el); |
| 113 |
}); |
| 114 |
} |
| 115 |
|
| 116 |
// Global: outside click and Escape close all |
| 117 |
document.addEventListener("click", function () { |
| 118 |
closeAll(); |
| 119 |
}); |
| 120 |
document.addEventListener("keydown", function (e) { |
| 121 |
if (e.key === "Escape") closeAll(); |
| 122 |
}); |
| 123 |
|
| 124 |
// Auto-init on DOM ready |
| 125 |
if (document.readyState === "loading") { |
| 126 |
document.addEventListener("DOMContentLoaded", function () { |
| 127 |
init(); |
| 128 |
}); |
| 129 |
} else { |
| 130 |
init(); |
| 131 |
} |
| 132 |
|
| 133 |
// Public API |
| 134 |
window.WPSubsAdvSelect = { init: init }; |
| 135 |
})(); |
| 136 |
|