ldc-com-app-bo/resources/js/components/FoldersSelect.vue
Vincent BRIDIER f01e1501b4 init
2021-01-15 14:47:59 +01:00

200 lines
6.6 KiB
Vue

<template>
<div class="FoldersSelect" v-click-outside="closeSelect">
<input type="text" class="value" readonly="readonly" :value="selected" placeholder="Aucun dossier sélectionné"
:class="displaySelect ? 'is-focus' : ''"
@focus="displaySelect = true">
<span class="el-input__suffix" @click="displaySelect = !displaySelect">
<span class="el-input__suffix-inner">
<i class="el-select__caret el-input__icon fas fa-sort-down"></i>
</span>
</span>
<div class="select" v-show="displaySelect">
<FolderTree :directories="directories" class="first" @choose="choose" :selectedFolder="selectedFolder"/>
</div>
</div>
</template>
<script>
import FolderTree from './FolderTree'
export default {
name: 'FoldersSelect',
props: {
directories: {required: true},
value: {required: true},
onlyLastChild: {required: true},
selectedFolder: {default: false}
},
components: {
FolderTree
},
data() {
return {
selectedId: '',
displaySelect: false,
parentDatas: [],
parentID: 0,
}
},
computed: {
selected() {
if (this.selectedId === '') return '';
return this.displayTree(this.directories, this.selectedId)
}
},
methods: {
choose(id, val) {
if (this.onlyLastChild === 'true') {
let parent = this.directories.find(item => item.id === id);
if (typeof parent != "undefined") {
this.parentID = parent.id;
} else {
let dir = this.findNode(id, this.directories.find(item => item.id === this.parentID));
if (typeof dir.children === "undefined") {
this.selectedId = id;
this.$emit('input', id);
}
}
}else{
this.selectedId = id;
this.$emit('input', id);
}
},
findNode(id, currentNode) {
let i,
currentChild,
result;
if (id === currentNode.id) {
return currentNode;
} else {
if (typeof currentNode.children !== "undefined") {
// Use a for loop instead of forEach to avoid nested functions
// Otherwise "return" will not work properly
for (i = 0; i < currentNode.children.length; i += 1) {
currentChild = currentNode.children[i];
// Search in the current child
result = this.findNode(id, currentChild);
// Return the result if the node has been found
if (result !== false) {
return result;
}
}
} else {
let tmpDir = this.findNode(currentNode.parent, this.directories.find(item => item.id === this.parentID));
for (i = 0; i < tmpDir.children.length; i += 1) {
currentChild = tmpDir.children[i];
if (currentChild.id === id) {
return currentChild;
}
}
}
// The node has not been found and we have no more options
return false;
}
},
closeSelect() {
this.displaySelect = false
},
displayTree(list, id) { // recursive
for (let i = 0; i < list.length; i++) {
if (list[i].id == id) {
return JSON.parse(list[i].name)['fr']
}
if (list[i].children) {
let r = this.displayTree(list[i].children, id);
if (r) return JSON.parse(list[i].name)['fr'] + ' / ' + r
}
}
return ''
}
},
watch: {
value() {
this.selectedId = this.value
}
},
mounted() {
this.selectedId = this.value
},
}
</script>
<style lang="scss">
.FoldersSelect {
position: relative;
.value {
background-color: #FFF;
background-image: none;
border-radius: 4px;
border: 1px solid #DCDFE6;
box-sizing: border-box;
color: #606266;
display: inline-block;
font-size: inherit;
height: 40px;
line-height: 40px;
outline: 0;
padding: 0 15px;
transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
width: 100%;
font-size: 14px;
cursor: pointer;
&:focus, &.is-focus {
border-color: #409EFF;
outline: 0;
}
&::placeholder {
color: #c0c4cc;
}
}
.el-input__suffix {
position: absolute;
top: 50%;
right: 5px;
transform: translateY(-50%);
cursor: pointer;
font-size: 15px;
color: grey;
padding: 10px;
}
.value.is-focus + .el-input__suffix {
transform: rotate(-180deg) translateY(40%);
}
.select {
position: absolute;
top: 50px;
padding: 12px;
background: #fff;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
width: 100%;
box-sizing: border-box;
z-index: 2009;
&:after {
bottom: 100%;
left: 35px;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(255, 255, 255, 0);
border-bottom-color: #fff;
border-width: 4px;
margin-left: -4px;
}
.first {
max-height: 120px;
overflow: auto;
}
}
}
</style>