pm_tree.js 21 KB

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