pm_tree.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**
  2. * Created by Zhong on 2018/3/21.
  3. */
  4. const pmTree = {
  5. createNew: function (setting, arrData) {
  6. function sortTreeItems(tree) {
  7. var addItems = function (items) {
  8. var i;
  9. for (i = 0; i < items.length; i++) {
  10. tree.items.push(items[i]);
  11. addItems(items[i].children);
  12. }
  13. };
  14. tree.items.splice(0, tree.items.length);
  15. addItems(tree._root.children);
  16. }
  17. var Node = (function () {
  18. function Node(tree, data, nodeState = null) {
  19. this.parent = null;
  20. this.nextSibling = null;
  21. this.children = [];
  22. this.tree = tree;
  23. this.data = data;
  24. this.setting = tree.setting;
  25. this.expanded = true;
  26. this.row = null;
  27. this.expandBtn = null;
  28. if (nodeState) {
  29. for (const attr in nodeState) {
  30. this[attr] = nodeState[attr];
  31. }
  32. }
  33. }
  34. Node.prototype.firstChild = function () {
  35. return (this.children.length === 0) ? null : this.children[0];
  36. };
  37. Node.prototype.lastChild = function () {
  38. return (this.children.length === 0) ? null : this.children[this.children.length - 1];
  39. };
  40. Node.prototype.deepestRow = function () {
  41. return (this.children.length === 0) ? this.row : this.lastChild().deepestRow();
  42. };
  43. Node.prototype.depth = function () {
  44. return this.parent ? this.parent.depth() + 1 : 0;
  45. };
  46. Node.prototype.isFirst = function () {
  47. if (this.parent) {
  48. return this.parent.children.indexOf(this) === 0 ? true : false;
  49. } else {
  50. return this.tree._root.children.indexOf(this) === 0 ? true : false;
  51. }
  52. };
  53. Node.prototype.isLast = function () {
  54. if (this.parent) {
  55. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  56. } else {
  57. return this.tree._root.children.indexOf(this) === this.tree._root.children.length - 1 ? true : false;
  58. }
  59. };
  60. Node.prototype.setExpanded = function (expanded) {
  61. var setNodesVisible = function (nodes, visible) {
  62. nodes.forEach(function (node) {
  63. node.visible = visible;
  64. setNodesVisible(node.children, visible && node.expanded);
  65. })
  66. };
  67. this.expanded = expanded;
  68. setNodesVisible(this.children, expanded);
  69. };
  70. Node.prototype.posterityCount = function () {
  71. var iCount = 0;
  72. if (this.children.length !== 0) {
  73. iCount += this.children.length;
  74. this.children.forEach(function (child) {
  75. iCount += child.posterityCount();
  76. });
  77. }
  78. return iCount;
  79. };
  80. Node.prototype.addChild = function (child, childNext) {
  81. if (childNext) {
  82. this.children.push(child);
  83. } else {
  84. if (this.childIndex(childNext) > -1) {
  85. this.children.splice(this.childIndex(childNext) - 1, 0, child);
  86. } else {
  87. this.children.push(child);
  88. }
  89. }
  90. return child;
  91. };
  92. Node.prototype.childIndex = function (child) {
  93. return this.children.indexOf(child);
  94. };
  95. Node.prototype.depth = function () {
  96. return this.parent ? this.parent.depth() + 1 : 0;
  97. };
  98. Node.prototype.domId = function () {
  99. return this.data ? this.tree.domId + '_' + this.data[this.setting.tree.id] : '';
  100. };
  101. Node.prototype.expand = function (bool) {
  102. this.expanded = bool;
  103. _view._refreshTreeBtn(this);
  104. if (this.expanded) {
  105. _view._showNodes(this.children);
  106. } else {
  107. _view._hideNodes(this.children);
  108. }
  109. };
  110. Node.prototype.getCollapseParents = function () {
  111. const rst = [];
  112. let parent = this.parent;
  113. while (parent && parent.data) {
  114. if (!parent.expanded) {
  115. rst.push(parent);
  116. }
  117. parent = parent.parent;
  118. }
  119. return rst;
  120. };
  121. Node.prototype.serialNo = function () {
  122. return this.tree.items.indexOf(this);
  123. };
  124. Node.prototype.preSibling = function () {
  125. var iIndex = this.parent.childIndex(this);
  126. if (iIndex === -1) {
  127. return null;
  128. } else {
  129. return iIndex > 0 ? this.parent.children[iIndex - 1] : null;
  130. }
  131. };
  132. Node.prototype.setParent = function (parent) {
  133. if (parent && this.parent !== parent) {
  134. this.parent = parent;
  135. this.data[this.setting.tree.pid] = this.pid();
  136. }
  137. };
  138. Node.prototype.setNextSibling = function (nextSibling) {
  139. if (this.nextSibling !== nextSibling) {
  140. this.nextSibling = nextSibling;
  141. this.data[this.setting.tree.nid] = this.nid();
  142. }
  143. }
  144. Node.prototype.id = function () {
  145. return this.data ? this.data[this.setting.tree.id] : -1;
  146. };
  147. Node.prototype.pid = function () {
  148. return this.parent ? this.parent.id() : -1;
  149. };
  150. Node.prototype.nid = function () {
  151. return this.nextSibling ? this.nextSibling.id() : -1;
  152. };
  153. Node.prototype.propertyJoin = function (dataName) {
  154. return this.parent ? this.parent.propertyJoin(dataName) + ';' + this.data[dataName] : this.data[dataName];
  155. };
  156. Node.prototype.getAllChildren = function () {
  157. let childrenList = [];
  158. getChildren(this);
  159. function getChildren(node) {
  160. for (let c of node.children) {
  161. childrenList.push(c);
  162. getChildren(c)
  163. }
  164. }
  165. return childrenList;
  166. };
  167. return Node;
  168. })();
  169. var Tree = (function () {
  170. function Tree(setting) {
  171. this._root = new Node(this);
  172. this.items = [];
  173. this.selected = null;
  174. this.setting = setting;
  175. var _maxNodeId = 0;
  176. this.newNodeId = function (id) {
  177. if (arguments.length > 0) {
  178. _maxNodeId = (id > _maxNodeId) ? id : _maxNodeId;
  179. } else {
  180. _maxNodeId += 1;
  181. return _maxNodeId;
  182. }
  183. };
  184. this.maxNodeId = function (id) {
  185. if (arguments.length > 0) {
  186. _maxNodeId = Math.max(id, _maxNodeId);
  187. } else {
  188. return _maxNodeId;
  189. }
  190. }
  191. };
  192. Tree.prototype.firstNode = function () {
  193. return this._root.firstChild();
  194. };
  195. Tree.prototype.traverseDF = function (callback) {
  196. var recurse = function (node) {
  197. var i;
  198. if (node !== this._root) {
  199. callback(node);
  200. }
  201. for (i = 0; i < node.children.length; i++) {
  202. recurse(node.children[i]);
  203. }
  204. }
  205. recurse(this._root);
  206. };
  207. Tree.prototype.findNode = function (id) {
  208. var treenode = null,
  209. callback = function (node) {
  210. if (node.data && node.data[node.setting.tree.id] === id) {
  211. treenode = node;
  212. }
  213. };
  214. this.traverseDF.call(this, callback);
  215. return treenode;
  216. };
  217. Tree.prototype.findNodeByNid = function (nid) {
  218. let treenode = null,
  219. callback = function (node) {
  220. if (node.data && node.data[node.setting.tree.nid] === nid) {
  221. treenode = node;
  222. }
  223. };
  224. this.traverseDF.call(this, callback);
  225. return treenode;
  226. };
  227. Tree.prototype.removeNode = function (node) {
  228. var iIndex;
  229. if (node) {
  230. iIndex = node.parent.childIndex(node);
  231. if (iIndex > 0) {
  232. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  233. }
  234. node.parent.children.splice(iIndex, 1);
  235. }
  236. sortTreeItems(this);
  237. this.sourceData = this.items.reduce((acc, cur) => {
  238. acc.push(cur.data);
  239. return acc;
  240. }, []);
  241. };
  242. function sortChildren(nodes) {
  243. const IDMap = {};
  244. const nextMap = {};
  245. let firstNode = null;
  246. const newList = [];
  247. for (const node of nodes) {
  248. // 递规排序
  249. if (node.children && node.children.length > 0) {
  250. node.children = sortChildren(node.children);
  251. }
  252. IDMap[node.data.ID] = node;
  253. if (node.data.NextSiblingID != -1) {
  254. nextMap[node.data.NextSiblingID] = node;
  255. }
  256. }
  257. for (const node of nodes) {
  258. if (!nextMap[node.data.ID]) { //如果在下一节点映射没找到,则是第一个节点
  259. firstNode = node;
  260. break;
  261. }
  262. }
  263. if (firstNode) {
  264. newList.push(firstNode);
  265. delete IDMap[firstNode.data.ID];
  266. setNext(firstNode, newList);
  267. }
  268. // 容错处理,如果链断了的情况,直接添加到后面
  269. for (const key in IDMap) {
  270. if (IDMap[key]) {
  271. newList.push(IDMap[key]);
  272. }
  273. }
  274. return newList;
  275. function setNext(node, array) {
  276. if (node.data.NextSiblingID != -1) {
  277. const next = IDMap[node.data.NextSiblingID];
  278. node.nextSibling = next || null;
  279. if (next) {
  280. array.push(next);
  281. delete IDMap[next.data.ID];
  282. setNext(next, array);
  283. }
  284. }
  285. }
  286. }
  287. Tree.prototype.loadData = function (treeData) {
  288. this.sourceData = treeData;
  289. // 建立节点及索引
  290. const nodes = [];
  291. const parentMap = {};
  292. const IDMap = {};
  293. treeData.forEach(nodeData => {
  294. const node = new Node(this, nodeData);
  295. this.maxNodeId(node.id());
  296. IDMap[node.data.ID] = node;
  297. (parentMap[node.data.ParentID] || (parentMap[node.data.ParentID] = [])).push(node);
  298. nodes.push(node);
  299. });
  300. // 过滤出各节点的children、设置节点parent
  301. nodes.forEach(node => {
  302. node.children = parentMap[node.data.ID] || [];
  303. node.parent = IDMap[node.data.ParentID] || this._root;
  304. });
  305. const roots = parentMap['-1'] || [];
  306. // 将各节点的children进行排序,并设置nextSibling
  307. this._root.children = sortChildren(roots);
  308. // 生成tree.items
  309. sortTreeItems(this);
  310. };
  311. /* Tree.prototype.loadData = function (arrData) {
  312. this.sourceData = arrData;
  313. let i, that = this;
  314. let nodesIndex = {};
  315. function getPreNode(id) {
  316. for (let index in nodesIndex) {
  317. let node = nodesIndex[index];
  318. if (node['data'][node.setting.tree.nid] === id) {
  319. return node;
  320. }
  321. }
  322. return null;
  323. }
  324. function loadNode(data, setting) {//mark
  325. var node = nodesIndex[data[setting.tree.id]] || null,
  326. parent = nodesIndex[data[setting.tree.pid]] || that._root,
  327. next = nodesIndex[data[setting.tree.nid]] || null,
  328. pre = getPreNode(data[setting.tree.id]) || null,
  329. tempData;
  330. if (!node) {
  331. node = new Node(that, data);
  332. }
  333. that.maxNodeId(node.id());
  334. if (parent.childIndex(node) === -1) {
  335. if (!pre) {
  336. parent.children.unshift(node);
  337. }
  338. else if (pre && parent.childIndex(pre) !== -1) {
  339. parent.children.splice(parent.childIndex(pre) + 1, 0, node);
  340. }
  341. else if (next && parent.childIndex(next) !== -1) {
  342. parent.children.splice(parent.childIndex(next), 0, node);
  343. }
  344. else {
  345. parent.children.push(node);
  346. }
  347. }
  348. if (pre && parent.childIndex(pre) === -1) {
  349. parent.children.splice(parent.childIndex(node), 0, pre);
  350. }
  351. if (next && parent.childIndex(next) === -1) {
  352. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  353. }
  354. if (pre && parent.childIndex(pre) !== parent.childIndex(node) - 1) {
  355. parent.children.splice(parent.childIndex(pre), 1);
  356. parent.children.splice(parent.childIndex(node), 0, pre);
  357. }
  358. if (next && parent.childIndex(next) !== parent.childIndex(node) + 1) {
  359. parent.children.splice(parent.childIndex(next), 1);
  360. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  361. }
  362. node.parent = parent;
  363. node.nextSibling = next;
  364. }
  365. //建立索引
  366. for (let data of arrData) {
  367. nodesIndex[data.ID] = new Node(that, data);
  368. }
  369. for (i = 0; i < arrData.length; i++) {
  370. loadNode(arrData[i], this.setting);
  371. }
  372. //set items
  373. sortTreeItems(this);
  374. }; */
  375. Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
  376. this.sourceData.push(data);
  377. var node = null;
  378. var pNode = parent ? parent : this._root;
  379. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  380. node = new Node(this, data, nodeState);
  381. this.maxNodeId(data[this.setting.tree.id]);
  382. this.move(node, pNode, nextSibling);
  383. }
  384. return node;
  385. };
  386. Tree.prototype.move = function (node, parent, nextSibling) {
  387. var iIndex = -1, pre;
  388. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  389. if (node) {
  390. if (node.parent) {
  391. iIndex = node.parent.childIndex(node);
  392. if (iIndex > 0) {
  393. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  394. }
  395. node.parent.children.splice(iIndex, 1);
  396. }
  397. if (nextSibling) {
  398. iIndex = parent.childIndex(nextSibling);
  399. if (iIndex > 0) {
  400. pre = parent.children[iIndex - 1];
  401. pre.setNextSibling(node);
  402. //parent.children.splice(iIndex - 1, 0, node);
  403. parent.children.splice(iIndex, 0, node);
  404. } else {
  405. parent.children.splice(0, 0, node);
  406. }
  407. } else {
  408. if (parent.children.length > 0) {
  409. pre = parent.lastChild();
  410. pre.setNextSibling(node);
  411. }
  412. parent.children.push(node);
  413. }
  414. node.setParent(parent);
  415. node.setNextSibling(nextSibling);
  416. //sort items
  417. sortTreeItems(this);
  418. }
  419. } else {
  420. this.e.throw('Error: information of moving node has mistake.');
  421. }
  422. };
  423. Tree.prototype.insert = function (sheet, items, node) {
  424. const rIdx = items.indexOf(node);
  425. const cIdx = sheet.getActiveColumnIndex();
  426. sheet.addRows(rIdx, 1);
  427. //set selection selected
  428. sheet.setSelection(rIdx, cIdx, 1, 1);
  429. this.selected = node;
  430. sheet.getCell(rIdx, 0).cellType(this);
  431. };
  432. Tree.prototype.check = function (_root) {
  433. if (this.sourceData.length !== this.items.length) {
  434. const data = this.sourceData.filter(item => {
  435. const findData = this.items.find(node => node.data.ID === item.ID);
  436. return !findData;
  437. });
  438. console.log('丢失数据:');
  439. console.log(data);
  440. return false;
  441. }
  442. return isValid(_root.children);
  443. function isValid(nodes) {
  444. for (let node of nodes) {
  445. if (node.data.ParentID !== -1 &&
  446. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  447. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  448. return false;
  449. }
  450. if (node.data.ParentID === -1 && node.parent && node.parent !== _root) {
  451. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  452. return false;
  453. }
  454. if (node.data.NextSiblingID !== -1 &&
  455. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  456. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  457. return false;
  458. }
  459. if (node.data.NextSiblingID === -1 && node.nextSibling) {
  460. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  461. return false;
  462. }
  463. let parent = node.parent,
  464. nodeIdx = parent.children.indexOf(node),
  465. nextIdx = parent.children.indexOf(node.nextSibling);
  466. if (nodeIdx !== -1 && nextIdx !== -1 && nodeIdx > nextIdx) {
  467. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  468. return false;
  469. }
  470. // nextSibling跟parent children的下一节点对应不上
  471. if (nodeIdx !== -1 &&
  472. (nodeIdx === parent.children.length - 1 && nextIdx !== -1) ||
  473. (nodeIdx !== parent.children.length - 1 && nodeIdx + 1 !== nextIdx)) {
  474. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  475. return false;
  476. }
  477. if (node.nextSibling && node.parent !== node.nextSibling.parent) {
  478. console.log(`${node.serialNo() + 1}:${node.data.name} 与兄弟节点 ${node.nextSibling.serialNo() + 1}:${node.nextSibling.data.name} 父节点不同`);
  479. return false;
  480. }
  481. if (node.children.length) {
  482. let v = isValid(node.children);
  483. if (!v) {
  484. return false;
  485. }
  486. }
  487. }
  488. return true;
  489. }
  490. };
  491. Tree.prototype.getExpState = function (nodes) {
  492. let sessionExpanded = [];
  493. function getStat(items) {
  494. for (let item of items) {
  495. sessionExpanded.push(item.expanded ? 1 : 0);
  496. }
  497. }
  498. getStat(nodes);
  499. let expState = sessionExpanded.join('');
  500. return expState;
  501. };
  502. //节点根据展开收起列表'010101'展开收起
  503. Tree.prototype.setExpandedByState = function (nodes, expState) {
  504. let expStateArr = expState.split('');
  505. for (let i = 0; i < nodes.length; i++) {
  506. let expanded = expStateArr[i] == 1 ? true : false;
  507. if (nodes[i].expanded === expanded) {
  508. continue;
  509. }
  510. nodes[i].setExpanded(expanded);
  511. }
  512. };
  513. Tree.prototype.setNodesExpanded = function (nodes, sheet) {
  514. TREE_SHEET_HELPER.massOperationSheet(sheet, () => {
  515. nodes.forEach(node => {
  516. node.setExpanded(true);
  517. const index = node.serialNo();
  518. const count = node.posterityCount();
  519. for (let i = 0; i < count; i++) {
  520. const row = index + i;
  521. const child = this.items[row + 1];
  522. sheet.setRowVisible(row + 1, child.visible);
  523. }
  524. });
  525. });
  526. };
  527. return Tree;
  528. })()
  529. let tree = new Tree(setting);
  530. tree.loadData(arrData);
  531. return tree;
  532. }
  533. };