添加文件管理功能

This commit is contained in:
marvzhang
2019-12-25 20:49:27 +08:00
parent 715761049e
commit 7f771a2103
3 changed files with 110 additions and 14 deletions

View File

@@ -7,15 +7,42 @@
<font-awesome-icon :icon="['fa', 'arrow-left']"/>
{{$t('Back')}}
</el-button>
<el-button v-if="showFile" type="primary" size="small" style="margin-right: 10px;" @click="onFileSave">
<el-popover v-model="isShowDelete">
<el-button size="small" type="default" @click="() => this.isShowDelete = false">
{{$t('Cancel')}}
</el-button>
<el-button size="small" type="danger" @click="onFileDelete">
{{$t('Confirm')}}
</el-button>
<template slot="reference">
<el-button v-if="currentPath !== ''" type="danger" size="small" style="margin-right: 10px;"
@click="() => this.isShowDelete = true">
<font-awesome-icon :icon="['fa', 'trash']"/>
{{$t('Remove')}}
</el-button>
</template>
</el-popover>
<el-button v-if="showFile" type="success" size="small" style="margin-right: 10px;" @click="onFileSave">
<font-awesome-icon :icon="['fa', 'save']"/>
{{$t('Save')}}
</el-button>
<!--TODO: new files/directories-->
<el-button v-if="false" type="primary" size="small" style="margin-right: 10px;">
<font-awesome-icon :icon="['fa', 'file-alt']"/>
{{$t('New File')}}
</el-button>
<el-popover v-if="!showFile" v-model="isShowAdd" @hide="onHideAdd">
<el-input v-model="name" :placeholder="$t('Name')"/>
<div class="add-type-list">
<el-button size="small" type="success" icon="el-icon-document-add" @click="onAddFile">
{{$t('File')}}
</el-button>
<el-button size="small" type="primary" icon="el-icon-folder-add" @click="onAddDir">
{{$t('Directory')}}
</el-button>
</div>
<template slot="reference">
<el-button type="primary" size="small" style="margin-right: 10px;">
<font-awesome-icon :icon="['fa', 'plus']"/>
{{$t('Create')}}
</el-button>
</template>
</el-popover>
</div>
<!--./back-->
@@ -70,7 +97,10 @@ export default {
return {
code: 'var hello = \'world\'',
isEdit: false,
showFile: false
showFile: false,
name: '',
isShowAdd: false,
isShowDelete: false
}
},
computed: {
@@ -110,15 +140,46 @@ export default {
this.$store.commit('file/SET_CURRENT_PATH', path)
this.$store.dispatch('file/getFileList', { path: this.currentPath })
},
onFileSave () {
this.$store.dispatch('file/saveFileContent', { path: this.currentPath })
.then(() => {
this.$message.success(this.$t('Saved file successfully'))
})
async onFileSave () {
await this.$store.dispatch('file/saveFileContent', { path: this.currentPath })
this.$message.success(this.$t('Saved file successfully'))
},
onBackFile () {
this.showFile = false
this.onBack()
},
onHideAdd () {
this.name = ''
},
async onAddFile () {
if (!this.name) {
this.$message.error(this.$t('Name cannot be empty'))
return
}
const path = this.currentPath + '/' + this.name
await this.$store.dispatch('file/addFile', { path })
await this.$store.dispatch('file/getFileList', { path: this.currentPath })
this.isShowAdd = false
this.showFile = true
this.$store.commit('file/SET_FILE_CONTENT', '')
this.$store.commit('file/SET_CURRENT_PATH', path)
await this.$store.dispatch('file/getFileContent', { path })
},
async onAddDir () {
if (!this.name) {
this.$message.error(this.$t('Name cannot be empty'))
return
}
await this.$store.dispatch('file/addDir', { path: this.currentPath + '/' + this.name })
await this.$store.dispatch('file/getFileList', { path: this.currentPath })
this.isShowAdd = false
},
async onFileDelete () {
await this.$store.dispatch('file/deleteFile', { path: this.currentPath })
this.$message.success(this.$t('Deleted successfully'))
this.isShowDelete = false
this.onBackFile()
}
},
created () {
@@ -236,4 +297,14 @@ export default {
font-size: 14px;
color: rgba(3, 47, 98, 1);
}
.add-type-list {
text-align: right;
margin-top: 10px;
}
.add-type {
cursor: pointer;
font-weight: bolder;
}
</style>

View File

@@ -40,6 +40,7 @@ export default {
// 操作
Add: '添加',
Create: '创建',
Run: '运行',
Deploy: '部署',
Save: '保存',
@@ -234,6 +235,9 @@ export default {
// 文件
'Choose Folder': '选择文件',
'File': '文件',
'Folder': '文件夹',
'Directory': '目录',
// 导入
'Import Spider': '导入爬虫',

View File

@@ -25,8 +25,9 @@ const actions = {
const { path } = payload
const spiderId = rootState.spider.spiderForm._id
commit('SET_CURRENT_PATH', path)
request.get(`/spiders/${spiderId}/dir`, { path })
return request.get(`/spiders/${spiderId}/dir`, { path })
.then(response => {
if (!response.data.data) response.data.data = []
commit(
'SET_FILE_LIST',
response.data.data
@@ -38,10 +39,30 @@ const actions = {
getFileContent ({ commit, rootState }, payload) {
const { path } = payload
const spiderId = rootState.spider.spiderForm._id
request.get(`/spiders/${spiderId}/file`, { path })
return request.get(`/spiders/${spiderId}/file`, { path })
.then(response => {
commit('SET_FILE_CONTENT', response.data.data)
})
},
saveFileContent ({ state, rootState }, payload) {
const { path } = payload
const spiderId = rootState.spider.spiderForm._id
return request.post(`/spiders/${spiderId}/file`, { path, content: state.fileContent })
},
addFile ({ rootState }, payload) {
const { path } = payload
const spiderId = rootState.spider.spiderForm._id
return request.put(`/spiders/${spiderId}/file`, { path })
},
addDir ({ rootState }, payload) {
const { path } = payload
const spiderId = rootState.spider.spiderForm._id
return request.put(`/spiders/${spiderId}/dir`, { path })
},
deleteFile ({ rootState }, payload) {
const { path } = payload
const spiderId = rootState.spider.spiderForm._id
return request.delete(`/spiders/${spiderId}/file`, { path })
}
}