From f67bda4f46dfb1bf77fee3697c2393ae81631570 Mon Sep 17 00:00:00 2001 From: CzBiX Date: Sun, 29 Mar 2020 18:12:53 +0800 Subject: [PATCH] Refine to use Vue class component --- src/App.vue | 172 ++++---- src/components/AddForm.vue | 232 +++++------ src/components/Drawer.vue | 273 ++++++------ src/components/Footer.vue | 133 +++--- src/components/Torrents.vue | 391 +++++++++--------- .../dialogs/ConfirmDeleteDialog.vue | 90 ++-- .../dialogs/ConfirmSetCategoryDialog.vue | 89 ++-- src/components/dialogs/EditTrackerDialog.vue | 173 ++++---- src/components/dialogs/InfoDialog.vue | 71 ++-- src/components/dialogs/LogsDialog.vue | 82 ++-- src/components/dialogs/Peers.vue | 131 +++--- src/components/dialogs/TorrentContent.vue | 208 +++++----- src/components/dialogs/TorrentInfo.vue | 251 +++++------ src/components/dialogs/Trackers.vue | 78 ++-- src/components/dialogs/baseTorrentInfo.ts | 37 ++ src/mixins/hasTask.ts | 6 +- src/mixins/taskable.ts | 22 - src/store/config.ts | 4 +- src/store/types.ts | 11 + src/types.ts | 41 +- 20 files changed, 1244 insertions(+), 1251 deletions(-) create mode 100644 src/components/dialogs/baseTorrentInfo.ts delete mode 100644 src/mixins/taskable.ts diff --git a/src/App.vue b/src/App.vue index a0f095b..c7095a5 100644 --- a/src/App.vue +++ b/src/App.vue @@ -77,12 +77,14 @@ import LogsDialog from './components/dialogs/LogsDialog.vue'; import RssDialog from './components/dialogs/RssDialog.vue'; import api from './Api'; -import { sleep } from './utils'; +import { timeout } from './utils'; +import Component from 'vue-class-component'; +import { Watch } from 'vue-property-decorator'; +import { MainData } from './types'; let appWrapEl: HTMLElement; -export default Vue.extend({ - name: 'app', +@Component({ components: { AddForm, Drawer, @@ -95,28 +97,6 @@ export default Vue.extend({ GlobalSnackBar, RssDialog, }, - data() { - return { - needAuth: false, - drawer: true, - drawerOptions: { - showLogs: false, - showRss: false, - }, - task: 0, - }; - }, - async created() { - await this.getInitData(); - appWrapEl = (this.$refs.app as any).$el.querySelector('.v-application--wrap'); - appWrapEl.addEventListener('paste', this.onPaste); - }, - beforeDestroy() { - if (this.task) { - clearTimeout(this.task); - } - appWrapEl.removeEventListener('paste', this.onPaste); - }, computed: { ...mapState([ 'mainData', @@ -124,9 +104,6 @@ export default Vue.extend({ 'preferences', ]), ...mapGetters(['config']), - phoneLayout() { - return this.$vuetify.breakpoint.xsOnly; - }, }, methods: { ...mapMutations([ @@ -134,64 +111,105 @@ export default Vue.extend({ 'updatePreferences', 'setPasteUrl', ]), - async getInitData() { - try { - await this.getMainData(); - } catch (e) { - if (e.response.status === 403) { - this.needAuth = true; - } + } +}) +export default class App extends Vue { + needAuth = false + drawer = true + drawerOptions = { + showLogs: false, + showRss: false, + } + task = 0 - return; + mainData!: MainData + rid!: number + preferences!: any + config!: any + + updateMainData!: (_: any) => void + updatePreferences!: (_: any) => void + setPasteUrl!: (_: any) => void + + get phoneLayout() { + return this.$vuetify.breakpoint.xsOnly; + } + + async created() { + await this.getInitData(); + appWrapEl = (this.$refs.app as any).$el.querySelector('.v-application--wrap'); + appWrapEl.addEventListener('paste', this.onPaste); + } + + beforeDestroy() { + if (this.task) { + clearTimeout(this.task); + } + appWrapEl.removeEventListener('paste', this.onPaste); + } + + async getInitData() { + try { + await this.getMainData(); + } catch (e) { + if (e.response.status === 403) { + this.needAuth = true; } - await this.getPreferences(); - }, - async getPreferences() { - const resp = await api.getAppPreferences(); + return; + } - this.updatePreferences(resp.data); - }, - async getMainData() { - const rid = this.rid ? this.rid : null; - const resp = await api.getMainData(rid); - const mainData = resp.data; + await this.getPreferences(); + } - this.updateMainData(mainData); + async getPreferences() { + const resp = await api.getAppPreferences(); - this.task = setTimeout(this.getMainData, this.config.updateInterval); - }, - async drawerFooterOpen(v: boolean) { - if (!v) { - return; - } - await sleep(3000); + this.updatePreferences(resp.data); + } - (this.$refs.end as HTMLElement).scrollIntoView({ - behavior: 'smooth', + async getMainData() { + const rid = this.rid ? this.rid : undefined; + const resp = await api.getMainData(rid); + const mainData = resp.data; + + this.updateMainData(mainData); + + this.task = setTimeout(this.getMainData, this.config.updateInterval); + } + + async drawerFooterOpen(v: boolean) { + if (!v) { + return; + } + + await timeout(3000); + + (this.$refs.end as HTMLElement).scrollIntoView({ + behavior: 'smooth', + }); + } + + onPaste(e: ClipboardEvent) { + if ((e.target as HTMLElement).tagName === 'INPUT') { + return; + } + + const text = e.clipboardData!.getData('text'); + if (text) { + this.setPasteUrl({ + url: text, }); - }, - onPaste(e: ClipboardEvent) { - if ((e.target as HTMLElement).tagName === 'INPUT') { - return; - } + } + } - const text = e.clipboardData!.getData('text'); - if (text) { - this.setPasteUrl({ - url: text, - }); - } - }, - }, - watch: { - async needAuth(v) { - if (!v) { - await this.getInitData(); - } - }, - }, -}); + @Watch('needAuth') + onNeedAuth(v: boolean) { + if (!v) { + this.getInitData(); + } + } +} diff --git a/src/components/dialogs/ConfirmDeleteDialog.vue b/src/components/dialogs/ConfirmDeleteDialog.vue index ad1a59c..5238ce9 100644 --- a/src/components/dialogs/ConfirmDeleteDialog.vue +++ b/src/components/dialogs/ConfirmDeleteDialog.vue @@ -46,62 +46,66 @@