@alicloud/cloud-charts
Version:

123 lines (115 loc) • 3.8 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.convertThemeKey = convertThemeKey;
exports["default"] = void 0;
exports.getTheme = getTheme;
exports.setTheme = setTheme;
var _eventBus = _interopRequireDefault(require("../common/eventBus"));
var _constants = require("../constants");
var _log = require("../common/log");
var _themeTools = require("./themeTools");
var _normal = _interopRequireDefault(require("./normal.style"));
var _dark = _interopRequireDefault(require("./dark.style"));
// [theme].style 文件根据 [theme].scss 自动生成,请勿直接修改
// 记录所有主题
var themeMap = {
normal: {
js: (0, _themeTools.convertKey)((0, _themeTools.convertJsStyle)('normal', _normal["default"])),
css: (0, _themeTools.convertCSS)(_normal["default"]),
rawCSS: _normal["default"]
},
dark: {
js: (0, _themeTools.convertKey)((0, _themeTools.convertJsStyle)('dark', _dark["default"])),
css: (0, _themeTools.convertCSS)(_dark["default"]),
rawCSS: _dark["default"]
}
};
// alias index as normal
themeMap.index = themeMap.normal;
var normalMap = {
index: true,
normal: true
};
// 默认主题包
var defaultTheme = process.env.NODE_ENV === 'production' ? _constants.THEME : 'normal';
/** 当前图表库主题包 */
var themes = {
getTheme: getTheme,
setTheme: setTheme
};
var currentThemeName = '';
var currentRawCss = {};
function getTheme(name) {
if (!name) {
return themes;
} else if (themeMap[name]) {
return themeMap[name].js;
}
return undefined;
}
function convertThemeKey(theme) {
if (theme === void 0) {
theme = defaultTheme;
}
var newTheme = {};
if (typeof theme === 'string' && themeMap[theme]) {
newTheme = themeMap[theme].js;
(0, _themeTools.setThemeStyle)(themeMap[theme].css);
} else if (typeof theme === 'object') {
newTheme = (0, _themeTools.convertKey)(theme);
var newCSS = Object.assign({}, currentRawCss, theme);
(0, _themeTools.setThemeStyle)((0, _themeTools.convertCSS)(newCSS));
}
return Object.assign(themes, newTheme);
}
function setTheme(theme, refreshChart) {
if (theme === void 0) {
theme = defaultTheme;
}
if (refreshChart === void 0) {
refreshChart = true;
}
if (typeof theme === 'string' && themeMap[theme] && (theme === currentThemeName || theme in normalMap && currentThemeName in normalMap)) {
return;
}
var newTheme = convertThemeKey(theme);
if (typeof theme === 'string' && themeMap[theme]) {
// 传入字符串名字,设置对应主题包
// newTheme = convertThemeKey(theme);
currentThemeName = theme;
// setThemeStyle(themeMap[theme].css);
currentRawCss = themeMap[theme].rawCSS;
// 打点
(0, _log.themeLog)(theme);
} else if (typeof theme === 'object') {
// 传入对象,直接覆盖对应的key和css
// newTheme = convertThemeKey(theme);
// // 多次传入对象,css 会在 currentRawCss 的基础上处理
// const newCSS = Object.assign({}, currentRawCss, theme);
// setThemeStyle(convertCSS(newCSS));
// 打点
(0, _log.themeLog)(newTheme.name || 'customTheme');
} else {
return;
}
// Object.assign(themes, newTheme)
(0, _themeTools.setG2Theme)(newTheme);
if (refreshChart) {
// TODO 优化重新渲染逻辑
_eventBus["default"].emit('setTheme');
}
}
setTheme(defaultTheme, false);
if (window[_constants.FullThemeName]) {
setTheme(window[_constants.FullThemeName], false);
}
// 根据事件设置图表主题
document.addEventListener(_constants.FullThemeEventName, function (e) {
if (e.detail) {
setTheme(e.detail);
}
});
// themes.getTheme = getTheme;
// themes.setTheme = setTheme;
var _default = exports["default"] = themes;