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