Newer
Older
Sébastien DA ROCHA
committed
<template>
<div class="ui segment">
<div class="field required">
<label for="basemap-title">Titre</label>
<input
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">
<div
v-if="basemap.layers"
class="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>
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,
},
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);
},
},
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
watch: {
'basemap.title': {
deep: true,
handler: function (newValue, oldValue) {
if (newValue !== oldValue) {
this.basemap.title = newValue;
if (newValue === '')
this.basemap.errors = 'Veuillez compléter ce champ.';
else this.basemap.errors = '';
this.updateTitle(this.basemap);
}
},
},
},
created() {
if (this.basemap.layers) {
//* add datakeys to layers coming from api
this.fillLayersWithDatakey(this.basemap.layers);
}
},
// destroyed(){
// this.errors = [];
// }
mounted() {
//* not present in original
this.initSortable();
// if (!this.basemap.title) {
// this.$store.commit("map/UPDATE_BASEMAP", {
// id: this.basemap.id,
// title: newValue,
// });
// }
},
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) {
this.$store.commit('map/UPDATE_BASEMAP', {
Sébastien DA ROCHA
committed
id: this.basemap.id,
title: evt.title,
errors: evt.errors,
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),
});
},