id_tree.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. next = pre ? pre.nextSibling : null;
  91. } else if (arguments.length === 3) {
  92. pre = children.length === 0 ? null : children[children.length - 1];
  93. next = null;
  94. }
  95. if (pre) {
  96. pre.setNextSibling(nodes[0]);
  97. } else {
  98. nodes[0].preSibling = null;
  99. }
  100. nodes[nodes.length - 1].setNextSibling(next);
  101. for (i = 0; i < nodes.length; i++) {
  102. if (arguments.length === 4) {
  103. children.splice(iIndex + i, 0, nodes[i]);
  104. } else if (arguments.length === 3) {
  105. children.push(nodes[i]);
  106. }
  107. nodes[i].setParent(parent ? parent : null);
  108. }
  109. },
  110. sortTreeItems: function (tree) {
  111. var addItems = function (items) {
  112. var i;
  113. for (i = 0; i < items.length; i++) {
  114. tree.items.push(items[i]);
  115. addItems(items[i].children);
  116. }
  117. };
  118. tree.items.splice(0, tree.items.length);
  119. addItems(tree.roots);
  120. },
  121. addUpdateDataForParent: function (datas, nodes, pid) {
  122. nodes.forEach(function (node) {
  123. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), pid, node.getNextSiblingID())});
  124. });
  125. },
  126. addUpdateDataForNextSibling: function (datas, node, nid) {
  127. if (node) {
  128. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), node.getParentID(), nid)});
  129. }
  130. }
  131. };
  132. var Node = function (tree, data) {
  133. // 以下的属性,本单元外均不可直接修改
  134. this.tree = tree;
  135. this.data = data;
  136. this.children = [];
  137. this.parent = null;
  138. this.nextSibling = null;
  139. this.preSibling = null;
  140. this.expanded = true;
  141. this.visible = true;
  142. this.visible = true;
  143. };
  144. Node.prototype.getID = function () {
  145. return this.data[this.tree.setting.id];
  146. };
  147. Node.prototype.getParentID = function () {
  148. return this.parent ? this.parent.getID() : -1;
  149. };
  150. Node.prototype.getNextSiblingID = function () {
  151. return this.nextSibling ? this.nextSibling.getID() : -1;
  152. };
  153. Node.prototype.setParent = function (parent) {
  154. this.parent = parent;
  155. if (this.tree.setting.autoUpdate) {
  156. this.data[this.tree.setting.pid] = this.getParentID();
  157. }
  158. };
  159. Node.prototype.setNextSibling = function (nextSibling) {
  160. this.nextSibling = nextSibling;
  161. if (nextSibling) {
  162. nextSibling.preSibling = this;
  163. }
  164. if (this.tree.setting.autoUpdate) {
  165. this.data[this.tree.setting.nid] = this.getNextSiblingID();
  166. }
  167. }
  168. Node.prototype.firstChild = function () {
  169. return this.children.length === 0 ? null : this.children[0];
  170. };
  171. Node.prototype.lastChild = function () {
  172. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  173. };
  174. Node.prototype.depth = function () {
  175. return this.parent ? this.parent.depth() + 1 : 0;
  176. };
  177. Node.prototype.isFirst = function () {
  178. if (this.parent) {
  179. return this.parent.children.indexOf(this) === 0 ? true : false;
  180. } else {
  181. return this.tree.roots.indexOf(this) === 0 ? true : false;
  182. }
  183. };
  184. Node.prototype.isLast = function () {
  185. if (this.parent) {
  186. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  187. } else {
  188. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  189. }
  190. };
  191. Node.prototype.siblingIndex = function () {
  192. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  193. };
  194. Node.prototype.posterityCount = function () {
  195. var iCount = 0;
  196. if (this.children.length !== 0) {
  197. iCount += this.children.length;
  198. this.children.forEach(function (child) {
  199. iCount += child.posterityCount();
  200. });
  201. }
  202. return iCount;
  203. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  204. return x.posterityCount() + y.posterityCount();
  205. }) + node.children.count;*/
  206. };
  207. // 获取节点所有后代节点
  208. Node.prototype.getPosterity = function() {
  209. let posterity = [];
  210. getNodes(this.children);
  211. return posterity;
  212. function getNodes(nodes) {
  213. for (let node of nodes) {
  214. posterity.push(node);
  215. if (node.children.length > 0){
  216. getNodes(node.children);
  217. }
  218. }
  219. }
  220. };
  221. Node.prototype.setExpanded = function (expanded) {
  222. var setNodesVisible = function (nodes, visible) {
  223. nodes.forEach(function (node) {
  224. node.visible = visible;
  225. setNodesVisible(node.children, visible && node.expanded);
  226. })
  227. };
  228. this.expanded = expanded;
  229. setNodesVisible(this.children, expanded);
  230. };
  231. /*Node.prototype.vis = function () {
  232. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  233. };*/
  234. Node.prototype.serialNo = function () {
  235. return this.tree.items.indexOf(this);
  236. };
  237. Node.prototype.addChild = function (node) {
  238. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  239. node.parent = this;
  240. if (preSibling) {
  241. preSibling.nextSibling = node;
  242. }
  243. node.preSibling = preSibling;
  244. this.children.push(node);
  245. };
  246. Node.prototype.removeChild = function (node) {
  247. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  248. if (preSibling) {
  249. preSibling.nextSibling = nextSibling;
  250. }
  251. if (nextSibling) {
  252. nextSibling.preSibling = preSibling;
  253. }
  254. this.children.splice(node.siblingIndex, 1);
  255. };
  256. Node.prototype.canUpLevel = function () {
  257. return this.parent ? true : false;
  258. };
  259. Node.prototype.getUpLevelData = function () {
  260. var data = [];
  261. if (this.canUpLevel()) {
  262. if (!this.isLast()) {
  263. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  264. }
  265. if (this.preSibling) {
  266. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  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. var Tree = function (setting) {
  382. this.nodes = {};
  383. this.roots = [];
  384. this.items = [];
  385. this.setting = setting;
  386. this.prefix = 'id_';
  387. this.selected = null;
  388. this.event = {};
  389. this.eventType = _eventType;
  390. };
  391. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  392. var data = {};
  393. data[this.setting.id] = id;
  394. data[this.setting.pid] = pid;
  395. data[this.setting.nid] = nid;
  396. return data;
  397. };
  398. Tree.prototype.maxNodeID = (function () {
  399. var maxID = 0;
  400. return function (ID) {
  401. if (arguments.length === 0) {
  402. return maxID;
  403. } else {
  404. maxID = Math.max(maxID, ID);
  405. }
  406. };
  407. })();
  408. Tree.prototype.rangeNodeID = (function () {
  409. var rangeID = -1;
  410. return function (ID) {
  411. if (arguments.length === 0) {
  412. return rangeID;
  413. } else {
  414. rangeID = Math.max(rangeID, ID);
  415. }
  416. }
  417. })();
  418. Tree.prototype.newNodeID = function () {
  419. if (this.rangeNodeID() == -1) {
  420. return this.maxNodeID() + 1;
  421. } else {
  422. if (this.maxNodeID() < this.rangeNodeID()) {
  423. return this.maxNodeID() + 1;
  424. } else {
  425. return -1;
  426. }
  427. }
  428. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  429. return -1;
  430. } else {
  431. return this.maxNodeID() + 1;
  432. }*/
  433. };
  434. Tree.prototype.clearNodes = function () {
  435. this.nodes = {};
  436. this.roots = [];
  437. this.items = [];
  438. };
  439. Tree.prototype.loadDatas = function (datas) {
  440. var prefix = this.prefix, i, node, parent, next, that = this;
  441. this.nodes = {};
  442. this.roots = [];
  443. this.items = [];
  444. // prepare index
  445. datas.forEach(function (data) {
  446. var node = new Node(that, data);
  447. that.nodes[prefix + data[that.setting.id]] = node;
  448. that.maxNodeID(data[that.setting.id]);
  449. });
  450. // set parent by pid, set nextSibling by nid
  451. datas.forEach(function (data) {
  452. node = that.nodes[prefix + data[that.setting.id]];
  453. if (data[that.setting.pid] == that.setting.rootId) {
  454. that.roots.push(node);
  455. } else {
  456. parent = that.nodes[prefix + data[that.setting.pid]];
  457. if (parent) {
  458. node.parent = parent;
  459. parent.children.push(node);
  460. }
  461. }
  462. if (data[that.setting.nid] !== that.setting.rootId) {
  463. next = that.nodes[prefix + data[that.setting.nid]];
  464. if (next) {
  465. node.nextSibling = next;
  466. next.preSibling = node;
  467. }
  468. }
  469. })
  470. // sort by nid
  471. this.roots = tools.reSortNodes(this.roots, true);
  472. tools.sortTreeItems(this);
  473. };
  474. Tree.prototype.firstNode = function () {
  475. return this.roots.length === 0 ? null : this.roots[0];
  476. };
  477. Tree.prototype.findNode = function (id) {
  478. return this.nodes[this.prefix + id];
  479. };
  480. Tree.prototype.count = function () {
  481. var iCount = 0;
  482. if (this.roots.length !== 0) {
  483. iCount += this.roots.length;
  484. this.roots.forEach(function (node) {
  485. iCount += node.posterityCount();
  486. });
  487. }
  488. return iCount;
  489. };
  490. Tree.prototype.insert = function (parentID, nextSiblingID) {
  491. var newID = this.newNodeID(), node = null, data = {};
  492. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  493. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  494. if (newID !== -1) {
  495. data = {};
  496. data[this.setting.id] = newID;
  497. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  498. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  499. node = new Node(this, data);
  500. if (nextSibling) {
  501. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  502. } else {
  503. tools.addNodes(this, parent, [node]);
  504. }
  505. this.nodes[this.prefix + newID] = node;
  506. tools.sortTreeItems(this);
  507. this.maxNodeID(newID);
  508. }
  509. return node;
  510. };
  511. Tree.prototype.m_insert = function (datas,parentID, nextSiblingID) {
  512. // var newID = this.newNodeID(), node = null, data = {};
  513. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  514. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  515. let preInsertNode = null,nodes = [];
  516. for(let d of datas){
  517. let node = new Node(this,d.data);
  518. if(preInsertNode == null){
  519. if (nextSibling) {
  520. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  521. } else {
  522. tools.addNodes(this, parent, [node]);
  523. }
  524. }else {
  525. tools.addNodes(this, parent, [node], preInsertNode.siblingIndex());
  526. }
  527. this.nodes[this.prefix + d.data.ID] = node;
  528. if(preInsertNode) node.setNextSibling(preInsertNode);
  529. preInsertNode = node;
  530. nodes.push(node);
  531. }
  532. tools.sortTreeItems(this);
  533. return nodes;
  534. };
  535. Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) {
  536. var node = null, data = {};
  537. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  538. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  539. if (newID) {
  540. data = {};
  541. data[this.setting.id] = newID;
  542. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  543. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  544. node = new Node(this, data);
  545. if (nextSibling) {
  546. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  547. } else {
  548. tools.addNodes(this, parent, [node]);
  549. }
  550. this.nodes[this.prefix + newID] = node;
  551. tools.sortTreeItems(this);
  552. }
  553. return node;
  554. };
  555. Tree.prototype.getInsertData = function (parentID, nextSiblingID) {
  556. var data = [];
  557. var newID = this.newNodeID();
  558. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  559. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  560. if (newID !== -1) {
  561. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  562. if (nextSibling && nextSibling.preSibling) {
  563. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  564. } else if (parent && parent.children.length !== 0) {
  565. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  566. } else if (!parent && this.roots.length !== 0) {
  567. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  568. }
  569. }
  570. return data;
  571. };
  572. //插入多行
  573. Tree.prototype.getInsertDatas = function (rowCount,parentID, nextSiblingID) {
  574. let data = [],preInsertID = null,lastID;
  575. let parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  576. let nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  577. for(let i=0;i<rowCount ;i++){//先插入的在最后,后插的在最前
  578. let newID = this.newNodeID();
  579. if(newID !== -1){
  580. if(preInsertID == null){//说明是第一个插入的
  581. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  582. }else {//其它的下一节点ID取上一个插入的节点
  583. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, preInsertID)});
  584. }
  585. this.maxNodeID(newID);
  586. preInsertID = newID;
  587. }
  588. }
  589. if (nextSibling && nextSibling.preSibling) {
  590. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, preInsertID);
  591. } else if (parent && parent.children.length !== 0) {
  592. tools.addUpdateDataForNextSibling(data, parent.lastChild(), preInsertID);
  593. } else if (!parent && this.roots.length !== 0) {
  594. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], preInsertID);
  595. }
  596. return data;
  597. };
  598. Tree.prototype.insertByData = function (data, parentID, nextSiblingID) {
  599. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  600. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  601. var node = this.nodes[this.prefix + data[this.setting.id]];
  602. if (node) {
  603. return node;
  604. } else {
  605. node = new Node(this, data);
  606. if (nextSibling) {
  607. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  608. } else {
  609. tools.addNodes(this, parent, [node]);
  610. }
  611. this.nodes[this.prefix + data[this.setting.id]] = node;
  612. tools.sortTreeItems(this);
  613. this.maxNodeID( data[this.setting.id]);
  614. return node;
  615. }
  616. };
  617. //批量新增节点到节点后项,节点已有树结构数据
  618. Tree.prototype.insertDatasTo = function (preData, datas) {
  619. let rst = [];
  620. for(let data of datas){
  621. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  622. this.nodes[this.prefix + data.ID]['data'] = data;
  623. rst.push(this.nodes[this.prefix + data.ID]);
  624. }
  625. for(let data of datas){
  626. let node = this.nodes[this.prefix + data.ID];
  627. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  628. node.parent = parent;
  629. if(!parent){
  630. this.roots.push(node);
  631. }
  632. else {
  633. parent.children.push(node);
  634. }
  635. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  636. node.nextSibling = next;
  637. if(next){
  638. next.preSibling = node;
  639. }
  640. }
  641. let preNode = this.nodes[this.prefix + preData.ID];
  642. if(preNode){
  643. preNode.nextSibling = this.nodes[this.prefix + preData.NextSiblingID] ? this.nodes[this.prefix + preData.NextSiblingID] : null;
  644. if(preNode.nextSibling){
  645. preNode.nextSibling.preSibling = preNode;
  646. }
  647. }
  648. //resort
  649. this.roots = tools.reSortNodes(this.roots, true);
  650. tools.sortTreeItems(this);
  651. return rst;
  652. };
  653. Tree.prototype.delete = function (node) {
  654. var success = false, that = this;
  655. if(node) success = that.m_delete([node]);
  656. return success;
  657. };
  658. Tree.prototype.m_delete = function (nodes) {
  659. let success = false, that = this;
  660. let deleteIdIndex = function (nodes) {
  661. nodes.forEach(function (node) {
  662. delete that.nodes[that.prefix + node.getID()];
  663. deleteIdIndex(node.children);
  664. })
  665. };
  666. for(let n of nodes){
  667. deleteIdIndex([n]);
  668. if (n.preSibling) {
  669. n.preSibling.setNextSibling(n.nextSibling);
  670. } else if (n.nextSibling) {
  671. n.nextSibling.preSibling = null;
  672. }
  673. if (n.parent) {
  674. n.parent.children.splice(n.siblingIndex(), 1);
  675. } else {
  676. this.roots.splice(n.siblingIndex(), 1);
  677. }
  678. }
  679. tools.sortTreeItems(this);
  680. success = true;
  681. return success;
  682. };
  683. Tree.prototype.m_upLevel = function (nodes) {//原先的父节点变成前一个节点,原先的兄弟节点变成子节点
  684. let o_parent = nodes[0].parent;//原来的父节点
  685. let o_next = o_parent.nextSibling;//父节点的下一节点
  686. let o_pre = nodes[0].preSibling;
  687. let o_children = o_parent.children;//旧的所有兄弟节点
  688. let children = o_parent.parent?o_parent.parent.children:this.roots;//新的兄弟节点
  689. let last;
  690. let lastNext;//最后一个选中节点后面的所有兄弟节点变成最后一个节点的子节点
  691. for(let i = 0; i<nodes.length;i++){
  692. let index = children.indexOf(o_parent) + 1;
  693. children.splice(index + i,0,nodes[i]);//往新的父节点的子节点插入节点
  694. o_children.splice(nodes[i].siblingIndex(), 1);//旧的数组删除节点
  695. if(i == 0){//第一个节点变成原来父节点的下一节点
  696. o_parent.setNextSibling(nodes[i]);
  697. if(o_pre) o_pre.setNextSibling(null); //第一个选中节点的前一节点的下一节点设置为空
  698. }
  699. nodes[i].setParent(o_parent.parent);
  700. last = nodes[i];
  701. lastNext = last.nextSibling;
  702. }
  703. last.setNextSibling(o_next);//最后一个选中的节点的下一个节点设置为原父节点的下一节点
  704. if(lastNext){
  705. let t_index = o_children.indexOf(lastNext);
  706. for(let j = t_index;j <o_children.length;j++ ){//剩下的添加为最后一个选中节点的子节点
  707. last.addChild(o_children[j]);
  708. }
  709. if(o_children.length > t_index) o_children.splice(t_index, o_children.length - t_index);//从原先的children中移除
  710. }
  711. if (o_parent.parent&& !o_parent.parent.expanded) o_parent.parent.setExpanded(true);
  712. tools.sortTreeItems(this);
  713. return true;
  714. };
  715. Tree.prototype.getUpLevelDatas = function (nodes) {
  716. //getParentID
  717. let o_parentID = nodes[0].getParentID();
  718. let o_children = nodes[0].parent.children;//旧的所有兄弟节点
  719. let o_pre = nodes[0].preSibling;
  720. let new_parentID = nodes[0].parent.getParentID();
  721. let o_nextID = nodes[0].parent.getNextSiblingID();
  722. let dataMap = {},updateDatas=[],lastID,lastNext;
  723. for(let i = 0; i<nodes.length;i++){
  724. if(i == 0){
  725. dataMap[o_parentID] = {"ID":o_parentID,"NextSiblingID":nodes[i].getID()};
  726. if(o_pre) dataMap[o_pre.getID()] = {"ID":o_pre.getID(),"NextSiblingID":-1}; //nodes[i].preSibling.setNextSibling(null);
  727. }
  728. dataMap[nodes[i].getID()] = {"ID":nodes[i].getID(),"ParentID":new_parentID};
  729. lastID = nodes[i].getID();
  730. lastNext = nodes[i].nextSibling;
  731. }
  732. if(dataMap[lastID] !== undefined){
  733. dataMap[lastID].NextSiblingID = o_nextID;
  734. }
  735. if(lastNext){
  736. let t_index = o_children.indexOf(lastNext);
  737. for(let j = t_index;j <o_children.length;j++ ){//剩下的添加为最后一个选中节点的子节点
  738. dataMap[o_children[j].getID()] = {"ID":o_children[j].getID(),"ParentID":lastID};
  739. }
  740. }
  741. for(let key in dataMap){
  742. updateDatas.push({type: 'update', data:dataMap[key]});
  743. }
  744. return updateDatas;
  745. };
  746. Tree.prototype.m_downLevel = function (nodes) {
  747. let pre = nodes[0].preSibling ; //第一个节点的前一节点,即会成为新的父节点
  748. let next ;//最后一个节点的后一节点,会成为pre 的下一个节点
  749. let last ;//选中的最后一个节点,nextSibling要设置为0
  750. for( let n of nodes){
  751. next = n.nextSibling;
  752. last = n;
  753. let children = n.parent?n.parent.children:this.roots;
  754. children.splice(n.siblingIndex(), 1);
  755. pre.addChild(n);
  756. }
  757. if (!pre.expanded) pre.setExpanded(true);
  758. pre.setNextSibling(next);
  759. last.nextSibling = null;
  760. tools.sortTreeItems(this);
  761. return true;
  762. };
  763. Tree.prototype.getDownLevelDatas = function (nodes) {
  764. let dataMap = {},updateDatas=[],nextID,last;//注释同m_downLevel 方法
  765. let newParent = nodes[0].preSibling;//{"type":"update","data":{"ID":3,"ParentID":-1,"NextSiblingID":5}}
  766. let newPre = newParent.children && newParent.children.length > 0 ? newParent.children[newParent.children.length -1]:null;
  767. if(newPre){ //如果新的父节点有子节点,则把新的父节点的最后一个子节点的下一节点的值改成第一个选中节点的ID
  768. dataMap[newPre.getID()] = {"ID":newPre.getID(),"NextSiblingID":nodes[0].getID()}
  769. }
  770. for(let n of nodes){
  771. nextID = n.getNextSiblingID();
  772. last = n;
  773. dataMap[n.getID()] = {"ID":n.getID(),"ParentID":newParent.getID()}//修改父ID;
  774. }
  775. dataMap[newParent.getID()] = {"ID":newParent.getID(),"NextSiblingID":nextID}//设置新的父节点的下一个节点ID;
  776. if(dataMap[last.getID()]!==undefined){//把最后一个节点的下一个节点ID变成-1
  777. dataMap[last.getID()].NextSiblingID = -1
  778. }else {
  779. dataMap[last.getID()] = {"ID":last.getID(),"NextSiblingID":-1};
  780. }
  781. for(let key in dataMap){
  782. updateDatas.push({type: 'update', data:dataMap[key]});
  783. }
  784. return updateDatas;
  785. };
  786. Tree.prototype.getDeleteData = function (node) {
  787. var data = [];
  788. var addUpdateDataForDelete = function (datas, nodes) {
  789. nodes.forEach(function (node) {
  790. var delData = {};
  791. delData[node.tree.setting.id] = node.getID();
  792. datas.push({type: 'delete', data: delData});
  793. addUpdateDataForDelete(datas, node.children);
  794. })
  795. };
  796. if (node) {
  797. addUpdateDataForDelete(data, [node]);
  798. if (node.preSibling) {
  799. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  800. }
  801. }
  802. return data;
  803. };
  804. Tree.prototype.getDeleteDatas = function (deleteMap,deleteNodes){//批量删除
  805. let datas = [];
  806. addDeleteDatas(datas,deleteNodes);
  807. for(let d of deleteNodes){
  808. addPreUpdateData(datas,deleteMap,d.preSibling,d.nextSibling);
  809. }
  810. function addPreUpdateData(updateDatas,map,preSibling,nextSibling) {
  811. if(preSibling && (map[preSibling.getID()] == undefined || map[preSibling.getID()] == null)){
  812. if(nextSibling){
  813. if(map[nextSibling.getID()]){//如果下一个节点也是要删除的,则再往下顺延
  814. addPreUpdateData(updateDatas,map,preSibling,nextSibling.nextSibling);
  815. }else {
  816. updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),nextSibling.getID())});
  817. }
  818. }else {
  819. updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),-1)});
  820. }
  821. }
  822. }
  823. function addDeleteDatas(dataArray,nodes) {
  824. for(let n of nodes){
  825. let delData = {};
  826. delData[n.tree.setting.id] = n.getID();
  827. dataArray.push({type: 'delete', data: delData});
  828. addDeleteDatas(dataArray,n.children);
  829. }
  830. }
  831. return datas;
  832. };
  833. Tree.prototype.getExpState = function (nodes) {
  834. let sessionExpanded = [];
  835. function getStat(items){
  836. for(let item of items){
  837. sessionExpanded.push(item.expanded ? 1 : 0);
  838. }
  839. }
  840. getStat(nodes);
  841. let expState = sessionExpanded.join('');
  842. return expState;
  843. };
  844. //节点根据展开收起列表'010101'展开收起
  845. Tree.prototype.setExpandedByState = function (nodes, expState) {
  846. let expStateArr = expState.split('');
  847. for(let i = 0; i < nodes.length; i++){
  848. let expanded = expStateArr[i] == 1 ? true : false;
  849. if(nodes[i].expanded === expanded){
  850. continue;
  851. }
  852. nodes[i].setExpanded(expanded);
  853. }
  854. };
  855. // 检查树结构数据有没问题
  856. Tree.prototype.check = function (roots) {
  857. return isValid(roots);
  858. function isValid(nodes) {
  859. for (let node of nodes) {
  860. if (node.data.ParentID != -1 &&
  861. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  862. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  863. return false;
  864. }
  865. if (node.data.ParentID == -1 && node.parent) {
  866. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  867. return false;
  868. }
  869. if (node.data.NextSiblingID != -1 &&
  870. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  871. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  872. return false;
  873. }
  874. if (node.data.NextSiblingID == -1 && node.nextSibling) {
  875. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  876. return false;
  877. }
  878. let sameDepthNodes = node.parent ? node.parent.children : roots,
  879. nodeIdx = sameDepthNodes.indexOf(node),
  880. nextIdx = sameDepthNodes.indexOf(node.nextSibling);
  881. if (nodeIdx != -1 && nextIdx != -1 && nodeIdx > nextIdx) {
  882. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  883. return false;
  884. }
  885. // nextSibling跟parent children的下一节点对应不上
  886. if (nodeIdx != -1 &&
  887. (nodeIdx === sameDepthNodes.length - 1 && nextIdx != -1) ||
  888. (nodeIdx !== sameDepthNodes.length - 1 && nodeIdx + 1 !== nextIdx)) {
  889. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  890. return false;
  891. }
  892. if (node.children.length) {
  893. let v = isValid(node.children);
  894. if (!v) {
  895. return false;
  896. }
  897. }
  898. }
  899. return true;
  900. }
  901. };
  902. /*Tree.prototype.editedData = function (field, id, newText) {
  903. var node = this.findNode(id), result = {allow: false, nodes: []};
  904. if (this.event[this.eventType.editedData]) {
  905. return this.event[this.eventType.editedData](field, node.data);
  906. } else {
  907. node.data[field] = newText;
  908. result.allow = true;
  909. return result;
  910. }
  911. };*/
  912. Tree.prototype.bind = function (eventName, eventFun) {
  913. this.event[eventName] = eventFun;
  914. };
  915. return new Tree(setting);
  916. },
  917. updateType: {update: 'update', new: 'new', delete: 'delete'}
  918. };