import { ActionContext , ActionTree , CommitOptions , DispatchOptions , GetterTree , Module , MutationTree , Store as VuexStore } from 'vuex' ; import { RootState } from '../index' ; import { AppRouteRecordRaw } from '@/common/types/appRoute.type' ; export enum TagsViewMutationContants { ADD_VISITED_VIEW = 'ADD_VISITED_VIEW' , ADD_CACHED_VIEW = 'ADD_CACHED_VIEW' , DEL_VISITED_VIEW = 'DEL_VISITED_VIEW' , DEL_CACHED_VIEW = 'DEL_CACHED_VIEW' , DEL_OTHERS_VISITED_VIEWS = 'DEL_OTHERS_VISITED_VIEWS' , DEL_OTHERS_CACHED_VIEWS = 'DEL_OTHERS_CACHED_VIEWS' , DEL_ALL_VISITED_VIEWS = 'DEL_ALL_VISITED_VIEWS' , DEL_ALL_CACHED_VIEWS = 'DEL_ALL_CACHED_VIEWS' , UPDATE_VISITED_VIEW = 'UPDATE_VISITED_VIEW' } export enum TagsViewActionContants { ADD_VISITED_VIEW = 'tagView/addVisitedView' , ADD_CACHED_VIEW = 'tagView/addCachedView' , DEL_VISITED_VIEW = 'tagView/delVisitedView' , DEL_CACHED_VIEW = 'tagView/delCachedView' , DEL_OTHERS_VISITED_VIEWS = 'tagView/delOthersVisitedViews' , DEL_OTHERS_CACHED_VIEWS = 'tagView/delOthersCachedViews' , DEL_ALL_VISITED_VIEWS = 'tagView/delAllVisitedViews' , DEL_ALL_CACHED_VIEWS = 'tagView/delAllCachedViews' , UPDATE_VISITED_VIEW = 'tagView/updateVisitedView' } export type TagsViewState = { visitedViews : AppRouteRecordRaw [ ] ; cachedViews : string [ ] ; } ; const state = { visitedViews : [ ] , cachedViews : [ ] } ; interface TagsViewMutation { [ TagsViewMutationContants . ADD_VISITED_VIEW ] ( state , view ) : void ; [ TagsViewMutationContants . ADD_CACHED_VIEW ] ( state , view ) : void ; [ TagsViewMutationContants . DEL_VISITED_VIEW ] ( state , view ) : void ; [ TagsViewMutationContants . DEL_CACHED_VIEW ] ( state , view ) : void ; [ TagsViewMutationContants . DEL_OTHERS_VISITED_VIEWS ] ( state , view ) : void ; [ TagsViewMutationContants . DEL_OTHERS_CACHED_VIEWS ] ( state , view ) : void ; [ TagsViewMutationContants . DEL_ALL_VISITED_VIEWS ] ( state ) : void ; [ TagsViewMutationContants . DEL_ALL_CACHED_VIEWS ] ( state ) : void ; [ TagsViewMutationContants . UPDATE_VISITED_VIEW ] ( state , view ) : void ; } const mutations = { [ TagsViewMutationContants . ADD_VISITED_VIEW ] : ( state , view ) => { if ( state . visitedViews . some ( v => v . path === view . path ) ) return ; state . visitedViews . push ( view ) ; } , [ TagsViewMutationContants . ADD_CACHED_VIEW ] : ( state , view ) => { if ( state . cachedViews . includes ( view ) ) return ; state . cachedViews . push ( view ) ; } , [ TagsViewMutationContants . DEL_VISITED_VIEW ] : ( state , view ) => { for ( const [ i , v ] of state . visitedViews . entries ( ) ) { if ( v . path === view . path ) { state . visitedViews . splice ( i , 1 ) ; break ; } } } , [ TagsViewMutationContants . DEL_CACHED_VIEW ] : ( state , view ) => { const views = [ ] ; views . push ( ... view ) ; views . forEach ( v => { const index = state . cachedViews . indexOf ( v ) ; index > - 1 && state . cachedViews . splice ( index , 1 ) ; } ) ; } , [ TagsViewMutationContants . DEL_OTHERS_VISITED_VIEWS ] : ( state , view ) => { state . visitedViews = state . visitedViews . filter ( v => { return v . meta || v . path === view . path ; } ) ; } , [ TagsViewMutationContants . DEL_OTHERS_CACHED_VIEWS ] : ( state , view ) => { const index = state . cachedViews . indexOf ( view ) ; if ( index > - 1 ) { state . cachedViews = state . cachedViews . slice ( index , index + 1 ) ; } else { state . cachedViews = [ ] ; } } , [ TagsViewMutationContants . DEL_ALL_VISITED_VIEWS ] : ( state ) => { state . visitedViews = [ ] ; } , [ TagsViewMutationContants . DEL_ALL_CACHED_VIEWS ] : ( state ) => { state . cachedViews = [ ] ; } , [ TagsViewMutationContants . UPDATE_VISITED_VIEW ] : ( state , view ) => { for ( let v of state . visitedViews ) { if ( v . path === view . path ) { v = Object . assign ( v , view ) ; break ; } } } } ; type AugmentedActionContext = { commit < K extends keyof TagsViewMutation > ( key , payload ? ) : ReturnType < TagsViewMutation [ K ] > ; } & Omit < ActionContext < TagsViewState , RootState > , 'commit' > ; interface TagsAction { addVisitedView ( { commit } , view ) : void ; addCachedView ( { commit } , view ) : void ; delVisitedView ( { commit } , view ) : void ; delCachedView ( { commit } , view ) : void ; delOthersVisitedViews ( { commit } , view ) : void ; delOthersCachedViews ( { commit } , view ) : void ; delAllVisitedViews ( { commit } ) : void ; delAllCachedViews ( { commit } ) : void ; updateVisitedView ( { commit } , view ) : void ; } const actions = { addVisitedView ( { commit } , view ) { commit ( TagsViewMutationContants . ADD_VISITED_VIEW , view ) ; } , addCachedView ( { commit } , view ) { commit ( TagsViewMutationContants . ADD_CACHED_VIEW , view ) ; } , delVisitedView ( { commit } , view ) { commit ( TagsViewMutationContants . DEL_VISITED_VIEW , view ) ; } , delCachedView ( { commit } , view ) { commit ( TagsViewMutationContants . DEL_CACHED_VIEW , view ) ; } , delOthersVisitedViews ( { commit } , view ) { commit ( TagsViewMutationContants . DEL_OTHERS_VISITED_VIEWS , view ) ; } , delOthersCachedViews ( { commit } , view ) { commit ( TagsViewMutationContants . DEL_OTHERS_CACHED_VIEWS , view ) ; } , delAllVisitedViews ( { commit } ) { commit ( TagsViewMutationContants . DEL_ALL_VISITED_VIEWS ) ; } , delAllCachedViews ( { commit } ) { commit ( TagsViewMutationContants . DEL_ALL_CACHED_VIEWS ) ; } , updateVisitedView ( { commit } , view ) { commit ( TagsViewMutationContants . UPDATE_VISITED_VIEW , view ) ; } } ; export type TagsStore < S = TagsViewState > = Omit < VuexStore < S > , 'commit' | 'dispatch' > & { commit < K extends keyof TagsViewMutation , P extends Parameters < TagsViewMutation [ K ] > [ 1 ] > ( key , payload ? , options ? ) : ReturnType < TagsViewMutation [ K ] > ; } & { dispatch < K extends keyof TagsAction , P extends Parameters < TagsAction [ K ] > [ 1 ] > ( key , payload ? , options ? ) : ReturnType < TagsAction [ K ] > ; } ; export const TagsViewModule = { namespaced : true , state , mutations , actions } ;