id_tree.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /**
  2. * Created by Mai on 2017/3/17.
  3. */
  4. var idTree = {
  5. createNew: function (setting) {
  6. var _setting = {
  7. id: 'id',
  8. pid: 'pid',
  9. nid: 'nid',
  10. rootId: -1,
  11. autoUpdate: false
  12. };
  13. var _eventType = {
  14. editedData: 'editedData'
  15. };
  16. var tools = {
  17. findNode: function (nodes, check) {
  18. for (var i = 0; i < nodes.length; i++) {
  19. if (check(nodes[i])) {
  20. return nodes[i];
  21. }
  22. }
  23. return null;
  24. },
  25. reSortNodes: function (nodes, recursive) {
  26. var temp = [], first;
  27. var findFirstNode = function (nodes) {
  28. return tools.findNode(nodes, function (node) {
  29. return node.preSibling === null;
  30. });
  31. };
  32. var moveNode = function (node, orgArray, newArray, newIndex) {
  33. var next;
  34. orgArray.splice(orgArray.indexOf(node), 1);
  35. newArray.splice(newIndex, 0, node);
  36. if (node.getNextSiblingID() !== -1) {
  37. next = node.nextSibling;
  38. if (next && (orgArray.indexOf(next) >= 0)) {
  39. moveNode(next, orgArray, newArray, newIndex + 1);
  40. }
  41. }
  42. };
  43. if (nodes.length === 0) {
  44. return nodes;
  45. }
  46. if (recursive) {
  47. nodes.forEach(function (node) {
  48. node.children = tools.reSortNodes(node.children, recursive);
  49. });
  50. }
  51. while (nodes.length > 0) {
  52. first = findFirstNode(nodes);
  53. first = first ? first : nodes[0];
  54. moveNode(first, nodes, temp, temp.length);
  55. }
  56. nodes = null;
  57. tools.reSiblingNodes(temp);
  58. return temp;
  59. },
  60. reSiblingNodes: function (nodes) {
  61. var i;
  62. for (i = 0; i < nodes.length; i++) {
  63. nodes[i].preSibling = (i === 0) ? null : nodes[i - 1];
  64. nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
  65. }
  66. },
  67. // 在nodes中,从iIndex(包括)开始全部移除
  68. removeNodes: function (tree, parent, iIndex, count) {
  69. var children = parent ? parent.children : tree.roots;
  70. var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
  71. var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
  72. if (pre) {
  73. pre.setNextSibling(next);
  74. } else if (next) {
  75. next.preSibling = null;
  76. }
  77. if (arguments.length === 4) {
  78. children.splice(iIndex, count);
  79. } else {
  80. children.splice(iIndex, children.length - iIndex);
  81. }
  82. },
  83. // 在parent.children/tree.roots中增加nodes, 位置从index开始
  84. addNodes: function (tree, parent, nodes, iIndex) {
  85. var children = parent ? parent.children : tree.roots;
  86. var pre, next, i;
  87. if (nodes.length === 0) { return; }
  88. if (arguments.length === 4) {
  89. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex-1];
  90. if(pre==null){
  91. next = iIndex==0?children[0]:null;
  92. }else {
  93. next = pre.nextSibling;
  94. }
  95. } else if (arguments.length === 3) {
  96. pre = children.length === 0 ? null : children[children.length - 1];
  97. next = null;
  98. }
  99. if (pre) {
  100. pre.setNextSibling(nodes[0]);
  101. } else {
  102. nodes[0].preSibling = null;
  103. }
  104. nodes[nodes.length - 1].setNextSibling(next);
  105. for (i = 0; i < nodes.length; i++) {
  106. if (arguments.length === 4) {
  107. children.splice(iIndex + i, 0, nodes[i]);
  108. } else if (arguments.length === 3) {
  109. children.push(nodes[i]);
  110. }
  111. nodes[i].setParent(parent ? parent : null);
  112. }
  113. },
  114. sortTreeItems: function (tree) {
  115. var addItems = function (items) {
  116. var i;
  117. for (i = 0; i < items.length; i++) {
  118. tree.items.push(items[i]);
  119. addItems(items[i].children);
  120. }
  121. };
  122. tree.items.splice(0, tree.items.length);
  123. addItems(tree.roots);
  124. },
  125. addUpdateDataForParent: function (datas, nodes, pid) {
  126. nodes.forEach(function (node) {
  127. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), pid, node.getNextSiblingID())});
  128. });
  129. },
  130. addUpdateDataForNextSibling: function (datas, node, nid) {
  131. if (node) {
  132. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), node.getParentID(), nid)});
  133. }
  134. }
  135. };
  136. var Node = function (tree, data) {
  137. // 以下的属性,本单元外均不可直接修改
  138. this.tree = tree;
  139. this.data = data;
  140. this.children = [];
  141. this.parent = null;
  142. this.nextSibling = null;
  143. this.preSibling = null;
  144. this.expanded = true;
  145. this.visible = true;
  146. this.visible = true;
  147. };
  148. Node.prototype.getID = function () {
  149. return this.data[this.tree.setting.id];
  150. };
  151. Node.prototype.getParentID = function () {
  152. return this.parent ? this.parent.getID() : -1;
  153. };
  154. Node.prototype.getNextSiblingID = function () {
  155. return this.nextSibling ? this.nextSibling.getID() : -1;
  156. };
  157. Node.prototype.setParent = function (parent) {
  158. this.parent = parent;
  159. if (this.tree.setting.autoUpdate) {
  160. this.data[this.tree.setting.pid] = this.getParentID();
  161. }
  162. };
  163. Node.prototype.setNextSibling = function (nextSibling) {
  164. this.nextSibling = nextSibling;
  165. if (nextSibling) {
  166. nextSibling.preSibling = this;
  167. }
  168. if (this.tree.setting.autoUpdate) {
  169. this.data[this.tree.setting.nid] = this.getNextSiblingID();
  170. }
  171. }
  172. Node.prototype.firstChild = function () {
  173. return this.children.length === 0 ? null : this.children[0];
  174. };
  175. Node.prototype.lastChild = function () {
  176. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  177. };
  178. Node.prototype.depth = function () {
  179. return this.parent ? this.parent.depth() + 1 : 0;
  180. };
  181. Node.prototype.isFirst = function () {
  182. if (this.parent) {
  183. return this.parent.children.indexOf(this) === 0 ? true : false;
  184. } else {
  185. return this.tree.roots.indexOf(this) === 0 ? true : false;
  186. }
  187. };
  188. Node.prototype.isLast = function () {
  189. if (this.parent) {
  190. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  191. } else {
  192. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  193. }
  194. };
  195. Node.prototype.siblingIndex = function () {
  196. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  197. };
  198. Node.prototype.posterityCount = function () {
  199. var iCount = 0;
  200. if (this.children.length !== 0) {
  201. iCount += this.children.length;
  202. this.children.forEach(function (child) {
  203. iCount += child.posterityCount();
  204. });
  205. }
  206. return iCount;
  207. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  208. return x.posterityCount() + y.posterityCount();
  209. }) + node.children.count;*/
  210. };
  211. Node.prototype.setExpanded = function (expanded) {
  212. var setNodesVisible = function (nodes, visible) {
  213. nodes.forEach(function (node) {
  214. node.visible = visible;
  215. setNodesVisible(node.children, visible && node.expanded);
  216. })
  217. };
  218. this.expanded = expanded;
  219. setNodesVisible(this.children, expanded);
  220. };
  221. Node.prototype.setExpandedNoRecur = function (expanded) {
  222. this.expanded = expanded;
  223. this.visible = expanded && this.visible;
  224. for(let node of this.children){
  225. node.visible = expanded && node.visible;
  226. }
  227. };
  228. /*Node.prototype.vis = function () {
  229. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  230. };*/
  231. Node.prototype.serialNo = function () {
  232. return this.tree.items.indexOf(this);
  233. };
  234. Node.prototype.addChild = function (node) {
  235. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  236. node.parent = this;
  237. if (preSibling) {
  238. preSibling.nextSibling = node;
  239. }
  240. node.preSibling = preSibling;
  241. this.children.push(node);
  242. };
  243. Node.prototype.removeChild = function (node) {
  244. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  245. if (preSibling) {
  246. preSibling.nextSibling = nextSibling;
  247. }
  248. if (nextSibling) {
  249. nextSibling.preSibling = preSibling;
  250. }
  251. this.children.splice(node.siblingIndex, 1);
  252. };
  253. Node.prototype.canUpLevel = function () {
  254. return this.parent ? true : false;
  255. };
  256. Node.prototype.getUpLevelData = function () {
  257. var data = [];
  258. if (this.canUpLevel()) {
  259. if (!this.isLast()) {
  260. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  261. }
  262. if (this.preSibling) {
  263. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  264. }
  265. if (this.lastChild() && this.nextSibling) {
  266. tools.addUpdateDataForNextSibling(data, this.lastChild(), this.getNextSiblingID());
  267. }
  268. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  269. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
  270. }
  271. return data;
  272. };
  273. Node.prototype.upLevel = function () {
  274. var result = {success: false, updateDatas: []};
  275. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  276. if (this.canUpLevel) {
  277. // NextSiblings become child
  278. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  279. // Orginal Parent remove node and nextSiblings
  280. tools.removeNodes(this.tree, this.parent, iIndex);
  281. // New Parent add node
  282. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  283. if (!this.expanded) {
  284. this.setExpanded(true);
  285. }
  286. result.success = true;
  287. }
  288. return result;
  289. };
  290. Node.prototype.canDownLevel = function () {
  291. return !this.isFirst();
  292. };
  293. Node.prototype.getDownLevelData = function () {
  294. var data = [];
  295. if (this.canDownLevel()) {
  296. if (this.preSibling.children.length !== 0) {
  297. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  298. }
  299. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  300. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  301. }
  302. return data;
  303. };
  304. Node.prototype.downLevel = function () {
  305. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  306. var newParent = this.preSibling;
  307. if (this.canDownLevel()) {
  308. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  309. tools.addNodes(this.tree, this.preSibling, [this]);
  310. if (!newParent.expanded) {
  311. newParent.setExpanded(true);
  312. }
  313. success = true;
  314. }
  315. return success;
  316. };
  317. Node.prototype.canUpMove = function () {
  318. return !this.isFirst();
  319. };
  320. Node.prototype.getUpMoveData = function () {
  321. var data = [];
  322. if (this.canUpMove()) {
  323. if (this.preSibling.preSibling) {
  324. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  325. }
  326. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  327. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  328. }
  329. return data;
  330. };
  331. Node.prototype.upMove = function () {
  332. var success = false;
  333. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  334. if (this.canUpMove()) {
  335. if (orgPre.preSibling) {
  336. orgPre.preSibling.setNextSibling(this);
  337. } else {
  338. this.preSibling = null;
  339. }
  340. orgPre.setNextSibling(this.nextSibling);
  341. this.setNextSibling(orgPre);
  342. belongArray.splice(iIndex, 1);
  343. belongArray.splice(iIndex - 1, 0, this);
  344. tools.sortTreeItems(this.tree);
  345. success = true;
  346. }
  347. return success;
  348. };
  349. Node.prototype.canDownMove = function () {
  350. return !this.isLast();
  351. };
  352. Node.prototype.getDownMoveData = function () {
  353. var data = [];
  354. if (this.canDownMove()) {
  355. if (this.preSibling) {
  356. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  357. }
  358. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  359. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  360. }
  361. return data;
  362. };
  363. Node.prototype.downMove = function () {
  364. var success = false;
  365. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  366. if (this.canDownMove()) {
  367. if (this.preSibling) {
  368. this.preSibling.setNextSibling(orgNext);
  369. } else if (orgNext) {
  370. orgNext.preSibling = null;
  371. }
  372. this.setNextSibling(orgNext.nextSibling);
  373. orgNext.setNextSibling(this);
  374. belongArray.splice(iIndex, 1);
  375. belongArray.splice(iIndex + 1, 0, this);
  376. tools.sortTreeItems(this.tree);
  377. success = true;
  378. }
  379. return success;
  380. };
  381. // 节点所属固定ID
  382. Node.prototype.getFlag = function() {
  383. if (!this.data || !this.data.flags || !this.data.flags[0] || ! this.data.flags[0].flag) {
  384. return 0;
  385. }
  386. return this.data.flags[0].flag;
  387. };
  388. // 节点所属固定ID
  389. Node.prototype.belongToFlag = function () {
  390. let node = this;
  391. while (node) {
  392. if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) {
  393. return node.data.flags[0].flag;
  394. }
  395. node = node.parent;
  396. }
  397. return null;
  398. };
  399. // 节点是否属于某些固定ID,会一直向上找,直到找到或到达顶层
  400. Node.prototype.isBelongToFlags = function (flags) {
  401. let node = this;
  402. while (node) {
  403. const flag = node.getFlag();
  404. if (flags.includes(flag)) {
  405. return true;
  406. }
  407. node = node.parent;
  408. }
  409. return false;
  410. }
  411. // 获取节点所有后代节点
  412. Node.prototype.getPosterity = function() {
  413. let posterity = [];
  414. getNodes(this.children);
  415. return posterity;
  416. function getNodes(nodes) {
  417. for (let node of nodes) {
  418. posterity.push(node);
  419. if (node.children.length > 0){
  420. getNodes(node.children);
  421. }
  422. }
  423. }
  424. };
  425. var Tree = function (setting) {
  426. this.nodes = {};
  427. this.roots = [];
  428. this.items = [];
  429. this.setting = setting;
  430. this.prefix = 'id_';
  431. this.selected = null;
  432. this.event = {};
  433. this.eventType = _eventType;
  434. };
  435. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  436. var data = {};
  437. data[this.setting.id] = id;
  438. data[this.setting.pid] = pid;
  439. data[this.setting.nid] = nid;
  440. return data;
  441. };
  442. Tree.prototype.maxNodeID = (function () {
  443. var maxID = 0;
  444. return function (ID) {
  445. if (arguments.length === 0) {
  446. return maxID;
  447. } else {
  448. maxID = Math.max(maxID, ID);
  449. }
  450. };
  451. })();
  452. Tree.prototype.rangeNodeID = (function () {
  453. var rangeID = -1;
  454. return function (ID) {
  455. if (arguments.length === 0) {
  456. return rangeID;
  457. } else {
  458. rangeID = Math.max(rangeID, ID);
  459. }
  460. }
  461. })();
  462. Tree.prototype.newNodeID = function () {
  463. if (this.rangeNodeID() == -1) {
  464. return this.maxNodeID() + 1;
  465. } else {
  466. if (this.maxNodeID() < this.rangeNodeID()) {
  467. return this.maxNodeID() + 1;
  468. } else {
  469. return -1;
  470. }
  471. }
  472. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  473. return -1;
  474. } else {
  475. return this.maxNodeID() + 1;
  476. }*/
  477. };
  478. Tree.prototype.clearNodes = function () {
  479. this.nodes = {};
  480. this.roots = [];
  481. this.items = [];
  482. };
  483. Tree.prototype.loadDatas = function (datas) {
  484. var prefix = this.prefix, i, node, parent, next, that = this;
  485. this.nodes = {};
  486. this.roots = [];
  487. this.items = [];
  488. // prepare index
  489. datas.forEach(function (data) {
  490. var node = new Node(that, data);
  491. that.nodes[prefix + data[that.setting.id]] = node;
  492. that.maxNodeID(data[that.setting.id]);
  493. });
  494. // set parent by pid, set nextSibling by nid
  495. datas.forEach(function (data) {
  496. node = that.nodes[prefix + data[that.setting.id]];
  497. if (data[that.setting.pid] == that.setting.rootId) {
  498. that.roots.push(node);
  499. } else {
  500. parent = that.nodes[prefix + data[that.setting.pid]];
  501. if (parent) {
  502. node.parent = parent;
  503. parent.children.push(node);
  504. }
  505. }
  506. if (data[that.setting.nid] !== that.setting.rootId) {
  507. next = that.nodes[prefix + data[that.setting.nid]];
  508. if (next) {
  509. node.nextSibling = next;
  510. next.preSibling = node;
  511. }
  512. }
  513. })
  514. // sort by nid
  515. this.roots = tools.reSortNodes(this.roots, true);
  516. tools.sortTreeItems(this);
  517. };
  518. Tree.prototype.firstNode = function () {
  519. return this.roots.length === 0 ? null : this.roots[0];
  520. };
  521. Tree.prototype.findNode = function (id) {
  522. return this.nodes[this.prefix + id];
  523. };
  524. Tree.prototype.count = function () {
  525. var iCount = 0;
  526. if (this.roots.length !== 0) {
  527. iCount += this.roots.length;
  528. this.roots.forEach(function (node) {
  529. iCount += node.posterityCount();
  530. });
  531. }
  532. return iCount;
  533. };
  534. Tree.prototype.insert = function (parentID, nextSiblingID) {
  535. var newID = this.newNodeID(), node = null, data = {};
  536. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  537. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  538. if (newID !== -1) {
  539. data = {};
  540. data[this.setting.id] = newID;
  541. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  542. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  543. node = new Node(this, data);
  544. if (nextSibling) {
  545. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  546. } else {
  547. tools.addNodes(this, parent, [node]);
  548. }
  549. this.nodes[this.prefix + newID] = node;
  550. tools.sortTreeItems(this);
  551. this.maxNodeID(newID);
  552. }
  553. return node;
  554. };
  555. Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) {
  556. var node = null, data = {};
  557. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  558. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  559. if (newID) {
  560. data = {};
  561. data[this.setting.id] = newID;
  562. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  563. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  564. node = new Node(this, data);
  565. if (nextSibling) {
  566. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  567. } else {
  568. tools.addNodes(this, parent, [node]);
  569. }
  570. this.nodes[this.prefix + newID] = node;
  571. tools.sortTreeItems(this);
  572. }
  573. return node;
  574. };
  575. Tree.prototype.getInsertData = function (parentID, nextSiblingID, uid = null) {
  576. var data = [];
  577. var newID = uid ? uuid.v1() : this.newNodeID();
  578. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  579. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  580. if (newID != -1) {
  581. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  582. if (nextSibling && nextSibling.preSibling) {
  583. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  584. } else if (parent && parent.children.length !== 0) {
  585. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  586. } else if (!parent && this.roots.length !== 0) {
  587. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  588. }
  589. }
  590. return data;
  591. };
  592. Tree.prototype.insertByData = function (data, parentID, nextSiblingID, uid = null, resort = true) {
  593. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  594. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  595. var node = this.nodes[this.prefix + data[this.setting.id]];
  596. if (node) {
  597. return node;
  598. } else {
  599. node = new Node(this, data);
  600. if (nextSibling) {
  601. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  602. } else {
  603. tools.addNodes(this, parent, [node]);
  604. }
  605. this.nodes[this.prefix + data[this.setting.id]] = node;
  606. if (resort) {
  607. tools.sortTreeItems(this);
  608. }
  609. if(!uid){
  610. this.maxNodeID( data[this.setting.id]);
  611. }
  612. return node;
  613. }
  614. };
  615. // 一次性插入多个连续的同层节点
  616. Tree.prototype.multiInsert = function (datas, preID) {
  617. const newNodes = [];
  618. for (const data of datas) {
  619. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  620. this.nodes[this.prefix + data.ID]['data'] = data;
  621. newNodes.push(this.nodes[this.prefix + data.ID]);
  622. }
  623. const fisrtNode = this.nodes[this.prefix + datas[0].ID];
  624. const firstPre = this.nodes[this.prefix + preID];
  625. if (firstPre) {
  626. firstPre.nextSibling = fisrtNode;
  627. firstPre.data.NextSiblingID = fisrtNode.getID();
  628. }
  629. const parent = this.nodes[this.prefix + datas[0].ParentID] || null;
  630. const parentChildren = parent ? parent.children : this.roots;
  631. let baseIndex = firstPre ? parentChildren.indexOf(firstPre) + 1 : 0;
  632. datas.forEach(data => {
  633. const node = this.nodes[this.prefix + data.ID];
  634. node.parent = parent;
  635. parentChildren.splice(baseIndex++, 0, node);
  636. const next = this.nodes[this.prefix + data.NextSiblingID] || null;
  637. node.nextSibling = next;
  638. if (next) {
  639. next.preSibling = node;
  640. }
  641. });
  642. this.roots = tools.reSortNodes(this.roots, true);
  643. tools.sortTreeItems(this);
  644. return newNodes;
  645. }
  646. // 插入某一完整片段到某节点的子项中
  647. Tree.prototype.insertByDatas = function (datas) {
  648. for(let data of datas){
  649. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  650. this.nodes[this.prefix + data.ID]['data'] = data;
  651. }
  652. for(let data of datas){
  653. let node = this.nodes[this.prefix + data.ID];
  654. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  655. node.parent = parent;
  656. if(!parent){
  657. this.roots.push(node);
  658. }
  659. else {
  660. parent.children.push(node);
  661. }
  662. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  663. node.nextSibling = next;
  664. if(next){
  665. next.preSibling = node;
  666. }
  667. }
  668. //resort
  669. this.roots = tools.reSortNodes(this.roots, true);
  670. tools.sortTreeItems(this);
  671. };
  672. Tree.prototype.delete = function (node) {
  673. var success = false;
  674. success=this.cascadeRemove(node);
  675. tools.sortTreeItems(this);
  676. return success;
  677. };
  678. Tree.prototype.m_delete=function(nodes){
  679. for(let node of nodes){
  680. this.cascadeRemove(node);
  681. }
  682. tools.sortTreeItems(this);
  683. return true;
  684. };
  685. Tree.prototype.cascadeRemove = function (node){
  686. var success = false, that = this;
  687. var deleteIdIndex = function (nodes) {
  688. nodes.forEach(function (node) {
  689. delete that.nodes[that.prefix + node.getID()];
  690. deleteIdIndex(node.children);
  691. })
  692. }
  693. if (node) {
  694. deleteIdIndex([node]);
  695. //delete this.nodes[this.prefix + node.getID()];
  696. if (node.preSibling) {
  697. node.preSibling.setNextSibling(node.nextSibling);
  698. } else if (node.nextSibling) {
  699. node.nextSibling.preSibling = null;
  700. }
  701. if (node.parent) {
  702. node.parent.children.splice(node.siblingIndex(), 1);
  703. } else {
  704. this.roots.splice(node.siblingIndex(), 1);
  705. }
  706. success = true;
  707. }
  708. return success;
  709. };
  710. Tree.prototype.singleDelete = function (node) {//删除本身不删除子项
  711. let that = this;
  712. let success = false;
  713. delete that.nodes[that.prefix + node.getID()];//删除本身
  714. if (node.parent) {//从父项的子节点中移除
  715. node.parent.children.splice(node.siblingIndex(), 1);
  716. } else {
  717. this.roots.splice(node.siblingIndex(), 1);
  718. }
  719. if(node.preSibling) node.preSibling.setNextSibling(node.nextSibling);//后兄弟设置为前兄弟的后兄弟
  720. if(node.children.length>0){
  721. if(node.preSibling){//子项变成前兄弟的子项
  722. for(let c of node.children){
  723. node.preSibling.addChild(c);
  724. }
  725. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  726. let oldChild = node.parent.children;
  727. node.parent.children = [];
  728. for(let c of node.children){
  729. node.parent.addChild(c);
  730. }
  731. for(let oc of oldChild){
  732. node.parent.addChild(oc);
  733. }
  734. }else {//都没有的情况
  735. for(let c of node.children){
  736. node.parent.addChild(c);
  737. }
  738. }
  739. tools.sortTreeItems(this);
  740. success = true;
  741. }
  742. return success;
  743. };
  744. Tree.prototype.getDeleteData = function (node) {
  745. var data = [];
  746. var addUpdateDataForDelete = function (datas, nodes) {
  747. nodes.forEach(function (node) {
  748. var delData = {};
  749. delData[node.tree.setting.id] = node.getID();
  750. datas.push({type: 'delete', data: delData});
  751. addUpdateDataForDelete(datas, node.children);
  752. })
  753. };
  754. if (node) {
  755. addUpdateDataForDelete(data, [node]);
  756. if (node.preSibling) {
  757. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  758. }
  759. }
  760. return data;
  761. };
  762. //非叶子节点默认收起展开
  763. Tree.prototype.setRootExpanded = function(nodes, expanded){
  764. for(let node of nodes){
  765. if(node.children.length > 0){
  766. node.setExpanded(expanded);
  767. this.setRootExpanded(node.children, expanded);
  768. }
  769. }
  770. };
  771. Tree.prototype.getExpState = function (nodes) {
  772. let sessionExpanded = [];
  773. function getStat(items){
  774. for(let item of items){
  775. sessionExpanded.push(item.expanded ? 1 : 0);
  776. }
  777. }
  778. getStat(nodes);
  779. let expState = sessionExpanded.join('');
  780. return expState;
  781. };
  782. //节点根据展开收起列表'010101'展开收起
  783. Tree.prototype.setExpandedByState = function (nodes, expState) {
  784. let expStateArr = expState.split('');
  785. for(let i = 0; i < nodes.length; i++){
  786. let expanded = expStateArr[i] == 1 ? true : false;
  787. if(nodes[i].expanded === expanded){
  788. continue;
  789. }
  790. nodes[i].setExpanded(expanded);
  791. }
  792. };
  793. /*Tree.prototype.editedData = function (field, id, newText) {
  794. var node = this.findNode(id), result = {allow: false, nodes: []};
  795. if (this.event[this.eventType.editedData]) {
  796. return this.event[this.eventType.editedData](field, node.data);
  797. } else {
  798. node.data[field] = newText;
  799. result.allow = true;
  800. return result;
  801. }
  802. };*/
  803. Tree.prototype.bind = function (eventName, eventFun) {
  804. this.event[eventName] = eventFun;
  805. };
  806. Tree.prototype.getNodeByID = function (ID) {
  807. let node = this.nodes[this.prefix+ID];
  808. return node;
  809. };
  810. //检查树结构数据有没问题
  811. Tree.prototype.check = function (roots) {
  812. return isValid(roots);
  813. function isValid(nodes) {
  814. for (let node of nodes) {
  815. if (node.data.ParentID != -1 &&
  816. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  817. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  818. return false;
  819. }
  820. if (node.data.ParentID == -1 && node.parent) {
  821. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  822. return false;
  823. }
  824. if (node.data.NextSiblingID != -1 &&
  825. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  826. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  827. return false;
  828. }
  829. if (node.data.NextSiblingID == -1 && node.nextSibling) {
  830. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  831. return false;
  832. }
  833. let sameDepthNodes = node.parent ? node.parent.children : roots,
  834. nodeIdx = sameDepthNodes.indexOf(node),
  835. nextIdx = sameDepthNodes.indexOf(node.nextSibling);
  836. if (nodeIdx != -1 && nextIdx != -1 && nodeIdx > nextIdx) {
  837. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  838. return false;
  839. }
  840. // nextSibling跟parent children的下一节点对应不上
  841. if (nodeIdx != -1 &&
  842. (nodeIdx === sameDepthNodes.length - 1 && nextIdx != -1) ||
  843. (nodeIdx !== sameDepthNodes.length - 1 && nodeIdx + 1 !== nextIdx)) {
  844. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  845. return false;
  846. }
  847. if (node.children.length) {
  848. let v = isValid(node.children);
  849. if (!v) {
  850. return false;
  851. }
  852. }
  853. }
  854. return true;
  855. }
  856. };
  857. return new Tree(setting);
  858. },
  859. updateType: {update: 'update', new: 'new', delete: 'delete'}
  860. };