/*
* Intechs Computers - typed Min/Max price boxes for the Wix Stores price slider.
* Keeps the slider. Adds two number boxes + Apply button that stay in sync with it.
* Add ?icdebug=1 to the page URL to see diagnostic messages in the browser console.
*/
(function () {
if (window.__icPriceInputs) return;
window.__icPriceInputs = true;
var DEBUG = location.search.indexOf('icdebug') > -1;
function log() {
if (DEBUG) console.log.apply(console, ['[price-inputs]'].concat([].slice.call(arguments)));
}
var WRAP_ID = 'ic-price-inputs';
// ---------- styles ----------
var css = document.createElement('style');
css.textContent =
'#' + WRAP_ID + '{display:flex;flex-wrap:wrap;gap:8px;align-items:flex-end;margin:0 0 14px;font-family:inherit}' +
'#' + WRAP_ID + ' label{display:flex;flex-direction:column;font-size:12px;gap:4px;flex:1 1 90px;min-width:80px}' +
'#' + WRAP_ID + ' input{width:100%;box-sizing:border-box;padding:8px;border:1px solid currentColor;border-radius:4px;background:transparent;color:inherit;font:inherit;font-size:14px}' +
'#' + WRAP_ID + ' button{padding:9px 14px;border:0;border-radius:4px;cursor:pointer;font:inherit;font-size:14px;background:#1fa94b;color:#fff}';
document.head.appendChild(css);
// ---------- find the slider ----------
function findHandles() {
var aria = [].slice.call(document.querySelectorAll('[role="slider"]'));
if (aria.length >= 2) return { kind: 'aria', lo: aria[0], hi: aria[1] };
var rng = [].slice.call(document.querySelectorAll('input[type="range"]'));
if (rng.length >= 2) return { kind: 'native', lo: rng[0], hi: rng[1] };
return null;
}
function num(v) { v = parseFloat(v); return isNaN(v) ? null : v; }
function limits(s) {
var a = s.kind === 'native' ? s.lo.min : s.lo.getAttribute('aria-valuemin');
var b = s.kind === 'native' ? s.hi.max : s.hi.getAttribute('aria-valuemax');
return { min: num(a), max: num(b) };
}
function getVal(s, h) {
return s.kind === 'native' ? num(h.value) : num(h.getAttribute('aria-valuenow'));
}
function commonAncestor(a, b) {
var n = a;
while (n && !n.contains(b)) n = n.parentElement;
return n;
}
// ---------- move a handle to a value ----------
function fire(el, type, x, y, Ctor, extra) {
var o = { bubbles: true, cancelable: true, clientX: x, clientY: y, button: 0, buttons: type.match(/up$/) ? 0 : 1, view: window };
for (var k in extra || {}) o[k] = extra[k];
el.dispatchEvent(new Ctor(type, o));
}
function setNative(h, v) {
var setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
setter.call(h, String(v));
h.dispatchEvent(new Event('input', { bubbles: true }));
h.dispatchEvent(new Event('change', { bubbles: true }));
}
function setAria(s, h, v) {
var L = limits(s);
if (L.min == null || L.max == null || L.max === L.min) return;
var track = commonAncestor(s.lo, s.hi);
if (!track) return;
var r = track.getBoundingClientRect();
var hr = h.getBoundingClientRect();
var cx = hr.left + hr.width / 2, cy = hr.top + hr.height / 2;
var tx = r.left + ((v - L.min) / (L.max - L.min)) * r.width;
var P = window.PointerEvent || MouseEvent;
var pe = { pointerId: 1, isPrimary: true, pointerType: 'mouse' };
fire(h, 'pointerdown', cx, cy, P, pe);
fire(h, 'mousedown', cx, cy, MouseEvent);
fire(document, 'pointermove', tx, cy, P, pe);
fire(document, 'mousemove', tx, cy, MouseEvent);
fire(document, 'pointerup', tx, cy, P, pe);
fire(document, 'mouseup', tx, cy, MouseEvent);
// fine-tune with arrow keys in case the drag landed near, not exactly on, the value
var guard = 0;
while (guard++ < 40) {
var cur = getVal(s, h);
if (cur == null || cur === v) break;
var key = cur < v ? 'ArrowRight' : 'ArrowLeft';
var before = cur;
h.dispatchEvent(new KeyboardEvent('keydown', { key: key, bubbles: true }));
h.dispatchEvent(new KeyboardEvent('keyup', { key: key, bubbles: true }));
var after = getVal(s, h);
if (after === before) break; // handle ignores keys, stop
if ((before < v && after > v) || (before > v && after < v)) break; // overshoot
}
}
function setHandle(s, h, v) {
if (s.kind === 'native') setNative(h, v); else setAria(s, h, v);
}
// ---------- build the boxes ----------
var dirty = false;
function build(s) {
var L = limits(s);
var wrap = document.createElement('div');
wrap.id = WRAP_ID;
wrap.innerHTML =
'