2018-06-25 18:29:57 +02:00
|
|
|
<template>
|
|
|
|
<v-container fluid fill-height class="no-padding">
|
2018-07-16 17:26:10 +02:00
|
|
|
<v-layout row wrap fill-height>
|
2018-07-05 22:40:24 +02:00
|
|
|
<ReportCard></ReportCard>
|
2018-07-21 20:00:37 +02:00
|
|
|
<v-flex xs12 fill-height v-if="mapCenter">
|
|
|
|
<Map
|
|
|
|
:accuracy="accuracy"
|
|
|
|
:center="mapCenter"
|
|
|
|
:heading="heading"
|
|
|
|
:markers="reportsMarkers"
|
|
|
|
:onMapCenterUpdate="onMapCenterUpdate"
|
|
|
|
:onMapZoomUpdate="onMapZoomUpdate"
|
|
|
|
:onPress="showReportDialog"
|
|
|
|
:polyline="positionHistory"
|
|
|
|
:positionLatLng="currentLatLng"
|
|
|
|
:reportLatLng="reportLatLng"
|
|
|
|
:zoom="mapZoom"
|
|
|
|
></Map>
|
2018-06-25 18:29:57 +02:00
|
|
|
<v-btn
|
2018-07-03 18:09:56 +02:00
|
|
|
absolute
|
2018-06-25 18:29:57 +02:00
|
|
|
dark
|
|
|
|
fab
|
2018-07-02 18:27:35 +02:00
|
|
|
large
|
2018-06-25 18:29:57 +02:00
|
|
|
bottom
|
|
|
|
right
|
|
|
|
color="orange"
|
|
|
|
class="overlayButton"
|
2018-06-28 14:40:56 +02:00
|
|
|
@click.native.stop="() => showReportDialog()"
|
2018-07-11 01:14:54 +02:00
|
|
|
role="button"
|
|
|
|
:aria-label="$t('buttons.reportProblem')"
|
2018-07-21 20:00:37 +02:00
|
|
|
v-if="!hasCenterProvidedByRoute"
|
2018-06-25 18:29:57 +02:00
|
|
|
>
|
|
|
|
<v-icon>report_problem</v-icon>
|
|
|
|
</v-btn>
|
2018-07-21 20:00:37 +02:00
|
|
|
<ReportDialog v-model="isReportDialogVisible" :latLng="reportLatLng" :onHide="resetReportLatLng"></ReportDialog>
|
2018-06-25 18:29:57 +02:00
|
|
|
</v-flex>
|
2018-07-17 22:49:00 +02:00
|
|
|
<v-flex xs12 sm6 offset-sm3 md4 offset-md4 fill-height v-else class="pa-3">
|
2018-07-21 20:00:37 +02:00
|
|
|
<LocationError :error="error" :retryFunction="initializePositionWatching" v-if="error"></LocationError>
|
|
|
|
<p class="text-xs-center" v-else>{{ $t('geolocation.fetching') }}</p>
|
2018-06-25 18:29:57 +02:00
|
|
|
</v-flex>
|
|
|
|
</v-layout>
|
|
|
|
</v-container>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2018-06-25 23:57:33 +02:00
|
|
|
import NoSleep from 'nosleep.js';
|
|
|
|
|
2018-07-21 20:00:37 +02:00
|
|
|
import LocationError from '@/components/LocationError.vue';
|
2018-06-25 18:29:57 +02:00
|
|
|
import Map from '@/components/Map.vue';
|
2018-07-05 22:40:24 +02:00
|
|
|
import ReportCard from '@/components/ReportCard.vue';
|
2018-06-25 18:29:57 +02:00
|
|
|
import ReportDialog from '@/components/ReportDialog/index.vue';
|
2018-07-17 23:19:28 +02:00
|
|
|
|
2018-06-27 11:17:38 +02:00
|
|
|
import * as constants from '@/constants';
|
2018-06-26 11:04:23 +02:00
|
|
|
import { distance, mockLocation } from '@/tools';
|
2018-07-21 20:00:37 +02:00
|
|
|
|
|
|
|
import i18n from '@/i18n';
|
2018-07-17 23:19:28 +02:00
|
|
|
import store from '@/store';
|
2018-06-26 11:04:23 +02:00
|
|
|
|
2018-07-21 20:00:37 +02:00
|
|
|
function handlePositionError(error) {
|
|
|
|
// TODO: Not translated when changing locale
|
|
|
|
store.dispatch('setLocationError', { error: error.code });
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPosition(position) {
|
|
|
|
const currentLatLng = store.state.location.currentLatLng;
|
|
|
|
if (currentLatLng) {
|
|
|
|
const distanceFromPreviousPoint = distance(
|
|
|
|
[currentLatLng[0], currentLatLng[1]],
|
|
|
|
[position.coords.latitude, position.coords.longitude],
|
|
|
|
);
|
|
|
|
if (distanceFromPreviousPoint > constants.UPDATE_REPORTS_DISTANCE_THRESHOLD) {
|
|
|
|
store.dispatch('fetchReports');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store.dispatch(
|
|
|
|
'setCurrentPosition',
|
|
|
|
{
|
|
|
|
accuracy: position.coords.accuracy ? position.coords.accuracy : null,
|
|
|
|
heading: (
|
|
|
|
(position.coords.heading !== null && !isNaN(position.coords.heading))
|
|
|
|
? position.coords.heading
|
|
|
|
: null
|
|
|
|
),
|
|
|
|
latLng: [position.coords.latitude, position.coords.longitude],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-25 18:29:57 +02:00
|
|
|
export default {
|
2018-07-21 20:00:37 +02:00
|
|
|
beforeDestroy() {
|
|
|
|
this.disableNoSleep();
|
|
|
|
this.$store.dispatch('hideReportDetails');
|
2018-06-25 18:29:57 +02:00
|
|
|
},
|
2018-07-17 23:19:28 +02:00
|
|
|
beforeRouteEnter(to, from, next) {
|
2018-07-21 20:00:37 +02:00
|
|
|
if (to.name !== 'SharedMap') {
|
|
|
|
// Check that intro was seen except if we are in SharedMap view.
|
|
|
|
// This is required in order to ensure NoSleep works well.
|
|
|
|
if (!store.state.hasGoneThroughIntro) {
|
|
|
|
return next({ name: 'Onboarding', replace: true });
|
|
|
|
}
|
2018-07-03 18:09:56 +02:00
|
|
|
}
|
2018-07-17 23:19:28 +02:00
|
|
|
return next();
|
2018-07-16 17:26:10 +02:00
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
components: {
|
|
|
|
LocationError,
|
|
|
|
Map,
|
|
|
|
ReportCard,
|
|
|
|
ReportDialog,
|
2018-06-25 18:29:57 +02:00
|
|
|
},
|
2018-06-26 11:04:23 +02:00
|
|
|
computed: {
|
2018-07-21 20:00:37 +02:00
|
|
|
accuracy() {
|
|
|
|
return this.$store.state.location.accuracy;
|
|
|
|
},
|
|
|
|
currentLatLng() {
|
|
|
|
const currentLatLng = this.$store.state.location.currentLatLng;
|
|
|
|
// Check that this is a correct position
|
|
|
|
if (currentLatLng.some(item => item === null)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return currentLatLng;
|
|
|
|
},
|
|
|
|
error() {
|
|
|
|
const errorCode = this.$store.state.location.error;
|
|
|
|
if (errorCode) {
|
|
|
|
let errorString = `${i18n.t('geolocation.errorFetchingPosition')} `;
|
|
|
|
if (errorCode === 1) {
|
|
|
|
errorString += i18n.t('geolocation.permissionDenied');
|
|
|
|
} else if (errorCode === 2) {
|
|
|
|
errorString += i18n.t('geolocation.positionUnavailable');
|
|
|
|
} else {
|
|
|
|
errorString += i18n.t('geolocation.timeout');
|
|
|
|
}
|
|
|
|
return errorString;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
hasCenterProvidedByRoute() {
|
|
|
|
return this.$route.params.lat && this.$route.params.lng;
|
|
|
|
},
|
|
|
|
heading() {
|
|
|
|
return this.$store.state.location.heading;
|
|
|
|
},
|
|
|
|
mapCenter() {
|
|
|
|
if (this.hasCenterProvidedByRoute) {
|
|
|
|
return [this.$route.params.lat, this.$route.params.lng];
|
|
|
|
}
|
|
|
|
return this.currentLatLng;
|
|
|
|
},
|
|
|
|
mapZoom() {
|
|
|
|
return (
|
|
|
|
this.$route.params.zoom
|
|
|
|
? parseInt(this.$route.params.zoom, 10)
|
|
|
|
: constants.DEFAULT_ZOOM
|
|
|
|
);
|
|
|
|
},
|
|
|
|
positionHistory() {
|
|
|
|
return this.$store.state.location.positionHistory;
|
|
|
|
},
|
2018-06-26 11:04:23 +02:00
|
|
|
reportsMarkers() {
|
2018-07-05 22:40:24 +02:00
|
|
|
return this.$store.getters.notDismissedReports.map(report => ({
|
2018-06-26 11:04:23 +02:00
|
|
|
id: report.id,
|
2018-06-27 14:59:45 +02:00
|
|
|
type: report.attributes.type,
|
2018-06-26 11:04:23 +02:00
|
|
|
latLng: [report.attributes.lat, report.attributes.lng],
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
},
|
2018-06-25 18:29:57 +02:00
|
|
|
data() {
|
|
|
|
return {
|
2018-07-21 20:00:37 +02:00
|
|
|
isReportDialogVisible: false,
|
2018-06-25 23:57:33 +02:00
|
|
|
noSleep: null,
|
2018-07-21 20:00:37 +02:00
|
|
|
reportLatLng: null,
|
2018-06-25 18:29:57 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
2018-07-21 20:00:37 +02:00
|
|
|
disableNoSleep() {
|
|
|
|
if (this.noSleep) {
|
|
|
|
this.noSleep.disable();
|
|
|
|
}
|
|
|
|
},
|
2018-06-25 18:29:57 +02:00
|
|
|
initializePositionWatching() {
|
2018-07-21 20:00:37 +02:00
|
|
|
if (this.$store.state.location.watcherID !== null) {
|
|
|
|
// Already watching location, no need to add another watcher
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$store.dispatch('setLocationError', { error: null }); // Reset any error
|
2018-06-25 18:29:57 +02:00
|
|
|
|
2018-07-21 20:00:37 +02:00
|
|
|
// Set up a watcher
|
|
|
|
let watchID = null;
|
2018-06-27 11:17:38 +02:00
|
|
|
if (constants.MOCK_LOCATION) {
|
2018-07-21 20:00:37 +02:00
|
|
|
setPosition(mockLocation());
|
|
|
|
watchID = setInterval(
|
|
|
|
() => setPosition(mockLocation()),
|
2018-06-27 11:17:38 +02:00
|
|
|
constants.MOCK_LOCATION_UPDATE_INTERVAL,
|
2018-06-26 11:04:23 +02:00
|
|
|
);
|
2018-06-25 18:29:57 +02:00
|
|
|
} else {
|
2018-06-26 11:04:23 +02:00
|
|
|
if (!('geolocation' in navigator)) {
|
2018-07-21 20:00:37 +02:00
|
|
|
this.$store.dispatch('setLocationError', { error: this.$t('geolocation.unavailable') });
|
|
|
|
return;
|
2018-06-26 11:04:23 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 20:00:37 +02:00
|
|
|
watchID = navigator.geolocation.watchPosition(
|
|
|
|
setPosition,
|
|
|
|
handlePositionError,
|
2018-06-25 18:29:57 +02:00
|
|
|
{
|
|
|
|
enableHighAccuracy: true,
|
|
|
|
maximumAge: 30000,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2018-07-21 20:00:37 +02:00
|
|
|
this.$store.dispatch('setLocationWatcherId', { id: watchID });
|
2018-06-25 18:29:57 +02:00
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
onMapCenterUpdate(center) {
|
|
|
|
this.$store.dispatch('setCurrentMapCenter', { center });
|
2018-06-25 18:29:57 +02:00
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
onMapZoomUpdate(zoom) {
|
|
|
|
this.$store.dispatch('setCurrentMapZoom', { zoom });
|
2018-06-25 18:29:57 +02:00
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
resetReportLatLng() {
|
|
|
|
this.reportLatLng = null;
|
2018-07-16 17:26:10 +02:00
|
|
|
},
|
2018-06-25 23:57:33 +02:00
|
|
|
setNoSleep() {
|
2018-07-17 16:22:18 +02:00
|
|
|
if (this.$store.state.settings.preventSuspend) {
|
2018-07-03 19:01:56 +02:00
|
|
|
this.noSleep = new NoSleep();
|
|
|
|
this.noSleep.enable();
|
|
|
|
}
|
2018-06-25 23:57:33 +02:00
|
|
|
},
|
2018-06-28 14:40:56 +02:00
|
|
|
showReportDialog(latlng) {
|
|
|
|
if (latlng) {
|
2018-07-21 20:00:37 +02:00
|
|
|
this.reportLatLng = [latlng.lat, latlng.lng];
|
2018-06-28 14:40:56 +02:00
|
|
|
} else {
|
2018-07-21 20:00:37 +02:00
|
|
|
this.reportLatLng = this.currentLatLng;
|
2018-06-28 14:40:56 +02:00
|
|
|
}
|
2018-07-21 20:00:37 +02:00
|
|
|
this.isReportDialogVisible = true;
|
2018-07-17 22:49:00 +02:00
|
|
|
},
|
2018-07-16 17:26:10 +02:00
|
|
|
},
|
|
|
|
mounted() {
|
2018-07-21 20:00:37 +02:00
|
|
|
if (this.$route.name !== 'SharedMap') {
|
|
|
|
// Only enable NoSleep in normal map view (with position tracking).
|
|
|
|
this.setNoSleep();
|
|
|
|
this.initializePositionWatching();
|
|
|
|
}
|
2018-07-16 17:26:10 +02:00
|
|
|
this.$store.dispatch('fetchReports');
|
2018-06-25 18:29:57 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
.no-padding {
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<style>
|
2018-07-03 18:09:56 +02:00
|
|
|
.v-overlay, .v-dialog__content {
|
2018-06-25 18:29:57 +02:00
|
|
|
z-index: 1002 !important;
|
|
|
|
}
|
2018-07-03 18:09:56 +02:00
|
|
|
|
|
|
|
.overlayButton {
|
|
|
|
z-index: 1000 !important;
|
|
|
|
position: absolute !important;
|
|
|
|
bottom: 24px !important;
|
|
|
|
border-radius: 50% !important;
|
|
|
|
}
|
2018-06-25 18:29:57 +02:00
|
|
|
</style>
|