Newer
Older
Sébastien DA ROCHA
committed
<template>
<div class="ui segment">
<div class="field required">
<label for="basemap-title">Titre</label>
<!-- eslint-disable -->
Sébastien DA ROCHA
committed
<input
:value="basemap.title"
@input="updateTitle"
Sébastien DA ROCHA
committed
type="text"
name="basemap-title"
required
<ul
v-if="basemap.errors && basemap.errors.length > 0"
id="errorlist-title"
class="errorlist"
>
<li>
{{ basemap.errors }}
</li>
</ul>
Sébastien DA ROCHA
committed
</div>
<div class="nested">
:class="[basemap.layers.length > 0 ? 'ui segments': '', 'layers-container']"
Sébastien DA ROCHA
committed
<ProjectMappingContextLayer
v-for="layer in basemap.layers"
:key="'layer-' + layer.dataKey"
:layer="layer"
:basemapid="basemap.id"
/>
</div>
<div class="ui buttons">
<a
class="ui compact small icon left floated button green"
Sébastien DA ROCHA
committed
>
Sébastien DA ROCHA
committed
<span>Ajouter une couche</span>
</a>
</div>
<div
class="ui buttons"
@click="deleteBasemap"
>
Sébastien DA ROCHA
committed
<a
class="
ui
compact
red
small
icon
right
floated
button button-hover-green
"
>
Sébastien DA ROCHA
committed
<span>Supprimer ce fond cartographique</span>
</a>
</div>
</div>
</div>
</template>
<script>
/* eslint-disable */
import Sortable from 'sortablejs';
import ProjectMappingContextLayer from '@/components/project/ProjectMappingContextLayer.vue';
Sébastien DA ROCHA
committed
export default {
Sébastien DA ROCHA
committed
components: {
ProjectMappingContextLayer,
},
props: {
basemap: {
type: Object,
default: null,
}
},
Sébastien DA ROCHA
committed
computed: {
maxLayersCount: function () {
return this.basemap.layers.reduce((acc, curr) => {
if (curr.dataKey > acc) {
return curr.dataKey;
} else {
return acc;
}
}, 0);
},
},
created() {
if (this.basemap.layers) {
//* add datakeys to layers coming from api
this.fillLayersWithDatakey(this.basemap.layers);
}
},
mounted() {
this.initSortable();
},
Sébastien DA ROCHA
committed
methods: {
deleteBasemap() {
this.$store.commit('map/DELETE_BASEMAP', this.basemap.id);
Sébastien DA ROCHA
committed
},
addLayer(layer) {
const newLayer = {
dataKey: this.maxLayersCount + 1,
Sébastien DA ROCHA
committed
order: 0,
queryable: false,
Sébastien DA ROCHA
committed
...layer,
};
this.$store.commit('map/UPDATE_BASEMAP', {
Sébastien DA ROCHA
committed
layers: [...this.basemap.layers, newLayer],
id: this.basemap.id,
title: this.basemap.title,
Sébastien DA ROCHA
committed
});
},
updateTitle(evt) {
let errors = '';
if(evt.target.value === '') { //* delete or add error message while typing
errors = "Veuillez compléter ce champ.";
}
this.$store.commit('map/UPDATE_BASEMAP', {
Sébastien DA ROCHA
committed
id: this.basemap.id,
title: evt.target.value,
errors,
Sébastien DA ROCHA
committed
});
Sébastien DA ROCHA
committed
},
removeLayer(dataKey) {
this.layers = this.layers.filter((layer) => layer.dataKey !== dataKey);
},
fillLayersWithDatakey(layers) {
let dataKey = 0;
this.$store.commit('map/REPLACE_BASEMAP_LAYERS', {
Sébastien DA ROCHA
committed
basemapId: this.basemap.id,
layers: layers.map((el) => {
dataKey += 1;
return { dataKey, ...el };
}),
});
},
//* drag & drop *//
//* Get the names of the current layers in order.
const currentLayersNamesInOrder = Array.from(
document.getElementsByClassName('layer-item')
).map((el) => el.id);
//* increment value 'order' in this.basemap.layers looping over layers from template ^
let order = 0;
let movedLayers = [];
for (let id of currentLayersNamesInOrder) {
let matchingLayer = this.basemap.layers.find(
(el) => el.dataKey === Number(id)
);
if (matchingLayer) {
movedLayers.push(matchingLayer);
order += 1;
}
}
//* update the store
this.$store.commit('map/UPDATE_BASEMAP', {
layers: movedLayers,
id: this.basemap.id,
title: this.basemap.title,
errors: this.basemap.errors,
});
},
initSortable() {
new Sortable(document.getElementById(`list-${this.basemap.id}`), {
animation: 150,
handle: '.layer-handle-sort', // The element that is active to drag
ghostClass: 'blue-background-class',
dragClass: 'white-opacity-background-class',
onEnd: this.onlayerMove.bind(this),
});
},