diff options
Diffstat (limited to 'Blog/wwwroot')
| -rw-r--r-- | Blog/wwwroot/Blog.lib.module.js | 83 | ||||
| -rw-r--r-- | Blog/wwwroot/app.css | 280 | ||||
| -rw-r--r-- | Blog/wwwroot/common.module.js | 146 | ||||
| -rw-r--r-- | Blog/wwwroot/lz-string.module.js | 425 |
4 files changed, 934 insertions, 0 deletions
diff --git a/Blog/wwwroot/Blog.lib.module.js b/Blog/wwwroot/Blog.lib.module.js new file mode 100644 index 0000000..c1a5870 --- /dev/null +++ b/Blog/wwwroot/Blog.lib.module.js | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | const pageScriptInfoBySrc = new Map(); | ||
| 2 | |||
| 3 | function registerPageScriptElement(src) { | ||
| 4 | if (!src) { | ||
| 5 | throw new Error('Must provide a non-empty value for the "src" attribute.'); | ||
| 6 | } | ||
| 7 | |||
| 8 | let pageScriptInfo = pageScriptInfoBySrc.get(src); | ||
| 9 | |||
| 10 | if (pageScriptInfo) { | ||
| 11 | pageScriptInfo.referenceCount++; | ||
| 12 | } else { | ||
| 13 | pageScriptInfo = { referenceCount: 1, module: null }; | ||
| 14 | pageScriptInfoBySrc.set(src, pageScriptInfo); | ||
| 15 | initializePageScriptModule(src, pageScriptInfo); | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | function unregisterPageScriptElement(src) { | ||
| 20 | if (!src) { | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | |||
| 24 | const pageScriptInfo = pageScriptInfoBySrc.get(src); | ||
| 25 | |||
| 26 | if (!pageScriptInfo) { | ||
| 27 | return; | ||
| 28 | } | ||
| 29 | |||
| 30 | pageScriptInfo.referenceCount--; | ||
| 31 | } | ||
| 32 | |||
| 33 | async function initializePageScriptModule(src, pageScriptInfo) { | ||
| 34 | if (src.startsWith("./")) { | ||
| 35 | src = new URL(src.substr(2), document.baseURI).toString(); | ||
| 36 | } | ||
| 37 | |||
| 38 | const module = await import(src); | ||
| 39 | |||
| 40 | if (pageScriptInfo.referenceCount <= 0) { | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | pageScriptInfo.module = module; | ||
| 45 | module.onLoad?.(); | ||
| 46 | module.onUpdate?.(); | ||
| 47 | } | ||
| 48 | |||
| 49 | function onEnhancedLoad() { | ||
| 50 | for (const [src, { module, referenceCount }] of pageScriptInfoBySrc) { | ||
| 51 | if (referenceCount <= 0) { | ||
| 52 | module?.onDispose?.(); | ||
| 53 | pageScriptInfoBySrc.delete(src); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | for (const { module } of pageScriptInfoBySrc.values()) { | ||
| 58 | module?.onUpdate?.(); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | export function afterWebStarted(blazor) { | ||
| 63 | console.log("afterWebStarted"); | ||
| 64 | customElements.define('page-script', class extends HTMLElement { | ||
| 65 | static observedAttributes = ['src']; | ||
| 66 | |||
| 67 | attributeChangedCallback(name, oldValue, newValue) { | ||
| 68 | if (name !== 'src') { | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | this.src = newValue; | ||
| 73 | unregisterPageScriptElement(oldValue); | ||
| 74 | registerPageScriptElement(newValue); | ||
| 75 | } | ||
| 76 | |||
| 77 | disconnectedCallback() { | ||
| 78 | unregisterPageScriptElement(this.src); | ||
| 79 | } | ||
| 80 | }); | ||
| 81 | |||
| 82 | blazor.addEventListener('enhancedload', onEnhancedLoad); | ||
| 83 | } \ No newline at end of file | ||
diff --git a/Blog/wwwroot/app.css b/Blog/wwwroot/app.css new file mode 100644 index 0000000..0065fd6 --- /dev/null +++ b/Blog/wwwroot/app.css | |||
| @@ -0,0 +1,280 @@ | |||
| 1 | h1:focus { | ||
| 2 | outline: none; | ||
| 3 | } | ||
| 4 | |||
| 5 | .valid.modified:not([type=checkbox]) { | ||
| 6 | outline: 1px solid #26b050; | ||
| 7 | } | ||
| 8 | |||
| 9 | .invalid { | ||
| 10 | outline: 1px solid #e50000; | ||
| 11 | } | ||
| 12 | |||
| 13 | .validation-message { | ||
| 14 | color: #e50000; | ||
| 15 | } | ||
| 16 | |||
| 17 | .blazor-error-boundary { | ||
| 18 | background: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTYiIGhlaWdodD0iNDkiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIG92ZXJmbG93PSJoaWRkZW4iPjxkZWZzPjxjbGlwUGF0aCBpZD0iY2xpcDAiPjxyZWN0IHg9IjIzNSIgeT0iNTEiIHdpZHRoPSI1NiIgaGVpZ2h0PSI0OSIvPjwvY2xpcFBhdGg+PC9kZWZzPjxnIGNsaXAtcGF0aD0idXJsKCNjbGlwMCkiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0yMzUgLTUxKSI+PHBhdGggZD0iTTI2My41MDYgNTFDMjY0LjcxNyA1MSAyNjUuODEzIDUxLjQ4MzcgMjY2LjYwNiA1Mi4yNjU4TDI2Ny4wNTIgNTIuNzk4NyAyNjcuNTM5IDUzLjYyODMgMjkwLjE4NSA5Mi4xODMxIDI5MC41NDUgOTIuNzk1IDI5MC42NTYgOTIuOTk2QzI5MC44NzcgOTMuNTEzIDI5MSA5NC4wODE1IDI5MSA5NC42NzgyIDI5MSA5Ny4wNjUxIDI4OS4wMzggOTkgMjg2LjYxNyA5OUwyNDAuMzgzIDk5QzIzNy45NjMgOTkgMjM2IDk3LjA2NTEgMjM2IDk0LjY3ODIgMjM2IDk0LjM3OTkgMjM2LjAzMSA5NC4wODg2IDIzNi4wODkgOTMuODA3MkwyMzYuMzM4IDkzLjAxNjIgMjM2Ljg1OCA5Mi4xMzE0IDI1OS40NzMgNTMuNjI5NCAyNTkuOTYxIDUyLjc5ODUgMjYwLjQwNyA1Mi4yNjU4QzI2MS4yIDUxLjQ4MzcgMjYyLjI5NiA1MSAyNjMuNTA2IDUxWk0yNjMuNTg2IDY2LjAxODNDMjYwLjczNyA2Ni4wMTgzIDI1OS4zMTMgNjcuMTI0NSAyNTkuMzEzIDY5LjMzNyAyNTkuMzEzIDY5LjYxMDIgMjU5LjMzMiA2OS44NjA4IDI1OS4zNzEgNzAuMDg4N0wyNjEuNzk1IDg0LjAxNjEgMjY1LjM4IDg0LjAxNjEgMjY3LjgyMSA2OS43NDc1QzI2Ny44NiA2OS43MzA5IDI2Ny44NzkgNjkuNTg3NyAyNjcuODc5IDY5LjMxNzkgMjY3Ljg3OSA2Ny4xMTgyIDI2Ni40NDggNjYuMDE4MyAyNjMuNTg2IDY2LjAxODNaTTI2My41NzYgODYuMDU0N0MyNjEuMDQ5IDg2LjA1NDcgMjU5Ljc4NiA4Ny4zMDA1IDI1OS43ODYgODkuNzkyMSAyNTkuNzg2IDkyLjI4MzcgMjYxLjA0OSA5My41Mjk1IDI2My41NzYgOTMuNTI5NSAyNjYuMTE2IDkzLjUyOTUgMjY3LjM4NyA5Mi4yODM3IDI2Ny4zODcgODkuNzkyMSAyNjcuMzg3IDg3LjMwMDUgMjY2LjExNiA4Ni4wNTQ3IDI2My41NzYgODYuMDU0N1oiIGZpbGw9IiNGRkU1MDAiIGZpbGwtcnVsZT0iZXZlbm9kZCIvPjwvZz48L3N2Zz4=) no-repeat 1rem/1.8rem, #b32121; | ||
| 19 | padding: 1rem 1rem 1rem 3.7rem; | ||
| 20 | color: white; | ||
| 21 | } | ||
| 22 | |||
| 23 | .blazor-error-boundary::after { | ||
| 24 | content: "An error has occurred." | ||
| 25 | } | ||
| 26 | |||
| 27 | .darker-border-checkbox.form-check-input { | ||
| 28 | border-color: #929292; | ||
| 29 | } | ||
| 30 | |||
| 31 | .form-floating > .form-control-plaintext::placeholder, .form-floating > .form-control::placeholder { | ||
| 32 | color: var(--bs-secondary-color); | ||
| 33 | text-align: end; | ||
| 34 | } | ||
| 35 | |||
| 36 | .form-floating > .form-control-plaintext:focus::placeholder, .form-floating > .form-control:focus::placeholder { | ||
| 37 | text-align: start; | ||
| 38 | } | ||
| 39 | |||
| 40 | * { | ||
| 41 | box-sizing: border-box; | ||
| 42 | font-size: calc(1rem + 0.5vw); | ||
| 43 | font-family: monospace; | ||
| 44 | } | ||
| 45 | |||
| 46 | :root { | ||
| 47 | --black: #050505; | ||
| 48 | --white: #fafafa; | ||
| 49 | --error: #da0000; | ||
| 50 | --info: #669aba; | ||
| 51 | |||
| 52 | --ratio: 1.5; | ||
| 53 | --s0: 1rem; | ||
| 54 | --s1: calc(var(--s0) * var(--ratio)); | ||
| 55 | } | ||
| 56 | |||
| 57 | @media (prefers-color-scheme: dark) { | ||
| 58 | :root { | ||
| 59 | --background: var(--black); | ||
| 60 | --color: var(--white); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | @media (prefers-color-scheme: light) { | ||
| 65 | :root { | ||
| 66 | --background: var(--white); | ||
| 67 | --color: var(--black); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | body { | ||
| 72 | background-color: var(--background); | ||
| 73 | color: var(--color); | ||
| 74 | } | ||
| 75 | |||
| 76 | .center { | ||
| 77 | box-sizing: content-box; | ||
| 78 | max-inline-size: 60ch; | ||
| 79 | margin-inline: auto; | ||
| 80 | padding-inline-start: var(--s1); | ||
| 81 | padding-inline-end: var(--s1); | ||
| 82 | } | ||
| 83 | |||
| 84 | #logo { | ||
| 85 | height: 5em; | ||
| 86 | display: inline-block; | ||
| 87 | } | ||
| 88 | |||
| 89 | header > h1 { | ||
| 90 | color: var(--header); | ||
| 91 | display: inline-block; | ||
| 92 | } | ||
| 93 | |||
| 94 | li:has(a:focus)::marker { | ||
| 95 | content: "==> "; | ||
| 96 | } | ||
| 97 | |||
| 98 | li:has(a:hover)::marker { | ||
| 99 | content: " >"; | ||
| 100 | } | ||
| 101 | |||
| 102 | li:has(a:hover:focus)::marker { | ||
| 103 | content: "==>"; | ||
| 104 | } | ||
| 105 | |||
| 106 | li::marker { | ||
| 107 | content: "> "; | ||
| 108 | } | ||
| 109 | |||
| 110 | a { | ||
| 111 | color: var(--color); | ||
| 112 | } | ||
| 113 | |||
| 114 | a:empty::before { | ||
| 115 | content: attr(href); | ||
| 116 | } | ||
| 117 | |||
| 118 | input[type="text"] { | ||
| 119 | width: 80%; | ||
| 120 | display: inline-block; | ||
| 121 | background-color: var(--background); | ||
| 122 | color: var(--color); | ||
| 123 | border-color: var(--color); | ||
| 124 | border-radius: 0.5em; | ||
| 125 | } | ||
| 126 | |||
| 127 | input[type="text"] { | ||
| 128 | width: 100%; | ||
| 129 | display: inline-block; | ||
| 130 | margin-inline: 0 1em; | ||
| 131 | font-family: monospace; | ||
| 132 | white-space: pre-wrap; | ||
| 133 | background-color: var(--background); | ||
| 134 | color: var(--color); | ||
| 135 | border-color: var(--border); | ||
| 136 | border-radius: 0.5em; | ||
| 137 | } | ||
| 138 | |||
| 139 | label + input { | ||
| 140 | margin-block-end: 1em; | ||
| 141 | } | ||
| 142 | |||
| 143 | label { | ||
| 144 | display: block; | ||
| 145 | } | ||
| 146 | |||
| 147 | textarea { | ||
| 148 | width: 100%; | ||
| 149 | display: block; | ||
| 150 | margin-inline: 0 1em; | ||
| 151 | font-family: monospace; | ||
| 152 | white-space: pre-wrap; | ||
| 153 | background-color: var(--background); | ||
| 154 | color: var(--color); | ||
| 155 | border-color: var(--border); | ||
| 156 | border-radius: 0.5em; | ||
| 157 | } | ||
| 158 | |||
| 159 | pre { | ||
| 160 | font-family: monospace; | ||
| 161 | white-space: pre-line; | ||
| 162 | } | ||
| 163 | |||
| 164 | button, | ||
| 165 | input[type="button"], | ||
| 166 | input[type="submit"], | ||
| 167 | input[type="file"]::file-selector-button { | ||
| 168 | min-width: 20%; | ||
| 169 | display: inline-block; | ||
| 170 | color: var(--color); | ||
| 171 | background-color: var(--background); | ||
| 172 | border: 1px solid var(--color); | ||
| 173 | border-radius: 0.5em; | ||
| 174 | padding: 0.1em 1em; | ||
| 175 | cursor: pointer; | ||
| 176 | font-family: monospace; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | .flex-row { | ||
| 181 | display: flex; | ||
| 182 | gap: 1em; | ||
| 183 | } | ||
| 184 | |||
| 185 | .flex-spread { | ||
| 186 | display: flex; | ||
| 187 | gap: 1em; | ||
| 188 | justify-content: space-between; | ||
| 189 | } | ||
| 190 | |||
| 191 | .history { | ||
| 192 | filter: opacity(30%); | ||
| 193 | } | ||
| 194 | |||
| 195 | div + .history { | ||
| 196 | margin-block-start: 2em; | ||
| 197 | } | ||
| 198 | |||
| 199 | main { | ||
| 200 | border-block-start: 2px dotted var(--color); | ||
| 201 | border-block-end: 2px dotted var(--color); | ||
| 202 | padding-block: 1em; | ||
| 203 | } | ||
| 204 | |||
| 205 | .docs { | ||
| 206 | margin: 2em 0; | ||
| 207 | border: 2px dashed var(--color); | ||
| 208 | padding: 0.5em 1em; | ||
| 209 | border-radius: 0.5em; | ||
| 210 | } | ||
| 211 | |||
| 212 | .error::before { | ||
| 213 | content: "ERR: "; | ||
| 214 | color: var(--error); | ||
| 215 | } | ||
| 216 | |||
| 217 | .info::before { | ||
| 218 | content: "INF: "; | ||
| 219 | color: var(--info); | ||
| 220 | } | ||
| 221 | |||
| 222 | label:has(> #debug:not(:checked)) + #log .debug { | ||
| 223 | display: none; | ||
| 224 | } | ||
| 225 | |||
| 226 | .debug::before { | ||
| 227 | content: "DBG: "; | ||
| 228 | color: var(--info); | ||
| 229 | } | ||
| 230 | |||
| 231 | .row + .row { | ||
| 232 | margin-block-start: 1em; | ||
| 233 | } | ||
| 234 | |||
| 235 | .noselect { | ||
| 236 | user-select: none; | ||
| 237 | -moz-user-select: -moz-none; | ||
| 238 | -webkit-user-select: none; | ||
| 239 | -ms-user-select: none; | ||
| 240 | } | ||
| 241 | |||
| 242 | img { | ||
| 243 | margin-inline: auto; | ||
| 244 | margin-block: 1em; | ||
| 245 | } | ||
| 246 | |||
| 247 | footer > ul { | ||
| 248 | columns: 2; | ||
| 249 | } | ||
| 250 | |||
| 251 | legend::before { | ||
| 252 | content: "( "; | ||
| 253 | } | ||
| 254 | |||
| 255 | legend::after { | ||
| 256 | content: " )"; | ||
| 257 | } | ||
| 258 | |||
| 259 | summary { | ||
| 260 | cursor: help; | ||
| 261 | } | ||
| 262 | |||
| 263 | summary::marker { | ||
| 264 | content: "> "; | ||
| 265 | } | ||
| 266 | |||
| 267 | details[open] summary::marker { | ||
| 268 | content: "x "; | ||
| 269 | } | ||
| 270 | |||
| 271 | input[type="checkbox"] { | ||
| 272 | appearance: none; | ||
| 273 | } | ||
| 274 | input[type="checkbox"]:checked::after { | ||
| 275 | content: "[x]" | ||
| 276 | } | ||
| 277 | |||
| 278 | input[type="checkbox"]::after { | ||
| 279 | content: "[ ]" | ||
| 280 | } \ No newline at end of file | ||
diff --git a/Blog/wwwroot/common.module.js b/Blog/wwwroot/common.module.js new file mode 100644 index 0000000..6007790 --- /dev/null +++ b/Blog/wwwroot/common.module.js | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | function getById(id) { | ||
| 2 | let element = document.getElementById(id); | ||
| 3 | if (element) { | ||
| 4 | return element; | ||
| 5 | } else { | ||
| 6 | throw `Element with id '${id}' not found.`; | ||
| 7 | } | ||
| 8 | } | ||
| 9 | |||
| 10 | /** | ||
| 11 | * @param {string} href | ||
| 12 | * @returns {HTMLAnchorElement} | ||
| 13 | */ | ||
| 14 | function a(href, pageName) { | ||
| 15 | const aElem = document.createElement("a"); | ||
| 16 | aElem.href = href; | ||
| 17 | aElem.text = pageName; | ||
| 18 | return aElem; | ||
| 19 | } | ||
| 20 | |||
| 21 | function div(...children) { | ||
| 22 | const divElem = document.createElement("div"); | ||
| 23 | divElem.replaceChildren(...children); | ||
| 24 | return divElem; | ||
| 25 | } | ||
| 26 | |||
| 27 | function span(text) { | ||
| 28 | const spanElem = document.createElement("span"); | ||
| 29 | spanElem.textContent = text; | ||
| 30 | return spanElem; | ||
| 31 | } | ||
| 32 | |||
| 33 | function h(elementTag, ...children) { | ||
| 34 | const elem = document.createElement(elementTag); | ||
| 35 | elem.replaceChildren(...children); | ||
| 36 | return elem; | ||
| 37 | } | ||
| 38 | |||
| 39 | /** @param {HTMLElement} element */ | ||
| 40 | function addClass(element, newClass) { | ||
| 41 | element.classList.add(newClass); | ||
| 42 | return element; | ||
| 43 | } | ||
| 44 | |||
| 45 | function writeError(error) { | ||
| 46 | writeLog(error, "error"); | ||
| 47 | } | ||
| 48 | |||
| 49 | function writeInfo(msg) { | ||
| 50 | writeLog(msg, "info"); | ||
| 51 | } | ||
| 52 | |||
| 53 | function writeDebug(msg) { | ||
| 54 | writeLog(msg, "debug"); | ||
| 55 | } | ||
| 56 | |||
| 57 | function writeLog(msg, className) { | ||
| 58 | if (typeof msg === "string" || msg instanceof String) { | ||
| 59 | log.appendChild(div(addClass(span(msg), className))); | ||
| 60 | } else { | ||
| 61 | log.appendChild(div(addClass(msg, className))); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | function resetLog() { | ||
| 66 | log.replaceChildren(); | ||
| 67 | } | ||
| 68 | |||
| 69 | const log = getById("log"); | ||
| 70 | |||
| 71 | // https://stackoverflow.com/questions/75988682/debounce-in-javascript | ||
| 72 | // https://www.joshwcomeau.com/snippets/javascript/debounce/ | ||
| 73 | function debounce(callback, wait) { | ||
| 74 | let timeoutId = null; | ||
| 75 | return (...args) => { | ||
| 76 | window.clearTimeout(timeoutId); | ||
| 77 | timeoutId = window.setTimeout(() => { | ||
| 78 | callback(...args); | ||
| 79 | }, wait); | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | |||
| 83 | const stylesheet = new CSSStyleSheet(); | ||
| 84 | stylesheet.replaceSync(` | ||
| 85 | a { | ||
| 86 | color: var(--color); | ||
| 87 | } | ||
| 88 | nav > ul { | ||
| 89 | list-style-type: none; | ||
| 90 | padding-inline-start: 0; | ||
| 91 | } | ||
| 92 | |||
| 93 | nav > ul > li { | ||
| 94 | display: inline-block; | ||
| 95 | } | ||
| 96 | |||
| 97 | nav > ul > li > a::after { | ||
| 98 | display: inline-block; | ||
| 99 | color: var(--color); | ||
| 100 | content: ">"; | ||
| 101 | padding: 0 1em; | ||
| 102 | } | ||
| 103 | `); | ||
| 104 | |||
| 105 | customElements.define("mb-nav", | ||
| 106 | class MBNav extends HTMLElement { | ||
| 107 | constructor() { | ||
| 108 | super(); | ||
| 109 | } | ||
| 110 | |||
| 111 | get pagename() { | ||
| 112 | return this.getAttribute("pagename"); | ||
| 113 | } | ||
| 114 | |||
| 115 | set pagename(value) { | ||
| 116 | this.setAttribute("pagename", value); | ||
| 117 | } | ||
| 118 | |||
| 119 | connectedCallback() { | ||
| 120 | const shadow = this.attachShadow({ mode: "open" }); | ||
| 121 | shadow.adoptedStyleSheets = [stylesheet]; | ||
| 122 | |||
| 123 | shadow.appendChild( | ||
| 124 | h("nav", | ||
| 125 | h("ul", | ||
| 126 | h("li", a("/", "MB.bes.is")), | ||
| 127 | h("li", span(this.pagename)), | ||
| 128 | ) | ||
| 129 | ) | ||
| 130 | ) | ||
| 131 | } | ||
| 132 | }); | ||
| 133 | |||
| 134 | export { | ||
| 135 | getById, | ||
| 136 | a, | ||
| 137 | div, | ||
| 138 | span, | ||
| 139 | h, | ||
| 140 | addClass, | ||
| 141 | writeError, | ||
| 142 | writeInfo, | ||
| 143 | writeDebug, | ||
| 144 | resetLog, | ||
| 145 | debounce, | ||
| 146 | }; | ||
diff --git a/Blog/wwwroot/lz-string.module.js b/Blog/wwwroot/lz-string.module.js new file mode 100644 index 0000000..6167fd2 --- /dev/null +++ b/Blog/wwwroot/lz-string.module.js | |||
| @@ -0,0 +1,425 @@ | |||
| 1 | // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net> | ||
| 2 | // This work is free. You can redistribute it and/or modify it | ||
| 3 | // under the terms of the WTFPL, Version 2 | ||
| 4 | // For more information see LICENSE.txt or http://www.wtfpl.net/ | ||
| 5 | // | ||
| 6 | // For more information, the home page: | ||
| 7 | // http://pieroxy.net/blog/pages/lz-string/testing.html | ||
| 8 | // | ||
| 9 | // LZ-based compression algorithm, version 1.4.5 | ||
| 10 | var LzStringModule = (function() { | ||
| 11 | |||
| 12 | // private property | ||
| 13 | var f = String.fromCharCode; | ||
| 14 | var keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$"; | ||
| 15 | var baseReverseDic = {}; | ||
| 16 | |||
| 17 | function getBaseValue(alphabet, character) { | ||
| 18 | if (!baseReverseDic[alphabet]) { | ||
| 19 | baseReverseDic[alphabet] = {}; | ||
| 20 | for (var i=0 ; i<alphabet.length ; i++) { | ||
| 21 | baseReverseDic[alphabet][alphabet.charAt(i)] = i; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | return baseReverseDic[alphabet][character]; | ||
| 25 | } | ||
| 26 | |||
| 27 | var LZString = { | ||
| 28 | //compress into a string that is already URI encoded | ||
| 29 | compressToEncodedURIComponent: function (input) { | ||
| 30 | if (input == null) return ""; | ||
| 31 | return LZString._compress(input, 6, function(a){return keyStrUriSafe.charAt(a);}); | ||
| 32 | }, | ||
| 33 | |||
| 34 | //decompress from an output of compressToEncodedURIComponent | ||
| 35 | decompressFromEncodedURIComponent:function (input) { | ||
| 36 | if (input == null) return ""; | ||
| 37 | if (input == "") return null; | ||
| 38 | input = input.replace(/ /g, "+"); | ||
| 39 | return LZString._decompress(input.length, 32, function(index) { return getBaseValue(keyStrUriSafe, input.charAt(index)); }); | ||
| 40 | }, | ||
| 41 | |||
| 42 | _compress: function (uncompressed, bitsPerChar, getCharFromInt) { | ||
| 43 | if (uncompressed == null) return ""; | ||
| 44 | var i, value, | ||
| 45 | context_dictionary= {}, | ||
| 46 | context_dictionaryToCreate= {}, | ||
| 47 | context_c="", | ||
| 48 | context_wc="", | ||
| 49 | context_w="", | ||
| 50 | context_enlargeIn= 2, // Compensate for the first entry which should not count | ||
| 51 | context_dictSize= 3, | ||
| 52 | context_numBits= 2, | ||
| 53 | context_data=[], | ||
| 54 | context_data_val=0, | ||
| 55 | context_data_position=0, | ||
| 56 | ii; | ||
| 57 | |||
| 58 | for (ii = 0; ii < uncompressed.length; ii += 1) { | ||
| 59 | context_c = uncompressed.charAt(ii); | ||
| 60 | if (!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)) { | ||
| 61 | context_dictionary[context_c] = context_dictSize++; | ||
| 62 | context_dictionaryToCreate[context_c] = true; | ||
| 63 | } | ||
| 64 | |||
| 65 | context_wc = context_w + context_c; | ||
| 66 | if (Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)) { | ||
| 67 | context_w = context_wc; | ||
| 68 | } else { | ||
| 69 | if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) { | ||
| 70 | if (context_w.charCodeAt(0)<256) { | ||
| 71 | for (i=0 ; i<context_numBits ; i++) { | ||
| 72 | context_data_val = (context_data_val << 1); | ||
| 73 | if (context_data_position == bitsPerChar-1) { | ||
| 74 | context_data_position = 0; | ||
| 75 | context_data.push(getCharFromInt(context_data_val)); | ||
| 76 | context_data_val = 0; | ||
| 77 | } else { | ||
| 78 | context_data_position++; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | value = context_w.charCodeAt(0); | ||
| 82 | for (i=0 ; i<8 ; i++) { | ||
| 83 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 84 | if (context_data_position == bitsPerChar-1) { | ||
| 85 | context_data_position = 0; | ||
| 86 | context_data.push(getCharFromInt(context_data_val)); | ||
| 87 | context_data_val = 0; | ||
| 88 | } else { | ||
| 89 | context_data_position++; | ||
| 90 | } | ||
| 91 | value = value >> 1; | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | value = 1; | ||
| 95 | for (i=0 ; i<context_numBits ; i++) { | ||
| 96 | context_data_val = (context_data_val << 1) | value; | ||
| 97 | if (context_data_position ==bitsPerChar-1) { | ||
| 98 | context_data_position = 0; | ||
| 99 | context_data.push(getCharFromInt(context_data_val)); | ||
| 100 | context_data_val = 0; | ||
| 101 | } else { | ||
| 102 | context_data_position++; | ||
| 103 | } | ||
| 104 | value = 0; | ||
| 105 | } | ||
| 106 | value = context_w.charCodeAt(0); | ||
| 107 | for (i=0 ; i<16 ; i++) { | ||
| 108 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 109 | if (context_data_position == bitsPerChar-1) { | ||
| 110 | context_data_position = 0; | ||
| 111 | context_data.push(getCharFromInt(context_data_val)); | ||
| 112 | context_data_val = 0; | ||
| 113 | } else { | ||
| 114 | context_data_position++; | ||
| 115 | } | ||
| 116 | value = value >> 1; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | context_enlargeIn--; | ||
| 120 | if (context_enlargeIn == 0) { | ||
| 121 | context_enlargeIn = Math.pow(2, context_numBits); | ||
| 122 | context_numBits++; | ||
| 123 | } | ||
| 124 | delete context_dictionaryToCreate[context_w]; | ||
| 125 | } else { | ||
| 126 | value = context_dictionary[context_w]; | ||
| 127 | for (i=0 ; i<context_numBits ; i++) { | ||
| 128 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 129 | if (context_data_position == bitsPerChar-1) { | ||
| 130 | context_data_position = 0; | ||
| 131 | context_data.push(getCharFromInt(context_data_val)); | ||
| 132 | context_data_val = 0; | ||
| 133 | } else { | ||
| 134 | context_data_position++; | ||
| 135 | } | ||
| 136 | value = value >> 1; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | } | ||
| 141 | context_enlargeIn--; | ||
| 142 | if (context_enlargeIn == 0) { | ||
| 143 | context_enlargeIn = Math.pow(2, context_numBits); | ||
| 144 | context_numBits++; | ||
| 145 | } | ||
| 146 | // Add wc to the dictionary. | ||
| 147 | context_dictionary[context_wc] = context_dictSize++; | ||
| 148 | context_w = String(context_c); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | // Output the code for w. | ||
| 153 | if (context_w !== "") { | ||
| 154 | if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) { | ||
| 155 | if (context_w.charCodeAt(0)<256) { | ||
| 156 | for (i=0 ; i<context_numBits ; i++) { | ||
| 157 | context_data_val = (context_data_val << 1); | ||
| 158 | if (context_data_position == bitsPerChar-1) { | ||
| 159 | context_data_position = 0; | ||
| 160 | context_data.push(getCharFromInt(context_data_val)); | ||
| 161 | context_data_val = 0; | ||
| 162 | } else { | ||
| 163 | context_data_position++; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | value = context_w.charCodeAt(0); | ||
| 167 | for (i=0 ; i<8 ; i++) { | ||
| 168 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 169 | if (context_data_position == bitsPerChar-1) { | ||
| 170 | context_data_position = 0; | ||
| 171 | context_data.push(getCharFromInt(context_data_val)); | ||
| 172 | context_data_val = 0; | ||
| 173 | } else { | ||
| 174 | context_data_position++; | ||
| 175 | } | ||
| 176 | value = value >> 1; | ||
| 177 | } | ||
| 178 | } else { | ||
| 179 | value = 1; | ||
| 180 | for (i=0 ; i<context_numBits ; i++) { | ||
| 181 | context_data_val = (context_data_val << 1) | value; | ||
| 182 | if (context_data_position == bitsPerChar-1) { | ||
| 183 | context_data_position = 0; | ||
| 184 | context_data.push(getCharFromInt(context_data_val)); | ||
| 185 | context_data_val = 0; | ||
| 186 | } else { | ||
| 187 | context_data_position++; | ||
| 188 | } | ||
| 189 | value = 0; | ||
| 190 | } | ||
| 191 | value = context_w.charCodeAt(0); | ||
| 192 | for (i=0 ; i<16 ; i++) { | ||
| 193 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 194 | if (context_data_position == bitsPerChar-1) { | ||
| 195 | context_data_position = 0; | ||
| 196 | context_data.push(getCharFromInt(context_data_val)); | ||
| 197 | context_data_val = 0; | ||
| 198 | } else { | ||
| 199 | context_data_position++; | ||
| 200 | } | ||
| 201 | value = value >> 1; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | context_enlargeIn--; | ||
| 205 | if (context_enlargeIn == 0) { | ||
| 206 | context_enlargeIn = Math.pow(2, context_numBits); | ||
| 207 | context_numBits++; | ||
| 208 | } | ||
| 209 | delete context_dictionaryToCreate[context_w]; | ||
| 210 | } else { | ||
| 211 | value = context_dictionary[context_w]; | ||
| 212 | for (i=0 ; i<context_numBits ; i++) { | ||
| 213 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 214 | if (context_data_position == bitsPerChar-1) { | ||
| 215 | context_data_position = 0; | ||
| 216 | context_data.push(getCharFromInt(context_data_val)); | ||
| 217 | context_data_val = 0; | ||
| 218 | } else { | ||
| 219 | context_data_position++; | ||
| 220 | } | ||
| 221 | value = value >> 1; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | } | ||
| 226 | context_enlargeIn--; | ||
| 227 | if (context_enlargeIn == 0) { | ||
| 228 | context_enlargeIn = Math.pow(2, context_numBits); | ||
| 229 | context_numBits++; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | // Mark the end of the stream | ||
| 234 | value = 2; | ||
| 235 | for (i=0 ; i<context_numBits ; i++) { | ||
| 236 | context_data_val = (context_data_val << 1) | (value&1); | ||
| 237 | if (context_data_position == bitsPerChar-1) { | ||
| 238 | context_data_position = 0; | ||
| 239 | context_data.push(getCharFromInt(context_data_val)); | ||
| 240 | context_data_val = 0; | ||
| 241 | } else { | ||
| 242 | context_data_position++; | ||
| 243 | } | ||
| 244 | value = value >> 1; | ||
| 245 | } | ||
| 246 | |||
| 247 | // Flush the last char | ||
| 248 | while (true) { | ||
| 249 | context_data_val = (context_data_val << 1); | ||
| 250 | if (context_data_position == bitsPerChar-1) { | ||
| 251 | context_data.push(getCharFromInt(context_data_val)); | ||
| 252 | break; | ||
| 253 | } | ||
| 254 | else context_data_position++; | ||
| 255 | } | ||
| 256 | return context_data.join(''); | ||
| 257 | }, | ||
| 258 | |||
| 259 | _decompress: function (length, resetValue, getNextValue) { | ||
| 260 | var dictionary = [], | ||
| 261 | next, | ||
| 262 | enlargeIn = 4, | ||
| 263 | dictSize = 4, | ||
| 264 | numBits = 3, | ||
| 265 | entry = "", | ||
| 266 | result = [], | ||
| 267 | i, | ||
| 268 | w, | ||
| 269 | bits, resb, maxpower, power, | ||
| 270 | c, | ||
| 271 | data = {val:getNextValue(0), position:resetValue, index:1}; | ||
| 272 | |||
| 273 | for (i = 0; i < 3; i += 1) { | ||
| 274 | dictionary[i] = i; | ||
| 275 | } | ||
| 276 | |||
| 277 | bits = 0; | ||
| 278 | maxpower = Math.pow(2,2); | ||
| 279 | power=1; | ||
| 280 | while (power!=maxpower) { | ||
| 281 | resb = data.val & data.position; | ||
| 282 | data.position >>= 1; | ||
| 283 | if (data.position == 0) { | ||
| 284 | data.position = resetValue; | ||
| 285 | data.val = getNextValue(data.index++); | ||
| 286 | } | ||
| 287 | bits |= (resb>0 ? 1 : 0) * power; | ||
| 288 | power <<= 1; | ||
| 289 | } | ||
| 290 | |||
| 291 | switch (next = bits) { | ||
| 292 | case 0: | ||
| 293 | bits = 0; | ||
| 294 | maxpower = Math.pow(2,8); | ||
| 295 | power=1; | ||
| 296 | while (power!=maxpower) { | ||
| 297 | resb = data.val & data.position; | ||
| 298 | data.position >>= 1; | ||
| 299 | if (data.position == 0) { | ||
| 300 | data.position = resetValue; | ||
| 301 | data.val = getNextValue(data.index++); | ||
| 302 | } | ||
| 303 | bits |= (resb>0 ? 1 : 0) * power; | ||
| 304 | power <<= 1; | ||
| 305 | } | ||
| 306 | c = f(bits); | ||
| 307 | break; | ||
| 308 | case 1: | ||
| 309 | bits = 0; | ||
| 310 | maxpower = Math.pow(2,16); | ||
| 311 | power=1; | ||
| 312 | while (power!=maxpower) { | ||
| 313 | resb = data.val & data.position; | ||
| 314 | data.position >>= 1; | ||
| 315 | if (data.position == 0) { | ||
| 316 | data.position = resetValue; | ||
| 317 | data.val = getNextValue(data.index++); | ||
| 318 | } | ||
| 319 | bits |= (resb>0 ? 1 : 0) * power; | ||
| 320 | power <<= 1; | ||
| 321 | } | ||
| 322 | c = f(bits); | ||
| 323 | break; | ||
| 324 | case 2: | ||
| 325 | return ""; | ||
| 326 | } | ||
| 327 | dictionary[3] = c; | ||
| 328 | w = c; | ||
| 329 | result.push(c); | ||
| 330 | while (true) { | ||
| 331 | if (data.index > length) { | ||
| 332 | return ""; | ||
| 333 | } | ||
| 334 | |||
| 335 | bits = 0; | ||
| 336 | maxpower = Math.pow(2,numBits); | ||
| 337 | power=1; | ||
| 338 | while (power!=maxpower) { | ||
| 339 | resb = data.val & data.position; | ||
| 340 | data.position >>= 1; | ||
| 341 | if (data.position == 0) { | ||
| 342 | data.position = resetValue; | ||
| 343 | data.val = getNextValue(data.index++); | ||
| 344 | } | ||
| 345 | bits |= (resb>0 ? 1 : 0) * power; | ||
| 346 | power <<= 1; | ||
| 347 | } | ||
| 348 | |||
| 349 | switch (c = bits) { | ||
| 350 | case 0: | ||
| 351 | bits = 0; | ||
| 352 | maxpower = Math.pow(2,8); | ||
| 353 | power=1; | ||
| 354 | while (power!=maxpower) { | ||
| 355 | resb = data.val & data.position; | ||
| 356 | data.position >>= 1; | ||
| 357 | if (data.position == 0) { | ||
| 358 | data.position = resetValue; | ||
| 359 | data.val = getNextValue(data.index++); | ||
| 360 | } | ||
| 361 | bits |= (resb>0 ? 1 : 0) * power; | ||
| 362 | power <<= 1; | ||
| 363 | } | ||
| 364 | |||
| 365 | dictionary[dictSize++] = f(bits); | ||
| 366 | c = dictSize-1; | ||
| 367 | enlargeIn--; | ||
| 368 | break; | ||
| 369 | case 1: | ||
| 370 | bits = 0; | ||
| 371 | maxpower = Math.pow(2,16); | ||
| 372 | power=1; | ||
| 373 | while (power!=maxpower) { | ||
| 374 | resb = data.val & data.position; | ||
| 375 | data.position >>= 1; | ||
| 376 | if (data.position == 0) { | ||
| 377 | data.position = resetValue; | ||
| 378 | data.val = getNextValue(data.index++); | ||
| 379 | } | ||
| 380 | bits |= (resb>0 ? 1 : 0) * power; | ||
| 381 | power <<= 1; | ||
| 382 | } | ||
| 383 | dictionary[dictSize++] = f(bits); | ||
| 384 | c = dictSize-1; | ||
| 385 | enlargeIn--; | ||
| 386 | break; | ||
| 387 | case 2: | ||
| 388 | return result.join(''); | ||
| 389 | } | ||
| 390 | |||
| 391 | if (enlargeIn == 0) { | ||
| 392 | enlargeIn = Math.pow(2, numBits); | ||
| 393 | numBits++; | ||
| 394 | } | ||
| 395 | |||
| 396 | if (dictionary[c]) { | ||
| 397 | entry = dictionary[c]; | ||
| 398 | } else { | ||
| 399 | if (c === dictSize) { | ||
| 400 | entry = w + w.charAt(0); | ||
| 401 | } else { | ||
| 402 | return null; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | result.push(entry); | ||
| 406 | |||
| 407 | // Add w+entry[0] to the dictionary. | ||
| 408 | dictionary[dictSize++] = w + entry.charAt(0); | ||
| 409 | enlargeIn--; | ||
| 410 | |||
| 411 | w = entry; | ||
| 412 | |||
| 413 | if (enlargeIn == 0) { | ||
| 414 | enlargeIn = Math.pow(2, numBits); | ||
| 415 | numBits++; | ||
| 416 | } | ||
| 417 | |||
| 418 | } | ||
| 419 | } | ||
| 420 | }; | ||
| 421 | return LZString; | ||
| 422 | })(); | ||
| 423 | |||
| 424 | |||
| 425 | export default LzStringModule; \ No newline at end of file | ||