id_tree.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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.updateData={};
  142. this.parent = null;
  143. this.nextSibling = null;
  144. this.preSibling = null;
  145. this.expanded = true;
  146. this.visible = true;
  147. this.visible = true;
  148. };
  149. Node.prototype.getID = function () {
  150. return this.data[this.tree.setting.id];
  151. };
  152. Node.prototype.getParentID = function () {
  153. return this.parent ? this.parent.getID() : -1;
  154. };
  155. Node.prototype.getNextSiblingID = function () {
  156. return this.nextSibling ? this.nextSibling.getID() : -1;
  157. };
  158. Node.prototype.setParent = function (parent) {
  159. this.parent = parent;
  160. if (this.tree.setting.autoUpdate) {
  161. this.data[this.tree.setting.pid] = this.getParentID();
  162. }
  163. };
  164. Node.prototype.setNextSibling = function (nextSibling) {
  165. this.nextSibling = nextSibling;
  166. if (nextSibling) {
  167. nextSibling.preSibling = this;
  168. }
  169. if (this.tree.setting.autoUpdate) {
  170. this.data[this.tree.setting.nid] = this.getNextSiblingID();
  171. }
  172. }
  173. Node.prototype.firstChild = function () {
  174. return this.children.length === 0 ? null : this.children[0];
  175. };
  176. Node.prototype.lastChild = function () {
  177. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  178. };
  179. Node.prototype.depth = function () {
  180. return this.parent ? this.parent.depth() + 1 : 0;
  181. };
  182. Node.prototype.isFirst = function () {
  183. if (this.parent) {
  184. return this.parent.children.indexOf(this) === 0 ? true : false;
  185. } else {
  186. return this.tree.roots.indexOf(this) === 0 ? true : false;
  187. }
  188. };
  189. Node.prototype.isLast = function () {
  190. if (this.parent) {
  191. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  192. } else {
  193. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  194. }
  195. };
  196. Node.prototype.siblingIndex = function () {
  197. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  198. };
  199. Node.prototype.posterityCount = function () {
  200. var iCount = 0;
  201. if (this.children.length !== 0) {
  202. iCount += this.children.length;
  203. this.children.forEach(function (child) {
  204. iCount += child.posterityCount();
  205. });
  206. }
  207. return iCount;
  208. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  209. return x.posterityCount() + y.posterityCount();
  210. }) + node.children.count;*/
  211. };
  212. Node.prototype.setExpanded = function (expanded) {
  213. var setNodesVisible = function (nodes, visible) {
  214. nodes.forEach(function (node) {
  215. node.visible = visible;
  216. setNodesVisible(node.children, visible && node.expanded);
  217. })
  218. };
  219. this.expanded = expanded;
  220. setNodesVisible(this.children, expanded);
  221. };
  222. Node.prototype.setExpandedNoRecur = function (expanded) {
  223. this.expanded = expanded;
  224. this.visible = expanded && this.visible;
  225. for(let node of this.children){
  226. node.visible = expanded && node.visible;
  227. }
  228. };
  229. /*Node.prototype.vis = function () {
  230. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  231. };*/
  232. Node.prototype.serialNo = function () {
  233. return this.tree.items.indexOf(this);
  234. };
  235. Node.prototype.addChild = function (node) {
  236. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  237. node.parent = this;
  238. if (preSibling) {
  239. preSibling.nextSibling = node;
  240. }
  241. node.preSibling = preSibling;
  242. this.children.push(node);
  243. };
  244. Node.prototype.removeChild = function (node) {
  245. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  246. if (preSibling) {
  247. preSibling.nextSibling = nextSibling;
  248. }
  249. if (nextSibling) {
  250. nextSibling.preSibling = preSibling;
  251. }
  252. this.children.splice(node.siblingIndex, 1);
  253. };
  254. Node.prototype.canUpLevel = function () {
  255. return this.parent ? true : false;
  256. };
  257. Node.prototype.getUpLevelData = function () {
  258. var data = [];
  259. if (this.canUpLevel()) {
  260. if (!this.isLast()) {
  261. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  262. }
  263. if (this.preSibling) {
  264. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  265. }
  266. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  267. let updateData = this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID());
  268. if (!this.isLast()) {
  269. //如果设置了专项暂定,则需要清空
  270. if (this.data.specialProvisional) {
  271. this.data.specialProvisional = '';
  272. updateData.specialProvisional = '';
  273. }
  274. }
  275. data.push({type: 'update', data: updateData});
  276. }
  277. return data;
  278. };
  279. Node.prototype.upLevel = function () {
  280. var result = {success: false, updateDatas: []};
  281. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  282. if (this.canUpLevel) {
  283. // NextSiblings become child
  284. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  285. // Orginal Parent remove node and nextSiblings
  286. tools.removeNodes(this.tree, this.parent, iIndex);
  287. // New Parent add node
  288. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  289. if (!this.expanded) {
  290. this.setExpanded(true);
  291. }
  292. result.success = true;
  293. }
  294. return result;
  295. };
  296. Node.prototype.canDownLevel = function () {
  297. return !this.isFirst();
  298. };
  299. Node.prototype.getDownLevelData = function () {
  300. var data = [];
  301. if (this.canDownLevel()) {
  302. if (this.preSibling.children.length !== 0) {
  303. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  304. }
  305. let updateData = this.tree.getDataTemplate(this.preSibling.getID(), this.preSibling.getParentID(), this.getNextSiblingID());
  306. if (this.preSibling.data.specialProvisional) {
  307. //如果设置了专项暂定,则需要清空
  308. this.preSibling.data.specialProvisional = '';
  309. updateData.specialProvisional = '';
  310. }
  311. data.push({type: 'update', data: updateData});
  312. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  313. }
  314. return data;
  315. };
  316. Node.prototype.downLevel = function () {
  317. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  318. var newParent = this.preSibling;
  319. if (this.canDownLevel()) {
  320. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  321. tools.addNodes(this.tree, this.preSibling, [this]);
  322. if (!newParent.expanded) {
  323. newParent.setExpanded(true);
  324. }
  325. success = true;
  326. }
  327. return success;
  328. };
  329. Node.prototype.canUpMove = function () {
  330. return !this.isFirst();
  331. };
  332. Node.prototype.getUpMoveData = function () {
  333. var data = [];
  334. if (this.canUpMove()) {
  335. if (this.preSibling.preSibling) {
  336. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  337. }
  338. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  339. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  340. }
  341. return data;
  342. };
  343. Node.prototype.upMove = function () {
  344. var success = false;
  345. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  346. if (this.canUpMove()) {
  347. if (orgPre.preSibling) {
  348. orgPre.preSibling.setNextSibling(this);
  349. } else {
  350. this.preSibling = null;
  351. }
  352. orgPre.setNextSibling(this.nextSibling);
  353. this.setNextSibling(orgPre);
  354. belongArray.splice(iIndex, 1);
  355. belongArray.splice(iIndex - 1, 0, this);
  356. tools.sortTreeItems(this.tree);
  357. success = true;
  358. }
  359. return success;
  360. };
  361. Node.prototype.canDownMove = function () {
  362. return !this.isLast();
  363. };
  364. Node.prototype.getDownMoveData = function () {
  365. var data = [];
  366. if (this.canDownMove()) {
  367. if (this.preSibling) {
  368. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  369. }
  370. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  371. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  372. }
  373. return data;
  374. };
  375. Node.prototype.downMove = function () {
  376. var success = false;
  377. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  378. if (this.canDownMove()) {
  379. if (this.preSibling) {
  380. this.preSibling.setNextSibling(orgNext);
  381. } else if (orgNext) {
  382. orgNext.preSibling = null;
  383. }
  384. this.setNextSibling(orgNext.nextSibling);
  385. orgNext.setNextSibling(this);
  386. belongArray.splice(iIndex, 1);
  387. belongArray.splice(iIndex + 1, 0, this);
  388. tools.sortTreeItems(this.tree);
  389. success = true;
  390. }
  391. return success;
  392. };
  393. // 节点所属固定ID
  394. Node.prototype.getFlag = function () {
  395. if (!this.data || !this.data.flags || !this.data.flags[0] || !this.data.flags[0].flag) {
  396. return 0;
  397. }
  398. return this.data.flags[0].flag;
  399. };
  400. // 节点所属固定ID
  401. Node.prototype.belongToFlag = function () {
  402. let node = this;
  403. while (node) {
  404. if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) {
  405. return node.data.flags[0].flag;
  406. }
  407. node = node.parent;
  408. }
  409. return null;
  410. };
  411. // 节点是否属于某些固定ID,会一直向上找,直到找到或到达顶层
  412. Node.prototype.isBelongToFlags = function (flags) {
  413. let node = this;
  414. while (node) {
  415. const flag = node.getFlag();
  416. if (flags.includes(flag)) {
  417. return true;
  418. }
  419. node = node.parent;
  420. }
  421. return false;
  422. }
  423. var Tree = function (setting) {
  424. this.nodes = {};
  425. this.roots = [];
  426. this.items = [];
  427. this.setting = setting;
  428. this.prefix = 'id_';
  429. this.selected = null;
  430. this.event = {};
  431. this.eventType = _eventType;
  432. };
  433. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  434. var data = {};
  435. data[this.setting.id] = id;
  436. data[this.setting.pid] = pid;
  437. data[this.setting.nid] = nid;
  438. return data;
  439. };
  440. Tree.prototype.maxNodeID = (function () {
  441. var maxID = 0;
  442. return function (ID) {
  443. if (arguments.length === 0) {
  444. return maxID;
  445. } else {
  446. maxID = Math.max(maxID, ID);
  447. }
  448. };
  449. })();
  450. Tree.prototype.rangeNodeID = (function () {
  451. var rangeID = -1;
  452. return function (ID) {
  453. if (arguments.length === 0) {
  454. return rangeID;
  455. } else {
  456. rangeID = Math.max(rangeID, ID);
  457. }
  458. }
  459. })();
  460. Tree.prototype.newNodeID = function () {
  461. if (this.rangeNodeID() == -1) {
  462. return this.maxNodeID() + 1;
  463. } else {
  464. if (this.maxNodeID() < this.rangeNodeID()) {
  465. return this.maxNodeID() + 1;
  466. } else {
  467. return -1;
  468. }
  469. }
  470. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  471. return -1;
  472. } else {
  473. return this.maxNodeID() + 1;
  474. }*/
  475. };
  476. Tree.prototype.clearNodes = function () {
  477. this.nodes = {};
  478. this.roots = [];
  479. this.items = [];
  480. };
  481. Tree.prototype.loadDatas = function (datas) {
  482. var prefix = this.prefix, i, node, parent, next, that = this;
  483. this.nodes = {};
  484. this.roots = [];
  485. this.items = [];
  486. // prepare index
  487. datas.forEach(function (data) {
  488. var node = new Node(that, data);
  489. that.nodes[prefix + data[that.setting.id]] = node;
  490. that.maxNodeID(data[that.setting.id]);
  491. });
  492. // set parent by pid, set nextSibling by nid
  493. datas.forEach(function (data) {
  494. node = that.nodes[prefix + data[that.setting.id]];
  495. if (data[that.setting.pid] == that.setting.rootId) {
  496. that.roots.push(node);
  497. } else {
  498. parent = that.nodes[prefix + data[that.setting.pid]];
  499. if (parent) {
  500. node.parent = parent;
  501. parent.children.push(node);
  502. }
  503. }
  504. if (data[that.setting.nid] !== that.setting.rootId) {
  505. next = that.nodes[prefix + data[that.setting.nid]];
  506. if (next) {
  507. node.nextSibling = next;
  508. next.preSibling = node;
  509. }
  510. }
  511. })
  512. // sort by nid
  513. this.roots = tools.reSortNodes(this.roots, true);
  514. tools.sortTreeItems(this);
  515. };
  516. Tree.prototype.firstNode = function () {
  517. return this.roots.length === 0 ? null : this.roots[0];
  518. };
  519. Tree.prototype.findNode = function (id) {
  520. return this.nodes[this.prefix + id];
  521. };
  522. Tree.prototype.count = function () {
  523. var iCount = 0;
  524. if (this.roots.length !== 0) {
  525. iCount += this.roots.length;
  526. this.roots.forEach(function (node) {
  527. iCount += node.posterityCount();
  528. });
  529. }
  530. return iCount;
  531. };
  532. Tree.prototype.insert = function (parentID, nextSiblingID) {
  533. var newID = this.newNodeID(), node = null, data = {};
  534. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  535. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  536. if (newID !== -1) {
  537. data = {};
  538. data[this.setting.id] = newID;
  539. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  540. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  541. node = new Node(this, data);
  542. if (nextSibling) {
  543. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  544. } else {
  545. tools.addNodes(this, parent, [node]);
  546. }
  547. this.nodes[this.prefix + newID] = node;
  548. tools.sortTreeItems(this);
  549. this.maxNodeID(newID);
  550. }
  551. return node;
  552. };
  553. Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) {
  554. var node = null, data = {};
  555. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  556. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  557. if (newID) {
  558. data = {};
  559. data[this.setting.id] = newID;
  560. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  561. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  562. node = new Node(this, data);
  563. if (nextSibling) {
  564. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  565. } else {
  566. tools.addNodes(this, parent, [node]);
  567. }
  568. this.nodes[this.prefix + newID] = node;
  569. tools.sortTreeItems(this);
  570. }
  571. return node;
  572. };
  573. Tree.prototype.getInsertData = function (parentID, nextSiblingID, uid = null) {
  574. var data = [];
  575. var newID = uid ? uuid.v1() : this.newNodeID();
  576. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  577. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  578. if (newID != -1) {
  579. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  580. if (nextSibling && nextSibling.preSibling) {
  581. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  582. } else if (parent && parent.children.length !== 0) {
  583. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  584. } else if (!parent && this.roots.length !== 0) {
  585. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  586. }
  587. }
  588. return data;
  589. };
  590. Tree.prototype.insertByData = function (data, parentID, nextSiblingID, uid = null) {
  591. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  592. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  593. var node = this.nodes[this.prefix + data[this.setting.id]];
  594. if (node) {
  595. return node;
  596. } else {
  597. node = new Node(this, data);
  598. if (nextSibling) {
  599. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  600. } else {
  601. tools.addNodes(this, parent, [node]);
  602. }
  603. this.nodes[this.prefix + data[this.setting.id]] = node;
  604. tools.sortTreeItems(this);
  605. if(!uid){
  606. this.maxNodeID( data[this.setting.id]);
  607. }
  608. return node;
  609. }
  610. };
  611. //批量新增节点,节点已有树结构数据
  612. Tree.prototype.insertByDatas = function (datas) {
  613. for(let data of datas){
  614. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  615. this.nodes[this.prefix + data.ID]['data'] = data;
  616. }
  617. for(let data of datas){
  618. let node = this.nodes[this.prefix + data.ID];
  619. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  620. node.parent = parent;
  621. if(!parent){
  622. this.roots.push(node);
  623. }
  624. else {
  625. parent.children.push(node);
  626. }
  627. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  628. node.nextSibling = next;
  629. if(next){
  630. //将原本nextSibing的前节点的设置为node的上兄弟节点
  631. if (next.preSibling && next.preSibling.nextSibling !== node) {
  632. next.preSibling.nextSibling = node;
  633. next.preSibling.data.NextSiblingID = node.data.ID;
  634. node.preSibling = next.preSibling;
  635. }
  636. next.preSibling = node;
  637. } else {//插入了最末子节点,更新上一个最末子节点
  638. let lastChild = parent ? parent.children[parent.children.length - 2] : this.roots[this.roots.length - 2];
  639. if (lastChild && lastChild.nextSibling !== node) {
  640. lastChild.nextSibling = node;
  641. lastChild.data.NextSiblingID = node.data.ID;
  642. }
  643. }
  644. }
  645. //resort
  646. this.roots = tools.reSortNodes(this.roots, true);
  647. tools.sortTreeItems(this);
  648. };
  649. Tree.prototype.delete = function (node) {
  650. var success = false;
  651. success=this.cascadeRemove(node);
  652. tools.sortTreeItems(this);
  653. return success;
  654. };
  655. Tree.prototype.m_delete=function(nodes){
  656. for(let node of nodes){
  657. this.cascadeRemove(node);
  658. }
  659. tools.sortTreeItems(this);
  660. return true;
  661. };
  662. Tree.prototype.cascadeRemove = function (node){
  663. var success = false, that = this;
  664. var deleteIdIndex = function (nodes) {
  665. nodes.forEach(function (node) {
  666. delete that.nodes[that.prefix + node.getID()];
  667. deleteIdIndex(node.children);
  668. })
  669. }
  670. if (node) {
  671. deleteIdIndex([node]);
  672. //delete this.nodes[this.prefix + node.getID()];
  673. if (node.preSibling) {
  674. node.preSibling.setNextSibling(node.nextSibling);
  675. } else if (node.nextSibling) {
  676. node.nextSibling.preSibling = null;
  677. }
  678. if (node.parent) {
  679. node.parent.children.splice(node.siblingIndex(), 1);
  680. } else {
  681. this.roots.splice(node.siblingIndex(), 1);
  682. }
  683. success = true;
  684. }
  685. return success;
  686. };
  687. Tree.prototype.singleDelete = function (node) {//删除本身不删除子项
  688. let that = this;
  689. let success = false;
  690. delete that.nodes[that.prefix + node.getID()];//删除本身
  691. if (node.parent) {//从父项的子节点中移除
  692. node.parent.children.splice(node.siblingIndex(), 1);
  693. } else {
  694. this.roots.splice(node.siblingIndex(), 1);
  695. }
  696. if(node.children.length>0){
  697. if(node.preSibling){//子项变成前兄弟的子项
  698. for(let c of node.children){
  699. node.preSibling.addChild(c);
  700. }
  701. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  702. let oldChild = node.parent.children;
  703. node.parent.children = [];
  704. for(let c of node.children){
  705. node.parent.addChild(c);
  706. }
  707. for(let oc of oldChild){
  708. node.parent.addChild(oc);
  709. }
  710. }else {//都没有的情况
  711. for(let c of node.children){
  712. node.parent.addChild(c);
  713. }
  714. }
  715. tools.sortTreeItems(this);
  716. success = true;
  717. }
  718. return success;
  719. };
  720. Tree.prototype.getDeleteData = function (node) {
  721. var data = [];
  722. var addUpdateDataForDelete = function (datas, nodes) {
  723. nodes.forEach(function (node) {
  724. var delData = {};
  725. delData[node.tree.setting.id] = node.getID();
  726. datas.push({type: 'delete', data: delData});
  727. addUpdateDataForDelete(datas, node.children);
  728. })
  729. };
  730. if (node) {
  731. addUpdateDataForDelete(data, [node]);
  732. if (node.preSibling) {
  733. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  734. }
  735. }
  736. return data;
  737. };
  738. //非叶子节点默认收起展开
  739. Tree.prototype.setRootExpanded = function(nodes, expanded){
  740. for(let node of nodes){
  741. if(node.children.length > 0){
  742. node.setExpanded(expanded);
  743. this.setRootExpanded(node.children, expanded);
  744. }
  745. }
  746. };
  747. Tree.prototype.getExpState = function (nodes) {
  748. let sessionExpanded = [];
  749. function getStat(items){
  750. for(let item of items){
  751. sessionExpanded.push(item.expanded ? 1 : 0);
  752. }
  753. }
  754. getStat(nodes);
  755. let expState = sessionExpanded.join('');
  756. return expState;
  757. };
  758. //节点根据展开收起列表'010101'展开收起
  759. Tree.prototype.setExpandedByState = function (nodes, expState) {
  760. let expStateArr = expState.split('');
  761. for(let i = 0; i < nodes.length; i++){
  762. let expanded = expStateArr[i] == 1 ? true : false;
  763. if(nodes[i].expanded === expanded){
  764. continue;
  765. }
  766. nodes[i].setExpanded(expanded);
  767. }
  768. };
  769. /*Tree.prototype.editedData = function (field, id, newText) {
  770. var node = this.findNode(id), result = {allow: false, nodes: []};
  771. if (this.event[this.eventType.editedData]) {
  772. return this.event[this.eventType.editedData](field, node.data);
  773. } else {
  774. node.data[field] = newText;
  775. result.allow = true;
  776. return result;
  777. }
  778. };*/
  779. Tree.prototype.bind = function (eventName, eventFun) {
  780. this.event[eventName] = eventFun;
  781. };
  782. Tree.prototype.getNodeByID = function (ID) {
  783. let node = this.nodes[this.prefix+ID];
  784. return node;
  785. };
  786. return new Tree(setting);
  787. },
  788. updateType: {update: 'update', new: 'new', delete: 'delete'}
  789. };