luna-data-grid
Version:
Grid for displaying datasets
135 lines (134 loc) • 5.42 kB
JavaScript
;
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 theme_1 = __importDefault(require("licia/theme"));
var startWith_1 = __importDefault(require("licia/startWith"));
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, t = _d === void 0 ? 'light' : _d;
var _this = _super.call(this) || this;
_this.subComponents = [];
_this.theme = '';
_this.onThemeChange = function (t) {
if (_this.options.theme === 'auto') {
_this.setTheme(t);
}
};
_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('changeOption', function (name, val) {
if (name === 'theme' && val) {
var t_1 = val;
if (val === 'auto') {
t_1 = theme_1.default.get();
}
_this.setTheme(t_1);
(0, each_1.default)(_this.subComponents, function (component) {
return component.setOption('theme', val);
});
}
});
theme_1.default.on('change', _this.onThemeChange);
_this.setOption('theme', t);
return _this;
}
Component.prototype.destroy = function () {
var _this = this;
this.destroySubComponents();
var $container = this.$container;
var classes = $container.attr('class');
(0, each_1.default)(classes.split(/\s+/), function (c) {
if ((0, startWith_1.default)(c, "luna-".concat(_this.compName))) {
$container.rmClass(c);
}
});
$container.html('');
this.emit('destroy');
this.removeAllListeners();
theme_1.default.off('change', this.onThemeChange);
};
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;
if (val === oldVal) {
return;
}
_this.emit('changeOption', 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));
};
Component.prototype.setTheme = function (theme) {
var _a = this, c = _a.c, $container = _a.$container;
if (this.theme) {
$container.rmClass(c("theme-".concat(this.theme)));
}
$container.addClass(c("theme-".concat(theme)));
this.theme = theme;
};
return Component;
}(Emitter_1.default));
exports.default = Component;