setting.js 1.39 KB
Newer Older
jim's avatar
jim committed
1 2 3 4 5 6 7 8 9 10 11
const defaultSetting = {
  collapse: false,
  silderTheme: 'dark',
  themeColor: '#1890FF',
  layout: 'sidemenu',
  grid: 'Fluid',
  fixedHeader: false,
  autoHideHeader: false,
  fixSiderbar: false,
  colorWeak: 'close',
};
12

jim's avatar
jim committed
13 14
export default {
  namespace: 'setting',
jim's avatar
jim committed
15
  state: defaultSetting,
jim's avatar
jim committed
16
  reducers: {
jim's avatar
jim committed
17
    getSetting(state) {
jim's avatar
jim committed
18
      const setting = {};
jim's avatar
jim committed
19 20 21 22
      const urlParams = new URL(window.location.href);
      Object.keys(state).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          const value = urlParams.searchParams.get(key);
jim's avatar
jim committed
23
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
24 25
        }
      });
jim's avatar
jim committed
26 27 28 29
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
30
    },
jim's avatar
jim committed
31 32
    changeSetting(state, { payload }) {
      const urlParams = new URL(window.location.href);
jim's avatar
jim committed
33 34 35 36 37
      Object.keys(defaultSetting).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
38 39 40
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
41
        }
jim's avatar
jim committed
42 43 44 45
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
jim's avatar
jim committed
46 47 48
        if (defaultSetting[key] !== value) {
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
49 50 51 52 53 54 55 56 57
      });
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};