id_tree.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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.insertDatasTo = function (preData, datas) {
  632. let rst = [];
  633. for(let data of datas){
  634. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  635. this.nodes[this.prefix + data.ID]['data'] = data;
  636. rst.push(this.nodes[this.prefix + data.ID]);
  637. }
  638. for(let data of datas){
  639. let node = this.nodes[this.prefix + data.ID];
  640. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  641. node.parent = parent;
  642. if(!parent){
  643. this.roots.push(node);
  644. }
  645. else {
  646. parent.children.push(node);
  647. }
  648. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  649. node.nextSibling = next;
  650. if(next){
  651. next.preSibling = node;
  652. }
  653. }
  654. let preNode = this.nodes[this.prefix + preData.ID];
  655. if(preNode){
  656. preNode.nextSibling = this.nodes[this.prefix + preData.NextSiblingID] ? this.nodes[this.prefix + preData.NextSiblingID] : null;
  657. if(preNode.nextSibling){
  658. preNode.nextSibling.preSibling = preNode;
  659. }
  660. }
  661. //resort
  662. this.roots = tools.reSortNodes(this.roots, true);
  663. tools.sortTreeItems(this);
  664. return rst;
  665. };
  666. Tree.prototype.delete = function (node) {
  667. var success = false, that = this;
  668. if(node) success = that.m_delete([node]);
  669. return success;
  670. };
  671. Tree.prototype.m_delete = function (nodes) {
  672. let success = false, that = this;
  673. let deleteIdIndex = function (nodes) {
  674. nodes.forEach(function (node) {
  675. delete that.nodes[that.prefix + node.getID()];
  676. deleteIdIndex(node.children);
  677. })
  678. };
  679. for(let n of nodes){
  680. deleteIdIndex([n]);
  681. if (n.preSibling) {
  682. n.preSibling.setNextSibling(n.nextSibling);
  683. } else if (n.nextSibling) {
  684. n.nextSibling.preSibling = null;
  685. }
  686. if (n.parent) {
  687. n.parent.children.splice(n.siblingIndex(), 1);
  688. } else {
  689. this.roots.splice(n.siblingIndex(), 1);
  690. }
  691. }
  692. tools.sortTreeItems(this);
  693. success = true;
  694. return success;
  695. };
  696. Tree.prototype.m_upLevel = function (nodes) {//原先的父节点变成前一个节点,原先的兄弟节点变成子节点
  697. let o_parent = nodes[0].parent;//原来的父节点
  698. let o_next = o_parent.nextSibling;//父节点的下一节点
  699. let o_pre = nodes[0].preSibling;
  700. let o_children = o_parent.children;//旧的所有兄弟节点
  701. let children = o_parent.parent?o_parent.parent.children:this.roots;//新的兄弟节点
  702. let last;
  703. let lastNext;//最后一个选中节点后面的所有兄弟节点变成最后一个节点的子节点
  704. for(let i = 0; i<nodes.length;i++){
  705. let index = children.indexOf(o_parent) + 1;
  706. children.splice(index + i,0,nodes[i]);//往新的父节点的子节点插入节点
  707. o_children.splice(nodes[i].siblingIndex(), 1);//旧的数组删除节点
  708. if(i == 0){//第一个节点变成原来父节点的下一节点
  709. o_parent.setNextSibling(nodes[i]);
  710. if(o_pre) o_pre.setNextSibling(null); //第一个选中节点的前一节点的下一节点设置为空
  711. }
  712. nodes[i].setParent(o_parent.parent);
  713. last = nodes[i];
  714. lastNext = last.nextSibling;
  715. }
  716. last.setNextSibling(o_next);//最后一个选中的节点的下一个节点设置为原父节点的下一节点
  717. if(lastNext){
  718. let t_index = o_children.indexOf(lastNext);
  719. for(let j = t_index;j <o_children.length;j++ ){//剩下的添加为最后一个选中节点的子节点
  720. last.addChild(o_children[j]);
  721. }
  722. if(o_children.length > t_index) o_children.splice(t_index, o_children.length - t_index);//从原先的children中移除
  723. }
  724. if (o_parent.parent&& !o_parent.parent.expanded) o_parent.parent.setExpanded(true);
  725. tools.sortTreeItems(this);
  726. return true;
  727. };
  728. Tree.prototype.getUpLevelDatas = function (nodes) {
  729. //getParentID
  730. let o_parentID = nodes[0].getParentID();
  731. let o_children = nodes[0].parent.children;//旧的所有兄弟节点
  732. let o_pre = nodes[0].preSibling;
  733. let new_parentID = nodes[0].parent.getParentID();
  734. let o_nextID = nodes[0].parent.getNextSiblingID();
  735. let dataMap = {},updateDatas=[],lastID,lastNext;
  736. for(let i = 0; i<nodes.length;i++){
  737. if(i == 0){
  738. dataMap[o_parentID] = {"ID":o_parentID,"NextSiblingID":nodes[i].getID()};
  739. if(o_pre) dataMap[o_pre.getID()] = {"ID":o_pre.getID(),"NextSiblingID":-1}; //nodes[i].preSibling.setNextSibling(null);
  740. }
  741. dataMap[nodes[i].getID()] = {"ID":nodes[i].getID(),"ParentID":new_parentID};
  742. lastID = nodes[i].getID();
  743. lastNext = nodes[i].nextSibling;
  744. }
  745. if(dataMap[lastID] !== undefined){
  746. dataMap[lastID].NextSiblingID = o_nextID;
  747. }
  748. if(lastNext){
  749. let t_index = o_children.indexOf(lastNext);
  750. for(let j = t_index;j <o_children.length;j++ ){//剩下的添加为最后一个选中节点的子节点
  751. dataMap[o_children[j].getID()] = {"ID":o_children[j].getID(),"ParentID":lastID};
  752. }
  753. }
  754. for(let key in dataMap){
  755. updateDatas.push({type: 'update', data:dataMap[key]});
  756. }
  757. return updateDatas;
  758. };
  759. Tree.prototype.m_downLevel = function (nodes) {
  760. let pre = nodes[0].preSibling ; //第一个节点的前一节点,即会成为新的父节点
  761. let next ;//最后一个节点的后一节点,会成为pre 的下一个节点
  762. let last ;//选中的最后一个节点,nextSibling要设置为0
  763. for( let n of nodes){
  764. next = n.nextSibling;
  765. last = n;
  766. let children = n.parent?n.parent.children:this.roots;
  767. children.splice(n.siblingIndex(), 1);
  768. pre.addChild(n);
  769. }
  770. if (!pre.expanded) pre.setExpanded(true);
  771. pre.setNextSibling(next);
  772. last.nextSibling = null;
  773. tools.sortTreeItems(this);
  774. return true;
  775. };
  776. Tree.prototype.getDownLevelDatas = function (nodes) {
  777. let dataMap = {},updateDatas=[],nextID,last;//注释同m_downLevel 方法
  778. let newParent = nodes[0].preSibling;//{"type":"update","data":{"ID":3,"ParentID":-1,"NextSiblingID":5}}
  779. let newPre = newParent.children && newParent.children.length > 0 ? newParent.children[newParent.children.length -1]:null;
  780. if(newPre){ //如果新的父节点有子节点,则把新的父节点的最后一个子节点的下一节点的值改成第一个选中节点的ID
  781. dataMap[newPre.getID()] = {"ID":newPre.getID(),"NextSiblingID":nodes[0].getID()}
  782. }
  783. for(let n of nodes){
  784. nextID = n.getNextSiblingID();
  785. last = n;
  786. dataMap[n.getID()] = {"ID":n.getID(),"ParentID":newParent.getID()}//修改父ID;
  787. }
  788. dataMap[newParent.getID()] = {"ID":newParent.getID(),"NextSiblingID":nextID}//设置新的父节点的下一个节点ID;
  789. if(dataMap[last.getID()]!==undefined){//把最后一个节点的下一个节点ID变成-1
  790. dataMap[last.getID()].NextSiblingID = -1
  791. }else {
  792. dataMap[last.getID()] = {"ID":last.getID(),"NextSiblingID":-1};
  793. }
  794. for(let key in dataMap){
  795. updateDatas.push({type: 'update', data:dataMap[key]});
  796. }
  797. return updateDatas;
  798. };
  799. Tree.prototype.getDeleteData = function (node) {
  800. var data = [];
  801. var addUpdateDataForDelete = function (datas, nodes) {
  802. nodes.forEach(function (node) {
  803. var delData = {};
  804. delData[node.tree.setting.id] = node.getID();
  805. datas.push({type: 'delete', data: delData});
  806. addUpdateDataForDelete(datas, node.children);
  807. })
  808. };
  809. if (node) {
  810. addUpdateDataForDelete(data, [node]);
  811. if (node.preSibling) {
  812. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  813. }
  814. }
  815. return data;
  816. };
  817. Tree.prototype.getDeleteDatas = function (deleteMap,deleteNodes){//批量删除
  818. let datas = [];
  819. addDeleteDatas(datas,deleteNodes);
  820. for(let d of deleteNodes){
  821. addPreUpdateData(datas,deleteMap,d.preSibling,d.nextSibling);
  822. }
  823. function addPreUpdateData(updateDatas,map,preSibling,nextSibling) {
  824. if(preSibling && (map[preSibling.getID()] == undefined || map[preSibling.getID()] == null)){
  825. if(nextSibling){
  826. if(map[nextSibling.getID()]){//如果下一个节点也是要删除的,则再往下顺延
  827. addPreUpdateData(updateDatas,map,preSibling,nextSibling.nextSibling);
  828. }else {
  829. updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),nextSibling.getID())});
  830. }
  831. }else {
  832. updateDatas.push({type: 'update', data: preSibling.tree.getDataTemplate(preSibling.getID(), preSibling.getParentID(),-1)});
  833. }
  834. }
  835. }
  836. function addDeleteDatas(dataArray,nodes) {
  837. for(let n of nodes){
  838. let delData = {};
  839. delData[n.tree.setting.id] = n.getID();
  840. dataArray.push({type: 'delete', data: delData});
  841. addDeleteDatas(dataArray,n.children);
  842. }
  843. }
  844. return datas;
  845. };
  846. Tree.prototype.getExpState = function (nodes) {
  847. let sessionExpanded = [];
  848. function getStat(items){
  849. for(let item of items){
  850. sessionExpanded.push(item.expanded ? 1 : 0);
  851. }
  852. }
  853. getStat(nodes);
  854. let expState = sessionExpanded.join('');
  855. return expState;
  856. };
  857. //节点根据展开收起列表'010101'展开收起
  858. Tree.prototype.setExpandedByState = function (nodes, expState) {
  859. let expStateArr = expState.split('');
  860. for(let i = 0; i < nodes.length; i++){
  861. let expanded = expStateArr[i] == 1 ? true : false;
  862. if(nodes[i].expanded === expanded){
  863. continue;
  864. }
  865. nodes[i].setExpanded(expanded);
  866. }
  867. };
  868. // 检查树结构数据有没问题
  869. Tree.prototype.check = function (roots) {
  870. return isValid(roots);
  871. function isValid(nodes) {
  872. for (let node of nodes) {
  873. if (node.data.ParentID != -1 &&
  874. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  875. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  876. return false;
  877. }
  878. if (node.data.ParentID == -1 && node.parent) {
  879. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  880. return false;
  881. }
  882. if (node.data.NextSiblingID != -1 &&
  883. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  884. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  885. return false;
  886. }
  887. if (node.data.NextSiblingID == -1 && node.nextSibling) {
  888. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  889. return false;
  890. }
  891. let sameDepthNodes = node.parent ? node.parent.children : roots,
  892. nodeIdx = sameDepthNodes.indexOf(node),
  893. nextIdx = sameDepthNodes.indexOf(node.nextSibling);
  894. if (nodeIdx != -1 && nextIdx != -1 && nodeIdx > nextIdx) {
  895. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  896. return false;
  897. }
  898. // nextSibling跟parent children的下一节点对应不上
  899. if (nodeIdx != -1 &&
  900. (nodeIdx === sameDepthNodes.length - 1 && nextIdx != -1) ||
  901. (nodeIdx !== sameDepthNodes.length - 1 && nodeIdx + 1 !== nextIdx)) {
  902. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  903. return false;
  904. }
  905. if (node.children.length) {
  906. let v = isValid(node.children);
  907. if (!v) {
  908. return false;
  909. }
  910. }
  911. }
  912. return true;
  913. }
  914. };
  915. /*Tree.prototype.editedData = function (field, id, newText) {
  916. var node = this.findNode(id), result = {allow: false, nodes: []};
  917. if (this.event[this.eventType.editedData]) {
  918. return this.event[this.eventType.editedData](field, node.data);
  919. } else {
  920. node.data[field] = newText;
  921. result.allow = true;
  922. return result;
  923. }
  924. };*/
  925. Tree.prototype.bind = function (eventName, eventFun) {
  926. this.event[eventName] = eventFun;
  927. };
  928. return new Tree(setting);
  929. },
  930. updateType: {update: 'update', new: 'new', delete: 'delete'}
  931. };