search.js 2.21 KB

const Types = {
    Home: { size: 10 },
    RegIssue: { size: 10 },
    InfoDisclosure: { size: 10 },
    SelfRegulating: { size: 10 }
}

import utils from '@/utils/index'
export default {
    namespaced: true,
    state: {
        Home: [],
        RegIssue: [],
        InfoDisclosure: [],
        SelfRegulating: []
    },
    mutations: {
        MSetHistoryDatas (state, { key, datas }) {
            state[key] = datas
        },

        MSetHomeHistory (state, datas) {
            state.Home = datas
        },

        MSetRegIssueHistory (state, datas) {
            state.RegIssue = datas
        },

        MSetInfoDisclosureHistory (state, datas) {
            state.InfoDisclosure = datas
        },

        MCleanHistory (state, stateKey) {
            state[stateKey] = []
        }
    },
    actions: {
        ACleanHistory ({ commit }, stateKey) {
            commit('MCleanHistory', stateKey)
        },

        APushSearchValue ({ state, commit }, param = {}) {
            const { stateKey, value } = param
            if (Object.hasOwnProperty.call(Types, stateKey)) {
                const newDatas = pushSearchValue(state[stateKey], value, Types[stateKey].size)
                commit('MSetHistoryDatas', { key: stateKey, datas: newDatas })
            }
        },

        APushHomeSearchValue ({ state, commit }, val) {
            const newDatas = pushSearchValue(state.Home, val, Types.Home.size)
            commit('MSetHomeHistory', newDatas)
        },

        APushRegIssueSearchValue ({ state, commit }, val) {
            const newDatas = pushSearchValue(state.RegIssue, val, Types.RegIssue.size)
            commit('MSetRegIssueHistory', newDatas)
        },

        APushInfoDisclosureSearchValue ({ state, commit }, val) {
            const newDatas = pushSearchValue(state.InfoDisclosure, val, Types.InfoDisclosure.size)
            commit('MSetInfoDisclosureHistory', newDatas)
        },
    },
    modules: {
    }
}


function pushSearchValue (datas, value, size) {
    const newDatas = [...datas]

    if (!utils.isNull(value) && newDatas.indexOf(value) === -1) {
        newDatas.unshift(value.trim())
        if (newDatas.length > size) {
            newDatas.pop()
        }
    }
    return newDatas
}