Stackedit/src/components/Explorer.vue

131 lines
4.1 KiB
Vue
Raw Normal View History

2017-07-28 20:04:12 +00:00
<template>
2017-07-31 09:04:01 +00:00
<div class="explorer flex flex--column">
<div class="side-title flex flex--row flex--space-between">
<div class="flex flex--row">
2017-10-07 11:22:24 +00:00
<button class="side-title__button button" @click="newItem()" v-title="'New file'">
<icon-file-plus></icon-file-plus>
</button>
2017-10-07 11:22:24 +00:00
<button class="side-title__button button" @click="newItem(true)" v-title="'New folder'">
<icon-folder-plus></icon-folder-plus>
</button>
2017-10-07 11:22:24 +00:00
<button class="side-title__button button" @click="deleteItem()" v-title="'Remove'">
<icon-delete></icon-delete>
</button>
2017-12-17 15:08:52 +00:00
<button class="side-title__button button" @click="editItem()" v-title="'Rename'">
<icon-pen></icon-pen>
</button>
</div>
2017-10-07 11:22:24 +00:00
<button class="side-title__button button" @click="toggleExplorer(false)" v-title="'Close explorer'">
2017-07-31 09:04:01 +00:00
<icon-close></icon-close>
</button>
</div>
<div class="explorer__tree" :class="{'explorer__tree--new-item': !newChildNode.isNil}" tabindex="0">
<explorer-node :node="rootNode" :depth="0"></explorer-node>
2017-07-28 20:04:12 +00:00
</div>
</div>
</template>
<script>
import { mapState, mapGetters, mapActions } from 'vuex';
2017-07-31 09:04:01 +00:00
import ExplorerNode from './ExplorerNode';
2017-07-28 20:04:12 +00:00
export default {
2017-07-31 09:04:01 +00:00
components: {
ExplorerNode,
},
computed: {
...mapState('explorer', [
'newChildNode',
]),
...mapGetters('explorer', [
'rootNode',
]),
},
methods: {
...mapActions('data', [
2017-07-31 09:04:01 +00:00
'toggleExplorer',
]),
newItem(isFolder) {
2017-10-05 07:16:35 +00:00
let parentId = this.$store.getters['explorer/selectedNodeFolder'].item.id;
if (parentId === 'trash') {
parentId = null;
2017-10-05 07:16:35 +00:00
}
2017-07-31 09:04:01 +00:00
this.$store.dispatch('explorer/openNode', parentId);
this.$store.commit('explorer/setNewItem', {
type: isFolder ? 'folder' : 'file',
2017-07-31 09:04:01 +00:00
parentId,
});
},
editItem() {
const selectedNode = this.$store.getters['explorer/selectedNode'];
2017-10-05 07:16:35 +00:00
if (selectedNode.item.id !== 'trash') {
this.$store.commit('explorer/setEditingId', selectedNode.item.id);
}
2017-07-31 09:04:01 +00:00
},
deleteItem() {
const selectedNode = this.$store.getters['explorer/selectedNode'];
2017-08-04 08:20:50 +00:00
if (!selectedNode.isNil) {
2017-10-05 07:16:35 +00:00
if (selectedNode.item.id === 'trash' || selectedNode.item.parentId === 'trash') {
this.$store.dispatch('modal/trashDeletion');
return;
}
this.$store.dispatch(selectedNode.isFolder
? 'modal/folderDeletion'
: 'modal/fileDeletion',
selectedNode.item)
.then(() => {
if (selectedNode === this.$store.getters['explorer/selectedNode']) {
if (selectedNode.isFolder) {
2017-10-05 07:16:35 +00:00
const recursiveMoveToTrash = (folderNode) => {
folderNode.folders.forEach(recursiveMoveToTrash);
folderNode.files.forEach((fileNode) => {
2017-10-05 07:16:35 +00:00
this.$store.commit('file/patchItem', {
id: fileNode.item.id,
parentId: 'trash',
});
});
2017-08-17 23:10:35 +00:00
this.$store.commit('folder/deleteItem', folderNode.item.id);
};
2017-10-05 07:16:35 +00:00
recursiveMoveToTrash(selectedNode);
} else {
2017-10-05 07:16:35 +00:00
this.$store.commit('file/patchItem', {
id: selectedNode.item.id,
parentId: 'trash',
});
this.$store.commit('file/setCurrentId', this.$store.getters['data/lastOpenedIds'][1]);
}
}
});
}
2017-07-31 09:04:01 +00:00
},
},
created() {
this.$store.watch(
2017-08-17 23:10:35 +00:00
() => this.$store.getters['file/current'].id,
2017-07-31 09:04:01 +00:00
(currentFileId) => {
this.$store.commit('explorer/setSelectedId', currentFileId);
this.$store.dispatch('explorer/openNode', currentFileId);
2017-07-31 09:04:01 +00:00
}, {
immediate: true,
});
},
2017-07-28 20:04:12 +00:00
};
</script>
<style lang="scss">
2017-07-31 09:04:01 +00:00
.explorer,
.explorer__tree {
height: 100%;
}
.explorer__tree {
overflow: auto;
/* fake element */
& > .explorer-node > .explorer-node__children > .explorer-node:last-child > .explorer-node__item {
height: 20px;
cursor: auto;
}
}
2017-07-28 20:04:12 +00:00
</style>