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

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

afc163's avatar
afc163 committed
63 64 65 66
const updateColorWeak = colorWeak => {
  document.body.className = colorWeak ? 'colorWeak' : '';
};

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