/* eslint-disable no-console */ /* eslint-disable no-alert */ /* eslint-disable no-undef */ /* eslint-disable no-new */ 'use strict'; new Vue({ el: '#app', components: { apexchart: VueApexCharts, }, data: { authenticated: null, authenticating: false, password: null, requiresPassword: null, clients: null, clientsPersist: {}, clientDelete: null, clientCreate: null, clientCreateName: '', clientEditName: null, clientEditNameId: null, clientEditAddress: null, clientEditAddressId: null, qrcode: null, currentRelease: null, latestRelease: null, chartOptions: { chart: { background: 'transparent', type: 'area', toolbar: { show: false, }, }, fill: { type: 'gradient', }, colors: ['#CCCCCC'], dataLabels: { enabled: false, }, stroke: { curve: 'smooth', width: 0, }, xaxis: { labels: { show: false, }, axisTicks: { show: false, }, axisBorder: { show: false, }, }, yaxis: { labels: { show: false, }, min: 0, }, tooltip: { enabled: false, }, legend: { show: false, }, grid: { show: false, padding: { left: -10, right: 0, bottom: -15, top: -15, }, column: { opacity: 0, }, xaxis: { lines: { show: false, }, }, }, }, }, methods: { dateTime: value => { return new Intl.DateTimeFormat(undefined, { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', }).format(value); }, async refresh() { if (!this.authenticated) return; const clients = await this.api.getClients(); this.clients = clients.map(client => { if (client.name.includes('@') && client.name.includes('.')) { client.avatar = `https://www.gravatar.com/avatar/${md5(client.name)}?d=blank`; } if (!this.clientsPersist[client.id]) { this.clientsPersist[client.id] = {}; this.clientsPersist[client.id].transferRxHistory = Array(20).fill(0); this.clientsPersist[client.id].transferRxPrevious = client.transferRx; this.clientsPersist[client.id].transferTxHistory = Array(20).fill(0); this.clientsPersist[client.id].transferTxPrevious = client.transferTx; this.clientsPersist[client.id].chartOptions = { ...this.chartOptions, yaxis: { ...this.chartOptions.yaxis, max: () => this.clientsPersist[client.id].chartMax, }, }; } this.clientsPersist[client.id].transferRxCurrent = client.transferRx - this.clientsPersist[client.id].transferRxPrevious; this.clientsPersist[client.id].transferRxPrevious = client.transferRx; this.clientsPersist[client.id].transferTxCurrent = client.transferTx - this.clientsPersist[client.id].transferTxPrevious; this.clientsPersist[client.id].transferTxPrevious = client.transferTx; this.clientsPersist[client.id].transferRxHistory.push(this.clientsPersist[client.id].transferRxCurrent); this.clientsPersist[client.id].transferRxHistory.shift(); this.clientsPersist[client.id].transferTxHistory.push(this.clientsPersist[client.id].transferTxCurrent); this.clientsPersist[client.id].transferTxHistory.shift(); client.transferTxCurrent = this.clientsPersist[client.id].transferTxCurrent; client.transferTxSeries = [{ name: 'tx', data: this.clientsPersist[client.id].transferTxHistory, }]; client.transferRxCurrent = this.clientsPersist[client.id].transferRxCurrent; client.transferRxSeries = [{ name: 'rx', data: this.clientsPersist[client.id].transferRxHistory, }]; this.clientsPersist[client.id].chartMax = Math.max(...this.clientsPersist[client.id].transferTxHistory, ...this.clientsPersist[client.id].transferRxHistory); client.chartOptions = this.clientsPersist[client.id].chartOptions; return client; }); }, login(e) { e.preventDefault(); if (!this.password) return; if (this.authenticating) return; this.authenticating = true; this.api.createSession({ password: this.password, }) .then(async () => { const session = await this.api.getSession(); this.authenticated = session.authenticated; this.requiresPassword = session.requiresPassword; return this.refresh(); }) .catch(err => { alert(err.message || err.toString()); }) .finally(() => { this.authenticating = false; this.password = null; }); }, logout(e) { e.preventDefault(); this.api.deleteSession() .then(() => { this.authenticated = false; this.clients = null; }) .catch(err => { alert(err.message || err.toString()); }); }, createClient() { const name = this.clientCreateName; if (!name) return; this.api.createClient({ name }) .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, deleteClient(client) { this.api.deleteClient({ clientId: client.id }) .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, enableClient(client) { this.api.enableClient({ clientId: client.id }) .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, disableClient(client) { this.api.disableClient({ clientId: client.id }) .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, updateClientName(client, name) { this.api.updateClientName({ clientId: client.id, name }) .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, updateClientAddress(client, address) { this.api.updateClientAddress({ clientId: client.id, address }) .catch(err => alert(err.message || err.toString())) .finally(() => this.refresh().catch(console.error)); }, }, filters: { timeago: value => { return timeago().format(value); }, bytes: (bytes, decimals, kib, maxunit) => { kib = kib || false; if (bytes === 0) return '0 B'; if (Number.isNaN(parseFloat(bytes)) && !Number.isFinite(bytes)) return 'NaN'; const k = kib ? 1024 : 1000; const dm = decimals != null && !Number.isNaN(decimals) && decimals >= 0 ? decimals : 2; const sizes = kib ? ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB', 'BiB'] : ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB']; let i = Math.floor(Math.log(bytes) / Math.log(k)); if (maxunit !== undefined) { const index = sizes.indexOf(maxunit); if (index !== -1) i = index; } // eslint-disable-next-line no-restricted-properties return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; }, }, mounted() { this.api = new API(); this.api.getSession() .then(session => { this.authenticated = session.authenticated; this.requiresPassword = session.requiresPassword; this.refresh().catch(err => { alert(err.message || err.toString()); }); }) .catch(err => { alert(err.message || err.toString()); }); setInterval(() => { this.refresh().catch(console.error); }, 1000); Promise.resolve().then(async () => { const currentRelease = await this.api.getRelease(); const latestRelease = await fetch('https://weejewel.github.io/wg-easy/changelog.json') .then(res => res.json()) .then(releases => { const releasesArray = Object.entries(releases).map(([version, changelog]) => ({ version: parseInt(version, 10), changelog, })); releasesArray.sort((a, b) => { return b.version - a.version; }); return releasesArray[0]; }); console.log(`Current Release: ${currentRelease}`); console.log(`Latest Release: ${latestRelease.version}`); if (currentRelease >= latestRelease.version) return; this.currentRelease = currentRelease; this.latestRelease = latestRelease; }).catch(console.error); }, });