pm_tree.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.serialNo = function () {
  111. return this.tree.items.indexOf(this);
  112. };
  113. Node.prototype.preSibling = function () {
  114. var iIndex = this.parent.childIndex(this);
  115. if (iIndex === -1){
  116. return null;
  117. } else {
  118. return iIndex > 0 ? this.parent.children[iIndex-1] : null;
  119. }
  120. };
  121. Node.prototype.setParent = function (parent) {
  122. if (parent && this.parent !== parent) {
  123. this.parent = parent;
  124. this.data[this.setting.tree.pid] = this.pid();
  125. }
  126. };
  127. Node.prototype.setNextSibling = function (nextSibling) {
  128. if (this.nextSibling !== nextSibling) {
  129. this.nextSibling = nextSibling;
  130. this.data[this.setting.tree.nid] = this.nid();
  131. }
  132. }
  133. Node.prototype.id = function () {
  134. return this.data ? this.data[this.setting.tree.id] : -1;
  135. };
  136. Node.prototype.pid = function () {
  137. return this.parent ? this.parent.id() : -1;
  138. };
  139. Node.prototype.nid = function () {
  140. return this.nextSibling ? this.nextSibling.id() : -1;
  141. };
  142. Node.prototype.propertyJoin = function (dataName) {
  143. return this.parent ? this.parent.propertyJoin(dataName) + ';' + this.data[dataName] : this.data[dataName];
  144. };
  145. Node.prototype.getAllChildren = function () {
  146. let childrenList = [];
  147. getChildren(this);
  148. function getChildren(node) {
  149. for(let c of node.children){
  150. childrenList.push(c);
  151. getChildren(c)
  152. }
  153. }
  154. return childrenList;
  155. };
  156. return Node;
  157. })();
  158. var Tree = (function () {
  159. function Tree(setting) {
  160. this._root = new Node(this);
  161. this.items = [];
  162. this.selected = null;
  163. this.setting = setting;
  164. var _maxNodeId = 0;
  165. this.newNodeId = function (id) {
  166. if (arguments.length > 0){
  167. _maxNodeId = (id > _maxNodeId) ? id : _maxNodeId;
  168. } else {
  169. _maxNodeId += 1;
  170. return _maxNodeId;
  171. }
  172. };
  173. this.maxNodeId = function (id){
  174. if (arguments.length > 0) {
  175. _maxNodeId = Math.max(id, _maxNodeId);
  176. } else {
  177. return _maxNodeId;
  178. }
  179. }
  180. };
  181. Tree.prototype.firstNode = function (){
  182. return this._root.firstChild();
  183. };
  184. Tree.prototype.traverseDF = function(callback){
  185. var recurse = function (node) {
  186. var i;
  187. if (node !== this._root) {
  188. callback(node);
  189. }
  190. for (i = 0; i < node.children.length; i++){
  191. recurse(node.children[i]);
  192. }
  193. }
  194. recurse(this._root);
  195. };
  196. Tree.prototype.findNode = function (id){
  197. var treenode = null,
  198. callback = function (node) {
  199. if (node.data && node.data[node.setting.tree.id] === id){
  200. treenode = node;
  201. }
  202. };
  203. this.traverseDF.call(this, callback);
  204. return treenode;
  205. };
  206. Tree.prototype.findNodeByNid = function (nid) {
  207. let treenode = null,
  208. callback = function (node) {
  209. if(node.data && node.data[node.setting.tree.nid] === nid){
  210. treenode = node;
  211. }
  212. };
  213. this.traverseDF.call(this, callback);
  214. return treenode;
  215. };
  216. Tree.prototype.removeNode = function (node) {
  217. var iIndex;
  218. if (node) {
  219. iIndex = node.parent.childIndex(node);
  220. if (iIndex > 0) {
  221. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  222. }
  223. node.parent.children.splice(iIndex, 1);
  224. }
  225. sortTreeItems(this);
  226. this.sourceData = this.items.reduce((acc, cur) => {
  227. acc.push(cur.data);
  228. return acc;
  229. }, []);
  230. };
  231. Tree.prototype.loadData = function (arrData) {
  232. this.sourceData = arrData;
  233. let i, that = this;
  234. let nodesIndex = {};
  235. function getPreNode(id){
  236. for(let index in nodesIndex){
  237. let node = nodesIndex[index];
  238. if(node['data'][node.setting.tree.nid] === id){
  239. return node;
  240. }
  241. }
  242. return null;
  243. }
  244. function loadNode (data, setting) {//mark
  245. var node = nodesIndex[data[setting.tree.id]] || null,
  246. parent = nodesIndex[data[setting.tree.pid]] || that._root,
  247. next = nodesIndex[data[setting.tree.nid]] || null,
  248. pre = getPreNode(data[setting.tree.id]) || null,
  249. tempData;
  250. if (!node) {
  251. node = new Node(that, data);
  252. }
  253. that.maxNodeId(node.id());
  254. if(parent.childIndex(node) === -1){
  255. if (!pre) {
  256. parent.children.unshift(node);
  257. }
  258. else if(pre && parent.childIndex(pre) !== -1){
  259. parent.children.splice(parent.childIndex(pre) + 1, 0, node);
  260. }
  261. else if(next && parent.childIndex(next) !== -1){
  262. parent.children.splice(parent.childIndex(next), 0, node);
  263. }
  264. else {
  265. parent.children.push(node);
  266. }
  267. }
  268. if(pre && parent.childIndex(pre) === -1){
  269. parent.children.splice(parent.childIndex(node), 0, pre);
  270. }
  271. if(next && parent.childIndex(next) === -1){
  272. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  273. }
  274. if(pre && parent.childIndex(pre) !== parent.childIndex(node) - 1){
  275. parent.children.splice(parent.childIndex(pre), 1);
  276. parent.children.splice(parent.childIndex(node), 0, pre);
  277. }
  278. if(next && parent.childIndex(next) !== parent.childIndex(node) + 1){
  279. parent.children.splice(parent.childIndex(next), 1);
  280. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  281. }
  282. node.parent = parent;
  283. node.nextSibling = next;
  284. }
  285. //建立索引
  286. for(let data of arrData){
  287. nodesIndex[data.ID] = new Node(that, data);
  288. }
  289. for (i = 0; i < arrData.length; i++){
  290. loadNode(arrData[i], this.setting);
  291. }
  292. //set items
  293. sortTreeItems(this);
  294. };
  295. Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
  296. this.sourceData.push(data);
  297. var node = null;
  298. var pNode = parent ? parent : this._root;
  299. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  300. node = new Node(this, data, nodeState);
  301. this.maxNodeId(data[this.setting.tree.id]);
  302. this.move(node, pNode, nextSibling);
  303. }
  304. return node;
  305. };
  306. Tree.prototype.move = function(node, parent, nextSibling) {
  307. var iIndex = -1, pre;
  308. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  309. if (node) {
  310. if (node.parent) {
  311. iIndex = node.parent.childIndex(node);
  312. if (iIndex > 0) {
  313. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  314. }
  315. node.parent.children.splice(iIndex, 1);
  316. }
  317. if (nextSibling) {
  318. iIndex = parent.childIndex(nextSibling);
  319. if (iIndex > 0){
  320. pre = parent.children[iIndex - 1];
  321. pre.setNextSibling(node);
  322. //parent.children.splice(iIndex - 1, 0, node);
  323. parent.children.splice(iIndex, 0, node);
  324. } else {
  325. parent.children.splice(0, 0, node);
  326. }
  327. } else {
  328. if (parent.children.length > 0){
  329. pre = parent.lastChild();
  330. pre.setNextSibling(node);
  331. }
  332. parent.children.push(node);
  333. }
  334. node.setParent(parent);
  335. node.setNextSibling(nextSibling);
  336. //sort items
  337. sortTreeItems(this);
  338. }
  339. } else {
  340. this.e.throw('Error: information of moving node has mistake.');
  341. }
  342. };
  343. Tree.prototype.insert = function (sheet, items, node) {
  344. const rIdx = items.indexOf(node);
  345. const cIdx = sheet.getActiveColumnIndex();
  346. sheet.addRows(rIdx, 1);
  347. //set selection selected
  348. sheet.setSelection(rIdx, cIdx, 1, 1);
  349. this.selected = node;
  350. sheet.getCell(rIdx, 0).cellType(this);
  351. };
  352. Tree.prototype.check = function (_root) {
  353. if (this.sourceData.length !== this.items.length) {
  354. const data = this.sourceData.filter(item => {
  355. const findData = this.items.find(node => node.data.ID === item.ID);
  356. return !findData;
  357. });
  358. console.log('丢失数据:');
  359. console.log(data);
  360. return false;
  361. }
  362. return isValid(_root.children);
  363. function isValid(nodes) {
  364. for (let node of nodes) {
  365. if (node.data.ParentID !== -1 &&
  366. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  367. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  368. return false;
  369. }
  370. if (node.data.ParentID === -1 && node.parent && node.parent !== _root) {
  371. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  372. return false;
  373. }
  374. if (node.data.NextSiblingID !== -1 &&
  375. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  376. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  377. return false;
  378. }
  379. if (node.data.NextSiblingID === -1 && node.nextSibling) {
  380. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  381. return false;
  382. }
  383. let parent = node.parent,
  384. nodeIdx = parent.children.indexOf(node),
  385. nextIdx = parent.children.indexOf(node.nextSibling);
  386. if (nodeIdx !== -1 && nextIdx !== -1 && nodeIdx > nextIdx) {
  387. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  388. return false;
  389. }
  390. if (node.nextSibling && node.parent !== node.nextSibling.parent) {
  391. console.log(`${node.serialNo() + 1}:${node.data.name} 与兄弟节点 ${node.nextSibling.serialNo() + 1}:${node.nextSibling.data.name} 父节点不同`);
  392. return false;
  393. }
  394. if (node.children.length) {
  395. let v = isValid(node.children);
  396. if (!v) {
  397. return false;
  398. }
  399. }
  400. }
  401. return true;
  402. }
  403. };
  404. Tree.prototype.refreshData = function () {
  405. };
  406. return Tree;
  407. })()
  408. let tree = new Tree(setting);
  409. tree.loadData(arrData);
  410. return tree;
  411. }
  412. };