pm_tree.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. };
  227. Tree.prototype.loadData = function (arrData) {
  228. this.sourceData = arrData;
  229. let i, that = this;
  230. let nodesIndex = {};
  231. function getPreNode(id){
  232. for(let index in nodesIndex){
  233. let node = nodesIndex[index];
  234. if(node['data'][node.setting.tree.nid] === id){
  235. return node;
  236. }
  237. }
  238. return null;
  239. }
  240. function loadNode (data, setting) {//mark
  241. var node = nodesIndex[data[setting.tree.id]] || null,
  242. parent = nodesIndex[data[setting.tree.pid]] || that._root,
  243. next = nodesIndex[data[setting.tree.nid]] || null,
  244. pre = getPreNode(data[setting.tree.id]) || null,
  245. tempData;
  246. if (!node) {
  247. node = new Node(that, data);
  248. }
  249. that.maxNodeId(node.id());
  250. if(parent.childIndex(node) === -1){
  251. if (!pre) {
  252. parent.children.unshift(node);
  253. }
  254. else if(pre && parent.childIndex(pre) !== -1){
  255. parent.children.splice(parent.childIndex(pre) + 1, 0, node);
  256. }
  257. else if(next && parent.childIndex(next) !== -1){
  258. parent.children.splice(parent.childIndex(next), 0, node);
  259. }
  260. else {
  261. parent.children.push(node);
  262. }
  263. }
  264. if(pre && parent.childIndex(pre) === -1){
  265. parent.children.splice(parent.childIndex(node), 0, pre);
  266. }
  267. if(next && parent.childIndex(next) === -1){
  268. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  269. }
  270. if(pre && parent.childIndex(pre) !== parent.childIndex(node) - 1){
  271. parent.children.splice(parent.childIndex(pre), 1);
  272. parent.children.splice(parent.childIndex(node), 0, pre);
  273. }
  274. if(next && parent.childIndex(next) !== parent.childIndex(node) + 1){
  275. parent.children.splice(parent.childIndex(next), 1);
  276. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  277. }
  278. node.parent = parent;
  279. node.nextSibling = next;
  280. }
  281. //建立索引
  282. for(let data of arrData){
  283. nodesIndex[data.ID] = new Node(that, data);
  284. }
  285. for (i = 0; i < arrData.length; i++){
  286. loadNode(arrData[i], this.setting);
  287. }
  288. //set items
  289. sortTreeItems(this);
  290. };
  291. Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
  292. var node = null;
  293. var pNode = parent ? parent : this._root;
  294. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  295. node = new Node(this, data, nodeState);
  296. this.maxNodeId(data[this.setting.tree.id]);
  297. this.move(node, pNode, nextSibling);
  298. }
  299. return node;
  300. };
  301. Tree.prototype.move = function(node, parent, nextSibling) {
  302. var iIndex = -1, pre;
  303. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  304. if (node) {
  305. if (node.parent) {
  306. iIndex = node.parent.childIndex(node);
  307. if (iIndex > 0) {
  308. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  309. }
  310. node.parent.children.splice(iIndex, 1);
  311. }
  312. if (nextSibling) {
  313. iIndex = parent.childIndex(nextSibling);
  314. if (iIndex > 0){
  315. pre = parent.children[iIndex - 1];
  316. pre.setNextSibling(node);
  317. //parent.children.splice(iIndex - 1, 0, node);
  318. parent.children.splice(iIndex, 0, node);
  319. } else {
  320. parent.children.splice(0, 0, node);
  321. }
  322. } else {
  323. if (parent.children.length > 0){
  324. pre = parent.lastChild();
  325. pre.setNextSibling(node);
  326. }
  327. parent.children.push(node);
  328. }
  329. node.setParent(parent);
  330. node.setNextSibling(nextSibling);
  331. //sort items
  332. sortTreeItems(this);
  333. }
  334. } else {
  335. this.e.throw('Error: information of moving node has mistake.');
  336. }
  337. };
  338. Tree.prototype.insert = function (sheet, items, node) {
  339. const rIdx = items.indexOf(node);
  340. const cIdx = sheet.getActiveColumnIndex();
  341. sheet.addRows(rIdx, 1);
  342. //set selection selected
  343. sheet.setSelection(rIdx, cIdx, 1, 1);
  344. this.selected = node;
  345. sheet.getCell(rIdx, 0).cellType(this);
  346. };
  347. Tree.prototype.check = function (_root) {
  348. if (this.sourceData.length !== this.items.length) {
  349. const data = this.sourceData.filter(item => {
  350. const findData = this.items.find(node => node.data.ID === item.ID);
  351. return !findData;
  352. });
  353. console.log('丢失数据:');
  354. console.log(data);
  355. return false;
  356. }
  357. return isValid(_root.children);
  358. function isValid(nodes) {
  359. for (let node of nodes) {
  360. if (node.data.ParentID !== -1 &&
  361. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  362. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  363. return false;
  364. }
  365. if (node.data.ParentID === -1 && node.parent && node.parent !== _root) {
  366. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  367. return false;
  368. }
  369. if (node.data.NextSiblingID !== -1 &&
  370. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  371. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  372. return false;
  373. }
  374. if (node.data.NextSiblingID === -1 && node.nextSibling) {
  375. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  376. return false;
  377. }
  378. let parent = node.parent,
  379. nodeIdx = parent.children.indexOf(node),
  380. nextIdx = parent.children.indexOf(node.nextSibling);
  381. if (nodeIdx !== -1 && nextIdx !== -1 && nodeIdx > nextIdx) {
  382. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  383. return false;
  384. }
  385. if (node.nextSibling && node.parent !== node.nextSibling.parent) {
  386. console.log(`${node.serialNo() + 1}:${node.data.name} 与兄弟节点 ${node.nextSibling.serialNo() + 1}:${node.nextSibling.data.name} 父节点不同`);
  387. return false;
  388. }
  389. if (node.children.length) {
  390. let v = isValid(node.children);
  391. if (!v) {
  392. return false;
  393. }
  394. }
  395. }
  396. return true;
  397. }
  398. };
  399. Tree.prototype.refreshData = function () {
  400. };
  401. return Tree;
  402. })()
  403. let tree = new Tree(setting);
  404. tree.loadData(arrData);
  405. return tree;
  406. }
  407. };