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