Skip to content
Snippets Groups Projects
FeatureDetail.vue 17.4 KiB
Newer Older
Florent Lavelle's avatar
Florent Lavelle committed
      v-if="currentFeature"
Florent Lavelle's avatar
Florent Lavelle committed
      class="ui grid stackable"
Florent Lavelle's avatar
Florent Lavelle committed
        <div class="sixteen wide column">
            v-if="project"
            :features-count="featuresCount"
            :slug-signal="slugSignal"
            :feature-type="featureType"
            :fast-edition-mode="project.fast_edition_mode"
            :is-feature-creator="isFeatureCreator"
            :can-edit-feature="canEditFeature"
            :can-delete-feature="canDeleteFeature"
            @fastEditFeature="validateFastEdition"
            @setIsDeleting="isDeleting = true"
Florent Lavelle's avatar
Florent Lavelle committed
        <div class="eight wide column">
          <FeatureTable
            v-if="project"
            ref="featureTable"
            :fast-edition-mode="project.fast_edition_mode"
Florent Lavelle's avatar
Florent Lavelle committed
          />
Florent Lavelle's avatar
Florent Lavelle committed
        <div class="eight wide column">
          <div
            id="map"
            ref="map"
          />
          <div
            id="popup"
DESPRES Damien's avatar
DESPRES Damien committed
            class="ol-popup"
          >
            <a
              id="popup-closer"
              href="#"
DESPRES Damien's avatar
DESPRES Damien committed
              class="ol-popup-closer"
            />
DESPRES Damien's avatar
DESPRES Damien committed
              id="popup-content"
            />
          </div>
Florent Lavelle's avatar
Florent Lavelle committed
        <div class="eight wide column">
          <FeatureAttachements
            :attachments="attachments"
          />
Florent Lavelle's avatar
Florent Lavelle committed
        <div class="eight wide column">
          <FeatureComments
            :events="events"
Florent Lavelle's avatar
Florent Lavelle committed
          />
        v-if="isDeleting"
        class="ui dimmer modals visible active"
            'ui mini modal',
            { 'active visible': isDeleting },
Florent Lavelle's avatar
Florent Lavelle committed
          <i
            class="close icon"
Florent Lavelle's avatar
Florent Lavelle committed
            aria-hidden="true"
            @click="isDeleting = false"
          <div
            v-if="isDeleting"
            class="ui icon header"
          >
Florent Lavelle's avatar
Florent Lavelle committed
            <i
              class="trash alternate icon"
              aria-hidden="true"
            />
Timothee P's avatar
Timothee P committed
            <button
              type="button"
              class="ui red compact fluid button"
              @click="deleteFeature"
Timothee P's avatar
Timothee P committed
              Confirmer la suppression
            </button>

      <div
        v-if="isLeaving"
        class="ui dimmer modals visible active"
      >
        <div
          :class="[
            'ui mini modal',
            { 'active visible': isLeaving },
          ]"
        >
          <i
            class="close icon"
            aria-hidden="true"
            @click="isLeaving = false"
          />
          <div class="ui icon header">
            <i
              :class="[project.fast_edition_mode && hasUnsavedChange ? 'sign-out' : 'random', 'icon']"
              aria-hidden="true"
            />
            Abandonner {{
              project.fast_edition_mode && hasUnsavedChange ?
                'les modifications' :
                'la vue signalement filtré'
            }}
          </div>
          <div class="content">
            {{
              project.fast_edition_mode && hasUnsavedChange ?
                'Les modifications apportées au signalement ne seront pas sauvegardées, continuer ?':
                `Vous allez quittez la vue signalement filtré,
                  l\'ordre des signalements pourrait changer après édition d\'un signalement.`
            }}
          </div>
          <div class="actions">
            <button
              type="button"
              class="ui green compact button"
              @click="stayOnPage"
            >
              Annuler
            </button>
            <button
              type="button"
              class="ui red compact button"
              @click="leavePage"
            >
              Continuer
            </button>
          </div>
        </div>
      </div>
Florent Lavelle's avatar
Florent Lavelle committed
    <div v-else>
      Pas de signalement correspondant trouvé
    </div>
