|
| 1 | +{ |
| 2 | + const data = __data__ // eslint-disable-line no-undef |
| 3 | + const defaultItems = [ 'nickname', 'myaccount', 'divider', 'kredit', 'recharge', 'divider', 'logout' ] |
| 4 | + const shortItems = { |
| 5 | + divider: () => ({ _isDivider: true, text: null }), |
| 6 | + nickname: () => ({ text: esc(data.nickname) }), |
| 7 | + myaccount: () => ({ text: '管理帐号信息', link: url('/') }), |
| 8 | + kredit: () => ({ text: 'Kredit 余额:' + data.kredit / 100 }), |
| 9 | + recharge: () => ({ text: '充值', link: url('/recharge') }), |
| 10 | + logout: () => ({ text: '退出登录', id: 'idframe-log-out', onclick: () => window.idFrame.logout() }), |
| 11 | + } |
| 12 | + |
| 13 | + const url = path => data.base + path |
| 14 | + const esc = str => str.replace(/</g, '<').replace(/>/g, '>') |
| 15 | + if (!('idFrame' in window)) window.idFrame = {} |
| 16 | + if (!('logout' in window.idFrame)) window.idFrame.logout = () => { |
| 17 | + const req = new XMLHttpRequest() |
| 18 | + req.onload = () => location.reload() |
| 19 | + req.onerror = () => alert('网络错误') |
| 20 | + req.open('DELETE', url('/api/token?set-cookie=true')) |
| 21 | + } |
| 22 | + |
| 23 | + const waitMdc = new Promise((resolve, reject) => { |
| 24 | + const timeout = 300000 |
| 25 | + setTimeout(reject, timeout) |
| 26 | + const intervalId = setInterval(() => { |
| 27 | + if (!('mdc' in window)) return |
| 28 | + if ('ripple' in window.mdc && 'menu' in window.mdc) return resolve() |
| 29 | + }, 200) |
| 30 | + setTimeout(() => clearInterval(intervalId), timeout) |
| 31 | + }) |
| 32 | + |
| 33 | + /** |
| 34 | + * Class representing a IDFrame object. |
| 35 | + * @constructor |
| 36 | + * @param {string|HTMLElement} config.container IDFrame container, string will be interpreted as selector |
| 37 | + * @param {string} [config.loginUrl] login button URL |
| 38 | + * @param {string} [config.signupUrl] signup button URL |
| 39 | + * @param {(string|object)[]} [config.items] items to show |
| 40 | + * @param {string} [config.serviceName] KAS service name to include in login and sign up URLs |
| 41 | + */ |
| 42 | + window.idFrame.AppBarFrame = class AppBarFrame { |
| 43 | + constructor (config) { |
| 44 | + this.config = config |
| 45 | + if (!(config.container instanceof HTMLElement)) { |
| 46 | + if (typeof config.container === 'string') config.container = document.querySelector(config.container) |
| 47 | + if (!(config.container instanceof HTMLElement)) throw new Error('Container not found.') |
| 48 | + } |
| 49 | + this.container = config.container |
| 50 | + if (!config.serviceName) { |
| 51 | + this.loginUrl = config.loginUrl || url('/login#login') |
| 52 | + this.signupUrl = config.signupUrl || url('/login#signup') |
| 53 | + } else { |
| 54 | + this.loginUrl = config.loginUrl || url(`/login?service=${config.serviceName}#login`) |
| 55 | + this.signupUrl = config.signupUrl || url(`/login?service=${config.serviceName}#signup`) |
| 56 | + } |
| 57 | + this.ready = this._init() |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Initializes frame UI. |
| 62 | + * @private |
| 63 | + */ |
| 64 | + _init () { |
| 65 | + return waitMdc.then(() => { |
| 66 | + if (!data.loggedIn) { |
| 67 | + this.container.innerHTML = ` |
| 68 | + <span class="idframe idframe--appbar idframe--not-logged-in"> |
| 69 | + <a class="mdc-button" href="${this.loginUrl}"><div class="mdc-button__ripple"></div><span class="mdc-button__label">登录</span></a> |
| 70 | + <a class="mdc-button mdc-button--outlined" href="${this.signupUrl}"><div class="mdc-button__ripple"></div><span class="mdc-button__label">注册</span></a> |
| 71 | + </span>` |
| 72 | + const els = this.container.querySelectorAll('.mdc-button') |
| 73 | + for (let i = 0; i < els.length; i++) window.mdc.ripple.MDCRipple.attachTo(els[i]) |
| 74 | + } else { // logged in |
| 75 | + this.container.innerHTML = `<span class="idframe idframe--appbar" role="button" title="KEEER 帐号"> |
| 76 | + <span class="idframe--appbar__avatar" style="background-image: url('${data.avatar})"></span> |
| 77 | + <span class="idframe--appbar__nickname" dir="ltr">${esc(data.nickname)}</span> |
| 78 | + <span class="idframe--appbar__dropdown-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M7 10l5 5 5-5z"/><path d="M0 0h24v24H0z" fill="none"/></svg></span> |
| 79 | + </span>` |
| 80 | + this.container.firstElementChild.onclick = () => this._menu.open = true |
| 81 | + this.updateItems(this.config.items || [].concat(defaultItems)) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Gives layout of frame items. |
| 88 | + * @private |
| 89 | + */ |
| 90 | + _handleItems () { |
| 91 | + let html = '<ul class="mdc-list" role="menu" aria-hidden="true" aria-orientation="vertical" tabindex="-1">' |
| 92 | + const listeners = {} |
| 93 | + for (let i = 0; i < this.items.length; i++) { |
| 94 | + let item = this.items[i] |
| 95 | + if (typeof item === 'string') { |
| 96 | + if (!(item in shortItems)) throw new Error(`Item ${item} not found.`) |
| 97 | + item = shortItems[item](this) |
| 98 | + } |
| 99 | + if (!('text' in item)) throw new Error('Invalid item ' + item) |
| 100 | + if (item._isDivider) html += '<li role="separator" class="mdc-list-divider"></li>' |
| 101 | + else if ('link' in item) { |
| 102 | + html += `<a class="mdc-list-item" role="menuitem" href="${item.link}"><span class="mdc-list-item__text">${esc(item.text)}</span></a>` |
| 103 | + } else if ('onclick' in item) { |
| 104 | + if (!('id' in item)) throw new Error(`Item ${item} has an onclick listener without ID.`) |
| 105 | + const id = `${item.id}-${Math.floor(Math.random() * 100000)}` |
| 106 | + listeners[id] = item.onclick |
| 107 | + html += `<a id="${id}" class="mdc-list-item" role="menuitem" href="javascript:;"><span class="mdc-list-item__text">${esc(item.text)}</span></a>` |
| 108 | + } else { |
| 109 | + html += `<li class="mdc-list-item idframe-list-item--disabled" role="menuitem"><span class="mdc-list-item__text">${esc(item.text)}</span></li>` |
| 110 | + } |
| 111 | + } |
| 112 | + if (!this._menuEl) { |
| 113 | + const el = document.createElement('span') |
| 114 | + el.className = 'mdc-menu-surface--anchor' |
| 115 | + this._menuEl = document.createElement('div') |
| 116 | + this._menuEl.className = 'mdc-menu mdc-menu-surface' |
| 117 | + el.appendChild(this._menuEl) |
| 118 | + this.ready.then(() => this.container.firstElementChild.appendChild(el)) |
| 119 | + } |
| 120 | + html += '</ul>' |
| 121 | + this._menuEl.innerHTML = html |
| 122 | + this._menuEl.querySelector('ul').addEventListener('click', function (e) { e.stopPropagation() }) |
| 123 | + this._menu = new window.mdc.menu.MDCMenu(this._menuEl) |
| 124 | + const els = this._menu.list_.listElements |
| 125 | + for (let i = 0; i < els.length; i++) new window.mdc.ripple.MDCRipple(els[i]) // eslint-disable-line no-new |
| 126 | + setTimeout(() => { |
| 127 | + for (const id in listeners) document.getElementById(id).addEventListener('click', listeners[id]) |
| 128 | + }, 100) |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Updates menu items. |
| 133 | + * @param {(string|object)[]} items items to be displayed. |
| 134 | + */ |
| 135 | + updateItems (items) { |
| 136 | + if (!Array.isArray(items)) throw new TypeError('Items not an array') |
| 137 | + const oldItems = this.items |
| 138 | + this.items = items |
| 139 | + try { |
| 140 | + this._handleItems() |
| 141 | + } catch (e) { |
| 142 | + this.items = oldItems |
| 143 | + this._handleItems() |
| 144 | + throw e |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // initialize styles |
| 150 | + const styleEl = document.createElement('style') |
| 151 | + styleEl.innerHTML = ` |
| 152 | + .idframe--appbar { |
| 153 | + cursor: pointer; |
| 154 | + user-select: none; |
| 155 | + display: flex; |
| 156 | + align-items: center; |
| 157 | + } |
| 158 | + .idframe--not-logged-in { |
| 159 | + margin-top: 2px; |
| 160 | + margin-right: 2px; |
| 161 | + } |
| 162 | + .idframe--appbar__nickname { |
| 163 | + font-size: 16px; |
| 164 | + margin: 0 4px 0 8px; |
| 165 | + color: var(--mdc-theme-primary, black); |
| 166 | + } |
| 167 | + @media(max-width: 599px) { |
| 168 | + .idframe--appbar__nickname { |
| 169 | + display: none; |
| 170 | + } |
| 171 | + } |
| 172 | + .idframe--appbar__avatar { |
| 173 | + width: 40px; |
| 174 | + height: 40px; |
| 175 | + background-size: contain; |
| 176 | + display: inline-block; |
| 177 | + border-radius: 4px; |
| 178 | + } |
| 179 | + .idframe--appbar .mdc-menu-surface--anchor { |
| 180 | + align-self: start; |
| 181 | + } |
| 182 | + .idframe--appbar .idframe-list-item--disabled .mdc-list-item__text { |
| 183 | + opacity: .5; |
| 184 | + } |
| 185 | + .idframe--appbar__dropdown-icon, .idframe--appbar__dropdown-icon svg { |
| 186 | + display: inline-block; |
| 187 | + width: 20px; |
| 188 | + height: 20px; |
| 189 | + opacity: .8; |
| 190 | + fill: var(--mdc-theme-primary, black); |
| 191 | + }` |
| 192 | + document.head.appendChild(styleEl) |
| 193 | + |
| 194 | + // initialize MDC |
| 195 | + const used = { |
| 196 | + 'mdc.button.': [ 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.button.min.css' ], |
| 197 | + 'mdc.ripple.': [ 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.ripple.min.css', 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.ripple.min.js' ], |
| 198 | + 'mdc.list.': [ 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.list.min.css' ], |
| 199 | + 'mdc.menu-surface.': [ 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.menu-surface.min.css' ], |
| 200 | + 'mdc.menu.': [ 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.menu.min.css', 'https://cdn.jsdelivr.net/npm/@material/[email protected]/dist/mdc.menu.min.js' ], |
| 201 | + } |
| 202 | + for (const i in used) { |
| 203 | + let has = !used[i][0] |
| 204 | + if (!('mdc' in window.idFrame) || !window.idFrame.mdc.style) { |
| 205 | + for (let j = 0; j < document.styleSheets.length; j++) { |
| 206 | + if ((document.styleSheets[j].href || '').includes(i)) has = true |
| 207 | + } |
| 208 | + if (!has) { |
| 209 | + const el = document.createElement('link') |
| 210 | + el.rel = 'stylesheet' |
| 211 | + el.href = used[i][0] |
| 212 | + document.head.appendChild(el) |
| 213 | + } |
| 214 | + } |
| 215 | + if (!('mdc' in window.idFrame) || !window.idFrame.mdc.script) { |
| 216 | + has = !used[i][1] |
| 217 | + for (let j = 0; j < document.scripts.length; j++) { |
| 218 | + if ((document.scripts[j].src || '').includes(i)) has = true |
| 219 | + } |
| 220 | + if (!has) { |
| 221 | + const el = document.createElement('script') |
| 222 | + el.src = used[i][1] |
| 223 | + document.head.appendChild(el) |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments