id_tree.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /**
  2. * Created by Mai on 2017/3/17.
  3. */
  4. var idTree = {
  5. createNew: function (setting) {
  6. var _setting = {
  7. id: 'id',
  8. pid: 'pid',
  9. nid: 'nid',
  10. rootId: -1,
  11. autoUpdate: false
  12. };
  13. var _eventType = {
  14. editedData: 'editedData'
  15. };
  16. var tools = {
  17. findNode: function (nodes, check) {
  18. for (var i = 0; i < nodes.length; i++) {
  19. if (check(nodes[i])) {
  20. return nodes[i];
  21. }
  22. }
  23. return null;
  24. },
  25. reSortNodes: function (nodes, recursive) {
  26. var temp = [], first;
  27. var findFirstNode = function (nodes) {
  28. return tools.findNode(nodes, function (node) {
  29. return node.preSibling === null;
  30. });
  31. };
  32. var moveNode = function (node, orgArray, newArray, newIndex) {
  33. var next;
  34. orgArray.splice(orgArray.indexOf(node), 1);
  35. newArray.splice(newIndex, 0, node);
  36. if (node.getNextSiblingID() !== -1) {
  37. next = node.nextSibling;
  38. if (next && (orgArray.indexOf(next) >= 0)) {
  39. moveNode(next, orgArray, newArray, newIndex + 1);
  40. }
  41. }
  42. };
  43. if (nodes.length === 0) {
  44. return nodes;
  45. }
  46. if (recursive) {
  47. nodes.forEach(function (node) {
  48. node.children = tools.reSortNodes(node.children, recursive);
  49. });
  50. }
  51. while (nodes.length > 0) {
  52. first = findFirstNode(nodes);
  53. first = first ? first : nodes[0];
  54. moveNode(first, nodes, temp, temp.length);
  55. }
  56. nodes = null;
  57. tools.reSiblingNodes(temp);
  58. return temp;
  59. },
  60. reSiblingNodes: function (nodes) {
  61. var i;
  62. for (i = 0; i < nodes.length; i++) {
  63. nodes[i].preSibling = (i === 0) ? null : nodes[i - 1];
  64. nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
  65. }
  66. },
  67. // 在nodes中,从iIndex(包括)开始全部移除
  68. removeNodes: function (tree, parent, iIndex, count) {
  69. var children = parent ? parent.children : tree.roots;
  70. var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
  71. var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
  72. if (pre) {
  73. pre.setNextSibling(next);
  74. } else if (next) {
  75. next.preSibling = null;
  76. }
  77. if (arguments.length === 4) {
  78. children.splice(iIndex, count);
  79. } else {
  80. children.splice(iIndex, children.length - iIndex);
  81. }
  82. },
  83. // 在parent.children/tree.roots中增加nodes, 位置从index开始
  84. addNodes: function (tree, parent, nodes, iIndex) {
  85. var children = parent ? parent.children : tree.roots;
  86. var pre, next, i;
  87. if (nodes.length === 0) { return; }
  88. if (arguments.length === 4) {
  89. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex-1];
  90. if(pre==null){
  91. next = iIndex==0?children[0]:null;
  92. }else {
  93. next = pre.nextSibling;
  94. }
  95. } else if (arguments.length === 3) {
  96. pre = children.length === 0 ? null : children[children.length - 1];
  97. next = null;
  98. }
  99. if (pre) {
  100. pre.setNextSibling(nodes[0]);
  101. } else {
  102. nodes[0].preSibling = null;
  103. }
  104. nodes[nodes.length - 1].setNextSibling(next);
  105. for (i = 0; i < nodes.length; i++) {
  106. if (arguments.length === 4) {
  107. children.splice(iIndex + i, 0, nodes[i]);
  108. } else if (arguments.length === 3) {
  109. children.push(nodes[i]);
  110. }
  111. nodes[i].setParent(parent ? parent : null);
  112. }
  113. },
  114. sortTreeItems: function (tree) {
  115. var addItems = function (items) {
  116. var i;
  117. for (i = 0; i < items.length; i++) {
  118. tree.items.push(items[i]);
  119. addItems(items[i].children);
  120. }
  121. };
  122. tree.items.splice(0, tree.items.length);
  123. addItems(tree.roots);
  124. },
  125. addUpdateDataForParent: function (datas, nodes, pid) {
  126. nodes.forEach(function (node) {
  127. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), pid, node.getNextSiblingID())});
  128. });
  129. },
  130. addUpdateDataForNextSibling: function (datas, node, nid) {
  131. if (node) {
  132. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), node.getParentID(), nid)});
  133. }
  134. }
  135. };
  136. var Node = function (tree, data) {
  137. // 以下的属性,本单元外均不可直接修改
  138. this.tree = tree;
  139. this.data = data;
  140. this.children = [];
  141. this.updateData={};
  142. this.parent = null;
  143. this.nextSibling = null;
  144. this.preSibling = null;
  145. this.expanded = true;
  146. this.visible = true;
  147. this.visible = true;
  148. };
  149. Node.prototype.getID = function () {
  150. return this.data[this.tree.setting.id];
  151. };
  152. Node.prototype.getParentID = function () {
  153. return this.parent ? this.parent.getID() : -1;
  154. };
  155. Node.prototype.getNextSiblingID = function () {
  156. return this.nextSibling ? this.nextSibling.getID() : -1;
  157. };
  158. Node.prototype.setParent = function (parent) {
  159. this.parent = parent;
  160. if (this.tree.setting.autoUpdate) {
  161. this.data[this.tree.setting.pid] = this.getParentID();
  162. }
  163. };
  164. Node.prototype.setNextSibling = function (nextSibling) {
  165. this.nextSibling = nextSibling;
  166. if (nextSibling) {
  167. nextSibling.preSibling = this;
  168. }
  169. if (this.tree.setting.autoUpdate) {
  170. this.data[this.tree.setting.nid] = this.getNextSiblingID();
  171. }
  172. }
  173. Node.prototype.firstChild = function () {
  174. return this.children.length === 0 ? null : this.children[0];
  175. };
  176. Node.prototype.lastChild = function () {
  177. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  178. };
  179. Node.prototype.depth = function () {
  180. return this.parent ? this.parent.depth() + 1 : 0;
  181. };
  182. Node.prototype.isFirst = function () {
  183. if (this.parent) {
  184. return this.parent.children.indexOf(this) === 0 ? true : false;
  185. } else {
  186. return this.tree.roots.indexOf(this) === 0 ? true : false;
  187. }
  188. };
  189. Node.prototype.isLast = function () {
  190. if (this.parent) {
  191. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  192. } else {
  193. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  194. }
  195. };
  196. Node.prototype.siblingIndex = function () {
  197. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  198. };
  199. Node.prototype.posterityCount = function () {
  200. var iCount = 0;
  201. if (this.children.length !== 0) {
  202. iCount += this.children.length;
  203. this.children.forEach(function (child) {
  204. iCount += child.posterityCount();
  205. });
  206. }
  207. return iCount;
  208. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  209. return x.posterityCount() + y.posterityCount();
  210. }) + node.children.count;*/
  211. };
  212. Node.prototype.setExpanded = function (expanded) {
  213. var setNodesVisible = function (nodes, visible) {
  214. nodes.forEach(function (node) {
  215. node.visible = visible;
  216. setNodesVisible(node.children, visible && node.expanded);
  217. })
  218. };
  219. this.expanded = expanded;
  220. setNodesVisible(this.children, expanded);
  221. };
  222. Node.prototype.setExpandedNoRecur = function (expanded) {
  223. this.expanded = expanded;
  224. this.visible = expanded && this.visible;
  225. for(let node of this.children){
  226. node.visible = expanded && node.visible;
  227. }
  228. };
  229. /*Node.prototype.vis = function () {
  230. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  231. };*/
  232. Node.prototype.serialNo = function () {
  233. return this.tree.items.indexOf(this);
  234. };
  235. Node.prototype.row = function () {
  236. return this.serialNo() + 1;
  237. }
  238. Node.prototype.addChild = function (node) {
  239. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  240. node.parent = this;
  241. if (preSibling) {
  242. preSibling.nextSibling = node;
  243. }
  244. node.preSibling = preSibling;
  245. this.children.push(node);
  246. };
  247. Node.prototype.removeChild = function (node) {
  248. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  249. if (preSibling) {
  250. preSibling.nextSibling = nextSibling;
  251. }
  252. if (nextSibling) {
  253. nextSibling.preSibling = preSibling;
  254. }
  255. this.children.splice(node.siblingIndex, 1);
  256. };
  257. Node.prototype.canUpLevel = function () {
  258. return this.parent ? true : false;
  259. };
  260. Node.prototype.getUpLevelData = function () {
  261. var data = [];
  262. if (this.canUpLevel()) {
  263. if (!this.isLast()) {
  264. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  265. }
  266. if (this.preSibling) {
  267. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  268. }
  269. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  270. let updateData = this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID());
  271. if (!this.isLast()) {
  272. //如果设置了专项暂定,则需要清空
  273. if (this.data.specialProvisional) {
  274. this.data.specialProvisional = '';
  275. updateData.specialProvisional = '';
  276. }
  277. }
  278. data.push({type: 'update', data: updateData});
  279. }
  280. return data;
  281. };
  282. Node.prototype.upLevel = function () {
  283. var result = {success: false, updateDatas: []};
  284. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  285. if (this.canUpLevel()) {
  286. // NextSiblings become child
  287. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  288. // Orginal Parent remove node and nextSiblings
  289. tools.removeNodes(this.tree, this.parent, iIndex);
  290. // New Parent add node
  291. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  292. if (!this.expanded) {
  293. this.setExpanded(true);
  294. }
  295. result.success = true;
  296. }
  297. return result;
  298. };
  299. Node.prototype.canDownLevel = function () {
  300. return !this.isFirst();
  301. };
  302. Node.prototype.getDownLevelData = function () {
  303. var data = [];
  304. if (this.canDownLevel()) {
  305. if (this.preSibling.children.length !== 0) {
  306. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  307. }
  308. let updateData = this.tree.getDataTemplate(this.preSibling.getID(), this.preSibling.getParentID(), this.getNextSiblingID());
  309. if (this.preSibling.data.specialProvisional) {
  310. //如果设置了专项暂定,则需要清空
  311. this.preSibling.data.specialProvisional = '';
  312. updateData.specialProvisional = '';
  313. }
  314. data.push({type: 'update', data: updateData});
  315. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  316. }
  317. return data;
  318. };
  319. Node.prototype.downLevel = function () {
  320. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  321. var newParent = this.preSibling;
  322. if (this.canDownLevel()) {
  323. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  324. tools.addNodes(this.tree, this.preSibling, [this]);
  325. if (!newParent.expanded) {
  326. newParent.setExpanded(true);
  327. }
  328. success = true;
  329. }
  330. return success;
  331. };
  332. Node.prototype.canUpMove = function () {
  333. return !this.isFirst();
  334. };
  335. Node.prototype.getUpMoveData = function () {
  336. var data = [];
  337. if (this.canUpMove()) {
  338. if (this.preSibling.preSibling) {
  339. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  340. }
  341. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  342. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  343. }
  344. return data;
  345. };
  346. Node.prototype.upMove = function () {
  347. var success = false;
  348. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  349. if (this.canUpMove()) {
  350. if (orgPre.preSibling) {
  351. orgPre.preSibling.setNextSibling(this);
  352. } else {
  353. this.preSibling = null;
  354. }
  355. orgPre.setNextSibling(this.nextSibling);
  356. this.setNextSibling(orgPre);
  357. belongArray.splice(iIndex, 1);
  358. belongArray.splice(iIndex - 1, 0, this);
  359. tools.sortTreeItems(this.tree);
  360. success = true;
  361. }
  362. return success;
  363. };
  364. Node.prototype.canDownMove = function () {
  365. return !this.isLast();
  366. };
  367. Node.prototype.getDownMoveData = function () {
  368. var data = [];
  369. if (this.canDownMove()) {
  370. if (this.preSibling) {
  371. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  372. }
  373. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  374. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  375. }
  376. return data;
  377. };
  378. Node.prototype.downMove = function () {
  379. var success = false;
  380. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  381. if (this.canDownMove()) {
  382. if (this.preSibling) {
  383. this.preSibling.setNextSibling(orgNext);
  384. } else if (orgNext) {
  385. orgNext.preSibling = null;
  386. }
  387. this.setNextSibling(orgNext.nextSibling);
  388. orgNext.setNextSibling(this);
  389. belongArray.splice(iIndex, 1);
  390. belongArray.splice(iIndex + 1, 0, this);
  391. tools.sortTreeItems(this.tree);
  392. success = true;
  393. }
  394. return success;
  395. };
  396. // 节点所属固定ID
  397. Node.prototype.getFlag = function () {
  398. if (!this.data || !this.data.flags || !this.data.flags[0] || !this.data.flags[0].flag) {
  399. return 0;
  400. }
  401. return this.data.flags[0].flag;
  402. };
  403. // 节点所属固定ID
  404. Node.prototype.belongToFlag = function () {
  405. let node = this;
  406. while (node) {
  407. if (node.data && node.data.flags && node.data.flags[0] && node.data.flags[0].flag) {
  408. return node.data.flags[0].flag;
  409. }
  410. node = node.parent;
  411. }
  412. return null;
  413. };
  414. // 节点是否属于某些固定ID,会一直向上找,直到找到或到达顶层
  415. Node.prototype.isBelongToFlags = function (flags) {
  416. let node = this;
  417. while (node) {
  418. const flag = node.getFlag();
  419. if (flags.includes(flag)) {
  420. return true;
  421. }
  422. node = node.parent;
  423. }
  424. return false;
  425. }
  426. var Tree = function (setting) {
  427. this.nodes = {};
  428. this.roots = [];
  429. this.items = [];
  430. this.setting = setting;
  431. this.prefix = 'id_';
  432. this.selected = null;
  433. this.event = {};
  434. this.eventType = _eventType;
  435. };
  436. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  437. var data = {};
  438. data[this.setting.id] = id;
  439. data[this.setting.pid] = pid;
  440. data[this.setting.nid] = nid;
  441. return data;
  442. };
  443. Tree.prototype.maxNodeID = (function () {
  444. var maxID = 0;
  445. return function (ID) {
  446. if (arguments.length === 0) {
  447. return maxID;
  448. } else {
  449. maxID = Math.max(maxID, ID);
  450. }
  451. };
  452. })();
  453. Tree.prototype.rangeNodeID = (function () {
  454. var rangeID = -1;
  455. return function (ID) {
  456. if (arguments.length === 0) {
  457. return rangeID;
  458. } else {
  459. rangeID = Math.max(rangeID, ID);
  460. }
  461. }
  462. })();
  463. Tree.prototype.newNodeID = function () {
  464. if (this.rangeNodeID() == -1) {
  465. return this.maxNodeID() + 1;
  466. } else {
  467. if (this.maxNodeID() < this.rangeNodeID()) {
  468. return this.maxNodeID() + 1;
  469. } else {
  470. return -1;
  471. }
  472. }
  473. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  474. return -1;
  475. } else {
  476. return this.maxNodeID() + 1;
  477. }*/
  478. };
  479. Tree.prototype.clearNodes = function () {
  480. this.nodes = {};
  481. this.roots = [];
  482. this.items = [];
  483. };
  484. Tree.prototype.loadDatas = function (datas) {
  485. var prefix = this.prefix, i, node, parent, next, that = this;
  486. this.nodes = {};
  487. this.roots = [];
  488. this.items = [];
  489. // prepare index
  490. datas.forEach(function (data) {
  491. var node = new Node(that, data);
  492. that.nodes[prefix + data[that.setting.id]] = node;
  493. that.maxNodeID(data[that.setting.id]);
  494. });
  495. // set parent by pid, set nextSibling by nid
  496. datas.forEach(function (data) {
  497. node = that.nodes[prefix + data[that.setting.id]];
  498. if (data[that.setting.pid] == that.setting.rootId) {
  499. that.roots.push(node);
  500. } else {
  501. parent = that.nodes[prefix + data[that.setting.pid]];
  502. if (parent) {
  503. node.parent = parent;
  504. parent.children.push(node);
  505. }
  506. }
  507. if (data[that.setting.nid] !== that.setting.rootId) {
  508. next = that.nodes[prefix + data[that.setting.nid]];
  509. if (next) {
  510. node.nextSibling = next;
  511. next.preSibling = node;
  512. }
  513. }
  514. })
  515. // sort by nid
  516. this.roots = tools.reSortNodes(this.roots, true);
  517. tools.sortTreeItems(this);
  518. };
  519. Tree.prototype.firstNode = function () {
  520. return this.roots.length === 0 ? null : this.roots[0];
  521. };
  522. Tree.prototype.findNode = function (id) {
  523. return this.nodes[this.prefix + id];
  524. };
  525. Tree.prototype.count = function () {
  526. var iCount = 0;
  527. if (this.roots.length !== 0) {
  528. iCount += this.roots.length;
  529. this.roots.forEach(function (node) {
  530. iCount += node.posterityCount();
  531. });
  532. }
  533. return iCount;
  534. };
  535. Tree.prototype.insert = function (parentID, nextSiblingID) {
  536. var newID = this.newNodeID(), node = null, data = {};
  537. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  538. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  539. if (newID !== -1) {
  540. data = {};
  541. data[this.setting.id] = newID;
  542. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  543. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  544. node = new Node(this, data);
  545. if (nextSibling) {
  546. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  547. } else {
  548. tools.addNodes(this, parent, [node]);
  549. }
  550. this.nodes[this.prefix + newID] = node;
  551. tools.sortTreeItems(this);
  552. this.maxNodeID(newID);
  553. }
  554. return node;
  555. };
  556. Tree.prototype.insertByID = function (newID, parentID, nextSiblingID) {
  557. var node = null, data = {};
  558. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  559. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  560. if (newID) {
  561. data = {};
  562. data[this.setting.id] = newID;
  563. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  564. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  565. node = new Node(this, data);
  566. if (nextSibling) {
  567. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  568. } else {
  569. tools.addNodes(this, parent, [node]);
  570. }
  571. this.nodes[this.prefix + newID] = node;
  572. tools.sortTreeItems(this);
  573. }
  574. return node;
  575. };
  576. Tree.prototype.getInsertData = function (parentID, nextSiblingID, uid = null) {
  577. var data = [];
  578. var newID = uid ? uuid.v1() : this.newNodeID();
  579. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  580. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  581. if (newID != -1) {
  582. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  583. if (nextSibling && nextSibling.preSibling) {
  584. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  585. } else if (parent && parent.children.length !== 0) {
  586. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  587. } else if (!parent && this.roots.length !== 0) {
  588. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  589. }
  590. }
  591. return data;
  592. };
  593. Tree.prototype.insertByData = function (data, parentID, nextSiblingID, uid = null, resort = true) {
  594. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  595. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  596. var node = this.nodes[this.prefix + data[this.setting.id]];
  597. if (node) {
  598. return node;
  599. } else {
  600. node = new Node(this, data);
  601. if (nextSibling) {
  602. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  603. } else {
  604. tools.addNodes(this, parent, [node]);
  605. }
  606. this.nodes[this.prefix + data[this.setting.id]] = node;
  607. if (resort) {
  608. tools.sortTreeItems(this);
  609. }
  610. if(!uid){
  611. this.maxNodeID( data[this.setting.id]);
  612. }
  613. return node;
  614. }
  615. };
  616. Tree.prototype.multiInsert = function (datas, preID) {
  617. const newNodes = [];
  618. for (const data of datas) {
  619. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  620. this.nodes[this.prefix + data.ID]['data'] = data;
  621. newNodes.push(this.nodes[this.prefix + data.ID]);
  622. }
  623. const fisrtNode = this.nodes[this.prefix + datas[0].ID];
  624. const firstPre = this.nodes[this.prefix + preID];
  625. if (firstPre) {
  626. firstPre.nextSibling = fisrtNode;
  627. firstPre.data.NextSiblingID = fisrtNode.getID();
  628. }
  629. const parent = this.nodes[this.prefix + datas[0].ParentID] || null;
  630. const parentChildren = parent ? parent.children : this.roots;
  631. let baseIndex = firstPre ? parentChildren.indexOf(firstPre) + 1 : 0;
  632. datas.forEach(data => {
  633. const node = this.nodes[this.prefix + data.ID];
  634. node.parent = parent;
  635. parentChildren.splice(baseIndex++, 0, node);
  636. const next = this.nodes[this.prefix + data.NextSiblingID] || null;
  637. node.nextSibling = next;
  638. if (next) {
  639. next.preSibling = node;
  640. }
  641. });
  642. this.roots = tools.reSortNodes(this.roots, true);
  643. tools.sortTreeItems(this);
  644. return newNodes;
  645. }
  646. //批量新增节点,节点已有树结构数据
  647. Tree.prototype.insertByDatas = function (datas) {
  648. for(let data of datas){
  649. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  650. this.nodes[this.prefix + data.ID]['data'] = data;
  651. }
  652. for(let data of datas){
  653. let node = this.nodes[this.prefix + data.ID];
  654. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  655. node.parent = parent;
  656. if(!parent){
  657. this.roots.push(node);
  658. }
  659. else {
  660. parent.children.push(node);
  661. }
  662. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  663. node.nextSibling = next;
  664. if(next){
  665. //将原本nextSibing的前节点的设置为node的上兄弟节点
  666. if (next.preSibling && next.preSibling.nextSibling !== node) {
  667. next.preSibling.nextSibling = node;
  668. next.preSibling.data.NextSiblingID = node.data.ID;
  669. node.preSibling = next.preSibling;
  670. }
  671. next.preSibling = node;
  672. } else {//插入了最末子节点,更新上一个最末子节点
  673. let lastChild = parent ? parent.children[parent.children.length - 2] : this.roots[this.roots.length - 2];
  674. if (lastChild && lastChild.nextSibling !== node) {
  675. lastChild.nextSibling = node;
  676. lastChild.data.NextSiblingID = node.data.ID;
  677. }
  678. }
  679. }
  680. //resort
  681. this.roots = tools.reSortNodes(this.roots, true);
  682. tools.sortTreeItems(this);
  683. };
  684. Tree.prototype.delete = function (node) {
  685. var success = false;
  686. success=this.cascadeRemove(node);
  687. tools.sortTreeItems(this);
  688. return success;
  689. };
  690. Tree.prototype.m_delete=function(nodes){
  691. for(let node of nodes){
  692. this.cascadeRemove(node);
  693. }
  694. tools.sortTreeItems(this);
  695. return true;
  696. };
  697. Tree.prototype.cascadeRemove = function (node){
  698. var success = false, that = this;
  699. var deleteIdIndex = function (nodes) {
  700. nodes.forEach(function (node) {
  701. delete that.nodes[that.prefix + node.getID()];
  702. deleteIdIndex(node.children);
  703. })
  704. }
  705. if (node) {
  706. deleteIdIndex([node]);
  707. //delete this.nodes[this.prefix + node.getID()];
  708. if (node.preSibling) {
  709. node.preSibling.setNextSibling(node.nextSibling);
  710. } else if (node.nextSibling) {
  711. node.nextSibling.preSibling = null;
  712. }
  713. if (node.parent) {
  714. node.parent.children.splice(node.siblingIndex(), 1);
  715. } else {
  716. this.roots.splice(node.siblingIndex(), 1);
  717. }
  718. success = true;
  719. }
  720. return success;
  721. };
  722. Tree.prototype.singleDelete = function (node) {//删除本身不删除子项
  723. let that = this;
  724. let success = false;
  725. delete that.nodes[that.prefix + node.getID()];//删除本身
  726. if (node.parent) {//从父项的子节点中移除
  727. node.parent.children.splice(node.siblingIndex(), 1);
  728. } else {
  729. this.roots.splice(node.siblingIndex(), 1);
  730. }
  731. if(node.children.length>0){
  732. if(node.preSibling){//子项变成前兄弟的子项
  733. for(let c of node.children){
  734. node.preSibling.addChild(c);
  735. }
  736. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  737. let oldChild = node.parent.children;
  738. node.parent.children = [];
  739. for(let c of node.children){
  740. node.parent.addChild(c);
  741. }
  742. for(let oc of oldChild){
  743. node.parent.addChild(oc);
  744. }
  745. }else {//都没有的情况
  746. for(let c of node.children){
  747. node.parent.addChild(c);
  748. }
  749. }
  750. tools.sortTreeItems(this);
  751. success = true;
  752. }
  753. return success;
  754. };
  755. Tree.prototype.getDeleteData = function (node) {
  756. var data = [];
  757. var addUpdateDataForDelete = function (datas, nodes) {
  758. nodes.forEach(function (node) {
  759. var delData = {};
  760. delData[node.tree.setting.id] = node.getID();
  761. datas.push({type: 'delete', data: delData});
  762. addUpdateDataForDelete(datas, node.children);
  763. })
  764. };
  765. if (node) {
  766. addUpdateDataForDelete(data, [node]);
  767. if (node.preSibling) {
  768. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  769. }
  770. }
  771. return data;
  772. };
  773. //非叶子节点默认收起展开
  774. Tree.prototype.setRootExpanded = function(nodes, expanded){
  775. for(let node of nodes){
  776. if(node.children.length > 0){
  777. node.setExpanded(expanded);
  778. this.setRootExpanded(node.children, expanded);
  779. }
  780. }
  781. };
  782. Tree.prototype.getExpState = function (nodes) {
  783. let sessionExpanded = [];
  784. function getStat(items){
  785. for(let item of items){
  786. sessionExpanded.push(item.expanded ? 1 : 0);
  787. }
  788. }
  789. getStat(nodes);
  790. let expState = sessionExpanded.join('');
  791. return expState;
  792. };
  793. //节点根据展开收起列表'010101'展开收起
  794. Tree.prototype.setExpandedByState = function (nodes, expState) {
  795. let expStateArr = expState.split('');
  796. for(let i = 0; i < nodes.length; i++){
  797. let expanded = expStateArr[i] == 1 ? true : false;
  798. if(nodes[i].expanded === expanded){
  799. continue;
  800. }
  801. nodes[i].setExpanded(expanded);
  802. }
  803. };
  804. /*Tree.prototype.editedData = function (field, id, newText) {
  805. var node = this.findNode(id), result = {allow: false, nodes: []};
  806. if (this.event[this.eventType.editedData]) {
  807. return this.event[this.eventType.editedData](field, node.data);
  808. } else {
  809. node.data[field] = newText;
  810. result.allow = true;
  811. return result;
  812. }
  813. };*/
  814. Tree.prototype.bind = function (eventName, eventFun) {
  815. this.event[eventName] = eventFun;
  816. };
  817. Tree.prototype.getNodeByID = function (ID) {
  818. let node = this.nodes[this.prefix+ID];
  819. return node;
  820. };
  821. // nodes必须是有效的同层节点(选中的可升级的节点)
  822. Tree.prototype.getMultiUpLevelData = function (nodes) {
  823. const firstNode = nodes[0];
  824. const lastNode = nodes[nodes.length - 1];
  825. const data = [];
  826. if (!firstNode.canUpLevel()) {
  827. return;
  828. }
  829. if (!lastNode.isLast()) {
  830. const children = lastNode.parent.children.slice(lastNode.siblingIndex() + 1);
  831. tools.addUpdateDataForParent(data, children, lastNode.getID());
  832. }
  833. if (firstNode.preSibling) {
  834. tools.addUpdateDataForNextSibling(data, firstNode.preSibling, this.setting.rootId);
  835. }
  836. tools.addUpdateDataForNextSibling(data, firstNode.parent, firstNode.getID());
  837. const updateData = nodes.map(node => {
  838. const nextID = node === lastNode ? node.parent.getNextSiblingID() : node.getNextSiblingID();
  839. const nodeData = this.getDataTemplate(node.getID(), node.parent.getParentID(), nextID);
  840. return { type: 'update', data: nodeData };
  841. });
  842. data.push(...updateData);
  843. return data;
  844. };
  845. Tree.prototype.getMultiDownLevelData = function (nodes) {
  846. const firstNode = nodes[0];
  847. const lastNode = nodes[nodes.length - 1];
  848. const data = [];
  849. if (!firstNode.canDownLevel()) {
  850. return;
  851. }
  852. if (firstNode.preSibling.children.length) {
  853. tools.addUpdateDataForNextSibling(data, firstNode.preSibling.lastChild(), firstNode.getID());
  854. }
  855. const preSibling = firstNode.preSibling;
  856. const updatePreData = {
  857. type: 'update',
  858. data: this.getDataTemplate(preSibling.getID(), preSibling.getParentID(), lastNode.getNextSiblingID())
  859. };
  860. //如果设置了专项暂定,则需要清空
  861. if (preSibling.data.specialProvisional) {
  862. preSibling.data.specialProvisional = '';
  863. updatePreData.specialProvisional = '';
  864. }
  865. data.push(updatePreData);
  866. const updateData = nodes.map(node => {
  867. const nextID = node === lastNode ? this.setting.rootId : node.getNextSiblingID();
  868. return {
  869. type: 'update',
  870. data: this.getDataTemplate(node.getID(), preSibling.getID(), nextID)
  871. };
  872. });
  873. data.push(...updateData);
  874. return data;
  875. };
  876. Tree.prototype.getMultiUpMoveData = function (nodes) {
  877. const firstNode = nodes[0];
  878. const lastNode = nodes[nodes.length - 1];
  879. const data = [];
  880. if (!firstNode.canUpMove()) {
  881. return;
  882. }
  883. if (firstNode.preSibling.preSibling) {
  884. tools.addUpdateDataForNextSibling(data, firstNode.preSibling.preSibling, firstNode.getID());
  885. }
  886. tools.addUpdateDataForNextSibling(data, firstNode.preSibling, lastNode.getNextSiblingID());
  887. tools.addUpdateDataForNextSibling(data, lastNode, firstNode.preSibling.getID());
  888. return data;
  889. };
  890. Tree.prototype.getMultiDownMoveData = function (nodes) {
  891. const firstNode = nodes[0];
  892. const lastNode = nodes[nodes.length - 1];
  893. const data = [];
  894. if (!lastNode.canDownMove()) {
  895. return;
  896. }
  897. if (firstNode.preSibling) {
  898. tools.addUpdateDataForNextSibling(data, firstNode.preSibling, lastNode.getNextSiblingID());
  899. }
  900. tools.addUpdateDataForNextSibling(data, lastNode, lastNode.nextSibling.getNextSiblingID());
  901. tools.addUpdateDataForNextSibling(data, lastNode.nextSibling, firstNode.getID());
  902. return data;
  903. };
  904. //检查树结构数据有没问题
  905. Tree.prototype.check = function (roots) {
  906. return isValid(roots);
  907. function isValid(nodes) {
  908. for (let node of nodes) {
  909. if (node.data.ParentID !== -1 &&
  910. (!node.parent || node.parent.data.ID !== node.data.ParentID)) {
  911. console.log(`${node.serialNo() + 1}:${node.data.name} parent对应错误`);
  912. return false;
  913. }
  914. if (node.data.ParentID === -1 && node.parent) {
  915. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有parent`);
  916. return false;
  917. }
  918. if (node.data.NextSiblingID !== -1 &&
  919. (!node.nextSibling || node.nextSibling.data.ID !== node.data.NextSiblingID)) {
  920. console.log(`${node.serialNo() + 1}:${node.data.name} next对应错误`);
  921. return false;
  922. }
  923. if (node.data.NextSiblingID === -1 && node.nextSibling) {
  924. console.log(`${node.serialNo() + 1}:${node.data.name} 不应有next`);
  925. return false;
  926. }
  927. let sameDepthNodes = node.parent ? node.parent.children : roots,
  928. nodeIdx = sameDepthNodes.indexOf(node),
  929. nextIdx = sameDepthNodes.indexOf(node.nextSibling);
  930. if (nodeIdx !== -1 && nextIdx !== -1 && nodeIdx > nextIdx) {
  931. console.log(`${node.serialNo() + 1}:${node.data.name} node索引大于next索引`);
  932. return false;
  933. }
  934. // nextSibling跟parent children的下一节点对应不上
  935. if (nodeIdx !== -1 &&
  936. (nodeIdx === sameDepthNodes.length - 1 && nextIdx !== -1) ||
  937. (nodeIdx !== sameDepthNodes.length - 1 && nodeIdx + 1 !== nextIdx)) {
  938. console.log(`${node.serialNo() + 1}:${node.data.name} nextSibling与树显示的下一节点对应不上`);
  939. return false;
  940. }
  941. if (node.children.length) {
  942. let v = isValid(node.children);
  943. if (!v) {
  944. return false;
  945. }
  946. }
  947. }
  948. return true;
  949. }
  950. };
  951. return new Tree(setting);
  952. },
  953. updateType: {update: 'update', new: 'new', delete: 'delete'}
  954. };