UNPKG

@qn-pandora/pandora-visualization

Version:

Pandora 通用可视化库

193 lines (192 loc) 7.83 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var _a; import { observable, computed } from 'mobx'; import { get, isUndefined } from 'lodash'; import * as echarts from 'echarts'; import Store from '../../stores/store'; import bind from '../../utils/bind'; import { EThemeTypes, EPandoraThemeTypes, initialTheme } from '../../constants'; import LightTheme from '../../global/themes/pandora-light'; import DarkTheme from '../../global/themes/pandora-dark'; import EleganceGreenTheme from '../../global/themes/pandora-eleganceGreen'; import ThemeForJSMap from '../../global/themes/pandora-theme-forjs'; var ThemeFile = (_a = {}, _a[EThemeTypes.Light] = LightTheme, _a[EThemeTypes.Dark] = DarkTheme, _a[EThemeTypes.EleganceGreen] = EleganceGreenTheme, _a); var ThemeService = /** @class */ (function (_super) { __extends(ThemeService, _super); function ThemeService(theme, uid) { if (theme === void 0) { theme = initialTheme; } var _this = _super.call(this) || this; _this.theme = initialTheme; _this.themeOption = ThemeFile[initialTheme]; _this.hasInitedTheme = false; _this.initTheme(); // 初始化service的时候要切换主题,修改css变量 _this.uid = uid; _this.setTheme(theme); _this.addDisposer(_this.removeCssStyle); return _this; } Object.defineProperty(ThemeService.prototype, "themeForJS", { get: function () { return ThemeForJSMap[this.theme]; }, enumerable: false, configurable: true }); ThemeService.prototype.registerTheme = function (themeType) { this.theme = themeType; this.registerThemeOption(themeType); }; ThemeService.prototype.registerThemeOption = function (themeType) { this.themeOption = ThemeFile[themeType]; }; ThemeService.prototype.setTheme = function (themeType) { if (themeType === void 0) { themeType = initialTheme; } this.registerTheme(themeType); if (this.uid) { this.initCssVariable(); } }; ThemeService.prototype.initCssVariable = function () { var e_1, _a; var id = "themeStyle" + this.uid; var variables = get(this.themeOption, 'cssVariable'); var style = document.getElementById(id) || document.createElement('style'); var styleArr = []; try { for (var _b = __values(Object.entries(variables)), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), key = _d[0], value = _d[1]; if (!isUndefined(value)) { styleArr.push(key + ':' + value + ';'); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } style.innerHTML = ".chart-container-" + this.theme + " {" + styleArr.join('') + "}"; this.cssVariableStyle = style; if (!style.id) { style.id = id; document.head.append(style); } }; ThemeService.prototype.removeCssStyle = function () { var styleDom = document.getElementById("themeStyle" + this.uid); if (styleDom) { document.head.removeChild(styleDom); } }; ThemeService.prototype.initTheme = function () { echarts.registerTheme(EPandoraThemeTypes.Light, LightTheme); echarts.registerTheme(EPandoraThemeTypes.Dark, DarkTheme); echarts.registerTheme(EPandoraThemeTypes.EleganceGreen, EleganceGreenTheme); }; __decorate([ observable, __metadata("design:type", String) ], ThemeService.prototype, "theme", void 0); __decorate([ observable.ref, __metadata("design:type", Object) ], ThemeService.prototype, "themeOption", void 0); __decorate([ computed, __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ThemeService.prototype, "themeForJS", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], ThemeService.prototype, "registerTheme", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], ThemeService.prototype, "registerThemeOption", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], ThemeService.prototype, "setTheme", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ThemeService.prototype, "initCssVariable", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ThemeService.prototype, "removeCssStyle", null); __decorate([ bind, __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ThemeService.prototype, "initTheme", null); return ThemeService; }(Store)); export default ThemeService;