setting.js 2.42 KB
Newer Older
1
import { message } from 'antd';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2

jim's avatar
jim committed
3
const defaultSetting = {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
4
  collapse: false,
jim's avatar
jim committed
5 6 7 8 9 10 11
  silderTheme: 'dark',
  themeColor: '#1890FF',
  layout: 'sidemenu',
  grid: 'Fluid',
  fixedHeader: false,
  autoHideHeader: false,
  fixSiderbar: false,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12
  colorWeak: false,
jim's avatar
jim committed
13
};
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
const buildLessAndWeak = (themeColor, colorWeak) => {
  // Determine if the component is remounted
  if (themeColor && themeColor !== '#1890FF' && themeColor !== window['antd_pro_less_color']) {
    window.less.refresh().then(() => {
      const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0);
      setTimeout(() => {
        window.less
          .modifyVars({
            '@primary-color': themeColor,
            '@input-hover-border-color': themeColor,
          })
          .then(() => {
            window['antd_pro_less_color'] = themeColor;
            hideMessage();
          })
          .catch(() => {
            message.error(`Failed to update theme`);
          });
      }, 200);
    });
  }
  if (colorWeak) {
    document.body.className = 'colorWeak';
  } else {
    document.body.className = '';
  }
};

jim's avatar
jim committed
43 44
export default {
  namespace: 'setting',
jim's avatar
jim committed
45
  state: defaultSetting,
jim's avatar
jim committed
46
  reducers: {
jim's avatar
jim committed
47
    getSetting(state) {
jim's avatar
jim committed
48
      const setting = {};
jim's avatar
jim committed
49 50 51 52
      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
53
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
54 55
        }
      });
56 57
      const { themeColor, colorWeak } = setting;
      buildLessAndWeak(themeColor, colorWeak);
jim's avatar
jim committed
58 59 60 61
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
62
    },
jim's avatar
jim committed
63 64
    changeSetting(state, { payload }) {
      const urlParams = new URL(window.location.href);
jim's avatar
jim committed
65 66 67 68 69
      Object.keys(defaultSetting).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
70 71 72
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
73
        }
jim's avatar
jim committed
74 75 76 77
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
jim's avatar
jim committed
78 79 80
        if (defaultSetting[key] !== value) {
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
81
      });
82 83
      const { themeColor, colorWeak } = payload;
      buildLessAndWeak(themeColor, colorWeak);
jim's avatar
jim committed
84 85 86 87 88 89 90 91
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};