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