refactor: scenery view back button routing; component setup script

This commit is contained in:
2025-06-02 01:35:09 +02:00
parent c8d481a952
commit a91a00f88a
2 changed files with 89 additions and 117 deletions
+5 -2
View File
@@ -36,7 +36,10 @@ const routes: Array<RouteRecordRaw> = [
props: (route) => ({ props: (route) => ({
region: route.query.region, region: route.query.region,
station: route.query.station station: route.query.station
}) }),
beforeEnter: (to, from) => {
to.meta['prevPath'] = from.fullPath;
}
}, },
{ {
path: '/journal', path: '/journal',
@@ -72,7 +75,7 @@ const router = createRouter({
from.query['view'] === undefined && from.query['view'] === undefined &&
!savedPosition !savedPosition
) )
return { el: `.scenery-left`, behavior: 'instant', top: 3 }; return { el: `.app_main`, behavior: 'instant', top: -13 };
if (savedPosition) return savedPosition; if (savedPosition) return savedPosition;
}, },
+55 -86
View File
@@ -23,8 +23,8 @@
v-for="(viewMode, i) in viewModes" v-for="(viewMode, i) in viewModes"
:key="i" :key="i"
class="btn btn--option" class="btn btn--option"
:class="{ checked: currentMode == viewMode.component }" :class="{ checked: currentMode == viewMode.component.name }"
@click="setViewMode(viewMode.component)" @click="setViewMode(viewMode.component.name!)"
> >
{{ $t(viewMode.id) }} {{ $t(viewMode.id) }}
</button> </button>
@@ -32,17 +32,17 @@
<div <div
v-if=" v-if="
apiStore.dataStatuses.sceneries == Status.Loading || apiStore.dataStatuses.sceneries == Status.Data.Loading ||
apiStore.dataStatuses.connection == Status.Loading apiStore.dataStatuses.connection == Status.Data.Loading
" "
></div> ></div>
<keep-alive v-else> <keep-alive v-else>
<component <component
:is="currentMode" :is="currentViewComponent"
:onlineScenery="onlineSceneryInfo" :onlineScenery="onlineSceneryInfo"
:station="stationInfo" :station="stationInfo"
:key="currentMode" :key="currentViewComponent.name"
></component> ></component>
</keep-alive> </keep-alive>
</div> </div>
@@ -50,40 +50,28 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { computed, defineComponent } from 'vue'; import { computed, onMounted } from 'vue';
import { useRoute } from 'vue-router'; import { useRoute, useRouter } from 'vue-router';
import routerMixin from '../mixins/routerMixin';
import { useMainStore } from '../store/mainStore'; import { useMainStore } from '../store/mainStore';
import SceneryInfo from '../components/SceneryView/SceneryInfo.vue'; import SceneryInfo from '../components/SceneryView/SceneryInfo.vue';
import SceneryHeader from '../components/SceneryView/SceneryHeader.vue'; import SceneryHeader from '../components/SceneryView/SceneryHeader.vue';
import SceneryTimetable from '../components/SceneryView/SceneryTimetable.vue'; import SceneryTimetable from '../components/SceneryView/SceneryTimetable.vue';
import SceneryTimetablesHistory from '../components/SceneryView/SceneryTimetablesHistory.vue'; import SceneryTimetablesHistory from '../components/SceneryView/SceneryTimetablesHistory.vue';
import SceneryDispatchersHistory from '../components/SceneryView/SceneryDispatchersHistory.vue'; import SceneryDispatchersHistory from '../components/SceneryView/SceneryDispatchersHistory.vue';
import ActionButton from '../components/Global/ActionButton.vue';
import { Status } from '../typings/common';
import { useApiStore } from '../store/apiStore'; import { useApiStore } from '../store/apiStore';
import { ref } from 'vue';
import { Status } from '../typings/common';
enum SceneryViewMode { const route = useRoute();
'TIMETABLES_ACTIVE', const router = useRouter();
'TIMETABLES_HISTORY',
'SCENERY_HISTORY'
}
export default defineComponent({ const prevPath = ref('/');
name: 'SceneryView',
components: { const props = defineProps({
SceneryInfo,
SceneryTimetable,
ActionButton,
SceneryHeader,
SceneryTimetablesHistory,
SceneryDispatchersHistory
},
props: {
region: { region: {
type: String, type: String,
required: false required: false
@@ -93,87 +81,68 @@ export default defineComponent({
type: String, type: String,
required: true required: true
} }
}, });
mixins: [routerMixin], const store = useMainStore();
const apiStore = useApiStore();
data: () => ({ const viewModes = [
store: useMainStore(),
apiStore: useApiStore(),
viewModes: [
{ {
id: 'scenery.option-active-timetables', id: 'scenery.option-active-timetables',
component: 'SceneryTimetable' component: SceneryTimetable
}, },
{ {
id: 'scenery.option-timetables-history', id: 'scenery.option-timetables-history',
component: 'SceneryTimetablesHistory' component: SceneryTimetablesHistory
}, },
{ {
id: 'scenery.option-dispatchers-history', id: 'scenery.option-dispatchers-history',
component: 'SceneryDispatchersHistory' component: SceneryDispatchersHistory
} }
], ];
sceneryViewMode: SceneryViewMode,
selectedCheckpoint: '',
currentViewCompontent: 'SceneryTimetable',
onlineFrom: -1,
Status: Status.Data
}),
setup() { onMounted(() => {
const route = useRoute(); prevPath.value = (route.meta['prevPath'] as string) ?? '/';
});
const isComponentVisible = computed(() => route.path === '/scenery'); const currentMode = computed(() => {
return route.query.view?.toString() ?? 'SceneryTimetable';
});
return { const currentViewComponent = computed(() => {
isComponentVisible return (
}; viewModes.find((mode) => mode.component.name == currentMode.value)?.component ??
}, SceneryTimetable
computed: {
currentMode() {
return this.$route.query.view?.toString() ?? 'SceneryTimetable';
},
stationInfo() {
return this.store.stationList.find(
(station) => station.name === this.station?.toString().replace(/_/g, ' ')
); );
}, });
onlineSceneryInfo() { const stationInfo = computed(() => {
return this.store.activeSceneryList.find( return store.stationList.find(
(station) => station.name === props.station?.toString().replace(/_/g, ' ')
);
});
const onlineSceneryInfo = computed(() => {
return store.activeSceneryList.find(
(scenery) => (scenery) =>
scenery.name === this.station?.toString().replace(/_/g, ' ') && scenery.name === props.station?.toString().replace(/_/g, ' ') &&
scenery.region == this.store.region.id scenery.region == store.region.id
); );
} });
},
methods: { function setViewMode(componentName: string) {
setViewMode(componentName: string) { router.push({
this.$router.push({ path: route.path,
path: this.$route.path,
query: { query: {
...this.$route.query, ...route.query,
view: componentName view: componentName
} }
}); });
}, }
loadSelectedCheckpoint() { function onReturnButtonClick() {
if (!this.stationInfo?.generalInfo?.checkpoints) return; router.push(prevPath.value);
if (this.stationInfo.generalInfo.checkpoints.length == 0) return; }
this.selectedCheckpoint = this.stationInfo.generalInfo.checkpoints[0];
},
onReturnButtonClick() {
this.$router.back();
}
}
});
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>