id_tree.js 43 KB

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