@cgcs2000/l7plot-component
Version:
Components for L7Plot
237 lines • 7.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CategoryLegend = void 0;
var tslib_1 = require("tslib");
var util_1 = require("@antv/util");
var dom_util_1 = require("@antv/dom-util");
var theme_1 = tslib_1.__importDefault(require("./theme"));
var constants_1 = require("./constants");
var component_1 = require("../../core/component");
var dom_1 = require("../../utils/dom");
var CategoryLegend = /** @class */ (function (_super) {
tslib_1.__extends(CategoryLegend, _super);
function CategoryLegend() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* 获取默认配置
*/
CategoryLegend.prototype.getDefaultOptions = function () {
return (0, util_1.deepMix)({}, _super.prototype.getDefaultOptions.call(this), {
id: 'l7plot-category-legend',
name: 'l7plot-category-legend',
title: '',
items: [],
containerTpl: constants_1.CONTAINER_TPL,
itemTpl: constants_1.ITEM_TPL,
domStyles: theme_1.default,
className: constants_1.CONTAINER_CLASS,
});
};
/**
* 初始化 container
*/
CategoryLegend.prototype.initContainer = function () {
var customContent = this.options.customContent;
if (customContent) {
var container = this.getHtmlContentNode(customContent);
var parentContainer = this.getParentContainer();
if (parentContainer) {
parentContainer.appendChild(container);
}
return container;
}
else {
return _super.prototype.initContainer.call(this);
}
};
/**
* 初始化 DOM
*/
CategoryLegend.prototype.initDom = function () {
this.cacheDoms();
};
/**
* 初始化事件
*/
CategoryLegend.prototype.initEvent = function () {
//
};
/**
* 清理事件
*/
CategoryLegend.prototype.removeEvent = function () {
//
};
/**
* 缓存 DOM
*/
CategoryLegend.prototype.cacheDoms = function () {
var container = this.container;
var titleDom = container.getElementsByClassName(constants_1.TITLE_CLASS)[0];
var listDom = container.getElementsByClassName(constants_1.LIST_CLASS)[0];
this.titleDom = titleDom;
this.listDom = listDom;
};
/**
* 绘制组件
*/
CategoryLegend.prototype.render = function () {
if (this.options.customContent) {
this.renderCustomContent(this.options.customContent);
}
else {
this.resetTitle();
this.renderItems();
}
};
/**
* 显示
*/
CategoryLegend.prototype.show = function () {
var container = this.container;
if (!container || this.destroyed)
return;
(0, dom_util_1.modifyCSS)(container, {
visibility: 'visible',
});
};
/**
* 隐藏
*/
CategoryLegend.prototype.hide = function () {
var container = this.container;
if (!container || this.destroyed)
return;
(0, dom_util_1.modifyCSS)(container, {
visibility: 'hidden',
});
};
/**
* 更新
*/
CategoryLegend.prototype.updateInner = function (options) {
if (this.options.customContent) {
this.renderCustomContent(this.options.customContent);
}
else {
if (options.title) {
this.resetTitle();
}
if (options.items) {
this.renderItems();
}
}
_super.prototype.updateInner.call(this, options);
};
/**
* 根据 customContent 渲染 DOM
*/
CategoryLegend.prototype.renderCustomContent = function (customContent) {
var parentContainer = this.container.parentNode;
var node = this.getHtmlContentNode(customContent);
var curContainer = this.container;
if (parentContainer) {
parentContainer.replaceChild(node, curContainer);
}
this.container = node;
this.applyStyles();
};
/**
* 生成自定义内容 DOM
*/
CategoryLegend.prototype.getHtmlContentNode = function (customContent) {
var node;
var element = customContent(this.options.title || '', this.options.items);
if ((0, util_1.isString)(element)) {
node = this.createDom(element);
}
else {
node = element;
}
return node;
};
/**
* 重置 title
*/
CategoryLegend.prototype.resetTitle = function () {
var title = this.options.title;
if (title) {
this.showTitle();
this.setTitle(title);
}
else {
this.hideTitle();
}
};
/**
* 显示 title
*/
CategoryLegend.prototype.showTitle = function () {
var titleDom = this.titleDom;
if (titleDom) {
(0, dom_util_1.modifyCSS)(titleDom, {
display: 'block',
});
}
};
/**
* 隐藏 title
*/
CategoryLegend.prototype.hideTitle = function () {
var titleDom = this.titleDom;
if (titleDom) {
(0, dom_util_1.modifyCSS)(titleDom, {
display: 'none',
});
}
};
/**
* 设置 title 内容
*/
CategoryLegend.prototype.setTitle = function (content) {
var titleDom = this.titleDom;
if (titleDom) {
titleDom.innerHTML = content;
}
};
/**
* 渲染每项 item
*/
CategoryLegend.prototype.renderItems = function () {
var _this = this;
this.clearItemDoms();
var items = this.options.items.length > 30 ? this.options.items.slice(0, 30) : this.options.items;
var itemTpl = this.options.itemTpl || constants_1.ITEM_TPL;
var listDom = this.listDom;
if (listDom) {
items.forEach(function (item) {
var isEmpty = item.value === '';
var value = isEmpty ? '—' : Array.isArray(item.value) ? item.value.join(' - ') : item.value;
var substituteObj = tslib_1.__assign(tslib_1.__assign({}, item), { value: value });
var domStr = (0, util_1.substitute)(itemTpl, substituteObj);
var itemDom = _this.createDom(domStr);
listDom.appendChild(itemDom);
});
this.applyChildrenStyles(listDom, this.options.domStyles);
}
};
/**
* 清空 list DOM 下的 DOM 元素
*/
CategoryLegend.prototype.clearItemDoms = function () {
if (this.listDom) {
(0, dom_1.clearDom)(this.listDom);
}
};
/**
* 清空所有
*/
CategoryLegend.prototype.clear = function () {
this.setTitle('');
this.clearItemDoms();
};
return CategoryLegend;
}(component_1.Component));
exports.CategoryLegend = CategoryLegend;
//# sourceMappingURL=index.js.map