UNPKG

luna-dom-highlighter

Version:

Highlighter for html elements

110 lines (109 loc) 4.66 kB
"use strict"; 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) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var Emitter_1 = __importDefault(require("licia/Emitter")); var _1 = __importDefault(require("licia/$")); var util_1 = require("./util"); var each_1 = __importDefault(require("licia/each")); var extend_1 = __importDefault(require("licia/extend")); var defaults_1 = __importDefault(require("licia/defaults")); var remove_1 = __importDefault(require("licia/remove")); var Component = (function (_super) { __extends(Component, _super); function Component(container, _a, _b) { var compName = _a.compName; var _c = _b === void 0 ? {} : _b, _d = _c.theme, theme = _d === void 0 ? 'light' : _d; var _this = _super.call(this) || this; _this.subComponents = []; _this.compName = compName; _this.c = (0, util_1.classPrefix)(compName); _this.options = {}; _this.container = container; _this.$container = (0, _1.default)(container); _this.$container.addClass([ "luna-".concat(compName), _this.c("platform-".concat((0, util_1.getPlatform)())), ]); _this.on('optionChange', function (name, val, oldVal) { var c = _this.c; if (name === 'theme') { _this.$container .rmClass(c("theme-".concat(oldVal))) .addClass(c("theme-".concat(val))); (0, each_1.default)(_this.subComponents, function (component) { return component.setOption('theme', val); }); } }); _this.setOption('theme', theme); return _this; } Component.prototype.destroy = function () { this.destroySubComponents(); var c = this.c; this.$container .rmClass("luna-".concat(this.compName)) .rmClass(c("platform-".concat((0, util_1.getPlatform)()))) .rmClass(c("theme-".concat(this.options.theme))); this.$container.html(''); this.emit('destroy'); this.removeAllListeners(); }; Component.prototype.setOption = function (name, val) { var _this = this; var options = this.options; var newOptions = {}; if (typeof name === 'string') { newOptions[name] = val; } else { newOptions = name; } (0, each_1.default)(newOptions, function (val, name) { var oldVal = options[name]; options[name] = val; _this.emit('optionChange', name, val, oldVal); }); }; Component.prototype.getOption = function (name) { return this.options[name]; }; Component.prototype.addSubComponent = function (component) { component.setOption('theme', this.options.theme); this.subComponents.push(component); }; Component.prototype.removeSubComponent = function (component) { (0, remove_1.default)(this.subComponents, function (com) { return com === component; }); }; Component.prototype.destroySubComponents = function () { (0, each_1.default)(this.subComponents, function (component) { return component.destroy(); }); this.subComponents = []; }; Component.prototype.initOptions = function (options, defs) { if (defs === void 0) { defs = {}; } (0, defaults_1.default)(options, defs); (0, extend_1.default)(this.options, options); }; Component.prototype.find = function (selector) { return this.$container.find(this.c(selector)); }; return Component; }(Emitter_1.default)); exports.default = Component;