Tree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /**
  41. * Tree data structure
  42. */
  43. import * as zrUtil from 'zrender/lib/core/util.js';
  44. import linkSeriesData from './helper/linkSeriesData.js';
  45. import SeriesData from './SeriesData.js';
  46. import prepareSeriesDataSchema from './helper/createDimensions.js';
  47. import { convertOptionIdName } from '../util/model.js';
  48. var TreeNode = /** @class */function () {
  49. function TreeNode(name, hostTree) {
  50. this.depth = 0;
  51. this.height = 0;
  52. /**
  53. * Reference to list item.
  54. * Do not persistent dataIndex outside,
  55. * besause it may be changed by list.
  56. * If dataIndex -1,
  57. * this node is logical deleted (filtered) in list.
  58. */
  59. this.dataIndex = -1;
  60. this.children = [];
  61. this.viewChildren = [];
  62. this.isExpand = false;
  63. this.name = name || '';
  64. this.hostTree = hostTree;
  65. }
  66. /**
  67. * The node is removed.
  68. */
  69. TreeNode.prototype.isRemoved = function () {
  70. return this.dataIndex < 0;
  71. };
  72. TreeNode.prototype.eachNode = function (options, cb, context) {
  73. if (zrUtil.isFunction(options)) {
  74. context = cb;
  75. cb = options;
  76. options = null;
  77. }
  78. options = options || {};
  79. if (zrUtil.isString(options)) {
  80. options = {
  81. order: options
  82. };
  83. }
  84. var order = options.order || 'preorder';
  85. var children = this[options.attr || 'children'];
  86. var suppressVisitSub;
  87. order === 'preorder' && (suppressVisitSub = cb.call(context, this));
  88. for (var i = 0; !suppressVisitSub && i < children.length; i++) {
  89. children[i].eachNode(options, cb, context);
  90. }
  91. order === 'postorder' && cb.call(context, this);
  92. };
  93. /**
  94. * Update depth and height of this subtree.
  95. */
  96. TreeNode.prototype.updateDepthAndHeight = function (depth) {
  97. var height = 0;
  98. this.depth = depth;
  99. for (var i = 0; i < this.children.length; i++) {
  100. var child = this.children[i];
  101. child.updateDepthAndHeight(depth + 1);
  102. if (child.height > height) {
  103. height = child.height;
  104. }
  105. }
  106. this.height = height + 1;
  107. };
  108. TreeNode.prototype.getNodeById = function (id) {
  109. if (this.getId() === id) {
  110. return this;
  111. }
  112. for (var i = 0, children = this.children, len = children.length; i < len; i++) {
  113. var res = children[i].getNodeById(id);
  114. if (res) {
  115. return res;
  116. }
  117. }
  118. };
  119. TreeNode.prototype.contains = function (node) {
  120. if (node === this) {
  121. return true;
  122. }
  123. for (var i = 0, children = this.children, len = children.length; i < len; i++) {
  124. var res = children[i].contains(node);
  125. if (res) {
  126. return res;
  127. }
  128. }
  129. };
  130. /**
  131. * @param includeSelf Default false.
  132. * @return order: [root, child, grandchild, ...]
  133. */
  134. TreeNode.prototype.getAncestors = function (includeSelf) {
  135. var ancestors = [];
  136. var node = includeSelf ? this : this.parentNode;
  137. while (node) {
  138. ancestors.push(node);
  139. node = node.parentNode;
  140. }
  141. ancestors.reverse();
  142. return ancestors;
  143. };
  144. TreeNode.prototype.getAncestorsIndices = function () {
  145. var indices = [];
  146. var currNode = this;
  147. while (currNode) {
  148. indices.push(currNode.dataIndex);
  149. currNode = currNode.parentNode;
  150. }
  151. indices.reverse();
  152. return indices;
  153. };
  154. TreeNode.prototype.getDescendantIndices = function () {
  155. var indices = [];
  156. this.eachNode(function (childNode) {
  157. indices.push(childNode.dataIndex);
  158. });
  159. return indices;
  160. };
  161. TreeNode.prototype.getValue = function (dimension) {
  162. var data = this.hostTree.data;
  163. return data.getStore().get(data.getDimensionIndex(dimension || 'value'), this.dataIndex);
  164. };
  165. TreeNode.prototype.setLayout = function (layout, merge) {
  166. this.dataIndex >= 0 && this.hostTree.data.setItemLayout(this.dataIndex, layout, merge);
  167. };
  168. /**
  169. * @return {Object} layout
  170. */
  171. TreeNode.prototype.getLayout = function () {
  172. return this.hostTree.data.getItemLayout(this.dataIndex);
  173. };
  174. // @depcrecated
  175. // getModel<T = unknown, S extends keyof T = keyof T>(path: S): Model<T[S]>
  176. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  177. TreeNode.prototype.getModel = function (path) {
  178. if (this.dataIndex < 0) {
  179. return;
  180. }
  181. var hostTree = this.hostTree;
  182. var itemModel = hostTree.data.getItemModel(this.dataIndex);
  183. return itemModel.getModel(path);
  184. };
  185. // TODO: TYPE More specific model
  186. TreeNode.prototype.getLevelModel = function () {
  187. return (this.hostTree.levelModels || [])[this.depth];
  188. };
  189. TreeNode.prototype.setVisual = function (key, value) {
  190. this.dataIndex >= 0 && this.hostTree.data.setItemVisual(this.dataIndex, key, value);
  191. };
  192. /**
  193. * Get item visual
  194. * FIXME: make return type better
  195. */
  196. TreeNode.prototype.getVisual = function (key) {
  197. return this.hostTree.data.getItemVisual(this.dataIndex, key);
  198. };
  199. TreeNode.prototype.getRawIndex = function () {
  200. return this.hostTree.data.getRawIndex(this.dataIndex);
  201. };
  202. TreeNode.prototype.getId = function () {
  203. return this.hostTree.data.getId(this.dataIndex);
  204. };
  205. /**
  206. * index in parent's children
  207. */
  208. TreeNode.prototype.getChildIndex = function () {
  209. if (this.parentNode) {
  210. var children = this.parentNode.children;
  211. for (var i = 0; i < children.length; ++i) {
  212. if (children[i] === this) {
  213. return i;
  214. }
  215. }
  216. return -1;
  217. }
  218. return -1;
  219. };
  220. /**
  221. * if this is an ancestor of another node
  222. *
  223. * @param node another node
  224. * @return if is ancestor
  225. */
  226. TreeNode.prototype.isAncestorOf = function (node) {
  227. var parent = node.parentNode;
  228. while (parent) {
  229. if (parent === this) {
  230. return true;
  231. }
  232. parent = parent.parentNode;
  233. }
  234. return false;
  235. };
  236. /**
  237. * if this is an descendant of another node
  238. *
  239. * @param node another node
  240. * @return if is descendant
  241. */
  242. TreeNode.prototype.isDescendantOf = function (node) {
  243. return node !== this && node.isAncestorOf(this);
  244. };
  245. return TreeNode;
  246. }();
  247. export { TreeNode };
  248. ;
  249. var Tree = /** @class */function () {
  250. function Tree(hostModel) {
  251. this.type = 'tree';
  252. this._nodes = [];
  253. this.hostModel = hostModel;
  254. }
  255. Tree.prototype.eachNode = function (options, cb, context) {
  256. this.root.eachNode(options, cb, context);
  257. };
  258. Tree.prototype.getNodeByDataIndex = function (dataIndex) {
  259. var rawIndex = this.data.getRawIndex(dataIndex);
  260. return this._nodes[rawIndex];
  261. };
  262. Tree.prototype.getNodeById = function (name) {
  263. return this.root.getNodeById(name);
  264. };
  265. /**
  266. * Update item available by list,
  267. * when list has been performed options like 'filterSelf' or 'map'.
  268. */
  269. Tree.prototype.update = function () {
  270. var data = this.data;
  271. var nodes = this._nodes;
  272. for (var i = 0, len = nodes.length; i < len; i++) {
  273. nodes[i].dataIndex = -1;
  274. }
  275. for (var i = 0, len = data.count(); i < len; i++) {
  276. nodes[data.getRawIndex(i)].dataIndex = i;
  277. }
  278. };
  279. /**
  280. * Clear all layouts
  281. */
  282. Tree.prototype.clearLayouts = function () {
  283. this.data.clearItemLayouts();
  284. };
  285. /**
  286. * data node format:
  287. * {
  288. * name: ...
  289. * value: ...
  290. * children: [
  291. * {
  292. * name: ...
  293. * value: ...
  294. * children: ...
  295. * },
  296. * ...
  297. * ]
  298. * }
  299. */
  300. Tree.createTree = function (dataRoot, hostModel, beforeLink) {
  301. var tree = new Tree(hostModel);
  302. var listData = [];
  303. var dimMax = 1;
  304. buildHierarchy(dataRoot);
  305. function buildHierarchy(dataNode, parentNode) {
  306. var value = dataNode.value;
  307. dimMax = Math.max(dimMax, zrUtil.isArray(value) ? value.length : 1);
  308. listData.push(dataNode);
  309. var node = new TreeNode(convertOptionIdName(dataNode.name, ''), tree);
  310. parentNode ? addChild(node, parentNode) : tree.root = node;
  311. tree._nodes.push(node);
  312. var children = dataNode.children;
  313. if (children) {
  314. for (var i = 0; i < children.length; i++) {
  315. buildHierarchy(children[i], node);
  316. }
  317. }
  318. }
  319. tree.root.updateDepthAndHeight(0);
  320. var dimensions = prepareSeriesDataSchema(listData, {
  321. coordDimensions: ['value'],
  322. dimensionsCount: dimMax
  323. }).dimensions;
  324. var list = new SeriesData(dimensions, hostModel);
  325. list.initData(listData);
  326. beforeLink && beforeLink(list);
  327. linkSeriesData({
  328. mainData: list,
  329. struct: tree,
  330. structAttr: 'tree'
  331. });
  332. tree.update();
  333. return tree;
  334. };
  335. return Tree;
  336. }();
  337. /**
  338. * It is needed to consider the mess of 'list', 'hostModel' when creating a TreeNote,
  339. * so this function is not ready and not necessary to be public.
  340. */
  341. function addChild(child, node) {
  342. var children = node.children;
  343. if (child.parentNode === node) {
  344. return;
  345. }
  346. children.push(child);
  347. child.parentNode = node;
  348. }
  349. export default Tree;