First steps on refactoring terms list. Adds t library to travers trees.

This commit is contained in:
Mateus Machado Luna 2018-08-09 16:24:10 -03:00
parent 9e8710e76b
commit 8ee7fea7ef
6 changed files with 369 additions and 211 deletions

View File

@ -14,8 +14,10 @@
"moment": "^2.22.2",
"node-sass": "^4.9.3",
"qs": "^6.5.2",
"t": "^0.5.1",
"v-tooltip": "^2.0.0-rc.33",
"vue": "^2.5.17",
"vue-masonry-css": "^1.0.2",
"vue-router": "^3.0.1",
"vue-the-mask": "^0.11.1",
"vue2-hammer": "^2.0.1",

View File

@ -0,0 +1,336 @@
<template>
<div>
<div
class="term-item"
:class="{
'opened-term': term.opened,
}">
<span
v-if="term.depth > 0"
class="icon children-icon">
<i class="mdi mdi-24px mdi-subdirectory-arrow-right"/>
</span>
<span
class="term-name"
:class="{'is-danger': formWithErrors == term.id }">
{{ term.saved && !term.opened ? term.name : getUnsavedTermName(term) }}
</span>
<span
v-if="term.id != undefined"
class="label-details">
<span
class="not-saved"
v-if="!term.saved">
{{ $i18n.get('info_not_saved') }}
</span>
</span>
<span
class="children-dropdown"
v-if="!isEditingTerm && term.total_children > 0"
@click.prevent="loadChildTerms(term.id, index)">
<span class="icon">
<i
:class="{
'mdi-menu-right': !term.hasLoadedChildren || (term.hasCollapsedChildren && term.hasLoadedChildren),
'mdi-menu-down': (!term.hasCollapsedChildren && term.hasLoadedChildren) || (term.hasCollapsedChildren != undefined && term.hasCollapsedChildren) }"
class="mdi mdi-24px"/>
</span>
<span>{{ term.total_children + ' ' + $i18n.get('label_children_terms') }}</span>
</span>
<span class="controls" >
<a
@click="addNewChildTerm(term, index)"
:disabled="isEditingTerm">
<b-icon
size="is-small"
icon="plus-circle"/>
</a>
<a @click.prevent="editTerm(term, index)">
<b-icon
type="is-secondary"
icon="pencil"/>
</a>
<a
@click.prevent="tryToRemoveTerm(term)">
<b-icon
type="is-secondary"
icon="delete"/>
</a>
</span>
</div>
<div
class="term-item"
:class="{
'opened-term': term.opened,
}"
v-for="(childTerm, childIndex) in term.children"
:key="childTerm.id"
v-if="showChildren">
<recursive-terms-list
:term="childTerm"
:index="childIndex"
:taxonomy-id="taxonomyId"/>
</div>
</div>
</template>
<script>
import { mapActions, mapGetters } from 'vuex';
import RecursiveTermsList from './recursive-terms-list.vue';
export default {
name: 'RecursiveTermsList',
data(){
return {
isLoadingTerms: false,
searchQuery: '',
order: 'asc',
showChildren: false
}
},
props: {
term: Object,
index: Number,
taxonomyId: Number
},
components: {
RecursiveTermsList,
},
methods: {
...mapActions('taxonomy', [
'updateTerm',
'deleteTerm',
'fetchChildTerms',
'fetchTerms'
]),
addNewChildTerm(parent, parentIndex) {
if (this.isEditingTerm) {
let editingTermIndex = this.orderedTermsList.findIndex(anEditingTerm => anEditingTerm.opened == true);
if (editingTermIndex >= 0)
this.onTermEditionCanceled(this.orderedTermsList[editingTermIndex]);
}
let newTerm = {
taxonomyId: this.taxonomyId,
name: this.$i18n.get('label_term_without_name'),
description: '',
parent: parent.id,
id: 'new',
saved: false,
depth: parent.depth + 1
}
this.orderedTermsList.splice(parentIndex + 1, 0, newTerm);
this.editTerm(newTerm, parentIndex + 1);
},
getUnsavedTermName(term) {
// let originalIndex = this.termsList.findIndex(anOriginalTerm => anOriginalTerm.id == term.id);
// if (originalIndex >= 0)
// return this.termsList[originalIndex].name;
// else
return term.name;
},
loadChildTerms(parentId, parentIndex) {
if (this.term.children == undefined || this.term.children.length <= 0) {
this.isLoadingTerms = true;
let search = (this.searchQuery != undefined && this.searchQuery != '') ? { searchterm: this.searchQuery } : '';
this.fetchChildTerms({ parentId: parentId, taxonomyId: this.taxonomyId, fetchOnly: '', search: search, all: '', order: this.order})
.then(() => {
this.isLoadingTerms = false;
this.showChildren = true;
})
.catch((error) => {
this.isLoadingTerms = false;
this.$console.log(error);
});
} else {
this.showChildren = !this.showChildren;
}
},
editTerm(term, index) {
let container = document.getElementById('repository-container');
if (container && container.scrollTop && container.scrollTop > 80)
this.termEditionFormTop = container.scrollTop - 80;
else
this.termEditionFormTop = 0;
this.isEditingTerm = true;
if (!term.opened) {
for (let i = 0; i < this.orderedTermsList.length; i++) {
// Checks if other terms are opened
if (term.id != this.orderedTermsList[i].id && this.orderedTermsList[i].opened) {
let otherTerm = this.orderedTermsList[i];
// Checks there's an original version of term (wasn't saved)
let originalIndex = this.termsList.findIndex(anOriginalTerm => anOriginalTerm.id === otherTerm.id);
if (originalIndex >= 0) {
let originalTerm = JSON.parse(JSON.stringify(this.termsList[originalIndex]));
originalTerm.saved = otherTerm.saved;
originalTerm.opened = otherTerm.opened;
if (JSON.stringify(otherTerm) != JSON.stringify(originalTerm)) {
otherTerm.saved = false;
} else {
otherTerm.saved = true;
}
// A new term is being closed
} else {
otherTerm.saved = false;
}
otherTerm.opened = false;
this.orderedTermsList.splice(i, 1, otherTerm);
}
}
} else {
let originalIndex = this.termsList.findIndex(anOriginalTerm => anOriginalTerm.id === term.id);
if (originalIndex >= 0) {
let originalTerm = JSON.parse(JSON.stringify(this.termsList[originalIndex]));
originalTerm.saved = term.saved;
originalTerm.opened = term.opened;
if (JSON.stringify(term) != JSON.stringify(originalTerm))
term.saved = false;
else
term.saved = true;
} else {
term.saved = false;
}
this.isEditingTerm = false;
}
term.opened = !term.opened;
this.orderedTermsList.splice(index, 1, term);
},
},
created() {
//this.loadChildTerms(this.term.id, this.index);
}
}
</script>
<style lang="scss">
@import "../../scss/_variables.scss";
.term-item {
font-size: 14px;
padding: 0 0 0 1.75rem;
min-height: 40px;
display: flex;
position: relative;
align-items: center;
justify-content: space-between;
border-left: 1px solid transparent;
visibility: visible;
opacity: 1;
transition: display 0.3s, visibility 0.3s, opacity 0.3s;
&:hover {
background-color: $gray1 !important;
.controls {
visibility: visible;
opacity: 1.0;
}
&::before {
border-color: transparent transparent transparent $gray2 !important;
}
}
.children-icon {
color: $blue2;
position: absolute;
left: -25px;
}
.term-name {
text-overflow: ellipsis;
overflow-x: hidden;
white-space: nowrap;
margin-left: 0.4em;
margin-right: 0.4em;
display: inline-block;
max-width: 73%;
&.is-danger {
color: $danger !important;
}
}
.label-details {
font-weight: normal;
color: $gray3;
margin-right: auto;
}
.not-saved {
font-style: italic;
font-weight: bold;
color: $danger;
}
.children-dropdown {
margin-left: auto;
color: $blue4;
cursor: pointer;
padding-right: 1rem;
white-space: nowrap;
overflow: hidden;
display: flex;
align-items: center;
}
.controls {
visibility: hidden;
opacity: 0.0;
background-color: $gray2;
padding: 0.4375rem 0.875rem;
a {
margin: 0 0.375rem;
.icon {
bottom: 1px;
position: relative;
i, i:before { font-size: 20px; }
a {
margin-right: 8px;
}
}
}
}
&.opened-term {
cursor: default;
background-color: $blue1;
&:before {
content: '';
display: block;
position: absolute;
left: 100%;
right: -20px;
width: 0;
height: 0;
border-style: solid;
border-color: transparent transparent transparent $blue1;
border-left-width: 24px;
border-top-width: 20px;
border-bottom-width: 20px;
top: 0;
}
&:hover:before {
border-color: transparent transparent transparent $gray1;
}
}
&.collapsed-term {
display: none;
visibility: hidden;
opacity: 0;
transition: display 0.3s, visibility 0.3s, opacity 0.3s;
}
}
</style>

