rt-sim-training-client/src/store/modules/trainingList.js

67 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-07-26 13:32:43 +08:00
const trainingList = {
namespaced: true,
state: {
2019-11-14 16:45:31 +08:00
index: 0, // 当前选择实训索引
trainingList: [] // 实训初始列表
2019-07-26 13:32:43 +08:00
},
getters: {
trainingList: (state) => {
return state.trainingList;
},
prevTraining: (state) => () => {
if (state.trainingList && state.trainingList.length > 0) {
state.index = state.index - 1;
if (state.index >= 0) {
return state.trainingList[state.index];
}
}
},
nextTraining: (state) => () => {
if (state.trainingList && state.trainingList.length > 0) {
state.index = state.index + 1;
if (state.index < state.trainingList.length) {
return state.trainingList[state.index];
}
}
}
},
mutations: {
setIndex: (state, index) => {
state.index = index;
},
setTrainingList: (state, trainingList) => {
if (trainingList && trainingList.length) {
state.trainingList = trainingList;
} else {
state.trainingList = [];
}
2019-11-14 16:45:31 +08:00
}
2019-07-26 13:32:43 +08:00
},
actions: {
setIndex({ commit }, index) {
commit('setIndex', index);
},
setIndexByTrainingObj({ commit, state }, trainingId) {
let index = 0;
if (state.trainingList && state.trainingList.length) {
state.trainingList.forEach((elem, i) => {
if (elem.id === trainingId) {
index = i;
}
});
}
commit('setIndex', index);
},
setTrainingList({ commit }, trainingList) {
commit('setTrainingList', trainingList);
},
clearTrainingList({ commit }) {
commit('setTrainingList', []);
commit('setIndex', 0);
}
}
};
2019-11-14 16:45:31 +08:00
export default trainingList;