cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
238 lines (237 loc) • 7.23 kB
JavaScript
import { isArray, isFunction, isString } from "../../../zrender/lib/core/util.mjs";
import linkSeriesData from "./helper/linkSeriesData.mjs";
import SeriesData from "./SeriesData.mjs";
import prepareSeriesDataSchema from "./helper/createDimensions.mjs";
import { convertOptionIdName } from "../util/model.mjs";
var TreeNode = function() {
function TreeNode2(name, hostTree) {
this.depth = 0;
this.height = 0;
this.dataIndex = -1;
this.children = [];
this.viewChildren = [];
this.isExpand = false;
this.name = name || "";
this.hostTree = hostTree;
}
TreeNode2.prototype.isRemoved = function() {
return this.dataIndex < 0;
};
TreeNode2.prototype.eachNode = function(options, cb, context) {
if (isFunction(options)) {
context = cb;
cb = options;
options = null;
}
options = options || {};
if (isString(options)) {
options = {
order: options
};
}
var order = options.order || "preorder";
var children = this[options.attr || "children"];
var suppressVisitSub;
order === "preorder" && (suppressVisitSub = cb.call(context, this));
for (var i = 0; !suppressVisitSub && i < children.length; i++) {
children[i].eachNode(options, cb, context);
}
order === "postorder" && cb.call(context, this);
};
TreeNode2.prototype.updateDepthAndHeight = function(depth) {
var height = 0;
this.depth = depth;
for (var i = 0; i < this.children.length; i++) {
var child = this.children[i];
child.updateDepthAndHeight(depth + 1);
if (child.height > height) {
height = child.height;
}
}
this.height = height + 1;
};
TreeNode2.prototype.getNodeById = function(id) {
if (this.getId() === id) {
return this;
}
for (var i = 0, children = this.children, len = children.length; i < len; i++) {
var res = children[i].getNodeById(id);
if (res) {
return res;
}
}
};
TreeNode2.prototype.contains = function(node) {
if (node === this) {
return true;
}
for (var i = 0, children = this.children, len = children.length; i < len; i++) {
var res = children[i].contains(node);
if (res) {
return res;
}
}
};
TreeNode2.prototype.getAncestors = function(includeSelf) {
var ancestors = [];
var node = includeSelf ? this : this.parentNode;
while (node) {
ancestors.push(node);
node = node.parentNode;
}
ancestors.reverse();
return ancestors;
};
TreeNode2.prototype.getAncestorsIndices = function() {
var indices = [];
var currNode = this;
while (currNode) {
indices.push(currNode.dataIndex);
currNode = currNode.parentNode;
}
indices.reverse();
return indices;
};
TreeNode2.prototype.getDescendantIndices = function() {
var indices = [];
this.eachNode(function(childNode) {
indices.push(childNode.dataIndex);
});
return indices;
};
TreeNode2.prototype.getValue = function(dimension) {
var data = this.hostTree.data;
return data.getStore().get(data.getDimensionIndex(dimension || "value"), this.dataIndex);
};
TreeNode2.prototype.setLayout = function(layout, merge) {
this.dataIndex >= 0 && this.hostTree.data.setItemLayout(this.dataIndex, layout, merge);
};
TreeNode2.prototype.getLayout = function() {
return this.hostTree.data.getItemLayout(this.dataIndex);
};
TreeNode2.prototype.getModel = function(path) {
if (this.dataIndex < 0) {
return;
}
var hostTree = this.hostTree;
var itemModel = hostTree.data.getItemModel(this.dataIndex);
return itemModel.getModel(path);
};
TreeNode2.prototype.getLevelModel = function() {
return (this.hostTree.levelModels || [])[this.depth];
};
TreeNode2.prototype.setVisual = function(key, value) {
this.dataIndex >= 0 && this.hostTree.data.setItemVisual(this.dataIndex, key, value);
};
TreeNode2.prototype.getVisual = function(key) {
return this.hostTree.data.getItemVisual(this.dataIndex, key);
};
TreeNode2.prototype.getRawIndex = function() {
return this.hostTree.data.getRawIndex(this.dataIndex);
};
TreeNode2.prototype.getId = function() {
return this.hostTree.data.getId(this.dataIndex);
};
TreeNode2.prototype.getChildIndex = function() {
if (this.parentNode) {
var children = this.parentNode.children;
for (var i = 0; i < children.length; ++i) {
if (children[i] === this) {
return i;
}
}
return -1;
}
return -1;
};
TreeNode2.prototype.isAncestorOf = function(node) {
var parent = node.parentNode;
while (parent) {
if (parent === this) {
return true;
}
parent = parent.parentNode;
}
return false;
};
TreeNode2.prototype.isDescendantOf = function(node) {
return node !== this && node.isAncestorOf(this);
};
return TreeNode2;
}();
var Tree = function() {
function Tree2(hostModel) {
this.type = "tree";
this._nodes = [];
this.hostModel = hostModel;
}
Tree2.prototype.eachNode = function(options, cb, context) {
this.root.eachNode(options, cb, context);
};
Tree2.prototype.getNodeByDataIndex = function(dataIndex) {
var rawIndex = this.data.getRawIndex(dataIndex);
return this._nodes[rawIndex];
};
Tree2.prototype.getNodeById = function(name) {
return this.root.getNodeById(name);
};
Tree2.prototype.update = function() {
var data = this.data;
var nodes = this._nodes;
for (var i = 0, len = nodes.length; i < len; i++) {
nodes[i].dataIndex = -1;
}
for (var i = 0, len = data.count(); i < len; i++) {
nodes[data.getRawIndex(i)].dataIndex = i;
}
};
Tree2.prototype.clearLayouts = function() {
this.data.clearItemLayouts();
};
Tree2.createTree = function(dataRoot, hostModel, beforeLink) {
var tree = new Tree2(hostModel);
var listData = [];
var dimMax = 1;
buildHierarchy(dataRoot);
function buildHierarchy(dataNode, parentNode) {
var value = dataNode.value;
dimMax = Math.max(dimMax, isArray(value) ? value.length : 1);
listData.push(dataNode);
var node = new TreeNode(convertOptionIdName(dataNode.name, ""), tree);
parentNode ? addChild(node, parentNode) : tree.root = node;
tree._nodes.push(node);
var children = dataNode.children;
if (children) {
for (var i = 0; i < children.length; i++) {
buildHierarchy(children[i], node);
}
}
}
tree.root.updateDepthAndHeight(0);
var dimensions = prepareSeriesDataSchema(listData, {
coordDimensions: ["value"],
dimensionsCount: dimMax
}).dimensions;
var list = new SeriesData(dimensions, hostModel);
list.initData(listData);
beforeLink && beforeLink(list);
linkSeriesData({
mainData: list,
struct: tree,
structAttr: "tree"
});
tree.update();
return tree;
};
return Tree2;
}();
function addChild(child, node) {
var children = node.children;
if (child.parentNode === node) {
return;
}
children.push(child);
child.parentNode = node;
}
var Tree$1 = Tree;
export { TreeNode, Tree$1 as default };