import { mapState, mapActions, mapMutations, mapGetters } from 'vuex';
DESPRES Damien's avatar
DESPRES Damien committed
import mapService from '@/services/map-service';
Florent Lavelle's avatar
Florent Lavelle committed

import axios from '@/axios-client.js';
Florent Lavelle's avatar
Florent Lavelle committed
import featureAPI from '@/services/feature-api';

import FeatureHeader from '@/components/Feature/Detail/FeatureHeader';
import FeatureTable from '@/components/Feature/Detail/FeatureTable';
import FeatureAttachements from '@/components/Feature/Detail/FeatureAttachements';
import FeatureComments from '@/components/Feature/Detail/FeatureComments';
DESPRES Damien's avatar
DESPRES Damien committed
import { buffer } from 'ol/extent';
  name: 'FeatureDetail',
Florent Lavelle's avatar
Florent Lavelle committed
  components: {
    FeatureHeader,
    FeatureTable,
    FeatureAttachements,
    FeatureComments
Timothee P's avatar
Timothee P committed
  },

  beforeRouteEnter (to, from, next) {
    // if the user edited the feature, coming from filtered features details browsing,
    // display a button to turn back to the list view, in order to start again from the list
    // because order changes after edition, depending the sort criteria
    // in beforeRouteEnter, the component is not mounted and this doesn't exist yet, thus we store the value in window object
    window.displayToListButton = false; // reinitialisation of the value
    if (from.query.offset !== undefined) { // if a queryset for filtered features is stored in the route from
      window.displayToListButton = true; // toggle the value to display the button
    }
    next(); // continue page loading
  },

  beforeRouteUpdate (to, from, next) {
    if (this.hasUnsavedChange) {
      this.confirmLeave(next); // prompt user that there is unsaved changes or that features order might change
    } else {
      next(); // continue navigation
    if (this.hasUnsavedChange || (from.query.offset >= 0 && to.name === 'editer-signalement')) {
      this.confirmLeave(next); // prompt user that there is unsaved changes or that features order might change
    } else {
      next(); // continue navigation
      attachments: [],
          title: '',
          file: null,
          id_for_label: 'add-comment',
          html_name: 'add-comment',
          errors: '',
      isDeleting: false,
      isLeaving: false,
      'user'
Timothee P's avatar
Timothee P committed
    ...mapState('projects', [
      'project'
    ]),
Florent Lavelle's avatar
Florent Lavelle committed
    ...mapState('feature-type', [
    ]),
    ...mapState('feature', [
      'currentFeature',
    ...mapGetters('feature-type', [
      'feature_type',
    ]),
      if (this.project.fast_edition_mode && this.form && this.currentFeature) {
        if (this.form.title !== this.currentFeature.title) return true;
        if (this.form.description.value !== this.currentFeature.description) return true;
        if (this.form.status.value !== this.currentFeature.status) return true;
        for (const xForm of this.$store.state.feature.extra_forms) {
          const originalField = this.currentFeature.feature_data.find(el => el.label === xForm.label);
          if (originalField && xForm.value !== originalField.value) return true;
        }
    },

    isFeatureCreator() {
      if (this.currentFeature && this.user) {
        return this.currentFeature.creator === this.user.id;
      }
      return false;
    },

    isModerator() {
      return this.USER_LEVEL_PROJECTS && this.USER_LEVEL_PROJECTS[this.$route.params.slug] === 'Modérateur';
    },

    isAdministrator() {
      return this.USER_LEVEL_PROJECTS && this.USER_LEVEL_PROJECTS[this.$route.params.slug] === 'Administrateur projet';
    },

    canEditFeature() {
      return (this.permissions && this.permissions.can_update_feature) ||
                this.isFeatureCreator ||
                this.isModerator ||
                this.user.is_superuser;
    },

    canDeleteFeature() {
      return (this.permissions && this.permissions.can_delete_feature && this.isFeatureCreator) ||
                this.isFeatureCreator ||
                this.isModerator ||
                this.isAdministrator ||
                this.user.is_superuser;
Timothee P's avatar
Timothee P committed

  watch: {
    '$route.query'(newValue, oldValue) {
      if (newValue !== oldValue) { //* Navigate back or forward to the previous or next URL
        this.initPage(); //* doesn't update the page at query changes, thus it is done manually here
      }
    },
  },

Timothee P's avatar
Timothee P committed
  mounted() {
    // when this is available, set the value with previously stored value in windows to pass it as a prop
    this.displayToListButton = window.displayToListButton;
Timothee P's avatar
Timothee P committed
  },

  beforeDestroy() {
    this.$store.commit('CLEAR_MESSAGES');
Timothee P's avatar
Timothee P committed
  },

Florent Lavelle's avatar
Florent Lavelle committed
    ...mapMutations([
      'DISPLAY_LOADER',
      'DISCARD_LOADER'
    ]),
    ...mapMutations('feature', [
      'SET_CURRENT_FEATURE'
    ]),
Florent Lavelle's avatar
Florent Lavelle committed
    ...mapMutations('feature-type', [
      'SET_CURRENT_FEATURE_TYPE_SLUG'
    ]),
    ...mapActions('projects', [
      'GET_PROJECT',
      'GET_PROJECT_INFO'
    ]),
    ...mapActions('feature', [
Florent Lavelle's avatar
Florent Lavelle committed
      'GET_PROJECT_FEATURE',
      'GET_PROJECT_FEATURES'
    ]),
    async initPage() {
      await this.getPageInfo();
      this.initMap();
    },

    async getPageInfo() {
      if (this.$route.params.slug_signal && this.$route.params.slug_type_signal) { // if coming from the route with an id
        this.SET_CURRENT_FEATURE_TYPE_SLUG(this.$route.params.slug_type_signal);
        this.slugSignal = this.$route.params.slug_signal;
        this.featureType = this.feature_type;
      } //* else it would be retrieve after fetchFilteredFeature with offset
      this.DISPLAY_LOADER('Recherche du signalement');
      let promises = [];
      //* Chargements des features et infos projet en cas d'arrivée directe sur la page ou de refresh
      if (!this.project) {
        promises.push(
          this.GET_PROJECT(this.$route.params.slug),
          this.GET_PROJECT_INFO(this.$route.params.slug),
        );
      }
      //* changement de requête selon s'il y a un id ou un offset(dans le cas du parcours des signalements filtrés)
      if (this.$route.query.offset >= 0) {
        promises.push(this.fetchFilteredFeature());
      } else if (!this.currentFeature || this.currentFeature.feature_id !== this.slugSignal) {
        promises.push(
          this.GET_PROJECT_FEATURE({
            project_slug: this.$route.params.slug,
            feature_id: this.slugSignal,
          })
        );
      }
      await axios.all(promises);
      this.getFeatureEvents();
      this.getFeatureAttachments();
      this.getLinkedFeatures();
      if (this.project.fast_edition_mode) {
        this.$store.commit('feature/INIT_FORM');
        this.$store.dispatch('feature/INIT_EXTRA_FORMS');
      }
    confirmLeave(next) {
      this.next = next;
      this.isLeaving = true;
    },

    stayOnPage() {
      this.isLeaving = false;
    },

    leavePage() {
    async reloadPage() {
      await this.getPageInfo();
      mapService.removeFeatures();
    pushNgo(newEntry) {
      this.$router.push(newEntry) //* update the params or queries in the route/url
        .then(() => {
          this.reloadPage();
        })
        .catch(() => true); //* catch error if navigation get aborted (in beforeRouteUpdate)
    },

leandro's avatar
leandro committed
    goBackToProject(message) {
      this.$router.push({
        name: 'project_detail',
leandro's avatar
leandro committed
        params: {
leandro's avatar
leandro committed
          message,
        },
      });
    },
Florent Lavelle's avatar
Florent Lavelle committed
        .dispatch('feature/DELETE_FEATURE', { feature_id: this.currentFeature.feature_id })
        .then(async (response) => {
            await this.GET_PROJECT_FEATURES({
              project_slug: this.$route.params.slug
            });
    fetchFilteredFeature() {
      const queryString = new URLSearchParams(this.$route.query);
      const url = `${this.$store.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${this.$route.params.slug}/feature-paginated/?limit=1&${queryString}`;
      return featureAPI.getPaginatedFeatures(url)
        .then((data) => {
          if (data && data.results && data.results[0]) {
            this.featuresCount = data.count;
            this.previous = data.previous;
            this.next = data.next;
            this.SET_CURRENT_FEATURE(data.results[0]);
            const { feature_id, feature_type } = data.results[0];
            this.slugSignal = feature_id;
            this.featureType = feature_type;
            this.SET_CURRENT_FEATURE_TYPE_SLUG(feature_type.slug);
    initMap() {
      var mapDefaultViewCenter =
        this.$store.state.configuration.DEFAULT_MAP_VIEW.center;
      var mapDefaultViewZoom =
        this.$store.state.configuration.DEFAULT_MAP_VIEW.zoom;
DESPRES Damien's avatar
DESPRES Damien committed
      this.map = mapService.createMap(this.$refs.map, {
        maxZoom: this.project.map_max_zoom_level,
Florent Lavelle's avatar
Florent Lavelle committed
        interactions : {
          doubleClickZoom :false,
          mouseWheelZoom: false,
          dragPan: false
        }
      });

      // Update link to feature list with map zoom and center
DESPRES Damien's avatar
DESPRES Damien committed
      mapService.addMapEventListener('moveend', function () {
        // update link to feature list with map zoom and center
        /*var $featureListLink = $("#feature-list-link")
        var baseUrl = $featureListLink.attr("href").split("?")[0]

        $featureListLink.attr("href", baseUrl +`?zoom=${this.map.getZoom()}&lat=${this.map.getCenter().lat}&lng=${this.map.getCenter().lng}`)*/
      });

      // Load the layers.
      // - if one basemap exists, we load the layers of the first one
      // - if not, load the default map and service options
      let layersToLoad = null;
      var baseMaps = this.$store.state.map.basemaps;
      var layers = this.$store.state.map.availableLayers;
DESPRES Damien's avatar
DESPRES Damien committed
        const basemapIndex = 0;
        layersToLoad = baseMaps[basemapIndex].layers;
        layersToLoad.forEach((layerToLoad) => {
          layers.forEach((layer) => {
            if (layer.id === layerToLoad.id) {
              layerToLoad = Object.assign(layerToLoad, layer);
            }
          });
        });
        layersToLoad.reverse();
      }
DESPRES Damien's avatar
DESPRES Damien committed
      mapService.addLayers(
        this.$store.state.configuration.DEFAULT_BASE_MAP_SERVICE,
        this.$store.state.configuration.DEFAULT_BASE_MAP_OPTIONS,
        this.$store.state.configuration.DEFAULT_BASE_MAP_SCHEMA_TYPE,

      this.addFeatureToMap();
    },

    addFeatureToMap() {
leandro's avatar
leandro committed
      const url = `${this.$store.state.configuration.VUE_APP_DJANGO_API_BASE}projects/${this.$route.params.slug}/feature/` +
                  `?feature_id=${this.slugSignal}&output=geojson`;
          if (response.data.features.length > 0) {
DESPRES Damien's avatar
DESPRES Damien committed
            const featureGroup = mapService.addFeatures(
Florent Lavelle's avatar
Florent Lavelle committed
              this.feature_types,
              true
DESPRES Damien's avatar
DESPRES Damien committed
            mapService.fitExtent(buffer(featureGroup.getExtent(),200));
    getFeatureEvents() {
      featureAPI
        .then((data) => (this.events = data));
    },

    getFeatureAttachments() {
      featureAPI
        .then((data) => (this.attachments = data));
    },

    getLinkedFeatures() {
      featureAPI
          this.$store.commit('feature/SET_LINKED_FEATURES', data)

    checkAddedForm() {
      let isValid = true; //* fallback if all customForms returned true
      if (this.$refs.featureTable && this.$refs.featureTable.$refs.extraForm) {
        for (const extraForm of this.$refs.featureTable.$refs.extraForm) {
          if (extraForm.checkForm() === false) {
            isValid = false;
          }
        }
      }
      return isValid;
    },

    validateFastEdition() {
      let is_valid = true;
      is_valid = this.checkAddedForm();
      if (is_valid) {
        this.$store.dispatch('feature/SEND_FEATURE', this.$route.name)
          .then(() => {
            this.getFeatureEvents();
            mapService.removeFeatures();
            this.addFeatureToMap();
          });
Timothee P's avatar
Timothee P committed
<style scoped>
#map {
  width: 100%;
  height: 100%;
  min-height: 250px;
  max-height: 70vh;
}