@difizen/mana-core
Version:
181 lines • 10.2 kB
JavaScript
var _BuiltinThemeProvider, _dec, _dec2, _class, _class2, _descriptor;
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import { isPromiseLike } from '@difizen/mana-common';
import { Emitter, Disposable, objects } from '@difizen/mana-common';
import { prop } from '@difizen/mana-observable';
import { singleton } from '@difizen/mana-syringe';
import { localStorageService } from "../common";
import "./style/theme-base.less";
export var ThemeServiceSymbol = Symbol('ThemeService');
export var BuiltinThemeProvider = /*#__PURE__*/_createClass(function BuiltinThemeProvider() {
_classCallCheck(this, BuiltinThemeProvider);
});
_BuiltinThemeProvider = BuiltinThemeProvider;
BuiltinThemeProvider.darkTheme = {
id: 'dark',
type: 'dark',
label: 'Dark (mana)'
};
BuiltinThemeProvider.lightTheme = {
id: 'light',
type: 'light',
label: 'Light (mana)'
};
BuiltinThemeProvider.hcTheme = {
id: 'hc-mana',
type: 'hc',
label: 'High Contrast (mana)'
};
BuiltinThemeProvider.themes = [_BuiltinThemeProvider.darkTheme, _BuiltinThemeProvider.lightTheme, _BuiltinThemeProvider.hcTheme];
export var ThemeService = (_dec = singleton(), _dec2 = prop(), _dec(_class = (_class2 = /*#__PURE__*/function () {
function ThemeService() {
_classCallCheck(this, ThemeService);
this.themes = {};
_initializerDefineProperty(this, "activeTheme", _descriptor, this);
this.storageService = localStorageService;
this.themeChange = new Emitter();
this.onDidColorThemeChange = this.themeChange.event;
if ((typeof window === "undefined" ? "undefined" : _typeof(window)) !== undefined) {
window.addEventListener('mana-theme-change', function (event) {
// this.setCurrentTheme(event.);
});
}
}
_createClass(ThemeService, [{
key: "themeClassName",
get: function get() {
return "mana-".concat(this.getCurrentTheme().type);
}
}, {
key: "startupTheme",
value: function startupTheme() {
this.activeTheme = this.getCurrentTheme();
}
}, {
key: "register",
value: function register() {
var _this = this;
for (var _len = arguments.length, themes = new Array(_len), _key = 0; _key < _len; _key++) {
themes[_key] = arguments[_key];
}
for (var _i = 0, _themes = themes; _i < _themes.length; _i++) {
var theme = _themes[_i];
this.themes[theme.id] = theme;
}
this.validateActiveTheme();
return Disposable.create(function () {
for (var _i2 = 0, _themes2 = themes; _i2 < _themes2.length; _i2++) {
var _theme = _themes2[_i2];
delete _this.themes[_theme.id];
}
});
}
}, {
key: "validateActiveTheme",
value: function validateActiveTheme() {
if (!this.activeTheme) {
return;
}
var themeId = this.getCurrentTheme().id;
if (themeId !== this.activeTheme.id) {
this.setCurrentTheme(themeId);
}
}
}, {
key: "getThemes",
value: function getThemes() {
var result = [];
for (var o in this.themes) {
if (Object.prototype.hasOwnProperty.call(this.themes, o)) {
result.push(this.themes[o]);
}
}
return result;
}
}, {
key: "getTheme",
value: function getTheme(themeId) {
return this.themes[themeId] || this.defaultTheme;
}
}, {
key: "setCurrentTheme",
value: function setCurrentTheme(themeId) {
var tokens = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var newTheme = this.getTheme(themeId);
var oldTheme = this.activeTheme;
if (oldTheme) {
if (oldTheme.id === newTheme.id && !tokens) {
return;
}
}
this.activeTheme = objects.mixin(objects.deepClone(newTheme), {
extraTokens: _objectSpread({}, tokens)
});
this.storageService.setData('theme', themeId);
this.themeChange.fire({
newTheme: newTheme,
oldTheme: oldTheme
});
}
}, {
key: "getCurrentTheme",
value: function getCurrentTheme() {
var maybeThemeId = this.storageService.getData('theme');
var themeId = this.defaultTheme.id;
if (!isPromiseLike(maybeThemeId) && maybeThemeId) {
themeId = maybeThemeId;
}
return this.getTheme(themeId);
}
}, {
key: "getActiveTheme",
value: function getActiveTheme() {
return this.activeTheme || this.getCurrentTheme();
}
}, {
key: "defaultTheme",
get: function get() {
return BuiltinThemeProvider.lightTheme;
}
}, {
key: "reset",
value: function reset() {
this.setCurrentTheme(this.defaultTheme.id);
}
}], [{
key: "get",
value: function get() {
var global = window; // eslint-disable-line @typescript-eslint/no-explicit-any
if (!global[ThemeServiceSymbol]) {
var themeService = new ThemeService();
themeService.register.apply(themeService, _toConsumableArray(BuiltinThemeProvider.themes));
themeService.startupTheme();
global[ThemeServiceSymbol] = themeService;
}
return global[ThemeServiceSymbol];
}
}]);
return ThemeService;
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "activeTheme", [_dec2], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
})), _class2)) || _class);