2022-12-02 22:13:13 -06:00
|
|
|
/**
|
|
|
|
* This is a function that makes an HTMLElement draggable.
|
|
|
|
*
|
|
|
|
* The element must contain at least one child element with the class
|
|
|
|
* 'draggable', which will make it the handle for dragging the element
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element Element to make Draggable
|
|
|
|
*/
|
2022-11-22 16:24:55 -06:00
|
|
|
function makeDraggable(element) {
|
2022-11-21 05:24:35 -06:00
|
|
|
let dragging = false;
|
|
|
|
let offset = {x: 0, y: 0};
|
|
|
|
|
2022-11-30 15:44:51 -06:00
|
|
|
const margin = 10;
|
|
|
|
|
2022-12-02 22:13:13 -06:00
|
|
|
// Keeps the draggable element inside the window
|
2022-11-30 15:44:51 -06:00
|
|
|
const fixPos = () => {
|
|
|
|
const dbb = element.getBoundingClientRect();
|
|
|
|
if (dbb.left < margin) element.style.left = margin + "px";
|
|
|
|
else if (dbb.right > window.innerWidth - margin)
|
|
|
|
element.style.left =
|
|
|
|
dbb.left + (window.innerWidth - margin - dbb.right) + "px";
|
|
|
|
|
|
|
|
if (dbb.top < margin) element.style.top = margin + "px";
|
|
|
|
else if (dbb.bottom > window.innerHeight - margin)
|
|
|
|
element.style.top =
|
|
|
|
dbb.top + (window.innerHeight - margin - dbb.bottom) + "px";
|
|
|
|
};
|
2022-11-21 05:24:35 -06:00
|
|
|
|
2022-12-02 22:13:13 -06:00
|
|
|
// Detects the start of the mouse dragging event
|
2022-11-28 16:48:42 -06:00
|
|
|
mouse.listen.window.btn.left.onpaintstart.on((evn) => {
|
2022-11-21 05:24:35 -06:00
|
|
|
if (
|
|
|
|
element.contains(evn.target) &&
|
|
|
|
evn.target.classList.contains("draggable")
|
|
|
|
) {
|
|
|
|
const bb = element.getBoundingClientRect();
|
|
|
|
offset.x = evn.x - bb.x;
|
|
|
|
offset.y = evn.y - bb.y;
|
|
|
|
dragging = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-02 22:13:13 -06:00
|
|
|
// Runs when mouse moves
|
2022-11-28 16:48:42 -06:00
|
|
|
mouse.listen.window.btn.left.onpaint.on((evn) => {
|
2022-11-21 05:24:35 -06:00
|
|
|
if (dragging) {
|
2022-11-30 15:44:51 -06:00
|
|
|
element.style.right = null;
|
|
|
|
element.style.bottom = null;
|
2022-11-21 05:24:35 -06:00
|
|
|
element.style.top = evn.y - offset.y + "px";
|
|
|
|
element.style.left = evn.x - offset.x + "px";
|
2022-11-30 15:44:51 -06:00
|
|
|
|
|
|
|
fixPos();
|
2022-11-21 05:24:35 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-12-02 22:13:13 -06:00
|
|
|
// Stops dragging the element
|
2022-11-28 16:48:42 -06:00
|
|
|
mouse.listen.window.btn.left.onpaintend.on((evn) => {
|
2022-11-21 05:24:35 -06:00
|
|
|
dragging = false;
|
|
|
|
});
|
2022-11-30 15:44:51 -06:00
|
|
|
|
2022-12-02 22:13:13 -06:00
|
|
|
// Redraw after window resize
|
2022-11-30 15:44:51 -06:00
|
|
|
window.addEventListener("resize", () => {
|
|
|
|
fixPos();
|
|
|
|
});
|
2022-11-21 05:24:35 -06:00
|
|
|
}
|
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
/**
|
2022-12-02 22:13:13 -06:00
|
|
|
* Creates a custom slider element from a given div element
|
|
|
|
*
|
|
|
|
* @param {string} name The display name of the sliders
|
|
|
|
* @param {HTMLElement} wrapper The element to transform into a slider
|
|
|
|
* @param {object} options Extra options
|
|
|
|
* @param {number} options.min The minimum value of the slider
|
|
|
|
* @param {number} options.max The maximum value of the slider
|
|
|
|
* @param {number} options.step The step size for the slider
|
|
|
|
* @param {number} option.defaultValue The default value of the slider
|
2022-12-03 04:50:23 -06:00
|
|
|
* @param {number} [options.textStep=step] The step size for the slider text and setvalue \
|
|
|
|
* (usually finer, and an integer divisor of step size)
|
2022-12-28 23:39:32 -06:00
|
|
|
* @returns {{value: number, onchange: Observer<{value: number}>}} A reference to the value of the slider
|
2022-11-23 22:53:14 -06:00
|
|
|
*/
|
|
|
|
function createSlider(name, wrapper, options = {}) {
|
|
|
|
defaultOpt(options, {
|
|
|
|
valuecb: null,
|
|
|
|
min: 0,
|
|
|
|
max: 1,
|
|
|
|
step: 0.1,
|
|
|
|
defaultValue: 0.7,
|
2022-12-03 04:50:23 -06:00
|
|
|
textStep: null,
|
2022-11-23 22:53:14 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
let value = options.defaultValue;
|
|
|
|
|
|
|
|
// Use phantom range element for rounding
|
|
|
|
const phantomRange = document.createElement("input");
|
|
|
|
phantomRange.type = "range";
|
|
|
|
phantomRange.min = options.min;
|
|
|
|
phantomRange.max = options.max;
|
|
|
|
phantomRange.step = options.step;
|
|
|
|
|
2022-12-03 04:50:23 -06:00
|
|
|
let phantomTextRange = phantomRange;
|
|
|
|
if (options.textStep) {
|
|
|
|
phantomTextRange = document.createElement("input");
|
|
|
|
phantomTextRange.type = "range";
|
|
|
|
phantomTextRange.min = options.min;
|
|
|
|
phantomTextRange.max = options.max;
|
|
|
|
phantomTextRange.step = options.textStep;
|
|
|
|
}
|
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
// Build slider element
|
|
|
|
const underEl = document.createElement("div");
|
|
|
|
underEl.classList.add("under");
|
|
|
|
const textEl = document.createElement("input");
|
|
|
|
textEl.type = "text";
|
|
|
|
textEl.classList.add("text");
|
|
|
|
|
|
|
|
const overEl = document.createElement("div");
|
|
|
|
overEl.classList.add("over");
|
|
|
|
|
|
|
|
wrapper.classList.add("slider-wrapper");
|
|
|
|
wrapper.appendChild(underEl);
|
|
|
|
wrapper.appendChild(textEl);
|
|
|
|
wrapper.appendChild(overEl);
|
|
|
|
|
|
|
|
const bar = document.createElement("div");
|
|
|
|
bar.classList.add("slider-bar");
|
|
|
|
underEl.appendChild(bar);
|
|
|
|
underEl.appendChild(document.createElement("div"));
|
|
|
|
|
2022-12-28 23:39:32 -06:00
|
|
|
// Change observer
|
|
|
|
/** @type {Observer<{value: number}>} */
|
|
|
|
const onchange = new Observer();
|
|
|
|
|
2022-11-23 22:53:14 -06:00
|
|
|
// Set value
|
|
|
|
const setValue = (val) => {
|
2022-12-03 04:50:23 -06:00
|
|
|
phantomTextRange.value = val;
|
|
|
|
value = parseFloat(phantomTextRange.value);
|
2022-11-23 22:53:14 -06:00
|
|
|
bar.style.width = `${
|
2022-11-24 08:54:30 -06:00
|
|
|
100 * ((value - options.min) / (options.max - options.min))
|
|
|
|
}%`;
|
2022-11-23 22:53:14 -06:00
|
|
|
textEl.value = `${name}: ${value}`;
|
|
|
|
options.valuecb && options.valuecb(value);
|
2022-12-28 23:39:32 -06:00
|
|
|
onchange.emit({value: val});
|
2022-11-23 22:53:14 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
setValue(options.defaultValue);
|
|
|
|
|
|
|
|
// Events
|
|
|
|
textEl.addEventListener("blur", () => {
|
|
|
|
overEl.style.pointerEvents = "auto";
|
|
|
|
textEl.value = `${name}: ${value}`;
|
|
|
|
});
|
|
|
|
textEl.addEventListener("focus", () => {
|
2022-11-24 09:30:13 -06:00
|
|
|
overEl.style.pointerEvents = "none";
|
2022-11-23 22:53:14 -06:00
|
|
|
textEl.value = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
textEl.addEventListener("change", () => {
|
|
|
|
try {
|
|
|
|
if (Number.isNaN(parseFloat(textEl.value))) setValue(value);
|
|
|
|
else setValue(parseFloat(textEl.value));
|
|
|
|
} catch (e) {}
|
|
|
|
});
|
|
|
|
|
|
|
|
keyboard.listen.onkeyclick.on((evn) => {
|
|
|
|
if (evn.target === textEl && evn.code === "Enter") {
|
|
|
|
textEl.blur();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-28 16:48:42 -06:00
|
|
|
mouse.listen.window.btn.left.onclick.on((evn) => {
|
2022-11-23 22:53:14 -06:00
|
|
|
if (evn.target === overEl) {
|
|
|
|
textEl.select();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-28 16:48:42 -06:00
|
|
|
mouse.listen.window.btn.left.ondrag.on((evn) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
if (evn.initialTarget === overEl) {
|
2022-12-07 10:31:15 -06:00
|
|
|
const newv = Math.max(
|
2022-12-03 04:50:23 -06:00
|
|
|
options.min,
|
|
|
|
Math.min(
|
|
|
|
options.max,
|
2022-12-07 10:31:15 -06:00
|
|
|
((evn.evn.clientX - evn.initialTarget.getBoundingClientRect().left) /
|
|
|
|
wrapper.offsetWidth) *
|
|
|
|
(options.max - options.min) +
|
2022-12-03 04:50:23 -06:00
|
|
|
options.min
|
2022-11-23 22:53:14 -06:00
|
|
|
)
|
|
|
|
);
|
2022-12-07 10:31:15 -06:00
|
|
|
phantomRange.value = newv;
|
2022-12-03 04:50:23 -06:00
|
|
|
setValue(parseFloat(phantomRange.value));
|
2022-11-23 22:53:14 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2022-12-28 23:39:32 -06:00
|
|
|
onchange,
|
2022-11-23 22:53:14 -06:00
|
|
|
set value(val) {
|
|
|
|
setValue(val);
|
|
|
|
},
|
|
|
|
get value() {
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-12-08 15:23:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A function to transform a div into a autocompletable select element
|
|
|
|
*
|
|
|
|
* @param {string} name Name of the AutoComplete Select Element
|
|
|
|
* @param {HTMLDivElement} wrapper The div element that will wrap the input elements
|
|
|
|
* @param {object} options Extra options
|
2022-12-11 13:01:22 -06:00
|
|
|
* @param {boolean} options.multiple Whether multiple options can be selected
|
|
|
|
* @param {{name: string, value: string}[]} options.options Options to add to the selector
|
2022-12-08 15:23:17 -06:00
|
|
|
* @returns {AutoCompleteElement}
|
|
|
|
*/
|
|
|
|
function createAutoComplete(name, wrapper, options = {}) {
|
|
|
|
defaultOpt(options, {
|
2022-12-11 13:01:22 -06:00
|
|
|
multiple: false,
|
2022-12-08 15:23:17 -06:00
|
|
|
options: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.classList.add("autocomplete");
|
|
|
|
|
|
|
|
const inputEl = document.createElement("input");
|
|
|
|
inputEl.type = "text";
|
|
|
|
inputEl.classList.add("autocomplete-text");
|
|
|
|
|
|
|
|
const autocompleteEl = document.createElement("div");
|
|
|
|
autocompleteEl.classList.add("autocomplete-list", "display-none");
|
|
|
|
|
|
|
|
let timeout = null;
|
|
|
|
let ontext = false;
|
|
|
|
let onlist = false;
|
|
|
|
|
|
|
|
wrapper.appendChild(inputEl);
|
|
|
|
wrapper.appendChild(autocompleteEl);
|
|
|
|
|
|
|
|
const acobj = {
|
|
|
|
name,
|
|
|
|
wrapper,
|
2022-12-11 13:01:22 -06:00
|
|
|
_selectedOptions: new Set(),
|
2022-12-08 15:23:17 -06:00
|
|
|
_options: [],
|
|
|
|
|
|
|
|
/** @type {Observer<{name:string, value: string}>} */
|
|
|
|
onchange: new Observer(),
|
|
|
|
|
|
|
|
get value() {
|
2022-12-13 20:54:38 -06:00
|
|
|
const v = Array.from(this._selectedOptions).map((opt) => opt.value);
|
2022-12-11 13:01:22 -06:00
|
|
|
return options.multiple ? v : v[0];
|
2022-12-08 15:23:17 -06:00
|
|
|
},
|
2022-12-11 13:01:22 -06:00
|
|
|
set value(values) {
|
|
|
|
this._selectedOptions.clear();
|
|
|
|
|
|
|
|
for (const val of options.multiple ? values : [values]) {
|
|
|
|
const opt = this.options.find((option) => option.value === val);
|
|
|
|
|
|
|
|
if (!opt) continue; // Ignore invalid options
|
|
|
|
|
|
|
|
this._selectedOptions.add(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._sync();
|
|
|
|
},
|
|
|
|
|
|
|
|
_sync() {
|
|
|
|
const val = Array.from(this._selectedOptions).map((opt) => opt.value);
|
|
|
|
const name = Array.from(this._selectedOptions).map((opt) => opt.name);
|
2022-12-08 15:23:17 -06:00
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
for (const opt of this._options) {
|
|
|
|
if (acobj._selectedOptions.has(opt))
|
|
|
|
opt.optionElement.classList.add("selected");
|
|
|
|
else opt.optionElement.classList.remove("selected");
|
|
|
|
}
|
2022-12-08 15:23:17 -06:00
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
updateInputField();
|
2022-12-08 15:23:17 -06:00
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
this.onchange.emit({
|
|
|
|
name: options.multiple ? name : name[0],
|
|
|
|
value: options.multiple ? val : val[0],
|
|
|
|
});
|
2022-12-08 15:23:17 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
get options() {
|
|
|
|
return this._options;
|
|
|
|
},
|
|
|
|
set options(val) {
|
|
|
|
this._options = [];
|
|
|
|
|
|
|
|
while (autocompleteEl.lastChild) {
|
|
|
|
autocompleteEl.removeChild(autocompleteEl.lastChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add options
|
|
|
|
val.forEach((opt) => {
|
2022-12-11 13:01:22 -06:00
|
|
|
const {name, value, title} = opt;
|
2022-12-08 15:23:17 -06:00
|
|
|
|
|
|
|
const optionEl = document.createElement("option");
|
|
|
|
optionEl.classList.add("autocomplete-option");
|
2022-12-11 13:01:22 -06:00
|
|
|
optionEl.title = title || name;
|
|
|
|
|
|
|
|
const option = {name, value, optionElement: optionEl};
|
2022-12-08 15:23:17 -06:00
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
this._options.push(option);
|
|
|
|
|
|
|
|
optionEl.addEventListener("click", () => select(option));
|
2022-12-08 15:23:17 -06:00
|
|
|
|
|
|
|
autocompleteEl.appendChild(optionEl);
|
|
|
|
});
|
|
|
|
|
|
|
|
updateOptions();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
function updateInputField() {
|
|
|
|
inputEl.value = Array.from(acobj._selectedOptions)
|
|
|
|
.map((o) => o.name)
|
|
|
|
.join(", ");
|
|
|
|
inputEl.title = Array.from(acobj._selectedOptions)
|
|
|
|
.map((o) => o.name)
|
|
|
|
.join(", ");
|
|
|
|
}
|
|
|
|
|
2022-12-08 15:23:17 -06:00
|
|
|
function updateOptions() {
|
|
|
|
const text = inputEl.value.toLowerCase().trim();
|
|
|
|
|
|
|
|
acobj._options.forEach((opt) => {
|
|
|
|
const textLocation = opt.name.toLowerCase().indexOf(text);
|
|
|
|
|
|
|
|
while (opt.optionElement.lastChild) {
|
|
|
|
opt.optionElement.removeChild(opt.optionElement.lastChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.optionElement.append(
|
|
|
|
document.createTextNode(opt.name.substring(0, textLocation))
|
|
|
|
);
|
|
|
|
const span = document.createElement("span");
|
|
|
|
span.style.fontWeight = "bold";
|
|
|
|
span.textContent = opt.name.substring(
|
|
|
|
textLocation,
|
|
|
|
textLocation + text.length
|
|
|
|
);
|
|
|
|
opt.optionElement.appendChild(span);
|
|
|
|
opt.optionElement.appendChild(
|
|
|
|
document.createTextNode(
|
|
|
|
opt.name.substring(textLocation + text.length, opt.name.length)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (textLocation !== -1) {
|
|
|
|
opt.optionElement.classList.remove("display-none");
|
|
|
|
} else opt.optionElement.classList.add("display-none");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
function select(opt) {
|
2022-12-08 15:23:17 -06:00
|
|
|
ontext = false;
|
2022-12-11 13:01:22 -06:00
|
|
|
if (!options.multiple) {
|
|
|
|
onlist = false;
|
|
|
|
acobj._selectedOptions.clear();
|
|
|
|
autocompleteEl.classList.add("display-none");
|
|
|
|
for (const child of autocompleteEl.children) {
|
|
|
|
child.classList.remove("selected");
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 15:23:17 -06:00
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
if (options.multiple && acobj._selectedOptions.has(opt)) {
|
|
|
|
acobj._selectedOptions.delete(opt);
|
|
|
|
opt.optionElement.classList.remove("selected");
|
|
|
|
} else {
|
|
|
|
acobj._selectedOptions.add(opt);
|
|
|
|
opt.optionElement.classList.add("selected");
|
|
|
|
}
|
2022-12-08 15:23:17 -06:00
|
|
|
|
2022-12-11 13:01:22 -06:00
|
|
|
acobj._sync();
|
2022-12-08 15:23:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
inputEl.addEventListener("focus", () => {
|
|
|
|
ontext = true;
|
|
|
|
|
|
|
|
autocompleteEl.classList.remove("display-none");
|
|
|
|
inputEl.select();
|
|
|
|
});
|
|
|
|
inputEl.addEventListener("blur", () => {
|
|
|
|
ontext = false;
|
|
|
|
|
|
|
|
if (!onlist && !ontext) {
|
2022-12-11 13:01:22 -06:00
|
|
|
updateInputField();
|
2022-12-08 15:23:17 -06:00
|
|
|
|
|
|
|
autocompleteEl.classList.add("display-none");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
autocompleteEl.addEventListener("mouseenter", () => {
|
|
|
|
onlist = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
autocompleteEl.addEventListener("mouseleave", () => {
|
|
|
|
onlist = false;
|
|
|
|
|
|
|
|
if (!onlist && !ontext) {
|
2022-12-11 13:01:22 -06:00
|
|
|
updateInputField();
|
2022-12-08 15:23:17 -06:00
|
|
|
|
|
|
|
autocompleteEl.classList.add("display-none");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Filter
|
|
|
|
inputEl.addEventListener("input", () => {
|
|
|
|
updateOptions();
|
|
|
|
});
|
|
|
|
|
|
|
|
acobj.options = options.options;
|
|
|
|
|
|
|
|
return acobj;
|
|
|
|
}
|