MaketTahta/assets/js/utils.js
2023-10-26 01:26:55 +03:00

130 lines
No EOL
3.2 KiB
JavaScript

function getDigits(str, num) {
return ("0" + str).slice(-num);
}
/**
*
* @param {Object} obj
* @param {URLSearchParams} params
*/
function addObjectToConfig(obj, params) {
Object.entries(obj).forEach(element => {
const [key, value] = element;
if (params == 1) {
confs.set(key, value);
}
else if (params.has(key)) {
confs.set(key, params.get(key));
}
});
}
/**
*
* @param {Array} arr
* @param {URLSearchParams} params
*/
function addArrayToConfig(arr, params) {
arr.forEach(element => {
if (params.has(element)) {
confs.set(element, params.get(element));
}
});
}
/**
*
* @param {string} id id to be updated
* @param {string} config config to update with
* @param {any} type type
*/
function setToID(id, config, type = 0) {
let obj = document.getElementById(id)
if (obj.innerHTML != config && type == 0) {
document.getElementById(id).innerHTML = config;
} else if (typeof type === 'string') {
if (config == type) {
document.getElementById(id).innerHTML = "<span style='color:red;'>" + config + "</span>";
} else if (config != type) {
document.getElementById(id).innerHTML = config;
}
}
}
/**
* Hides all elements with matching id in a list
* @param {Array} elements element ids to hide
* @param {...any} except hide elements except these
*/
function hideAllExcept(elements, ...except) {
elements.forEach(element => {
if (!except.includes(element)) {
document.getElementById(element).style.display = "none";
}
});
}
/**
* @param {String} type id or class for getting elements to toggle
* @param {String} name id or class name
* @param {String} display display type, defaults to flex
*/
function toggleItemDisplay(type, name = "no", display = "flex") {
switch (type) {
case "auto":
if (name == "no") {
// console.log(1, event.srcElement.id); // DEPRECATED !
console.alert('dont use type auto for item display! WIP')
// get id with button/checkbox name! and auto detect
}
break;
case "id":
let item = document.getElementById(name);
if (item.style.display == display) {
item.style.display = "none";
} else {
item.style.display = display;
}
if (midItems.includes(name)) {
hideAllExcept(midItems, name);
} else if (timersItems.includes(name)) {
hideAllExcept(timersItems, name);
}
break;
default:
console.error("no toggle item except ids!");
break;
}
}
function toggleConf(type, name = "no") {
switch (type) {
case "auto":
// AUTO DETECT type, id etc.
alert("dont use auto for toggleConf");
break;
case "id-chx":
if (name != "no") {
let chx = document.getElementById("chx-" + name);
if (chx.checked) {
confs.set(name, 1);
} else {
confs.set(name, 0);
}
}
break;
default:
console.error("unvalid type");
break;
}
}
function toggleDevVersion() {
let chx = document.getElementById("chx-devVersion");
if (chx.checked) {
window.location.replace("dev.html" + window.location.search);
} else {
window.location.replace("index.html" + window.location.search);
}
}