-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-loading.js
54 lines (42 loc) · 1.4 KB
/
v-loading.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function addLoader(elem, loaderHtml) {
const styles = getComputedStyle(elem);
if (styles.position === 'static') {
elem.classList.add('v-l-pos-rel');
}
const loaderElem = document.createElement('div');
loaderElem.classList.add('v-l-loader');
loaderElem.classList.add('v-l-center-content');
loaderElem.innerHTML = loaderHtml;
elem.appendChild(loaderElem);
}
function removeLoader(elem) {
elem.classList.remove('v-l-pos-rel');
const loaders = elem.querySelectorAll('.v-l-loader');
[...loaders].forEach((loader) => elem.removeChild(loader));
}
function getLoaderContentHtml(options) {
const defaultSpinnerHtml = `
<svg class="v-l-spinner" width="40" height="40" viewBox="0 0 50 50">
<circle
class="v-l-circle" cx="25" cy="25" r="19" fill="none" stroke-linecap="round" stroke="currentColor"
></circle>
</svg>
`;
let html = defaultSpinnerHtml;
if (options?.loaderHtml) {
html = options.loaderHtml;
}
if (!options?.disableRotate) {
html = `<div class="v-l-rotating v-l-center-content">${html}</div>`;
}
html = `<div class="v-l-scale v-l-center-content">${html}</div>`;
return html;
}
function vLoading(options) {
const loaderHtmlContent = getLoaderContentHtml(options);
return function (elem, binding) {
if (binding.value) addLoader(elem, loaderHtmlContent);
if (!binding.value) removeLoader(elem);
};
}
export default vLoading;