id_tree.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  266. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
  267. }
  268. return data;
  269. };
  270. Node.prototype.upLevel = function () {
  271. var result = {success: false, updateDatas: []};
  272. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  273. if (this.canUpLevel) {
  274. // NextSiblings become child
  275. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  276. // Orginal Parent remove node and nextSiblings
  277. tools.removeNodes(this.tree, this.parent, iIndex);
  278. // New Parent add node
  279. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  280. if (!this.expanded) {
  281. this.setExpanded(true);
  282. }
  283. result.success = true;
  284. }
  285. return result;
  286. };
  287. Node.prototype.canDownLevel = function () {
  288. return !this.isFirst();
  289. };
  290. Node.prototype.getDownLevelData = function () {
  291. var data = [];
  292. if (this.canDownLevel()) {
  293. if (this.preSibling.children.length !== 0) {
  294. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  295. }
  296. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  297. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  298. }
  299. return data;
  300. };
  301. Node.prototype.downLevel = function () {
  302. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  303. var newParent = this.preSibling;
  304. if (this.canDownLevel()) {
  305. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  306. tools.addNodes(this.tree, this.preSibling, [this]);
  307. if (!newParent.expanded) {
  308. newParent.setExpanded(true);
  309. }
  310. success = true;
  311. }
  312. return success;
  313. };
  314. Node.prototype.canUpMove = function () {
  315. return !this.isFirst();
  316. };
  317. Node.prototype.getUpMoveData = function () {
  318. var data = [];
  319. if (this.canUpMove()) {
  320. if (this.preSibling.preSibling) {
  321. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  322. }
  323. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  324. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  325. }
  326. return data;
  327. };
  328. Node.prototype.upMove = function () {
  329. var success = false;
  330. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  331. if (this.canUpMove()) {
  332. if (orgPre.preSibling) {
  333. orgPre.preSibling.setNextSibling(this);
  334. } else {
  335. this.preSibling = null;
  336. }
  337. orgPre.setNextSibling(this.nextSibling);
  338. this.setNextSibling(orgPre);
  339. belongArray.splice(iIndex, 1);
  340. belongArray.splice(iIndex - 1, 0, this);
  341. tools.sortTreeItems(this.tree);
  342. success = true;
  343. }
  344. return success;
  345. };
  346. Node.prototype.canDownMove = function () {
  347. return !this.isLast();
  348. };
  349. Node.prototype.getDownMoveData = function () {
  350. var data = [];
  351. if (this.canDownMove()) {
  352. if (this.preSibling) {
  353. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  354. }
  355. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  356. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  357. }
  358. return data;
  359. };
  360. Node.prototype.downMove = function () {
  361. var success = false;
  362. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  363. if (this.canDownMove()) {
  364. if (this.preSibling) {
  365. this.preSibling.setNextSibling(orgNext);
  366. } else if (orgNext) {
  367. orgNext.preSibling = null;
  368. }
  369. this.setNextSibling(orgNext.nextSibling);
  370. orgNext.setNextSibling(this);
  371. belongArray.splice(iIndex, 1);
  372. belongArray.splice(iIndex + 1, 0, this);
  373. tools.sortTreeItems(this.tree);
  374. success = true;
  375. }
  376. return success;
  377. };
  378. // 节点所属固定ID
  379. Node.prototype.getFlag = function() {
  380. if (!this.data || !this.data.flags || !this.data.flags[0] || ! this.data.flags[0].flag) {
  381. return 0;
  382. }
  383. return this.data.flags[0].flag;
  384. };
  385. // 节点所属固定ID
  386. Node.prototype.belongToFlag = function () {
  387. let node = this;
  388. while (node) {
  389. if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) {
  390. return node.data.flags[0].flag;
  391. }
  392. node = node.parent;
  393. }
  394. return null;
  395. };
  396. // 节点是否属于某些固定ID,会一直向上找,直到找到或到达顶层
  397. Node.prototype.isBelongToFlags = function (flags) {
  398. let node = this;
  399. while (node) {
  400. const flag = node.getFlag();
  401. if (flags.includes(flag)) {
  402. return true;
  403. }
  404. node = node.parent;
  405. }
  406. return false;
  407. }
  408. // 获取节点所有后代节点
  409. Node.prototype.getPosterity = function() {
  410. let posterity = [];
  411. getNodes(this.children);
  412. return posterity;
  413. function getNodes(nodes) {
  414. for (let node of nodes) {
  415. posterity.push(node);
  416. if (node.children.length > 0){
  417. getNodes(node.children);
  418. }
  419. }
  420. }
  421. };
  422. var Tree = function (setting) {
  423. this.nodes = {};
  424. this.roots = [];
  425. this.items = [];
  426. this.setting = setting;
  427. this.prefix = 'id_';
  428. this.selected = null;
  429. this.event = {};
  430. this.eventType = _eventType;
  431. };
  432. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  433. var data = {};
  434. data[this.setting.id] = id;
  435. data[this.setting.pid] = pid;
  436. data[this.setting.nid] = nid;
  437. return data;
  438. };
  439. Tree.prototype.maxNodeID = (function () {
  440. var maxID = 0;
  441. return function (ID) {
  442. if (arguments.length === 0) {
  443. return maxID;
  444. } else {
  445. maxID = Math.max(maxID, ID);
  446. }
  447. };
  448. })();
  449. Tree.prototype.rangeNodeID = (function () {
  450. var rangeID = -1;
  451. return function (ID) {
  452. if (arguments.length === 0) {
  453. return rangeID;
  454. } else {
  455. rangeID = Math.max(rangeID, ID);
  456. }
  457. }
  458. })();
  459. Tree.prototype.newNodeID = function () {
  460. if (this.rangeNodeID() == -1) {
  461. return this.maxNodeID() + 1;
  462. } else {
  463. if (this.maxNodeID() < this.rangeNodeID()) {
  464. return this.maxNodeID() + 1;
  465. } else {
  466. return -1;
  467. }
  468. }
  469. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  470. return -1;
  471. } else {
  472. return this.maxNodeID() + 1;
  473. }*/
  474. };
  475. Tree.prototype.clearNodes = function () {
  476. this.nodes = {};
  477. this.roots = [];
  478. this.items = [];
  479. };
  480. Tree.prototype.loadDatas = function (datas) {
  481. var prefix = this.prefix, i, node, parent, next, that = this;
  482. this.nodes = {};
  483. this.roots = [];
  484. this.items = [];
  485. // prepare index
  486. datas.forEach(function (data) {
  487. var node = new Node(that, data);
  488. that.nodes[prefix + data[that.setting.id]] = node;
  489. that.maxNodeID(data[that.setting.id]);
  490. });
  491. // set parent by pid, set nextSibling by nid
  492. datas.forEach(function (data) {
  493. node = that.nodes[prefix + data[that.setting.id]];
  494. if (data[that.setting.pid] == that.setting.rootId) {
  495. that.roots.push(node);
  496. } else {
  497. parent = that.nodes[prefix + data[that.setting.pid]];
  498. if (parent) {
  499. node.parent = parent;
  500. parent.children.push(node);
  501. }
  502. }
  503. if (data[that.setting.nid] !== that.setting.rootId) {
  504. next = that.nodes[prefix + data[that.setting.nid]];
  505. if (next) {
  506. node.nextSibling = next;
  507. next.preSibling = node;
  508. }
  509. }
  510. })
  511. // sort by nid
  512. this.roots = tools.reSortNodes(this.roots, true);
  513. tools.sortTreeItems(this);
  514. };
  515. Tree.prototype.firstNode = function () {
  516. return this.roots.length === 0 ? null : this.roots[0];
  517. };
  518. Tree.prototype.findNode = function (id) {
  519. return this.nodes[this.prefix + id];
  520. };
  521. Tree.prototype.count = function () {
  522. var iCount = 0;
  523. if (this.roots.length !== 0) {
  524. iCount += this.roots.length;
  525. this.roots.forEach(function (node) {
  526. iCount += node.posterityCount();
  527. });
  528. }
  529. return iCount;
  530. };
  531. Tree.prototype.insert = function (parentID, nextSiblingID) {
  532. var newID = this.newNodeID(), node = null, data = {};
  533. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  534. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  535. if (newID !== -1) {
  536. data = {};
  537. data[this.setting.id] = newID;
  538. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  539. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  540. node = new Node(this, data);
  541. if (nextSibling) {
  542. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  543. } else {
  544. tools.addNodes(this, parent, [node]);
  545. }
  546. this.nodes[this.prefix + newID] = node;
  547. tools.sortTreeItems(this);
  548. this.maxNodeID(newID);
  549. }
  550. return node;
  551. };
  552. Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) {
  553. var node = null, data = {};
  554. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  555. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  556. if (newID) {
  557. data = {};
  558. data[this.setting.id] = newID;
  559. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  560. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  561. node = new Node(this, data);
  562. if (nextSibling) {
  563. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  564. } else {
  565. tools.addNodes(this, parent, [node]);
  566. }
  567. this.nodes[this.prefix + newID] = node;
  568. tools.sortTreeItems(this);
  569. }
  570. return node;
  571. };
  572. Tree.prototype.getInsertData = function (parentID, nextSiblingID, uid = null) {
  573. var data = [];
  574. var newID = uid ? uuid.v1() : this.newNodeID();
  575. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  576. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  577. if (newID != -1) {
  578. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  579. if (nextSibling && nextSibling.preSibling) {
  580. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  581. } else if (parent && parent.children.length !== 0) {
  582. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  583. } else if (!parent && this.roots.length !== 0) {
  584. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  585. }
  586. }
  587. return data;
  588. };
  589. Tree.prototype.insertByData = function (data, parentID, nextSiblingID, uid = null, resort = true) {
  590. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  591. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  592. var node = this.nodes[this.prefix + data[this.setting.id]];
  593. if (node) {
  594. return node;
  595. } else {
  596. node = new Node(this, data);
  597. if (nextSibling) {
  598. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  599. } else {
  600. tools.addNodes(this, parent, [node]);
  601. }
  602. this.nodes[this.prefix + data[this.setting.id]] = node;
  603. if (resort) {
  604. tools.sortTreeItems(this);
  605. }
  606. if(!uid){
  607. this.maxNodeID( data[this.setting.id]);
  608. }
  609. return node;
  610. }
  611. };
  612. // 一次性插入多个连续的同层节点
  613. Tree.prototype.multiInsert = function (datas, preID) {
  614. const newNodes = [];
  615. for (const data of datas) {
  616. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  617. this.nodes[this.prefix + data.ID]['data'] = data;
  618. newNodes.push(this.nodes[this.prefix + data.ID]);
  619. }
  620. const fisrtNode = this.nodes[this.prefix + datas[0].ID];
  621. const firstPre = this.nodes[this.prefix + preID];
  622. if (firstPre) {
  623. firstPre.nextSibling = fisrtNode;
  624. firstPre.data.NextSiblingID = fisrtNode.getID();
  625. }
  626. const parent = this.nodes[this.prefix + datas[0].ParentID] || null;
  627. const parentChildren = parent ? parent.children : this.roots;
  628. let baseIndex = firstPre ? parentChildren.indexOf(firstPre) + 1 : 0;
  629. datas.forEach(data => {
  630. const node = this.nodes[this.prefix + data.ID];
  631. node.parent = parent;
  632. parentChildren.splice(baseIndex++, 0, node);
  633. const next = this.nodes[this.prefix + data.NextSiblingID] || null;
  634. node.nextSibling = next;
  635. if (next) {
  636. next.preSibling = node;
  637. }
  638. });
  639. this.roots = tools.reSortNodes(this.roots, true);
  640. tools.sortTreeItems(this);
  641. return newNodes;
  642. }
  643. // 插入某一完整片段到某节点的子项中
  644. Tree.prototype.insertByDatas = function (datas) {
  645. for(let data of datas){
  646. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  647. this.nodes[this.prefix + data.ID]['data'] = data;
  648. }
  649. for(let data of datas){
  650. let node = this.nodes[this.prefix + data.ID];
  651. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  652. node.parent = parent;
  653. if(!parent){
  654. this.roots.push(node);
  655. }
  656. else {
  657. parent.children.push(node);
  658. }
  659. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  660. node.nextSibling = next;
  661. if(next){
  662. next.preSibling = node;
  663. }
  664. }
  665. //resort
  666. this.roots = tools.reSortNodes(this.roots, true);
  667. tools.sortTreeItems(this);
  668. };
  669. Tree.prototype.delete = function (node) {
  670. var success = false;
  671. success=this.cascadeRemove(node);
  672. tools.sortTreeItems(this);
  673. return success;
  674. };
  675. Tree.prototype.m_delete=function(nodes){
  676. for(let node of nodes){
  677. this.cascadeRemove(node);
  678. }
  679. tools.sortTreeItems(this);
  680. return true;
  681. };
  682. Tree.prototype.cascadeRemove = function (node){
  683. var success = false, that = this;
  684. var deleteIdIndex = function (nodes) {
  685. nodes.forEach(function (node) {
  686. delete that.nodes[that.prefix + node.getID()];
  687. deleteIdIndex(node.children);
  688. })
  689. }
  690. if (node) {
  691. deleteIdIndex([node]);
  692. //delete this.nodes[this.prefix + node.getID()];
  693. if (node.preSibling) {
  694. node.preSibling.setNextSibling(node.nextSibling);
  695. } else if (node.nextSibling) {
  696. node.nextSibling.preSibling = null;
  697. }
  698. if (node.parent) {
  699. node.parent.children.splice(node.siblingIndex(), 1);
  700. } else {
  701. this.roots.splice(node.siblingIndex(), 1);
  702. }
  703. success = true;
  704. }
  705. return success;
  706. };
  707. Tree.prototype.singleDelete = function (node) {//删除本身不删除子项
  708. let that = this;
  709. let success = false;
  710. delete that.nodes[that.prefix + node.getID()];//删除本身
  711. if (node.parent) {//从父项的子节点中移除
  712. node.parent.children.splice(node.siblingIndex(), 1);
  713. } else {
  714. this.roots.splice(node.siblingIndex(), 1);
  715. }
  716. if(node.children.length>0){
  717. if(node.preSibling){//子项变成前兄弟的子项
  718. for(let c of node.children){
  719. node.preSibling.addChild(c);
  720. }
  721. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  722. let oldChild = node.parent.children;
  723. node.parent.children = [];
  724. for(let c of node.children){
  725. node.parent.addChild(c);
  726. }
  727. for(let oc of oldChild){
  728. node.parent.addChild(oc);
  729. }
  730. }else {//都没有的情况
  731. for(let c of node.children){
  732. node.parent.addChild(c);
  733. }
  734. }
  735. tools.sortTreeItems(this);
  736. success = true;
  737. }
  738. return success;
  739. };
  740. Tree.prototype.getDeleteData = function (node) {
  741. var data = [];
  742. var addUpdateDataForDelete = function (datas, nodes) {
  743. nodes.forEach(function (node) {
  744. var delData = {};
  745. delData[node.tree.setting.id] = node.getID();
  746. datas.push({type: 'delete', data: delData});
  747. addUpdateDataForDelete(datas, node.children);
  748. })
  749. };
  750. if (node) {
  751. addUpdateDataForDelete(data, [node]);
  752. if (node.preSibling) {
  753. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  754. }
  755. }
  756. return data;
  757. };
  758. //非叶子节点默认收起展开
  759. Tree.prototype.setRootExpanded = function(nodes, expanded){
  760. for(let node of nodes){
  761. if(node.children.length > 0){
  762. node.setExpanded(expanded);
  763. this.setRootExpanded(node.children, expanded);
  764. }
  765. }
  766. };
  767. Tree.prototype.getExpState = function (nodes) {
  768. let sessionExpanded = [];
  769. function getStat(items){
  770. for(let item of items){
  771. sessionExpanded.push(item.expanded ? 1 : 0);
  772. }
  773. }
  774. getStat(nodes);
  775. let expState = sessionExpanded.join('');
  776. return expState;
  777. };
  778. //节点根据展开收起列表'010101'展开收起
  779. Tree.prototype.setExpandedByState = function (nodes, expState) {
  780. let expStateArr = expState.split('');
  781. for(let i = 0; i < nodes.length; i++){
  782. let expanded = expStateArr[i] == 1 ? true : false;
  783. if(nodes[i].expanded === expanded){
  784. continue;
  785. }
  786. nodes[i].setExpanded(expanded);
  787. }
  788. };
  789. /*Tree.prototype.editedData = function (field, id, newText) {
  790. var node = this.findNode(id), result = {allow: false, nodes: []};
  791. if (this.event[this.eventType.editedData]) {
  792. return this.event[this.eventType.editedData](field, node.data);
  793. } else {
  794. node.data[field] = newText;
  795. result.allow = true;
  796. return result;
  797. }
  798. };*/
  799. Tree.prototype.bind = function (eventName, eventFun) {
  800. this.event[eventName] = eventFun;
  801. };
  802. Tree.prototype.getNodeByID = function (ID) {
  803. let node = this.nodes[this.prefix+ID];
  804. return node;
  805. };
  806. //检查树结构数据有没问题
  807. Tree.prototype.check = function (roots) {
  808. return isValid(roots);
  809. function isValid(nodes) {
  810. for (let node of nodes) {
  811. if (node.data.ParentID !== -1 &&
  812. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  813. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  814. return false;
  815. }
  816. if (node.data.ParentID === -1 && node.parent) {
  817. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  818. return false;
  819. }
  820. if (node.data.NextSiblingID !== -1 &&
  821. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  822. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  823. return false;
  824. }
  825. if (node.data.NextSiblingID === -1 && node.nextSibling) {
  826. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  827. return false;
  828. }
  829. let sameDepthNodes = node.parent ? node.parent.children : roots,
  830. nodeIdx = sameDepthNodes.indexOf(node),
  831. nextIdx = sameDepthNodes.indexOf(node.nextSibling);
  832. if (nodeIdx !== -1 && nextIdx !== -1 && nodeIdx > nextIdx) {
  833. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  834. return false;
  835. }
  836. // nextSibling跟parent children的下一节点对应不上
  837. if (nodeIdx !== -1 &&
  838. (nodeIdx === sameDepthNodes.length - 1 && nextIdx !== -1) ||
  839. (nodeIdx !== sameDepthNodes.length - 1 && nodeIdx + 1 !== nextIdx)) {
  840. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  841. return false;
  842. }
  843. if (node.children.length) {
  844. let v = isValid(node.children);
  845. if (!v) {
  846. return false;
  847. }
  848. }
  849. }
  850. return true;
  851. }
  852. };
  853. return new Tree(setting);
  854. },
  855. updateType: {update: 'update', new: 'new', delete: 'delete'}
  856. };