Stackedit/src/components/Explorer.vue

141 lines
3.8 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">
<button class="side-title__button button" @click="newItem()">
<icon-file-plus></icon-file-plus>
</button>
<button class="side-title__button button" @click="newItem(true)">
<icon-folder-plus></icon-folder-plus>
</button>
<button class="side-title__button button" @click="editItem()">
<icon-pen></icon-pen>
</button>
<button class="side-title__button button" @click="deleteItem()">
<icon-delete></icon-delete>
</button>
</div>
<button class="side-title__button button" @click="toggleExplorer(false)">
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) {
const parentId = this.$store.getters['explorer/selectedNodeFolder'].item.id;
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'];
this.$store.commit('explorer/setEditingId', selectedNode.item.id);
2017-07-31 09:04:01 +00:00
},
deleteItem() {
const selectedNode = this.$store.getters['explorer/selectedNode'];
if (selectedNode.item.id) {
this.$store.dispatch(selectedNode.isFolder
? 'modal/folderDeletion'
: 'modal/fileDeletion',
selectedNode.item)
.then(() => {
if (selectedNode === this.$store.getters['explorer/selectedNode']) {
if (selectedNode.isFolder) {
const recursiveDelete = (folderNode) => {
folderNode.folders.forEach(recursiveDelete);
folderNode.files.forEach((fileNode) => {
this.$store.commit('files/deleteItem', fileNode.item.id);
});
this.$store.commit('folders/deleteItem', folderNode.item.id);
};
recursiveDelete(selectedNode);
} else {
this.$store.commit('files/deleteItem', selectedNode.item.id);
}
}
});
}
2017-07-31 09:04:01 +00:00
},
},
created() {
this.$store.watch(
() => this.$store.getters['files/current'].id,
(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-31 09:04:01 +00:00
.side-title {
height: 44px;
line-height: 44px;
padding: 4px 5px 0;
2017-07-31 09:04:01 +00:00
background-color: rgba(0, 0, 0, 0.1);
-webkit-flex: none;
flex: none;
2017-07-31 09:04:01 +00:00
}
.side-title__button {
width: 38px;
2017-07-31 09:04:01 +00:00
padding: 6px;
display: inline-block;
background-color: transparent;
opacity: 0.75;
/* prevent from seeing wrapped buttons */
margin-bottom: 20px;
&:active,
&:focus,
&:hover {
opacity: 1;
}
}
2017-07-28 20:04:12 +00:00
</style>