pm_tree.js 24 KB

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