stear/utils.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-08-10 21:56:12 +02:00
import { class_ } from "./main.js";
2022-11-12 14:30:11 +01:00
/**
* async wait/pause function
*
* @param {number} ms
* @returns
*/
2022-06-09 15:29:53 +02:00
export const wait = (ms) => {
return new Promise((res, rej) => {
setTimeout(res, ms);
});
}
2022-11-12 14:30:11 +01:00
/**
* fadeout an Element.
*
* @param {HTMLElement|class_} Selems Element ot fade
* @param {number} [ms] Duration in ms
* @param {boolean} [force]
*/
2022-08-10 21:56:12 +02:00
export const fadeout = async (Selems, ms = 200, force = false) => {
2022-06-10 15:01:23 +02:00
var oldTrans = [];
2022-08-10 21:56:12 +02:00
if (!Array.isArray(Selems)) Selems = [Selems];
var elems = [];
for (let i = 0; i < Selems.length; i++) {
elems[i] = Selems[i] instanceof class_ ? Selems[i]._ : Selems[i];
2022-06-10 15:01:23 +02:00
oldTrans[i] = elems[i].style.transition;
2022-06-09 15:29:53 +02:00
elems[i].style.transition = `opacity ${ms}ms`;
if(force){
elems[i].style.opacity = "1";
elems[i].style.display = "";
}
}
await wait(10);
for (let i = 0; i < elems.length; i++) {
elems[i].style.opacity = "0";
}
await wait(ms);
for (let i = 0; i < elems.length; i++) {
elems[i].style.display = "none";
2022-06-10 15:01:23 +02:00
elems[i].style.transition = oldTrans[i];
2022-06-09 15:29:53 +02:00
}
}
2022-11-12 14:30:11 +01:00
/**
* fadein an Element.
*
* @param {HTMLElement|class_} Selems Element to fade
* @param {number} [ms] Duration in ms
* @param {boolean} [force]
* @param {string} [display] Set destroyed display style tag.
*/
2022-08-10 21:56:12 +02:00
export const fadein = async (Selems, ms = 200, force = false, display="") => {
2022-06-10 15:01:23 +02:00
var oldTrans = [];
2022-08-10 21:56:12 +02:00
if (!Array.isArray(Selems)) Selems = [Selems];
var elems = [];
for (let i = 0; i < Selems.length; i++) {
elems[i] = (Selems[i] instanceof class_) ? Selems[i]._ : Selems[i];
2022-06-10 15:01:23 +02:00
oldTrans[i] = elems[i].style.transition
2022-06-09 15:29:53 +02:00
elems[i].style.transition = `opacity ${ms}ms`;
2022-06-10 15:01:23 +02:00
elems[i].style.display = display;
2022-06-09 15:29:53 +02:00
if (force) {
elems[i].style.opacity = "0";
}
}
await wait(10);
for (let i = 0; i < elems.length; i++) {
elems[i].style.opacity = "1";
}
2022-06-10 15:01:23 +02:00
await wait(ms+1);
for (let i = 0; i < elems.length; i++) {
elems[i].style.transition = oldTrans[i];
}
2022-06-09 15:29:53 +02:00
}
2022-06-10 01:07:55 +02:00
var bStack = [];
2022-11-12 14:30:11 +01:00
/**
* The callback will be triggered when the user intends to go back.
*
* @param {function} callback
* @returns {function} cancle callback
*/
2022-06-10 01:07:55 +02:00
export const subCancel = (callback) => {
bStack.push(callback);
return ()=>{
var id;
if ((id = bStack.indexOf(callback)) >= 0) {
bStack.splice(id, 1);
}
}
}
function back() {
if (bStack.length) {
bStack.pop()();
}
}
document.addEventListener("backbutton", back, false);
document.addEventListener('keyup', (e)=>{
if (e.code == "Escape")back();
});