View File

@ -57,67 +57,13 @@
<br>
<div
class="term-item"
:class="{
'opened-term': term.opened,
'collapsed-term': term.collapsed ? term.collapsed : false
}"
:style="{'margin-left': (term.depth * 55) + 'px', 'border-left-color': term.depth > 0 ? '#f2f2f2' : 'transparent'}"
v-for="(term, index) in orderedTermsList"
v-for="(term, index) in termsList"
:key="term.id">
<span
v-if="term.depth > 0"
class="icon children-icon">
<i class="mdi mdi-24px mdi-subdirectory-arrow-right"/>
</span>
<span
class="term-name"
:class="{'is-danger': formWithErrors == term.id }">
{{ term.saved && !term.opened ? term.name : getUnsavedTermName(term) }}
</span>
<span
v-if="term.id != undefined"
class="label-details">
<span
class="not-saved"
v-if="!term.saved">
{{ $i18n.get('info_not_saved') }}
</span>
</span>
<span
class="children-dropdown"
v-if="!isEditingTerm && term.total_children > 0"
@click.prevent="(term.hasLoadedChildren != undefined && term.hasLoadedChildren) ? collapseTermChildren(term.id, index) : loadTerms(term.id, index)">
<span class="icon">
<i
:class="{
'mdi-menu-right': !term.hasLoadedChildren || (term.hasCollapsedChildren && term.hasLoadedChildren),
'mdi-menu-down': (!term.hasCollapsedChildren && term.hasLoadedChildren) || (term.hasCollapsedChildren != undefined && term.hasCollapsedChildren) }"
class="mdi mdi-24px"/>
</span>
<span>{{ term.total_children + ' ' + $i18n.get('label_children_terms') }}</span>
</span>
<span class="controls" >
<a
@click="addNewChildTerm(term, index)"
:disabled="isEditingTerm">
<b-icon
size="is-small"
icon="plus-circle"/>
</a>
<a @click.prevent="editTerm(term, index)">
<b-icon
type="is-secondary"
icon="pencil"/>
</a>
<a
@click.prevent="tryToRemoveTerm(term)">
<b-icon
type="is-secondary"
icon="delete"/>
</a>
</span>
<recursive-terms-list
:term="term"
:index="index"
:taxonomy-id="taxonomyId"/>
</div>
</div>
@ -161,6 +107,7 @@
import { mapActions, mapGetters } from 'vuex';
import TermEditionForm from '../edition/term-edition-form.vue';
import CustomDialog from '../other/custom-dialog.vue';
import RecursiveTermsList from './recursive-terms-list.vue'
export default {
name: 'TermsList',
@ -193,6 +140,7 @@ export default {
}
},
components: {
RecursiveTermsList,
TermEditionForm
},
methods: {
@ -228,25 +176,7 @@ export default {
this.orderedTermsList.push(newTerm);
this.editTerm(newTerm, this.orderedTermsList.length - 1);
},
addNewChildTerm(parent, parentIndex) {
if (this.isEditingTerm) {
let editingTermIndex = this.orderedTermsList.findIndex(anEditingTerm => anEditingTerm.opened == true);
if (editingTermIndex >= 0)
this.onTermEditionCanceled(this.orderedTermsList[editingTermIndex]);
}
let newTerm = {
taxonomyId: this.taxonomyId,
name: this.$i18n.get('label_term_without_name'),
description: '',
parent: parent.id,
id: 'new',
saved: false,
depth: parent.depth + 1
}
this.orderedTermsList.splice(parentIndex + 1, 0, newTerm);
this.editTerm(newTerm, parentIndex + 1);
},
editTerm(term, index) {
let container = document.getElementById('repository-container');
@ -432,13 +362,7 @@ export default {
this.orderedTermsList = new Array();
this.buildOrderedTermsList(0, 0);
},
getUnsavedTermName(term) {
let originalIndex = this.termsList.findIndex(anOriginalTerm => anOriginalTerm.id == term.id);
if (originalIndex >= 0)
return this.termsList[originalIndex].name;
else
return term.name;
},
collapseTermChildren(parentId, parentIndex) {
if (this.orderedTermsList[parentIndex].hasCollapsedChildren != undefined && !this.orderedTermsList[parentIndex].hasCollapsedChildren) {
for (let i = 0; i < this.orderedTermsList.length; i++) {
@ -539,120 +463,6 @@ export default {
}
}
.term-item {
font-size: 14px;
padding: 0 0 0 1.75rem;
min-height: 40px;
display: flex;
position: relative;
align-items: center;
justify-content: space-between;
border-left: 1px solid transparent;
visibility: visible;
opacity: 1;
transition: display 0.3s, visibility 0.3s, opacity 0.3s;
&:hover {
background-color: $gray1 !important;
.controls {
visibility: visible;
opacity: 1.0;
}
&::before {
border-color: transparent transparent transparent $gray2 !important;
}
}
.children-icon {
color: $blue2;
position: absolute;
left: -25px;
}
.term-name {
text-overflow: ellipsis;
overflow-x: hidden;
white-space: nowrap;
margin-left: 0.4em;
margin-right: 0.4em;
display: inline-block;
max-width: 73%;
&.is-danger {
color: $danger !important;
}
}
.label-details {
font-weight: normal;
color: $gray3;
margin-right: auto;
}
.not-saved {
font-style: italic;
font-weight: bold;
color: $danger;
}
.children-dropdown {
margin-left: auto;
color: $blue4;
cursor: pointer;
padding-right: 1rem;
white-space: nowrap;
overflow: hidden;
display: flex;
align-items: center;
}
.controls {
visibility: hidden;
opacity: 0.0;
background-color: $gray2;
padding: 0.4375rem 0.875rem;
a {
margin: 0 0.375rem;
.icon {
bottom: 1px;
position: relative;
i, i:before { font-size: 20px; }
a {
margin-right: 8px;
}
}
}
}
&.opened-term {
cursor: default;
background-color: $blue1;
&:before {
content: '';
display: block;
position: absolute;
left: 100%;
right: -20px;
width: 0;
height: 0;
border-style: solid;
border-color: transparent transparent transparent $blue1;
border-left-width: 24px;
border-top-width: 20px;
border-bottom-width: 20px;
top: 0;
}
&:hover:before {
border-color: transparent transparent transparent $gray1;
}
}
&.collapsed-term {
display: none;
visibility: hidden;
opacity: 0;
transition: display 0.3s, visibility 0.3s, opacity 0.3s;
}
}
.edit-forms-list {
padding-left: 0;
}

View File

@ -8,6 +8,7 @@ import Buefy from 'buefy';
import VTooltip from 'v-tooltip';
import { VueHammer } from 'vue2-hammer';
import VueMasonry from 'vue-masonry-css';
import t from 't';
// Custom elements
import Text from '../../classes/metadata-types/text/Text.vue';
@ -49,6 +50,7 @@ import VueTheMask from 'vue-the-mask';
// Configure and Register Plugins
Vue.use(Buefy);
Vue.use(VTooltip);
Vue.use(t);
Vue.use(VueHammer);
Vue.use(VueMasonry);
Vue.use(I18NPlugin);

View File

@ -212,7 +212,6 @@ export const fetchChildTerms = ({ commit }, { parentId, taxonomyId, fetchOnly, s
} else if(fetchOnly && search && all ){
query = `?hideempty=0&order=${order}&${qs.stringify(fetchOnly)}&${qs.stringify(search)}`;
} else if(search && !all && !fetchOnly){
console.log(search)
query = `?hideempty=0&order=${order}&${qs.stringify(search)}`;
} else {
query =`?hideempty=0&order=${order}`;

View File

@ -1,4 +1,5 @@
import Vue from 'vue';
import t from 't';
// TAXONOMIES
export const setTaxonomy = (state, taxonomy) => {
@ -37,20 +38,28 @@ export const setTerms = (state, terms) => {
};
export const setChildTerms = (state, { terms, parent }) => {
let index = state.terms.findIndex(aTerm => aTerm.id == parent);
if (index >= 0) {
let newIndex = 1;
for (let i = 0; i < terms.length; i++) {
let termIndex = state.terms.findIndex(aTerm => aTerm.id == terms[i].id);
if (termIndex >= 0) {
state.terms[termIndex] = terms[i];
} else {
state.terms.splice(index + newIndex, 0, terms[i]);
newIndex++;
if (parent > 0 ) {
for (let term of state.terms) {
let parentTerm = t.find(term, [], (node, par) => { return node.id == parent; });
if (parentTerm != undefined) {
if (parentTerm['children'] == undefined)
Vue.set(parentTerm, 'children', []);
for (let term of terms){
parentTerm['children'].push(term);
}
}
}
} else {
state.terms = terms;
if (state.terms != undefined) {
for (let term of terms)
state.terms.push(term);
} else {
state.terms = terms;
}
}
};