index.js 1.71 KB
import {
    SET_ENV,
    SET_TITLE,
    ROUTE_CHANGE,
    INIT_ROUTE_CHANGE,
    PAGE_INIT_VISIBLE
} from './types';
import _ from 'lodash/core';

export function createYoho() {
    return {
        state: {
            title: '',
            env: {
                isApp: true,
                isiOS: false,
                isAndroid: false,
                isYohoBuy: false,
                channel: 'men',
                fs: true
            },
            visible: true,
            history: [],
            direction: 'forword'
        },
        mutations: {
            [SET_ENV](state, params) {
                Object.assign(state.env, params);
            },
            [SET_TITLE](state, params) {
                state.title = params.title;
            },
            [INIT_ROUTE_CHANGE](state, {routes}) {
                state.history = routes;
            },
            [ROUTE_CHANGE](state, {name, fullPath}) {
                let routeIndex;

                state.env.fs = false;
                _.each(state.history, (h, index) => {
                    if (h.name === name) {
                        routeIndex = index;
                        return false;
                    }
                });

                if (routeIndex >= 0) {
                    state.history = state.history.slice(0, routeIndex + 1);
                    state.direction = 'back';
                } else {
                    state.history.push({
                        name,
                        fullPath
                    });
                    state.direction = 'forword';
                }
            },
            [PAGE_INIT_VISIBLE](state, {visible}) {
                state.visible = visible;
            }
        }
    };
}