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

afc163's avatar
afc163 committed
4 5 6
let lessNodesAppended;
const buildSettings = (themeColor, colorWeak) => {
  document.body.className = colorWeak ? 'colorWeak' : '';
7
  // Determine if the component is remounted
afc163's avatar
afc163 committed
8 9
  if (!themeColor || themeColor === '#1890FF' || themeColor === window['antd_pro_less_color']) {
    return;
10
  }
afc163's avatar
afc163 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0);
  if (!lessNodesAppended) {
    console.log('append less nodes');
    const lessStyleNode = document.createElement('link');
    const lessConfigNode = document.createElement('script');
    const lessScriptNode = document.createElement('script');
    lessStyleNode.setAttribute('rel', 'stylesheet/less');
    lessStyleNode.setAttribute('href', '/color.less');
    lessConfigNode.innerHTML = `
      window.less = {
        async: true,
        env: 'production',
        javascriptEnabled: true
      };
    `;
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
    document.head.appendChild(lessStyleNode);
    document.head.appendChild(lessConfigNode);
    document.head.appendChild(lessScriptNode);
    lessNodesAppended = true;
36
  } else {
afc163's avatar
afc163 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    buildIt();
  }
  function buildIt() {
    if (!window.less) {
      return;
    }
    window.less
      .modifyVars({
        '@primary-color': themeColor,
        '@input-hover-border-color': themeColor,
      })
      .then(() => {
        console.log('start to compile');
        window['antd_pro_less_color'] = themeColor;
        hideMessage();
      })
      .catch(() => {
        message.error(`Failed to update theme`);
        hideMessage();
      });
57 58 59
  }
};

jim's avatar
jim committed
60 61
export default {
  namespace: 'setting',
jim's avatar
jim committed
62
  state: defaultSetting,
jim's avatar
jim committed
63
  reducers: {
jim's avatar
jim committed
64
    getSetting(state) {
jim's avatar
jim committed
65
      const setting = {};
jim's avatar
jim committed
66 67 68 69
      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
70
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
71 72
        }
      });
73
      const { themeColor, colorWeak } = setting;
afc163's avatar
afc163 committed
74
      buildSettings(themeColor, colorWeak);
jim's avatar
jim committed
75 76 77 78
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
79
    },
jim's avatar
jim committed
80 81
    changeSetting(state, { payload }) {
      const urlParams = new URL(window.location.href);
jim's avatar
jim committed
82 83 84 85 86
      Object.keys(defaultSetting).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
87 88 89
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
90
        }
jim's avatar
jim committed
91 92 93 94
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
jim's avatar
jim committed
95 96 97
        if (defaultSetting[key] !== value) {
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
98
      });
99
      const { themeColor, colorWeak } = payload;
afc163's avatar
afc163 committed
100
      buildSettings(themeColor, colorWeak);
jim's avatar
jim committed
101 102 103 104 105 106 107 108
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};