setting.js 3.2 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
let lessNodesAppended;
afc163's avatar
afc163 committed
5
const updateTheme = primaryColor => {
afc163's avatar
afc163 committed
6 7 8 9
  // Don't compile less in production!
  if (process.env.NODE_ENV === 'production') {
    return;
  }
10
  // Determine if the component is remounted
afc163's avatar
afc163 committed
11
  if (!primaryColor) {
afc163's avatar
afc163 committed
12
    return;
13
  }
afc163's avatar
afc163 committed
14 15 16 17 18 19 20 21 22 23
  const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0);
  if (!lessNodesAppended) {
    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,
afc163's avatar
afc163 committed
24
        env: 'production'
afc163's avatar
afc163 committed
25 26
      };
    `;
afc163's avatar
afc163 committed
27
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/2.7.3/less.min.js';
afc163's avatar
afc163 committed
28 29 30 31 32
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
33 34 35
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
36
    lessNodesAppended = true;
37
  } else {
afc163's avatar
afc163 committed
38 39 40 41 42 43
    buildIt();
  }
  function buildIt() {
    if (!window.less) {
      return;
    }
afc163's avatar
afc163 committed
44 45 46
    setTimeout(() => {
      window.less
        .modifyVars({
afc163's avatar
afc163 committed
47
          '@primary-color': primaryColor,
afc163's avatar
afc163 committed
48 49 50 51 52 53 54 55 56
        })
        .then(() => {
          hideMessage();
        })
        .catch(() => {
          message.error('Failed to update theme');
          hideMessage();
        });
    }, 200);
57 58 59
  }
};

afc163's avatar
afc163 committed
60 61 62 63
const updateColorWeak = colorWeak => {
  document.body.className = colorWeak ? 'colorWeak' : '';
};

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