pm_tree.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. Tree.prototype.loadData = function (arrData) {
  242. this.sourceData = arrData;
  243. let i, that = this;
  244. let nodesIndex = {};
  245. function getPreNode(id){
  246. for(let index in nodesIndex){
  247. let node = nodesIndex[index];
  248. if(node['data'][node.setting.tree.nid] === id){
  249. return node;
  250. }
  251. }
  252. return null;
  253. }
  254. function loadNode (data, setting) {//mark
  255. var node = nodesIndex[data[setting.tree.id]] || null,
  256. parent = nodesIndex[data[setting.tree.pid]] || that._root,
  257. next = nodesIndex[data[setting.tree.nid]] || null,
  258. pre = getPreNode(data[setting.tree.id]) || null,
  259. tempData;
  260. if (!node) {
  261. node = new Node(that, data);
  262. }
  263. that.maxNodeId(node.id());
  264. if(parent.childIndex(node) === -1){
  265. if (!pre) {
  266. parent.children.unshift(node);
  267. }
  268. else if(pre && parent.childIndex(pre) !== -1){
  269. parent.children.splice(parent.childIndex(pre) + 1, 0, node);
  270. }
  271. else if(next && parent.childIndex(next) !== -1){
  272. parent.children.splice(parent.childIndex(next), 0, node);
  273. }
  274. else {
  275. parent.children.push(node);
  276. }
  277. }
  278. if(pre && parent.childIndex(pre) === -1){
  279. parent.children.splice(parent.childIndex(node), 0, pre);
  280. }
  281. if(next && parent.childIndex(next) === -1){
  282. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  283. }
  284. if(pre && parent.childIndex(pre) !== parent.childIndex(node) - 1){
  285. parent.children.splice(parent.childIndex(pre), 1);
  286. parent.children.splice(parent.childIndex(node), 0, pre);
  287. }
  288. if(next && parent.childIndex(next) !== parent.childIndex(node) + 1){
  289. parent.children.splice(parent.childIndex(next), 1);
  290. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  291. }
  292. node.parent = parent;
  293. node.nextSibling = next;
  294. }
  295. //建立索引
  296. for(let data of arrData){
  297. nodesIndex[data.ID] = new Node(that, data);
  298. }
  299. for (i = 0; i < arrData.length; i++){
  300. loadNode(arrData[i], this.setting);
  301. }
  302. //set items
  303. sortTreeItems(this);
  304. };
  305. Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
  306. this.sourceData.push(data);
  307. var node = null;
  308. var pNode = parent ? parent : this._root;
  309. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  310. node = new Node(this, data, nodeState);
  311. this.maxNodeId(data[this.setting.tree.id]);
  312. this.move(node, pNode, nextSibling);
  313. }
  314. return node;
  315. };
  316. Tree.prototype.move = function(node, parent, nextSibling) {
  317. var iIndex = -1, pre;
  318. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  319. if (node) {
  320. if (node.parent) {
  321. iIndex = node.parent.childIndex(node);
  322. if (iIndex > 0) {
  323. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  324. }
  325. node.parent.children.splice(iIndex, 1);
  326. }
  327. if (nextSibling) {
  328. iIndex = parent.childIndex(nextSibling);
  329. if (iIndex > 0){
  330. pre = parent.children[iIndex - 1];
  331. pre.setNextSibling(node);
  332. //parent.children.splice(iIndex - 1, 0, node);
  333. parent.children.splice(iIndex, 0, node);
  334. } else {
  335. parent.children.splice(0, 0, node);
  336. }
  337. } else {
  338. if (parent.children.length > 0){
  339. pre = parent.lastChild();
  340. pre.setNextSibling(node);
  341. }
  342. parent.children.push(node);
  343. }
  344. node.setParent(parent);
  345. node.setNextSibling(nextSibling);
  346. //sort items
  347. sortTreeItems(this);
  348. }
  349. } else {
  350. this.e.throw('Error: information of moving node has mistake.');
  351. }
  352. };
  353. Tree.prototype.insert = function (sheet, items, node) {
  354. const rIdx = items.indexOf(node);
  355. const cIdx = sheet.getActiveColumnIndex();
  356. sheet.addRows(rIdx, 1);
  357. //set selection selected
  358. sheet.setSelection(rIdx, cIdx, 1, 1);
  359. this.selected = node;
  360. sheet.getCell(rIdx, 0).cellType(this);
  361. };
  362. Tree.prototype.check = function (_root) {
  363. if (this.sourceData.length !== this.items.length) {
  364. const data = this.sourceData.filter(item => {
  365. const findData = this.items.find(node => node.data.ID === item.ID);
  366. return !findData;
  367. });
  368. console.log('丢失数据:');
  369. console.log(data);
  370. return false;
  371. }
  372. return isValid(_root.children);
  373. function isValid(nodes) {
  374. for (let node of nodes) {
  375. if (node.data.ParentID !== -1 &&
  376. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  377. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  378. return false;
  379. }
  380. if (node.data.ParentID === -1 && node.parent && node.parent !== _root) {
  381. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  382. return false;
  383. }
  384. if (node.data.NextSiblingID !== -1 &&
  385. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  386. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  387. return false;
  388. }
  389. if (node.data.NextSiblingID === -1 && node.nextSibling) {
  390. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  391. return false;
  392. }
  393. let parent = node.parent,
  394. nodeIdx = parent.children.indexOf(node),
  395. nextIdx = parent.children.indexOf(node.nextSibling);
  396. if (nodeIdx !== -1 && nextIdx !== -1 && nodeIdx > nextIdx) {
  397. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  398. return false;
  399. }
  400. // nextSibling跟parent children的下一节点对应不上
  401. if (nodeIdx !== -1 &&
  402. (nodeIdx === parent.children.length - 1 && nextIdx !== -1) ||
  403. (nodeIdx !== parent.children.length - 1 && nodeIdx + 1 !== nextIdx)) {
  404. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  405. return false;
  406. }
  407. if (node.nextSibling && node.parent !== node.nextSibling.parent) {
  408. console.log(`${node.serialNo() + 1}:${node.data.name} 与兄弟节点 ${node.nextSibling.serialNo() + 1}:${node.nextSibling.data.name} 父节点不同`);
  409. return false;
  410. }
  411. if (node.children.length) {
  412. let v = isValid(node.children);
  413. if (!v) {
  414. return false;
  415. }
  416. }
  417. }
  418. return true;
  419. }
  420. };
  421. Tree.prototype.getExpState = function (nodes) {
  422. let sessionExpanded = [];
  423. function getStat(items){
  424. for(let item of items){
  425. sessionExpanded.push(item.expanded ? 1 : 0);
  426. }
  427. }
  428. getStat(nodes);
  429. let expState = sessionExpanded.join('');
  430. return expState;
  431. };
  432. //节点根据展开收起列表'010101'展开收起
  433. Tree.prototype.setExpandedByState = function (nodes, expState) {
  434. let expStateArr = expState.split('');
  435. for(let i = 0; i < nodes.length; i++){
  436. let expanded = expStateArr[i] == 1 ? true : false;
  437. if(nodes[i].expanded === expanded){
  438. continue;
  439. }
  440. nodes[i].setExpanded(expanded);
  441. }
  442. };
  443. Tree.prototype.setNodesExpanded = function (nodes, sheet) {
  444. TREE_SHEET_HELPER.massOperationSheet(sheet, () => {
  445. nodes.forEach(node => {
  446. node.setExpanded(true);
  447. const index = node.serialNo();
  448. const count = node.posterityCount();
  449. for (let i = 0; i < count; i++) {
  450. const row = index + i;
  451. const child = this.items[row + 1];
  452. sheet.setRowVisible(row + 1, child.visible);
  453. }
  454. });
  455. });
  456. };
  457. return Tree;
  458. })()
  459. let tree = new Tree(setting);
  460. tree.loadData(arrData);
  461. return tree;
  462. }
  463. };