setting.js 3.4 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
  // // Don't compile less in production!
  if (APP_TYPE !== 'site') {
afc163's avatar
afc163 committed
8 9
    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
  const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0);
ζ„šι“'s avatar
ζ„šι“ committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  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
33
  if (!lessNodesAppended) {
34
    // insert less.js and color.less
afc163's avatar
afc163 committed
35 36 37 38 39 40 41 42
    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,
43 44
        env: 'production',
        javascriptEnabled: true
afc163's avatar
afc163 committed
45 46
      };
    `;
47
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
afc163's avatar
afc163 committed
48 49 50 51 52
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
53 54 55
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
56
    lessNodesAppended = true;
57
  } else {
afc163's avatar
afc163 committed
58 59
    buildIt();
  }
60 61
};

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

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