|
| 1 | +let loadingWin: HTMLElement | null = null; |
| 2 | + |
| 3 | +const clsPrefix = "__universal_loading"; |
| 4 | + |
| 5 | +const styles = `.${clsPrefix} { |
| 6 | + background-color: rgba(0, 0, 0, 0.6); |
| 7 | + box-sizing: border-box; |
| 8 | + max-width: 80%; |
| 9 | + color: #ffffff; |
| 10 | + padding: 15px; |
| 11 | + position: fixed; |
| 12 | + left: 50%; |
| 13 | + bottom: 50%; |
| 14 | + border-radius: 4px; |
| 15 | + text-align: center; |
| 16 | + transform: translateX(-50%); |
| 17 | + -webkit-transform: translateX(-50%); |
| 18 | + z-index: 9999; |
| 19 | + outline: none; |
| 20 | +} |
| 21 | +.${clsPrefix}_circle { |
| 22 | + border: 2px solid rgba(200, 200, 200, 0.5); |
| 23 | + border-radius: 50%; |
| 24 | + position: relative; |
| 25 | + height: 40px; |
| 26 | + width: 40px; |
| 27 | + margin: auto; |
| 28 | +} |
| 29 | +.${clsPrefix}_circle:after { |
| 30 | + content: ""; |
| 31 | + display: block; |
| 32 | + border-radius: 50%; |
| 33 | + width: 100%; |
| 34 | + height: 100%; |
| 35 | + position: absolute; |
| 36 | + top: -2px; |
| 37 | + left: -2px; |
| 38 | + border: 2px solid; |
| 39 | + border-color: transparent; |
| 40 | + border-left-color: #fff; |
| 41 | + box-sizing: content-box; |
| 42 | + animation: ${clsPrefix}_roller 1s infinite linear; |
| 43 | +} |
| 44 | +.${clsPrefix}_text { |
| 45 | + margin-top: 6px; |
| 46 | + color: #fff; |
| 47 | + line-height: 1.5; |
| 48 | + font-size: 14px; |
| 49 | + text-align: center; |
| 50 | + font-weight: normal; |
| 51 | +} |
| 52 | +@keyframes ${clsPrefix}_roller { |
| 53 | + 0% { |
| 54 | + transform: rotateZ(0); |
| 55 | + } |
| 56 | + 100% { |
| 57 | + transform: rotateZ(360deg); |
| 58 | + } |
| 59 | +}`.replace(/\n/g, ""); |
| 60 | + |
| 61 | +/* |
| 62 | + * @param message {String} |
| 63 | + */ |
| 64 | +let styleElement : HTMLElement | null = null; |
| 65 | +export function show({ content = "" }): Promise<any> { |
| 66 | + if (!styleElement) { |
| 67 | + // create a style tag for keyframes |
| 68 | + styleElement = document.createElement("style"); |
| 69 | + styleElement.innerHTML = styles; |
| 70 | + document.body.appendChild(styleElement); |
| 71 | + } |
| 72 | + if (!loadingWin) { |
| 73 | + // create loading win |
| 74 | + loadingWin = document.createElement("div"); |
| 75 | + loadingWin.className = clsPrefix; |
| 76 | + loadingWin.setAttribute("role", "alert"); |
| 77 | + // support for ARIA, add tabindex for focus |
| 78 | + // https://developer.mozilla.org/zh-CN/docs/Web/HTML/Global_attributes/tabindex |
| 79 | + loadingWin.setAttribute("tabindex", "-1"); |
| 80 | + // add a circle element |
| 81 | + const circle = document.createElement("div"); |
| 82 | + circle.className = `${clsPrefix}_circle`; |
| 83 | + loadingWin.appendChild(circle); |
| 84 | + // add text element |
| 85 | + const text = document.createElement("div"); |
| 86 | + text.className = `${clsPrefix}_text`; |
| 87 | + loadingWin.appendChild(text); |
| 88 | + |
| 89 | + document.body.appendChild(loadingWin); |
| 90 | + } |
| 91 | + |
| 92 | + const text = loadingWin.querySelector(`.${clsPrefix}_text`) as HTMLDivElement; |
| 93 | + if (content) { |
| 94 | + text.style.display = "block"; |
| 95 | + text.innerText = content; |
| 96 | + } else { |
| 97 | + text.style.display = "none"; |
| 98 | + text.innerText = ""; |
| 99 | + } |
| 100 | + |
| 101 | + return Promise.resolve(); |
| 102 | +} |
| 103 | + |
| 104 | +export function hide(): Promise<any> { |
| 105 | + return new Promise((resolve) => { |
| 106 | + setTimeout(() => { |
| 107 | + if (loadingWin && loadingWin.parentNode) { |
| 108 | + loadingWin.parentNode.removeChild(loadingWin); |
| 109 | + } |
| 110 | + loadingWin = null; |
| 111 | + resolve(); |
| 112 | + }, 0); |
| 113 | + }); |
| 114 | +} |
0 commit comments