id_tree.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. Node.prototype.setExpanded = function (expanded) {
  208. var setNodesVisible = function (nodes, visible) {
  209. nodes.forEach(function (node) {
  210. node.visible = visible;
  211. setNodesVisible(node.children, visible && node.expanded);
  212. })
  213. };
  214. this.expanded = expanded;
  215. setNodesVisible(this.children, expanded);
  216. };
  217. /*Node.prototype.vis = function () {
  218. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  219. };*/
  220. Node.prototype.serialNo = function () {
  221. return this.tree.items.indexOf(this);
  222. };
  223. Node.prototype.addChild = function (node) {
  224. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  225. node.parent = this;
  226. if (preSibling) {
  227. preSibling.nextSibling = node;
  228. }
  229. node.preSibling = preSibling;
  230. this.children.push(node);
  231. };
  232. Node.prototype.removeChild = function (node) {
  233. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  234. if (preSibling) {
  235. preSibling.nextSibling = nextSibling;
  236. }
  237. if (nextSibling) {
  238. nextSibling.preSibling = preSibling;
  239. }
  240. this.children.splice(node.siblingIndex, 1);
  241. };
  242. Node.prototype.canUpLevel = function () {
  243. return this.parent ? true : false;
  244. };
  245. Node.prototype.getUpLevelData = function () {
  246. var data = [];
  247. if (this.canUpLevel()) {
  248. if (!this.isLast()) {
  249. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  250. }
  251. if (this.preSibling) {
  252. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  253. }
  254. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  255. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
  256. }
  257. return data;
  258. };
  259. Node.prototype.upLevel = function () {
  260. var result = {success: false, updateDatas: []};
  261. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  262. if (this.canUpLevel) {
  263. // NextSiblings become child
  264. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  265. // Orginal Parent remove node and nextSiblings
  266. tools.removeNodes(this.tree, this.parent, iIndex);
  267. // New Parent add node
  268. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  269. if (!this.expanded) {
  270. this.setExpanded(true);
  271. }
  272. result.success = true;
  273. }
  274. return result;
  275. };
  276. Node.prototype.canDownLevel = function () {
  277. return !this.isFirst();
  278. };
  279. Node.prototype.getDownLevelData = function () {
  280. var data = [];
  281. if (this.canDownLevel()) {
  282. if (this.preSibling.children.length !== 0) {
  283. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  284. }
  285. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  286. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  287. }
  288. return data;
  289. };
  290. Node.prototype.downLevel = function () {
  291. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  292. var newParent = this.preSibling;
  293. if (this.canDownLevel()) {
  294. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  295. tools.addNodes(this.tree, this.preSibling, [this]);
  296. if (!newParent.expanded) {
  297. newParent.setExpanded(true);
  298. }
  299. success = true;
  300. }
  301. return success;
  302. };
  303. Node.prototype.canUpMove = function () {
  304. return !this.isFirst();
  305. };
  306. Node.prototype.getUpMoveData = function () {
  307. var data = [];
  308. if (this.canUpMove()) {
  309. if (this.preSibling.preSibling) {
  310. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  311. }
  312. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  313. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  314. }
  315. return data;
  316. };
  317. Node.prototype.upMove = function () {
  318. var success = false;
  319. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  320. if (this.canUpMove()) {
  321. if (orgPre.preSibling) {
  322. orgPre.preSibling.setNextSibling(this);
  323. } else {
  324. this.preSibling = null;
  325. }
  326. orgPre.setNextSibling(this.nextSibling);
  327. this.setNextSibling(orgPre);
  328. belongArray.splice(iIndex, 1);
  329. belongArray.splice(iIndex - 1, 0, this);
  330. tools.sortTreeItems(this.tree);
  331. success = true;
  332. }
  333. return success;
  334. };
  335. Node.prototype.canDownMove = function () {
  336. return !this.isLast();
  337. };
  338. Node.prototype.getDownMoveData = function () {
  339. var data = [];
  340. if (this.canDownMove()) {
  341. if (this.preSibling) {
  342. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  343. }
  344. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  345. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  346. }
  347. return data;
  348. };
  349. Node.prototype.downMove = function () {
  350. var success = false;
  351. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  352. if (this.canDownMove()) {
  353. if (this.preSibling) {
  354. this.preSibling.setNextSibling(orgNext);
  355. } else if (orgNext) {
  356. orgNext.preSibling = null;
  357. }
  358. this.setNextSibling(orgNext.nextSibling);
  359. orgNext.setNextSibling(this);
  360. belongArray.splice(iIndex, 1);
  361. belongArray.splice(iIndex + 1, 0, this);
  362. tools.sortTreeItems(this.tree);
  363. success = true;
  364. }
  365. return success;
  366. };
  367. var Tree = function (setting) {
  368. this.nodes = {};
  369. this.roots = [];
  370. this.items = [];
  371. this.setting = setting;
  372. this.prefix = 'id_';
  373. this.selected = null;
  374. this.event = {};
  375. this.eventType = _eventType;
  376. };
  377. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  378. var data = {};
  379. data[this.setting.id] = id;
  380. data[this.setting.pid] = pid;
  381. data[this.setting.nid] = nid;
  382. return data;
  383. };
  384. Tree.prototype.maxNodeID = (function () {
  385. var maxID = 0;
  386. return function (ID) {
  387. if (arguments.length === 0) {
  388. return maxID;
  389. } else {
  390. maxID = Math.max(maxID, ID);
  391. }
  392. };
  393. })();
  394. Tree.prototype.rangeNodeID = (function () {
  395. var rangeID = -1;
  396. return function (ID) {
  397. if (arguments.length === 0) {
  398. return rangeID;
  399. } else {
  400. rangeID = Math.max(rangeID, ID);
  401. }
  402. }
  403. })();
  404. Tree.prototype.newNodeID = function () {
  405. if (this.rangeNodeID() == -1) {
  406. return this.maxNodeID() + 1;
  407. } else {
  408. if (this.maxNodeID() < this.rangeNodeID()) {
  409. return this.maxNodeID() + 1;
  410. } else {
  411. return -1;
  412. }
  413. }
  414. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  415. return -1;
  416. } else {
  417. return this.maxNodeID() + 1;
  418. }*/
  419. };
  420. Tree.prototype.clearNodes = function () {
  421. this.nodes = {};
  422. this.roots = [];
  423. this.items = [];
  424. };
  425. Tree.prototype.loadDatas = function (datas) {
  426. var prefix = this.prefix, i, node, parent, next, that = this;
  427. this.nodes = {};
  428. this.roots = [];
  429. this.items = [];
  430. // prepare index
  431. datas.forEach(function (data) {
  432. var node = new Node(that, data);
  433. that.nodes[prefix + data[that.setting.id]] = node;
  434. that.maxNodeID(data[that.setting.id]);
  435. });
  436. // set parent by pid, set nextSibling by nid
  437. datas.forEach(function (data) {
  438. node = that.nodes[prefix + data[that.setting.id]];
  439. if (data[that.setting.pid] == that.setting.rootId) {
  440. that.roots.push(node);
  441. } else {
  442. parent = that.nodes[prefix + data[that.setting.pid]];
  443. if (parent) {
  444. node.parent = parent;
  445. parent.children.push(node);
  446. }
  447. }
  448. if (data[that.setting.nid] !== that.setting.rootId) {
  449. next = that.nodes[prefix + data[that.setting.nid]];
  450. if (next) {
  451. node.nextSibling = next;
  452. next.preSibling = node;
  453. }
  454. }
  455. })
  456. // sort by nid
  457. this.roots = tools.reSortNodes(this.roots, true);
  458. tools.sortTreeItems(this);
  459. };
  460. Tree.prototype.firstNode = function () {
  461. return this.roots.length === 0 ? null : this.roots[0];
  462. };
  463. Tree.prototype.findNode = function (id) {
  464. return this.nodes[this.prefix + id];
  465. };
  466. Tree.prototype.count = function () {
  467. var iCount = 0;
  468. if (this.roots.length !== 0) {
  469. iCount += this.roots.length;
  470. this.roots.forEach(function (node) {
  471. iCount += node.posterityCount();
  472. });
  473. }
  474. return iCount;
  475. };
  476. Tree.prototype.insert = function (parentID, nextSiblingID) {
  477. var newID = this.newNodeID(), node = null, data = {};
  478. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  479. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  480. if (newID !== -1) {
  481. data = {};
  482. data[this.setting.id] = newID;
  483. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  484. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  485. node = new Node(this, data);
  486. if (nextSibling) {
  487. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  488. } else {
  489. tools.addNodes(this, parent, [node]);
  490. }
  491. this.nodes[this.prefix + newID] = node;
  492. tools.sortTreeItems(this);
  493. this.maxNodeID(newID);
  494. }
  495. return node;
  496. };
  497. Tree.prototype.m_insert = function (datas,parentID, nextSiblingID) {
  498. // var newID = this.newNodeID(), node = null, data = {};
  499. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  500. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  501. let preInsertNode = null,nodes = [];
  502. for(let d of datas){
  503. let node = new Node(this,d.data);
  504. if(preInsertNode == null){
  505. if (nextSibling) {
  506. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  507. } else {
  508. tools.addNodes(this, parent, [node]);
  509. }
  510. }else {
  511. tools.addNodes(this, parent, [node], preInsertNode.siblingIndex());
  512. }
  513. this.nodes[this.prefix + d.data.ID] = node;
  514. if(preInsertNode) node.setNextSibling(preInsertNode);
  515. preInsertNode = node;
  516. nodes.push(node);
  517. }
  518. tools.sortTreeItems(this);
  519. return nodes;
  520. };
  521. Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) {
  522. var node = null, data = {};
  523. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  524. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  525. if (newID) {
  526. data = {};
  527. data[this.setting.id] = newID;
  528. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  529. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  530. node = new Node(this, data);
  531. if (nextSibling) {
  532. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  533. } else {
  534. tools.addNodes(this, parent, [node]);
  535. }
  536. this.nodes[this.prefix + newID] = node;
  537. tools.sortTreeItems(this);
  538. }
  539. return node;
  540. };
  541. Tree.prototype.getInsertData = function (parentID, nextSiblingID) {
  542. var data = [];
  543. var newID = this.newNodeID();
  544. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  545. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  546. if (newID !== -1) {
  547. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  548. if (nextSibling && nextSibling.preSibling) {
  549. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  550. } else if (parent && parent.children.length !== 0) {
  551. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  552. } else if (!parent && this.roots.length !== 0) {
  553. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  554. }
  555. }
  556. return data;
  557. };
  558. //插入多行
  559. Tree.prototype.getInsertDatas = function (rowCount,parentID, nextSiblingID) {
  560. let data = [],preInsertID = null,lastID;
  561. let parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  562. let nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  563. for(let i=0;i<rowCount ;i++){//先插入的在最后,后插的在最前
  564. let newID = this.newNodeID();
  565. if(newID !== -1){
  566. if(preInsertID == null){//说明是第一个插入的
  567. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  568. }else {//其它的下一节点ID取上一个插入的节点
  569. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, preInsertID)});
  570. }
  571. this.maxNodeID(newID);
  572. preInsertID = newID;
  573. }
  574. }
  575. if (nextSibling && nextSibling.preSibling) {
  576. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, preInsertID);
  577. } else if (parent && parent.children.length !== 0) {
  578. tools.addUpdateDataForNextSibling(data, parent.lastChild(), preInsertID);
  579. } else if (!parent && this.roots.length !== 0) {
  580. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], preInsertID);
  581. }
  582. return data;
  583. };
  584. Tree.prototype.insertByData = function (data, parentID, nextSiblingID) {
  585. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  586. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  587. var node = this.nodes[this.prefix + data[this.setting.id]];
  588. if (node) {
  589. return node;
  590. } else {
  591. node = new Node(this, data);
  592. if (nextSibling) {
  593. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  594. } else {
  595. tools.addNodes(this, parent, [node]);
  596. }
  597. this.nodes[this.prefix + data[this.setting.id]] = node;
  598. tools.sortTreeItems(this);
  599. this.maxNodeID( data[this.setting.id]);
  600. return node;
  601. }
  602. };
  603. //批量新增节点到节点后项,节点已有树结构数据
  604. Tree.prototype.insertDatasTo = function (preData, datas) {
  605. let rst = [];
  606. for(let data of datas){
  607. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  608. this.nodes[this.prefix + data.ID]['data'] = data;
  609. rst.push(this.nodes[this.prefix + data.ID]);
  610. }
  611. for(let data of datas){
  612. let node = this.nodes[this.prefix + data.ID];
  613. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  614. node.parent = parent;
  615. if(!parent){
  616. this.roots.push(node);
  617. }
  618. else {
  619. parent.children.push(node);
  620. }
  621. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  622. node.nextSibling = next;
  623. if(next){
  624. next.preSibling = node;
  625. }
  626. }
  627. let preNode = this.nodes[this.prefix + preData.ID];
  628. if(preNode){
  629. preNode.nextSibling = this.nodes[this.prefix + preData.NextSiblingID] ? this.nodes[this.prefix + preData.NextSiblingID] : null;
  630. if(preNode.nextSibling){
  631. preNode.nextSibling.preSibling = preNode;
  632. }
  633. }
  634. //resort
  635. this.roots = tools.reSortNodes(this.roots, true);
  636. tools.sortTreeItems(this);
  637. return rst;
  638. };
  639. Tree.prototype.delete = function (node) {
  640. var success = false, that = this;
  641. if(node) success = that.m_delete([node]);
  642. return success;
  643. };
  644. Tree.prototype.m_delete = function (nodes) {
  645. let success = false, that = this;
  646. let deleteIdIndex = function (nodes) {
  647. nodes.forEach(function (node) {
  648. delete that.nodes[that.prefix + node.getID()];
  649. deleteIdIndex(node.children);
  650. })
  651. };
  652. for(let n of nodes){
  653. deleteIdIndex([n]);
  654. if (n.preSibling) {
  655. n.preSibling.setNextSibling(n.nextSibling);
  656. } else if (n.nextSibling) {
  657. n.nextSibling.preSibling = null;
  658. }
  659. if (n.parent) {
  660. n.parent.children.splice(n.siblingIndex(), 1);
  661. } else {
  662. this.roots.splice(n.siblingIndex(), 1);
  663. }
  664. }
  665. tools.sortTreeItems(this);
  666. success = true;
  667. return success;
  668. };
  669. Tree.prototype.m_upLevel = function (nodes) {//原先的父节点变成前一个节点,原先的兄弟节点变成子节点
  670. let o_parent = nodes[0].parent;//原来的父节点
  671. let o_next = o_parent.nextSibling;//父节点的下一节点
  672. let o_pre = nodes[0].preSibling;
  673. let o_children = o_parent.children;//旧的所有兄弟节点
  674. let children = o_parent.parent?o_parent.parent.children:this.roots;//新的兄弟节点
  675. let last;
  676. let lastNext;//最后一个选中节点后面的所有兄弟节点变成最后一个节点的子节点
  677. for(let i = 0; i<nodes.length;i++){
  678. let index = children.indexOf(o_parent) + 1;
  679. children.splice(index + i,0,nodes[i]);//往新的父节点的子节点插入节点
  680. o_children.splice(nodes[i].siblingIndex(), 1);//旧的数组删除节点
  681. if(i == 0){//第一个节点变成原来父节点的下一节点
  682. o_parent.setNextSibling(nodes[i]);
  683. if(o_pre) o_pre.setNextSibling(null); //第一个选中节点的前一节点的下一节点设置为空
  684. }
  685. nodes[i].setParent(o_parent.parent);
  686. last = nodes[i];
  687. lastNext = last.nextSibling;
  688. }
  689. last.setNextSibling(o_next);//最后一个选中的节点的下一个节点设置为原父节点的下一节点
  690. if(lastNext){
  691. let t_index = o_children.indexOf(lastNext);
  692. for(let j = t_index;j <o_children.length;j++ ){//剩下的添加为最后一个选中节点的子节点
  693. last.addChild(o_children[j]);
  694. }
  695. if(o_children.length > t_index) o_children.splice(t_index, o_children.length - t_index);//从原先的children中移除
  696. }
  697. if (o_parent.parent&& !o_parent.parent.expanded) o_parent.parent.setExpanded(true);
  698. tools.sortTreeItems(this);
  699. return true;
  700. };
  701. Tree.prototype.getUpLevelDatas = function (nodes) {
  702. //getParentID
  703. let o_parentID = nodes[0].getParentID();
  704. let o_children = nodes[0].parent.children;//旧的所有兄弟节点
  705. let o_pre = nodes[0].preSibling;
  706. let new_parentID = nodes[0].parent.getParentID();
  707. let o_nextID = nodes[0].parent.getNextSiblingID();
  708. let dataMap = {},updateDatas=[],lastID,lastNext;
  709. for(let i = 0; i<nodes.length;i++){
  710. if(i == 0){
  711. dataMap[o_parentID] = {"ID":o_parentID,"NextSiblingID":nodes[i].getID()};
  712. if(o_pre) dataMap[o_pre.getID()] = {"ID":o_pre.getID(),"NextSiblingID":-1}; //nodes[i].preSibling.setNextSibling(null);
  713. }
  714. dataMap[nodes[i].getID()] = {"ID":nodes[i].getID(),"ParentID":new_parentID};
  715. lastID = nodes[i].getID();
  716. lastNext = nodes[i].nextSibling;
  717. }
  718. if(dataMap[lastID] !== undefined){
  719. dataMap[lastID].NextSiblingID = o_nextID;
  720. }
  721. if(lastNext){
  722. let t_index = o_children.indexOf(lastNext);
  723. for(let j = t_index;j <o_children.length;j++ ){//剩下的添加为最后一个选中节点的子节点
  724. dataMap[o_children[j].getID()] = {"ID":o_children[j].getID(),"ParentID":lastID};
  725. }
  726. }
  727. for(let key in dataMap){
  728. updateDatas.push({type: 'update', data:dataMap[key]});
  729. }
  730. return updateDatas;
  731. };
  732. Tree.prototype.m_downLevel = function (nodes) {
  733. let pre = nodes[0].preSibling ; //第一个节点的前一节点,即会成为新的父节点
  734. let next ;//最后一个节点的后一节点,会成为pre 的下一个节点
  735. let last ;//选中的最后一个节点,nextSibling要设置为0
  736. for( let n of nodes){
  737. next = n.nextSibling;
  738. last = n;
  739. let children = n.parent?n.parent.children:this.roots;
  740. children.splice(n.siblingIndex(), 1);
  741. pre.addChild(n);
  742. }
  743. if (!pre.expanded) pre.setExpanded(true);
  744. pre.setNextSibling(next);
  745. last.nextSibling = null;
  746. tools.sortTreeItems(this);
  747. return true;
  748. };
  749. Tree.prototype.getDownLevelDatas = function (nodes) {
  750. let dataMap = {},updateDatas=[],nextID,last;//注释同m_downLevel 方法
  751. let newParent = nodes[0].preSibling;//{"type":"update","data":{"ID":3,"ParentID":-1,"NextSiblingID":5}}
  752. let newPre = newParent.children && newParent.children.length > 0 ? newParent.children[newParent.children.length -1]:null;
  753. if(newPre){ //如果新的父节点有子节点,则把新的父节点的最后一个子节点的下一节点的值改成第一个选中节点的ID
  754. dataMap[newPre.getID()] = {"ID":newPre.getID(),"NextSiblingID":nodes[0].getID()}
  755. }
  756. for(let n of nodes){
  757. nextID = n.getNextSiblingID();
  758. last = n;
  759. dataMap[n.getID()] = {"ID":n.getID(),"ParentID":newParent.getID()}//修改父ID;
  760. }
  761. dataMap[newParent.getID()] = {"ID":newParent.getID(),"NextSiblingID":nextID}//设置新的父节点的下一个节点ID;
  762. if(dataMap[last.getID()]!==undefined){//把最后一个节点的下一个节点ID变成-1
  763. dataMap[last.getID()].NextSiblingID = -1
  764. }else {
  765. dataMap[last.getID()] = {"ID":last.getID(),"NextSiblingID":-1};
  766. }
  767. for(let key in dataMap){
  768. updateDatas.push({type: 'update', data:dataMap[key]});
  769. }
  770. return updateDatas;
  771. };
  772. Tree.prototype.getDeleteData = function (node) {
  773. var data = [];
  774. var addUpdateDataForDelete = function (datas, nodes) {
  775. nodes.forEach(function (node) {
  776. var delData = {};
  777. delData[node.tree.setting.id] = node.getID();
  778. datas.push({type: 'delete', data: delData});
  779. addUpdateDataForDelete(datas, node.children);
  780. })
  781. };
  782. if (node) {
  783. addUpdateDataForDelete(data, [node]);
  784. if (node.preSibling) {
  785. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  786. }
  787. }
  788. return data;
  789. };
  790. Tree.prototype.getDeleteDatas = function (deleteMap,deleteNodes){//批量删除
  791. let datas = [];
  792. addDeleteDatas(datas,deleteNodes);
  793. for(let d of deleteNodes){
  794. addPreUpdateData(datas,deleteMap,d.preSibling,d.nextSibling);
  795. }
  796. function addPreUpdateData(updateDatas,map,preSibling,nextSibling) {
  797. if(preSibling && (map[preSibling.getID()] == undefined || map[preSibling.getID()] == null)){
  798. if(nextSibling){
  799. if(map[nextSibling.getID()]){//如果下一个节点也是要删除的,则再往下顺延
  800. addPreUpdateData(updateDatas,map,preSibling,nextSibling.nextSibling);
  801. }else {
  802. updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),nextSibling.getID())});
  803. }
  804. }else {
  805. updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),-1)});
  806. }
  807. }
  808. }
  809. function addDeleteDatas(dataArray,nodes) {
  810. for(let n of nodes){
  811. let delData = {};
  812. delData[n.tree.setting.id] = n.getID();
  813. dataArray.push({type: 'delete', data: delData});
  814. addDeleteDatas(dataArray,n.children);
  815. }
  816. }
  817. return datas;
  818. };
  819. /*Tree.prototype.editedData = function (field, id, newText) {
  820. var node = this.findNode(id), result = {allow: false, nodes: []};
  821. if (this.event[this.eventType.editedData]) {
  822. return this.event[this.eventType.editedData](field, node.data);
  823. } else {
  824. node.data[field] = newText;
  825. result.allow = true;
  826. return result;
  827. }
  828. };*/
  829. Tree.prototype.bind = function (eventName, eventFun) {
  830. this.event[eventName] = eventFun;
  831. };
  832. return new Tree(setting);
  833. },
  834. updateType: {update: 'update', new: 'new', delete: 'delete'}
  835. };