Skip to content

A full-featured download manager.

refactor: task actions commands

Dr_rOot3be8952cff2a
diff --git a/src/renderer/components/Command/CommandManager.js b/src/renderer/components/CommandManager/index.js
similarity index 85%
rename from src/renderer/components/Command/CommandManager.js
rename to src/renderer/components/CommandManager/index.js
index c94cc258..25a08004 100644
--- a/src/renderer/components/Command/CommandManager.js
+++ b/src/renderer/components/CommandManager/index.js
@@ -21,6 +21,14 @@ export default class CommandManager extends EventEmitter {
this.emit('commandRegistered', id)
}
+ unregister (id) {
+ if (this.commands[id]) {
+ delete this.commands[id]
+
+ this.emit('commandUnregistered', id)
+ }
+ }
+
execute (id, ...args) {
var fn = this.commands[id]
if (fn) {
diff --git a/src/renderer/components/CommandManager/instance.js b/src/renderer/components/CommandManager/instance.js
new file mode 100644
index 00000000..81cdfdf9
--- /dev/null
+++ b/src/renderer/components/CommandManager/instance.js
@@ -0,0 +1,3 @@
+import CommandManager from '.'
+
+export const commands = new CommandManager()
diff --git a/src/renderer/components/Native/Ipc.vue b/src/renderer/components/Native/Ipc.vue
index 90dc2326..e8343ba7 100644
--- a/src/renderer/components/Native/Ipc.vue
+++ b/src/renderer/components/Native/Ipc.vue
@@ -3,39 +3,24 @@
</template>
<script>
- import is from 'electron-is'
- import {
- commands
- } from '@/components/Command/index'
+ import { commands } from '@/components/CommandManager/instance'
export default {
name: 'mo-ipc',
- computed: {
- isMas: () => is.mas()
- },
- watch: {
- },
methods: {
- bindIpcEvents: function () {
+ bindIpcEvents () {
this.$electron.ipcRenderer.on('command', (event, command, ...args) => {
commands.execute(command, ...args)
})
},
- unbindIpcEvents: function () {
+ unbindIpcEvents () {
this.$electron.ipcRenderer.removeAllListeners('command')
}
},
- created: function () {
+ created () {
this.bindIpcEvents()
- // id of the menu item
- const visibleStates = {}
- if (this.isMas) {
- visibleStates['app.check-for-updates'] = false
- visibleStates['task.new-bt-task'] = false
- }
- this.$electron.ipcRenderer.send('command', 'application:change-menu-states', visibleStates, null, null)
},
- destroyed: function () {
+ destroyed () {
this.unbindIpcEvents()
}
}
diff --git a/src/renderer/components/Native/index.js b/src/renderer/components/Native/index.js
deleted file mode 100644
index e69de29b..00000000
diff --git a/src/renderer/components/Preference/Basic.vue b/src/renderer/components/Preference/Basic.vue
index 65417764..6bddfd23 100644
--- a/src/renderer/components/Preference/Basic.vue
+++ b/src/renderer/components/Preference/Basic.vue
@@ -281,8 +281,8 @@
}
},
computed: {
- isRenderer () { return is.renderer() },
- isMas () { return is.mas() },
+ isRenderer: () => is.renderer(),
+ isMas: () => is.mas(),
isLinux () { return is.linux() },
title () {
return this.$t('preferences.basic')
@@ -331,7 +331,7 @@
}
]
},
- subnavs: function () {
+ subnavs () {
return [
{
key: 'basic',
@@ -369,6 +369,7 @@
},
handleThemeChange (theme) {
this.form.theme = theme
+ // this.$store.dispatch('preference/changeThemeConfig', theme)
this.$electron.ipcRenderer.send('command',
'application:change-theme', theme)
},
diff --git a/src/renderer/components/Task/Index.vue b/src/renderer/components/Task/Index.vue
index f4fd1c3a..76258908 100644
--- a/src/renderer/components/Task/Index.vue
+++ b/src/renderer/components/Task/Index.vue
@@ -33,10 +33,24 @@
</template>
<script>
+ import { mapState } from 'vuex'
+ import * as clipboard from 'clipboard-polyfill'
+
+ import { commands } from '@/components/CommandManager/instance'
+ import { ADD_TASK_TYPE } from '@shared/constants'
import TaskSubnav from '@/components/Subnav/TaskSubnav'
import TaskActions from '@/components/Task/TaskActions'
import TaskList from '@/components/Task/TaskList'
import SubnavSwitcher from '@/components/Subnav/SubnavSwitcher'
+ import {
+ getTaskUri,
+ parseHeader
+ } from '@shared/utils'
+ import {
+ delayDeleteTaskFiles,
+ showItemInFolder,
+ moveTaskFilesToTrash
+ } from '@/utils/native'
export default {
name: 'mo-content-task',
@@ -53,7 +67,15 @@
}
},
computed: {
- subnavs: function () {
+ ...mapState('task', {
+ taskList: state => state.taskList,
+ selectedGidList: state => state.selectedGidList,
+ selectedGidListCount: state => state.selectedGidList.length
+ }),
+ ...mapState('preference', {
+ noConfirmBeforeDelete: state => state.config.noConfirmBeforeDeleteTask
+ }),
+ subnavs () {
return [
{
key: 'active',
@@ -72,7 +94,7 @@
}
]
},
- title: function () {
+ title () {
const subnav = this.subnavs.find((item) => item.key === this.status)
return subnav.title
}
@@ -86,10 +108,302 @@
},
changeCurrentList () {
this.$store.dispatch('task/changeCurrentList', this.status)
+ },
+ directAddTask (uri, options = {}) {
+ const uris = [uri]
+ const payload = {
+ uris,
+ options: {
+ ...options
+ }
+ }
+ this.$store.dispatch('task/addUri', payload)
+ .catch((err) => {
+ this.$msg.error(err.message)
+ })
+ },
+ showAddTaskDialog (uri, options = {}) {
+ const {
+ header,
+ ...rest
+ } = options
+ console.log('showAddTaskDialog.options====>', options)
+
+ const headers = parseHeader(header)
+ const newOptions = {
+ ...rest,
+ ...headers
+ }
+
+ this.$store.dispatch('app/updateAddTaskUrl', uri)
+ this.$store.dispatch('app/updateAddTaskOptions', newOptions)
+ this.$store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.URI)
+ },
+ deleteTaskFiles (task) {
+ try {
+ const result = moveTaskFilesToTrash(task)
+
+ if (!result) {
+ throw new Error('task.remove-task-file-fail')
+ }
+ } catch (err) {
+ this.$msg.error(this.$t(err.message))
+ }
+ },
+ removeTask (task, taskName, isRemoveWithFiles = false) {
+ this.$store.dispatch('task/forcePauseTask', task)
+ .finally(() => {
+ if (isRemoveWithFiles) {
+ this.deleteTaskFiles(task)
+ }
+
+ return this.removeTaskItem(task, taskName)
+ })
+ },
+ removeTaskRecord (task, taskName, isRemoveWithFiles = false) {
+ this.$store.dispatch('task/forcePauseTask', task)
+ .finally(() => {
+ if (isRemoveWithFiles) {
+ this.deleteTaskFiles(task)
+ }
+
+ return this.removeTaskRecordItem(task, taskName)
+ })
+ },
+ removeTaskItem (task, taskName) {
+ return this.$store.dispatch('task/removeTask', task)
+ .then(() => {
+ this.$msg.success(this.$t('task.delete-task-success', {
+ taskName
+ }))
+ })
+ .catch(({ code }) => {
+ if (code === 1) {
+ this.$msg.error(this.$t('task.delete-task-fail', {
+ taskName
+ }))
+ }
+ })
+ },
+ removeTaskRecordItem (task, taskName) {
+ return this.$store.dispatch('task/removeTaskRecord', task)
+ .then(() => {
+ this.$msg.success(this.$t('task.remove-record-success', {
+ taskName
+ }))
+ })
+ .catch(({ code }) => {
+ if (code === 1) {
+ this.$msg.error(this.$t('task.remove-record-fail', {
+ taskName
+ }))
+ }
+ })
+ },
+ removeTasks (taskList, isRemoveWithFiles = false) {
+ const gids = taskList.map((task) => task.gid)
+ this.$store.dispatch('task/batchForcePauseTask', gids)
+ .finally(() => {
+ if (isRemoveWithFiles) {
+ this.batchDeleteTaskFiles(taskList)
+ }
+
+ this.removeTaskItems(gids)
+ })
+ },
+ batchDeleteTaskFiles (taskList) {
+ const promises = taskList.map((task, index) => delayDeleteTaskFiles(task, index * 200))
+ Promise.all(promises).then(values => {
+ console.log(values)
+ })
+ },
+ removeTaskItems (gids) {
+ this.$store.dispatch('task/batchRemoveTask', gids)
+ .then(() => {
+ this.$msg.success(this.$t('task.batch-delete-task-success'))
+ })
+ .catch(({ code }) => {
+ if (code === 1) {
+ this.$msg.error(this.$t('task.batch-delete-task-fail'))
+ }
+ })
+ },
+ handlePauseTask (payload) {
+ const { task, taskName } = payload
+ this.$msg.info(this.$t('task.download-pause-message', { taskName }))
+ this.$store.dispatch('task/pauseTask', task)
+ .catch(({ code }) => {
+ if (code === 1) {
+ this.$msg.error(this.$t('task.pause-task-fail', { taskName }))
+ }
+ })
+ },
+ handleResumeTask (payload) {
+ const { task, taskName } = payload
+ this.$store.dispatch('task/resumeTask', task)
+ .catch(({ code }) => {
+ if (code === 1) {
+ this.$msg.error(this.$t('task.resume-task-fail', {
+ taskName
+ }))
+ }
+ })
+ },
+ handleStopTaskSeeding (payload) {
+ const { task } = payload
+ this.$store.dispatch('task/stopSeeding', task)
+ },
+ handleRestartTask (payload) {
+ const { task, taskName, showDialog } = payload
+ const { gid } = task
+ const uri = getTaskUri(task)
+
+ this.$store.dispatch('task/getTaskOption', gid)
+ .then((data) => {
+ console.log('[Motrix] get task option:', data)
+ const { dir, header, maxConnectionPerServer } = data
+ const options = {
+ dir,
+ header,
+ maxConnectionPerServer,
+ out: taskName
+ }
+
+ if (showDialog) {
+ this.showAddTaskDialog(uri, options)
+ } else {
+ this.directAddTask(uri, options)
+ this.$store.dispatch('task/removeTaskRecord', task)
+ }
+ })
+ },
+ handleRevealInFolder (payload) {
+ const { path } = payload
+ showItemInFolder(path, {
+ errorMsg: this.$t('task.file-not-exist')
+ })
+ },
+ handleDeleteTask (payload) {
+ const { task, taskName, deleteWithFiles } = payload
+ const { noConfirmBeforeDelete } = this
+
+ if (noConfirmBeforeDelete) {
+ this.removeTask(task, taskName, deleteWithFiles)
+ return
+ }
+
+ this.$electron.remote.dialog.showMessageBox({
+ type: 'warning',
+ title: this.$t('task.delete-task'),
+ message: this.$t('task.delete-task-confirm', { taskName }),
+ buttons: [this.$t('app.yes'), this.$t('app.no')],
+ cancelId: 1,
+ checkboxLabel: this.$t('task.delete-task-label'),
+ checkboxChecked: deleteWithFiles
+ }).then(({ response, checkboxChecked }) => {
+ if (response === 0) {
+ this.removeTask(task, taskName, checkboxChecked)
+ }
+ })
+ },
+ handleDeleteTaskRecord (payload) {
+ const { task, taskName, deleteWithFiles } = payload
+ const { noConfirmBeforeDelete } = this
+
+ if (noConfirmBeforeDelete) {
+ this.removeTaskRecord(task, taskName, deleteWithFiles)
+ return
+ }
+
+ this.$electron.remote.dialog.showMessageBox({
+ type: 'warning',
+ title: this.$t('task.remove-record'),
+ message: this.$t('task.remove-record-confirm', { taskName }),
+ buttons: [this.$t('app.yes'), this.$t('app.no')],
+ cancelId: 1,
+ checkboxLabel: this.$t('task.remove-record-label'),
+ checkboxChecked: !!deleteWithFiles
+ }).then(({ response, checkboxChecked }) => {
+ if (response === 0) {
+ this.removeTaskRecord(task, taskName, checkboxChecked)
+ }
+ })
+ },
+ handleBatchDeleteTask (payload) {
+ const { deleteWithFiles } = payload
+ const {
+ noConfirmBeforeDelete,
+ selectedGidList,
+ selectedGidListCount,
+ taskList
+ } = this
+ if (selectedGidListCount === 0) {
+ return
+ }
+
+ const selectedTaskList = taskList.filter((task) => {
+ return selectedGidList.includes(task.gid)
+ })
+
+ if (noConfirmBeforeDelete) {
+ this.removeTasks(selectedTaskList, deleteWithFiles)
+ return
+ }
+
+ const count = `${selectedGidListCount}`
+ this.$electron.remote.dialog.showMessageBox({
+ type: 'warning',
+ title: this.$t('task.delete-selected-task'),
+ message: this.$t('task.batch-delete-task-confirm', { count }),
+ buttons: [this.$t('app.yes'), this.$t('app.no')],
+ cancelId: 1,
+ checkboxLabel: this.$t('task.delete-task-label'),
+ checkboxChecked: deleteWithFiles
+ }).then(({ response, checkboxChecked }) => {
+ if (response === 0) {
+ this.removeTasks(selectedTaskList, checkboxChecked)
+ }
+ })
+ },
+ handleCopyTaskLink (payload) {
+ const { task } = payload
+ const uri = getTaskUri(task)
+ clipboard.writeText(uri)
+ .then(() => {
+ this.$msg.success(this.$t('task.copy-link-success'))
+ })
+ },
+ handleShowTaskInfo (payload) {
+ const { task } = payload
+ this.$store.dispatch('task/showTaskItemInfoDialog', task)
}
},
- created: function () {
+ created () {
this.changeCurrentList()
+ },
+ mounted () {
+ commands.on('pause-task', this.handlePauseTask)
+ commands.on('resume-task', this.handleResumeTask)
+ commands.on('stop-task-seeding', this.handleStopTaskSeeding)
+ commands.on('restart-task', this.handleRestartTask)
+ commands.on('reveal-in-folder', this.handleRevealInFolder)
+ commands.on('delete-task', this.handleDeleteTask)
+ commands.on('delete-task-record', this.handleDeleteTaskRecord)
+ commands.on('batch-delete-task', this.handleBatchDeleteTask)
+ commands.on('copy-task-link', this.handleCopyTaskLink)
+ commands.on('show-task-info', this.handleShowTaskInfo)
+ },
+ destroyed () {
+ commands.off('pause-task', this.handlePauseTask)
+ commands.off('resume-task', this.handleResumeTask)
+ commands.off('stop-task-seeding', this.handleStopTaskSeeding)
+ commands.off('restart-task', this.handleRestartTask)
+ commands.off('reveal-in-folder', this.handleRevealInFolder)
+ commands.off('delete-task', this.handleDeleteTask)
+ commands.off('delete-task-record', this.handleDeleteTaskRecord)
+ commands.off('batch-delete-task', this.handleBatchDeleteTask)
+ commands.off('copy-task-link', this.handleCopyTaskLink)
+ commands.off('show-task-info', this.handleShowTaskInfo)
}
}
</script>
diff --git a/src/renderer/components/Task/TaskActions.vue b/src/renderer/components/Task/TaskActions.vue
index 49d12f00..04d273ec 100644
--- a/src/renderer/components/Task/TaskActions.vue
+++ b/src/renderer/components/Task/TaskActions.vue
@@ -3,8 +3,8 @@
<el-tooltip
class="item hidden-md-and-up"
effect="dark"
- :content="$t('task.new-task')"
placement="bottom"
+ :content="$t('task.new-task')"
>
<i class="task-action" @click.stop="onAddClick">
<mo-icon name="menu-add" width="14" height="14" />
@@ -23,17 +23,32 @@
<mo-icon name="delete" width="14" height="14" />
</i>
</el-tooltip>
- <el-tooltip class="item" effect="dark" :content="$t('task.refresh-list')" placement="bottom">
+ <el-tooltip
+ class="item"
+ effect="dark"
+ placement="bottom"
+ :content="$t('task.refresh-list')"
+ >
<i class="task-action" @click="onRefreshClick">
<mo-icon name="refresh" width="14" height="14" :spin="refreshing" />
</i>
</el-tooltip>
- <el-tooltip class="item" effect="dark" :content="$t('task.resume-all-task')" placement="bottom">
+ <el-tooltip
+ class="item"
+ effect="dark"
+ placement="bottom"
+ :content="$t('task.resume-all-task')"
+ >
<i class="task-action" @click="onResumeAllClick">
<mo-icon name="task-start-line" width="14" height="14" />
</i>
</el-tooltip>
- <el-tooltip class="item" effect="dark" :content="$t('task.pause-all-task')" placement="bottom">
+ <el-tooltip
+ class="item"
+ effect="dark"
+ placement="bottom"
+ :content="$t('task.pause-all-task')"
+ >
<i class="task-action" @click="onPauseAllClick">
<mo-icon name="task-pause-line" width="14" height="14" />
</i>
@@ -41,8 +56,8 @@
<el-tooltip
class="item"
effect="dark"
- :content="$t('task.purge-record')"
placement="bottom"
+ :content="$t('task.purge-record')"
v-if="currentList === 'stopped'"
>
<i class="task-action" @click="onPurgeRecordClick">
@@ -54,11 +69,10 @@
<script>
import { mapState } from 'vuex'
+
+ import { commands } from '@/components/CommandManager/instance'
import { ADD_TASK_TYPE } from '@shared/constants'
import { bytesToSize, timeFormat } from '@shared/utils'
- import {
- moveTaskFilesToTrash
- } from '@/components/Native/utils'
import '@/components/Icons/menu-add'
import '@/components/Icons/refresh'
import '@/components/Icons/task-start-line'
@@ -72,7 +86,7 @@
components: {
},
props: ['task'],
- data: function () {
+ data () {
return {
refreshing: false
}
@@ -80,8 +94,6 @@
computed: {
...mapState('task', {
currentList: state => state.currentList,
- taskList: state => state.taskList,
- selectedGidList: state => state.selectedGidList,
selectedGidListCount: state => state.selectedGidList.length
})
},
@@ -90,7 +102,7 @@
timeFormat
},
methods: {
- refreshSpin: function () {
+ refreshSpin () {
this.t && clearTimeout(this.t)
this.refreshing = true
@@ -98,79 +110,15 @@
this.refreshing = false
}, 500)
},
- delayDeleteTaskFiles (task, delay) {
- return new Promise((resolve) => {
- setTimeout(() => {
- try {
- const result = moveTaskFilesToTrash(task)
- resolve(result)
- } catch (err) {
- console.log('[Motrix] batch delay delete task files fail', err)
- resolve(false)
- }
- }, delay)
- })
- },
- batchDeleteTaskFiles (taskList) {
- const promises = taskList.map((task, index) => this.delayDeleteTaskFiles(task, index * 200))
- Promise.all(promises).then(values => {
- console.log(values)
- })
- },
- removeTasks (taskList, isRemoveWithFiles) {
- const gids = taskList.map((task) => task.gid)
- this.$store.dispatch('task/batchForcePauseTask', gids)
- .finally(() => {
- if (isRemoveWithFiles) {
- this.batchDeleteTaskFiles(taskList)
- }
-
- this.removeTaskItems(gids)
- })
- },
- removeTaskItems (gids) {
- this.$store.dispatch('task/batchRemoveTask', gids)
- .then(() => {
- this.$msg.success(this.$t('task.batch-delete-task-success'))
- })
- .catch(({ code }) => {
- if (code === 1) {
- this.$msg.error(this.$t('task.batch-delete-task-fail'))
- }
- })
- },
- onBatchDeleteClick: function (event) {
- const self = this
- const { taskList, selectedGidList, selectedGidListCount } = this
- if (selectedGidListCount === 0) {
- event.preventDefault()
- return
- }
-
- const selectedTaskList = taskList.filter((task) => {
- return selectedGidList.includes(task.gid)
- })
- const count = `${selectedGidListCount}`
- const isChecked = !!event.shiftKey
- this.$electron.remote.dialog.showMessageBox({
- type: 'warning',
- title: this.$t('task.delete-selected-task'),
- message: this.$t('task.batch-delete-task-confirm', { count }),
- buttons: [this.$t('app.yes'), this.$t('app.no')],
- cancelId: 1,
- checkboxLabel: this.$t('task.delete-task-label'),
- checkboxChecked: isChecked
- }).then(({ response, checkboxChecked }) => {
- if (response === 0) {
- self.removeTasks(selectedTaskList, checkboxChecked)
- }
- })
+ onBatchDeleteClick (event) {
+ const deleteWithFiles = !!event.shiftKey
+ commands.emit('batch-delete-task', { deleteWithFiles })
},
- onRefreshClick: function () {
+ onRefreshClick () {
this.refreshSpin()
this.$store.dispatch('task/fetchList')
},
- onResumeAllClick: function () {
+ onResumeAllClick () {
this.$store.dispatch('task/resumeAllTask')
.then(() => {
this.$msg.success(this.$t('task.resume-all-task-success'))
@@ -181,7 +129,7 @@
}
})
},
- onPauseAllClick: function () {
+ onPauseAllClick () {
this.$store.dispatch('task/pauseAllTask')
.then(() => {
this.$msg.success(this.$t('task.pause-all-task-success'))
@@ -192,7 +140,7 @@
}
})
},
- onPurgeRecordClick: function () {
+ onPurgeRecordClick () {
this.$store.dispatch('task/purgeTaskRecord')
.then(() => {
this.$msg.success(this.$t('task.purge-record-success'))
@@ -203,7 +151,7 @@
}
})
},
- onAddClick: function () {
+ onAddClick () {
this.$store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.URI)
}
}
diff --git a/src/renderer/components/Task/TaskItemActions.vue b/src/renderer/components/Task/TaskItemActions.vue
index b035fe93..05df7b60 100644
--- a/src/renderer/components/Task/TaskItemActions.vue
+++ b/src/renderer/components/Task/TaskItemActions.vue
@@ -36,19 +36,15 @@
</template>
<script>
+ import { mapState } from 'vuex'
import is from 'electron-is'
- import * as clipboard from 'clipboard-polyfill'
- import { ADD_TASK_TYPE, TASK_STATUS } from '@shared/constants'
- import {
- showItemInFolder,
- moveTaskFilesToTrash
- } from '@/components/Native/utils'
+
+ import { commands } from '@/components/CommandManager/instance'
+ import { TASK_STATUS } from '@shared/constants'
import {
checkTaskIsSeeder,
getTaskFullPath,
- getTaskName,
- getTaskUri,
- parseHeader
+ getTaskName
} from '@shared/utils'
import '@/components/Icons/task-start-line'
import '@/components/Icons/task-pause-line'
@@ -84,6 +80,9 @@
}
},
computed: {
+ ...mapState('preference', {
+ noConfirmBeforeDelete: state => state.config.noConfirmBeforeDeleteTask
+ }),
taskName () {
return getTaskName(this.task)
},
@@ -117,206 +116,67 @@
}
},
methods: {
- deleteTaskFiles (task) {
- try {
- const result = moveTaskFilesToTrash(task)
-
- if (!result) {
- throw new Error('task.remove-task-file-fail')
- }
- } catch (err) {
- this.$msg.error(this.$t(err.message))
- }
- },
- removeTask (task, isRemoveWithFiles) {
- this.$store.dispatch('task/forcePauseTask', task)
- .finally(() => {
- if (isRemoveWithFiles) {
- this.deleteTaskFiles(task)
- }
-
- return this.removeTaskItem(task)
- })
- },
- removeTaskItem (task) {
- return this.$store.dispatch('task/removeTask', this.task)
- .then(() => {
- this.$msg.success(this.$t('task.delete-task-success', {
- taskName: this.taskName
- }))
- })
- .catch(({ code }) => {
- if (code === 1) {
- this.$msg.error(this.$t('task.delete-task-fail', {
- taskName: this.taskName
- }))
- }
- })
- },
- removeTaskRecord (task, isRemoveWithFiles) {
- this.$store.dispatch('task/forcePauseTask', task)
- .finally(() => {
- if (isRemoveWithFiles) {
- this.deleteTaskFiles(task)
- }
-
- return this.removeTaskRecordItem(task)
- })
- },
- removeTaskRecordItem (task) {
- return this.$store.dispatch('task/removeTaskRecord', this.task)
- .then(() => {
- this.$msg.success(this.$t('task.remove-record-success', {
- taskName: this.taskName
- }))
- })
- .catch(({ code }) => {
- if (code === 1) {
- this.$msg.error(this.$t('task.remove-record-fail', {
- taskName: this.taskName
- }))
- }
- })
- },
onResumeClick () {
- this.$store.dispatch('task/resumeTask', this.task)
- .catch(({ code }) => {
- if (code === 1) {
- this.$msg.error(this.$t('task.resume-task-fail', {
- taskName: this.taskName
- }))
- }
- })
+ const { task, taskName } = this
+ commands.emit('resume-task', {
+ task,
+ taskName
+ })
},
onRestartClick (event) {
const { task, taskName } = this
- const { gid, status } = task
- const uri = getTaskUri(task)
- const isNeedShowDialog = status === TASK_STATUS.COMPLETE || !!event.altKey
- this.$store.dispatch('task/getTaskOption', gid)
- .then((data) => {
- console.log('[Motrix] get task option:', data)
- const { dir, header, maxConnectionPerServer } = data
- const options = {
- dir,
- header,
- maxConnectionPerServer,
- out: taskName
- }
-
- if (isNeedShowDialog) {
- this.showAddTaskDialog(uri, options)
- } else {
- this.directAddTask(uri, options)
- this.$store.dispatch('task/removeTaskRecord', task)
- }
- })
- },
- directAddTask (uri, options = {}) {
- const uris = [uri]
- const payload = {
- uris,
- options: {
- ...options
- }
- }
- this.$store.dispatch('task/addUri', payload)
- .catch((err) => {
- this.$msg.error(err.message)
- })
- },
- showAddTaskDialog (uri, options = {}) {
- const {
- header,
- ...rest
- } = options
-
- const headers = parseHeader(header)
- const newOptions = {
- ...rest,
- ...headers
- }
-
- this.$store.dispatch('app/updateAddTaskUrl', uri)
- this.$store.dispatch('app/updateAddTaskOptions', newOptions)
- this.$store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.URI)
+ const { status } = task
+ const showDialog = status === TASK_STATUS.COMPLETE || !!event.altKey
+ commands.emit('restart-task', {
+ task,
+ taskName,
+ showDialog
+ })
},
onPauseClick () {
- this.pauseTask()
+ const { task, taskName } = this
+ commands.emit('pause-task', {
+ task,
+ taskName
+ })
},
onStopClick () {
- this.stopSeeding()
- },
- stopSeeding () {
if (!this.isSeeder) {
return
}
- this.$store.dispatch('task/stopSeeding', this.task)
- },
- pauseTask () {
- const { taskName } = this
- this.$msg.info(this.$t('task.download-pause-message', { taskName }))
- this.$store.dispatch('task/pauseTask', this.task)
- .catch(({ code }) => {
- if (code === 1) {
- this.$msg.error(this.$t('task.pause-task-fail', { taskName }))
- }
- })
+
+ const { task } = this
+ commands.emit('stop-task-seeding', { task })
},
onDeleteClick (event) {
- const self = this
- const { task } = this
- const isChecked = !!event.shiftKey
- this.$electron.remote.dialog.showMessageBox({
- type: 'warning',
- title: this.$t('task.delete-task'),
- message: this.$t('task.delete-task-confirm', { taskName: this.taskName }),
- buttons: [this.$t('app.yes'), this.$t('app.no')],
- cancelId: 1,
- checkboxLabel: this.$t('task.delete-task-label'),
- checkboxChecked: isChecked
- }).then(({ response, checkboxChecked }) => {
- if (response === 0) {
- self.removeTask(task, checkboxChecked)
- }
+ const { task, taskName } = this
+ const deleteWithFiles = !!event.shiftKey
+ commands.emit('delete-task', {
+ task,
+ taskName,
+ deleteWithFiles
})
},
onTrashClick (event) {
- const self = this
- const { task } = this
- const isChecked = !!event.shiftKey
- this.$electron.remote.dialog.showMessageBox({
- type: 'warning',
- title: this.$t('task.remove-record'),
- message: this.$t('task.remove-record-confirm', { taskName: this.taskName }),
- buttons: [this.$t('app.yes'), this.$t('app.no')],
- cancelId: 1,
- checkboxLabel: this.$t('task.remove-record-label'),
- checkboxChecked: isChecked
- }).then(({ response, checkboxChecked }) => {
- if (response === 0) {
- self.removeTaskRecord(task, checkboxChecked)
- }
+ const { task, taskName } = this
+ const deleteWithFiles = !!event.shiftKey
+ commands.emit('delete-task-record', {
+ task,
+ taskName,
+ deleteWithFiles
})
},
onFolderClick () {
- showItemInFolder(this.path, {
- errorMsg: this.$t('task.file-not-exist')
- })
+ const { path } = this
+ commands.emit('reveal-in-folder', { path })
},
onLinkClick () {
- this.$store.dispatch('app/fetchEngineOptions')
- .then((data) => {
- const { btTracker } = data
- const uri = getTaskUri(this.task, btTracker)
- clipboard.writeText(uri)
- .then(() => {
- this.$msg.success(this.$t('task.copy-link-success'))
- })
- })
+ const { task } = this
+ commands.emit('copy-task-link', { task })
},
onInfoClick () {
- this.$store.dispatch('task/showTaskItemInfoDialog', this.task)
+ const { task } = this
+ commands.emit('show-task-info', { task })
},
onMoreClick () {
}
diff --git a/src/renderer/components/Command/index.js b/src/renderer/pages/index/commands.js
similarity index 67%
rename from src/renderer/components/Command/index.js
rename to src/renderer/pages/index/commands.js
index c71cac8b..e75856ca 100644
--- a/src/renderer/components/Command/index.js
+++ b/src/renderer/pages/index/commands.js
@@ -6,35 +6,34 @@ import store from '@/store'
import { buildFileList } from '@shared/utils'
import { ADD_TASK_TYPE } from '@shared/constants'
import { getLocaleManager } from '@/components/Locale'
-import CommandManager from './CommandManager'
+import { commands } from '@/components/CommandManager/instance'
-const commands = new CommandManager()
const i18n = getLocaleManager().getI18n()
-function updateSystemTheme (theme) {
+const updateSystemTheme = (theme) => {
store.dispatch('app/updateSystemTheme', theme)
}
-function updateTheme (theme) {
+const updateTheme = (theme) => {
store.dispatch('preference/changeThemeConfig', theme)
}
-function showAboutPanel () {
+const showAboutPanel = () => {
store.dispatch('app/showAboutPanel')
}
-function showAddTask (taskType = ADD_TASK_TYPE.URI, task = '') {
- if (taskType === ADD_TASK_TYPE.URI && task) {
- store.dispatch('app/updateAddTaskUrl', task)
+const showAddTask = (taskType = ADD_TASK_TYPE.URI, uri = '') => {
+ if (taskType === ADD_TASK_TYPE.URI && uri) {
+ store.dispatch('app/updateAddTaskUrl', uri)
}
store.dispatch('app/showAddTaskDialog', taskType)
}
-function showAddBtTask () {
+const showAddBtTask = () => {
store.dispatch('app/showAddTaskDialog', ADD_TASK_TYPE.TORRENT)
}
-function showAddBtTaskWithFile (fileName, base64Data = '') {
+const showAddBtTaskWithFile = (fileName, base64Data = '') => {
const blob = base64StringToBlob(base64Data, 'application/x-bittorrent')
const file = new File([blob], fileName, { type: 'application/x-bittorrent' })
const fileList = buildFileList(file)
@@ -44,63 +43,67 @@ function showAddBtTaskWithFile (fileName, base64Data = '') {
}, 200)
}
-function navigateTaskList (status = 'active') {
+const navigateTaskList = (status = 'active') => {
router.push({ path: `/task/${status}` }).catch(err => {
console.log(err)
})
}
-function navigatePreferences () {
+const navigatePreferences = () => {
router.push({ path: '/preference' }).catch(err => {
console.log(err)
})
}
-function showUnderDevelopmentMessage () {
+const showUnderDevelopmentMessage = () => {
Message.info(i18n.t('app.under-development-message'))
}
-function pauseTask () {
+const pauseTask = () => {
store.dispatch('task/batchPauseSelectedTasks')
}
-function resumeTask () {
+const resumeTask = () => {
store.dispatch('task/batchResumeSelectedTasks')
}
-function deleteTask () {
- showUnderDevelopmentMessage()
+const deleteTask = () => {
+ commands.emit('batch-delete-task', {
+ deleteWithFiles: false
+ })
}
-function moveTaskUp () {
+const moveTaskUp = () => {
showUnderDevelopmentMessage()
}
-function moveTaskDown () {
+const moveTaskDown = () => {
showUnderDevelopmentMessage()
}
-function pauseAllTask () {
+const pauseAllTask = () => {
store.dispatch('task/pauseAllTask')
}
-function resumeAllTask () {
+const resumeAllTask = () => {
store.dispatch('task/resumeAllTask')
}
-function selectAllTask () {
+const selectAllTask = () => {
store.dispatch('task/selectAllTask')
}
-commands.register('application:system-theme', updateSystemTheme)
-commands.register('application:theme', updateTheme)
+const fetchPreference = () => {
+ store.dispatch('preference/fetchPreference')
+}
+
+commands.register('application:task-list', navigateTaskList)
+commands.register('application:preferences', navigatePreferences)
commands.register('application:about', showAboutPanel)
+
commands.register('application:new-task', showAddTask)
commands.register('application:new-bt-task', showAddBtTask)
commands.register('application:new-bt-task-with-file', showAddBtTaskWithFile)
-commands.register('application:task-list', navigateTaskList)
-commands.register('application:preferences', navigatePreferences)
-
commands.register('application:pause-task', pauseTask)
commands.register('application:resume-task', resumeTask)
commands.register('application:delete-task', deleteTask)
@@ -110,6 +113,6 @@ commands.register('application:pause-all-task', pauseAllTask)
commands.register('application:resume-all-task', resumeAllTask)
commands.register('application:select-all-task', selectAllTask)
-export {
- commands
-}
+commands.register('application:update-preference-config', fetchPreference)
+commands.register('application:update-system-theme', updateSystemTheme)
+commands.register('application:update-theme', updateTheme)
diff --git a/src/renderer/pages/index/main.js b/src/renderer/pages/index/main.js
index e6657c82..6e483736 100644
--- a/src/renderer/pages/index/main.js
+++ b/src/renderer/pages/index/main.js
@@ -12,6 +12,7 @@ import store from '@/store'
import { getLocaleManager } from '@/components/Locale'
import Icon from '@/components/Icons/Icon'
import Msg from '@/components/Msg'
+import { commands } from '@/components/CommandManager/instance'
import '@/components/Theme/Index.scss'
@@ -36,17 +37,17 @@ function init (config) {
Vue.use(Msg, Message, {
showClose: true
})
+ Vue.component('mo-icon', Icon)
const loading = Loading.service({
fullscreen: true,
background: 'rgba(0, 0, 0, 0.1)'
})
- Vue.component('mo-icon', Icon)
sync(store, router)
/* eslint-disable no-new */
- window.app = new Vue({
+ global.app = new Vue({
components: { App },
router,
store,
@@ -54,6 +55,9 @@ function init (config) {
template: '<App/>'
}).$mount('#app')
+ global.app.commands = commands
+ require('./commands')
+
setTimeout(() => {
loading.close()
}, 400)
diff --git a/src/renderer/store/modules/app.js b/src/renderer/store/modules/app.js
index a803e67f..6431f1a9 100644
--- a/src/renderer/store/modules/app.js
+++ b/src/renderer/store/modules/app.js
@@ -151,8 +151,8 @@ const actions = {
changeAddTaskType ({ commit }, taskType) {
commit('CHANGE_ADD_TASK_TYPE', taskType)
},
- updateAddTaskUrl ({ commit }, text = '') {
- commit('CHANGE_ADD_TASK_URL', text)
+ updateAddTaskUrl ({ commit }, uri = '') {
+ commit('CHANGE_ADD_TASK_URL', uri)
},
addTaskAddTorrents ({ commit }, { fileList }) {
commit('CHANGE_ADD_TASK_TORRENTS', fileList)
diff --git a/src/shared/locales/en-US/preferences.js b/src/shared/locales/en-US/preferences.js
index 38df86f4..31992a7a 100644
--- a/src/shared/locales/en-US/preferences.js
+++ b/src/shared/locales/en-US/preferences.js
@@ -20,6 +20,7 @@ export default {
'max-concurrent-downloads': 'Maximum active tasks',
'max-connection-per-server': 'Maximum connection per server',
'new-task-show-downloading': 'Automatically show downloading after adding task',
+ 'no-confirm-before-delete-task': 'No confirmation is required before deleting task',
'continue': 'Continue',
'task-completed-notify': 'Notification after download is complete',
'auto-purge-record': 'Automatically purge download records when exiting app',
diff --git a/src/shared/locales/zh-CN/preferences.js b/src/shared/locales/zh-CN/preferences.js
index 76f13075..71a00945 100644
--- a/src/shared/locales/zh-CN/preferences.js
+++ b/src/shared/locales/zh-CN/preferences.js
@@ -20,6 +20,7 @@ export default {
'max-concurrent-downloads': '同时下载的最大任务数',
'max-connection-per-server': '每个服务器最大连接数',
'new-task-show-downloading': '新建任务后自动跳转到下载页面',
+ 'no-confirm-before-delete-task': '删除任务前无需确认',
'continue': '断点续传',
'task-completed-notify': '下载完成后通知',
'auto-purge-record': '当应用退出时自动清除下载记录',
diff --git a/src/shared/locales/zh-CN/task.js b/src/shared/locales/zh-CN/task.js
index 0604211e..457e0646 100644
--- a/src/shared/locales/zh-CN/task.js
+++ b/src/shared/locales/zh-CN/task.js
@@ -18,7 +18,7 @@ export default {
'task-name': '任务名',
'task-out': '重命名',
'task-out-tips': '选填',
- 'task-split': '下载线程',
+ 'task-split': '分片数',
'task-dir': '存储路径',
'task-ua': 'UA',
'task-user-agent': 'User-Agent',
diff --git a/src/shared/locales/zh-TW/task.js b/src/shared/locales/zh-TW/task.js
index b5106035..4d9b72ea 100644
--- a/src/shared/locales/zh-TW/task.js
+++ b/src/shared/locales/zh-TW/task.js
@@ -18,7 +18,7 @@ export default {
'task-name': '任務名稱',
'task-out': '重新命名',
'task-out-tips': '選填',
- 'task-split': '下載執行緒',
+ 'task-split': '分片數',
'task-dir': '儲存資料夾',
'task-ua': 'UA',
'task-user-agent': 'User-Agent',