setting.js 2.42 KB
Newer Older
1
import { message } from 'antd';
jim's avatar
jim committed
2
const defaultSetting = {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3
  collapse: false,
jim's avatar
jim committed
4 5 6 7 8 9 10
  silderTheme: 'dark',
  themeColor: '#1890FF',
  layout: 'sidemenu',
  grid: 'Fluid',
  fixedHeader: false,
  autoHideHeader: false,
  fixSiderbar: false,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11
  colorWeak: false,
jim's avatar
jim committed
12
};
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
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
42 43
export default {
  namespace: 'setting',
jim's avatar
jim committed
44
  state: defaultSetting,
jim's avatar
jim committed
45
  reducers: {
jim's avatar
jim committed
46
    getSetting(state) {
jim's avatar
jim committed
47
      const setting = {};
jim's avatar
jim committed
48 49 50 51
      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
52
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
53 54
        }
      });
55 56
      const { themeColor, colorWeak } = setting;
      buildLessAndWeak(themeColor, colorWeak);
jim's avatar
jim committed
57 58 59 60
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
61
    },
jim's avatar
jim committed
62 63
    changeSetting(state, { payload }) {
      const urlParams = new URL(window.location.href);
jim's avatar
jim committed
64 65 66 67 68
      Object.keys(defaultSetting).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
69 70 71
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
72
        }
jim's avatar
jim committed
73 74 75 76
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
jim's avatar
jim committed
77 78 79
        if (defaultSetting[key] !== value) {
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
80
      });
81 82
      const { themeColor, colorWeak } = payload;
      buildLessAndWeak(themeColor, colorWeak);
jim's avatar
jim committed
83 84 85 86 87 88 89 90
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};