2025-02-20 02:19:32 +00:00
|
|
|
const MszLoadingIcon = function() {
|
|
|
|
const element = <div class="msz-loading-icon"/>;
|
2025-02-02 02:09:56 +00:00
|
|
|
for(let i = 0; i < 9; ++i)
|
2025-02-20 02:19:32 +00:00
|
|
|
element.appendChild(<div class="msz-loading-icon-block"/>);
|
2025-02-02 02:09:56 +00:00
|
|
|
|
|
|
|
// this is moderately cursed but it'll do
|
|
|
|
const blocks = [
|
|
|
|
element.children[3],
|
|
|
|
element.children[0],
|
|
|
|
element.children[1],
|
|
|
|
element.children[2],
|
|
|
|
element.children[5],
|
|
|
|
element.children[8],
|
|
|
|
element.children[7],
|
|
|
|
element.children[6],
|
|
|
|
];
|
|
|
|
|
|
|
|
let tsLastUpdate;
|
|
|
|
let counter = 0;
|
|
|
|
let playing = false;
|
|
|
|
|
|
|
|
const update = tsCurrent => {
|
|
|
|
try {
|
|
|
|
if(tsLastUpdate !== undefined && (tsCurrent - tsLastUpdate) < 50)
|
|
|
|
return;
|
|
|
|
tsLastUpdate = tsCurrent;
|
|
|
|
|
|
|
|
for(let i = 0; i < blocks.length; ++i)
|
2025-02-20 02:19:32 +00:00
|
|
|
blocks[(counter + i) % blocks.length].classList.toggle('msz-loading-icon-block-hidden', i < 3);
|
2025-02-02 02:09:56 +00:00
|
|
|
|
|
|
|
++counter;
|
|
|
|
} finally {
|
|
|
|
if(playing)
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const play = () => {
|
|
|
|
if(playing)
|
|
|
|
return;
|
|
|
|
playing = true;
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
};
|
|
|
|
const pause = () => { playing = false; };
|
|
|
|
const stop = () => { pause(); counter = 0; };
|
|
|
|
const restart = () => { stop(); play(); };
|
|
|
|
|
|
|
|
return {
|
|
|
|
get element() { return element; },
|
|
|
|
get playing() { return playing; },
|
|
|
|
|
|
|
|
play: play,
|
|
|
|
pause: pause,
|
|
|
|
stop: stop,
|
|
|
|
restart: restart,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2025-02-20 02:19:32 +00:00
|
|
|
const MszLoading = function(options=null) {
|
|
|
|
if(typeof options !== 'object')
|
|
|
|
throw 'options must be an object';
|
|
|
|
|
|
|
|
let {
|
|
|
|
element, size, colour,
|
|
|
|
width, height, inline,
|
|
|
|
containerWidth, containerHeight,
|
|
|
|
gap, margin, hidden,
|
|
|
|
} = options ?? {};
|
|
|
|
|
2025-02-02 02:09:56 +00:00
|
|
|
if(typeof element === 'string')
|
|
|
|
element = document.querySelector(element);
|
|
|
|
if(!(element instanceof HTMLElement))
|
2025-02-20 02:19:32 +00:00
|
|
|
element = <div class="msz-loading"/>;
|
|
|
|
|
|
|
|
if(!element.classList.contains('msz-loading'))
|
|
|
|
element.classList.add('msz-loading');
|
|
|
|
if(inline)
|
|
|
|
element.classList.add('msz-loading-inline');
|
|
|
|
if(hidden)
|
|
|
|
element.classList.add('hidden');
|
2025-02-02 02:09:56 +00:00
|
|
|
|
2025-02-20 02:19:32 +00:00
|
|
|
if(typeof size === 'number' && size > 0)
|
|
|
|
element.style.setProperty('--msz-loading-size', size);
|
|
|
|
if(typeof containerWidth === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-container-width', containerWidth);
|
|
|
|
if(typeof containerHeight === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-container-height', containerHeight);
|
|
|
|
if(typeof gap === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-gap', gap);
|
|
|
|
if(typeof margin === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-margin', margin);
|
|
|
|
if(typeof width === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-width', width);
|
|
|
|
if(typeof height === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-height', height);
|
|
|
|
if(typeof colour === 'string')
|
|
|
|
element.style.setProperty('--msz-loading-colour', colour);
|
2025-02-02 02:09:56 +00:00
|
|
|
|
|
|
|
let icon;
|
|
|
|
if(element.childElementCount < 1) {
|
2025-02-20 02:19:32 +00:00
|
|
|
icon = new MszLoadingIcon;
|
2025-02-02 02:09:56 +00:00
|
|
|
icon.play();
|
2025-02-20 02:19:32 +00:00
|
|
|
element.appendChild(<div class="msz-loading-frame">{icon}</div>);
|
2025-02-02 02:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
get element() { return element; },
|
|
|
|
|
|
|
|
get hasIcon() { return icon !== undefined; },
|
|
|
|
get icon() { return icon; },
|
|
|
|
|
|
|
|
get visible() { return !element.classList.contains('hidden'); },
|
|
|
|
set visible(state) { element.classList.toggle('hidden', !state); },
|
|
|
|
};
|
|
|
|
};
|