id_tree.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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.parent = null;
  142. this.nextSibling = null;
  143. this.preSibling = null;
  144. this.expanded = true;
  145. this.visible = true;
  146. this.visible = true;
  147. };
  148. Node.prototype.getID = function () {
  149. return this.data[this.tree.setting.id];
  150. };
  151. Node.prototype.getParentID = function () {
  152. return this.parent ? this.parent.getID() : -1;
  153. };
  154. Node.prototype.getNextSiblingID = function () {
  155. return this.nextSibling ? this.nextSibling.getID() : -1;
  156. };
  157. Node.prototype.setParent = function (parent) {
  158. this.parent = parent;
  159. if (this.tree.setting.autoUpdate) {
  160. this.data[this.tree.setting.pid] = this.getParentID();
  161. }
  162. };
  163. Node.prototype.setNextSibling = function (nextSibling) {
  164. this.nextSibling = nextSibling;
  165. if (nextSibling) {
  166. nextSibling.preSibling = this;
  167. }
  168. if (this.tree.setting.autoUpdate) {
  169. this.data[this.tree.setting.nid] = this.getNextSiblingID();
  170. }
  171. }
  172. Node.prototype.firstChild = function () {
  173. return this.children.length === 0 ? null : this.children[0];
  174. };
  175. Node.prototype.lastChild = function () {
  176. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  177. };
  178. Node.prototype.depth = function () {
  179. return this.parent ? this.parent.depth() + 1 : 0;
  180. };
  181. Node.prototype.isFirst = function () {
  182. if (this.parent) {
  183. return this.parent.children.indexOf(this) === 0 ? true : false;
  184. } else {
  185. return this.tree.roots.indexOf(this) === 0 ? true : false;
  186. }
  187. };
  188. Node.prototype.isLast = function () {
  189. if (this.parent) {
  190. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  191. } else {
  192. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  193. }
  194. };
  195. Node.prototype.siblingIndex = function () {
  196. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  197. };
  198. Node.prototype.posterityCount = function () {
  199. var iCount = 0;
  200. if (this.children.length !== 0) {
  201. iCount += this.children.length;
  202. this.children.forEach(function (child) {
  203. iCount += child.posterityCount();
  204. });
  205. }
  206. return iCount;
  207. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  208. return x.posterityCount() + y.posterityCount();
  209. }) + node.children.count;*/
  210. };
  211. Node.prototype.setExpanded = function (expanded) {
  212. var setNodesVisible = function (nodes, visible) {
  213. nodes.forEach(function (node) {
  214. node.visible = visible;
  215. setNodesVisible(node.children, visible && node.expanded);
  216. })
  217. };
  218. this.expanded = expanded;
  219. setNodesVisible(this.children, expanded);
  220. };
  221. /*Node.prototype.vis = function () {
  222. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  223. };*/
  224. Node.prototype.serialNo = function () {
  225. return this.tree.items.indexOf(this);
  226. };
  227. Node.prototype.addChild = function (node) {
  228. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  229. node.parent = this;
  230. if (preSibling) {
  231. preSibling.nextSibling = node;
  232. }
  233. node.preSibling = preSibling;
  234. this.children.push(node);
  235. };
  236. Node.prototype.removeChild = function (node) {
  237. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  238. if (preSibling) {
  239. preSibling.nextSibling = nextSibling;
  240. }
  241. if (nextSibling) {
  242. nextSibling.preSibling = preSibling;
  243. }
  244. this.children.splice(node.siblingIndex, 1);
  245. };
  246. Node.prototype.canUpLevel = function () {
  247. return this.parent ? true : false;
  248. };
  249. Node.prototype.getUpLevelData = function () {
  250. var data = [];
  251. if (this.canUpLevel()) {
  252. if (!this.isLast()) {
  253. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  254. }
  255. if (this.preSibling) {
  256. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  257. }
  258. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  259. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
  260. }
  261. return data;
  262. };
  263. Node.prototype.upLevel = function () {
  264. var result = {success: false, updateDatas: []};
  265. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  266. if (this.canUpLevel) {
  267. // NextSiblings become child
  268. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  269. // Orginal Parent remove node and nextSiblings
  270. tools.removeNodes(this.tree, this.parent, iIndex);
  271. // New Parent add node
  272. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  273. if (!this.expanded) {
  274. this.setExpanded(true);
  275. }
  276. result.success = true;
  277. }
  278. return result;
  279. };
  280. Node.prototype.canDownLevel = function () {
  281. return !this.isFirst();
  282. };
  283. Node.prototype.getDownLevelData = function () {
  284. var data = [];
  285. if (this.canDownLevel()) {
  286. if (this.preSibling.children.length !== 0) {
  287. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  288. }
  289. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  290. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  291. }
  292. return data;
  293. };
  294. Node.prototype.downLevel = function () {
  295. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  296. var newParent = this.preSibling;
  297. if (this.canDownLevel()) {
  298. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  299. tools.addNodes(this.tree, this.preSibling, [this]);
  300. if (!newParent.expanded) {
  301. newParent.setExpanded(true);
  302. }
  303. success = true;
  304. }
  305. return success;
  306. };
  307. Node.prototype.canUpMove = function () {
  308. return !this.isFirst();
  309. };
  310. Node.prototype.getUpMoveData = function () {
  311. var data = [];
  312. if (this.canUpMove()) {
  313. if (this.preSibling.preSibling) {
  314. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  315. }
  316. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  317. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  318. }
  319. return data;
  320. };
  321. Node.prototype.upMove = function () {
  322. var success = false;
  323. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  324. if (this.canUpMove()) {
  325. if (orgPre.preSibling) {
  326. orgPre.preSibling.setNextSibling(this);
  327. } else {
  328. this.preSibling = null;
  329. }
  330. orgPre.setNextSibling(this.nextSibling);
  331. this.setNextSibling(orgPre);
  332. belongArray.splice(iIndex, 1);
  333. belongArray.splice(iIndex - 1, 0, this);
  334. tools.sortTreeItems(this.tree);
  335. success = true;
  336. }
  337. return success;
  338. };
  339. Node.prototype.canDownMove = function () {
  340. return !this.isLast();
  341. };
  342. Node.prototype.getDownMoveData = function () {
  343. var data = [];
  344. if (this.canDownMove()) {
  345. if (this.preSibling) {
  346. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  347. }
  348. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  349. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  350. }
  351. return data;
  352. };
  353. Node.prototype.downMove = function () {
  354. var success = false;
  355. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  356. if (this.canDownMove()) {
  357. if (this.preSibling) {
  358. this.preSibling.setNextSibling(orgNext);
  359. } else if (orgNext) {
  360. orgNext.preSibling = null;
  361. }
  362. this.setNextSibling(orgNext.nextSibling);
  363. orgNext.setNextSibling(this);
  364. belongArray.splice(iIndex, 1);
  365. belongArray.splice(iIndex + 1, 0, this);
  366. tools.sortTreeItems(this.tree);
  367. success = true;
  368. }
  369. return success;
  370. };
  371. var Tree = function (setting) {
  372. this.nodes = {};
  373. this.roots = [];
  374. this.items = [];
  375. this.setting = setting;
  376. this.prefix = 'id_';
  377. this.selected = null;
  378. this.event = {};
  379. this.eventType = _eventType;
  380. };
  381. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  382. var data = {};
  383. data[this.setting.id] = id;
  384. data[this.setting.pid] = pid;
  385. data[this.setting.nid] = nid;
  386. return data;
  387. };
  388. Tree.prototype.maxNodeID = (function () {
  389. var maxID = 0;
  390. return function (ID) {
  391. if (arguments.length === 0) {
  392. return maxID;
  393. } else {
  394. maxID = Math.max(maxID, ID);
  395. }
  396. };
  397. })();
  398. Tree.prototype.rangeNodeID = (function () {
  399. var rangeID = -1;
  400. return function (ID) {
  401. if (arguments.length === 0) {
  402. return rangeID;
  403. } else {
  404. rangeID = Math.max(rangeID, ID);
  405. }
  406. }
  407. })();
  408. Tree.prototype.newNodeID = function () {
  409. if (this.rangeNodeID() == -1) {
  410. return this.maxNodeID() + 1;
  411. } else {
  412. if (this.maxNodeID() < this.rangeNodeID()) {
  413. return this.maxNodeID() + 1;
  414. } else {
  415. return -1;
  416. }
  417. }
  418. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  419. return -1;
  420. } else {
  421. return this.maxNodeID() + 1;
  422. }*/
  423. };
  424. Tree.prototype.clearNodes = function () {
  425. this.nodes = {};
  426. this.roots = [];
  427. this.items = [];
  428. };
  429. Tree.prototype.loadDatas = function (datas) {
  430. var prefix = this.prefix, i, node, parent, next, that = this;
  431. this.nodes = {};
  432. this.roots = [];
  433. this.items = [];
  434. // prepare index
  435. datas.forEach(function (data) {
  436. var node = new Node(that, data);
  437. that.nodes[prefix + data[that.setting.id]] = node;
  438. that.maxNodeID(data[that.setting.id]);
  439. });
  440. // set parent by pid, set nextSibling by nid
  441. datas.forEach(function (data) {
  442. node = that.nodes[prefix + data[that.setting.id]];
  443. if (data[that.setting.pid] == that.setting.rootId) {
  444. that.roots.push(node);
  445. } else {
  446. parent = that.nodes[prefix + data[that.setting.pid]];
  447. if (parent) {
  448. node.parent = parent;
  449. parent.children.push(node);
  450. }
  451. }
  452. if (data[that.setting.nid] !== that.setting.rootId) {
  453. next = that.nodes[prefix + data[that.setting.nid]];
  454. if (next) {
  455. node.nextSibling = next;
  456. next.preSibling = node;
  457. }
  458. }
  459. })
  460. // sort by nid
  461. this.roots = tools.reSortNodes(this.roots, true);
  462. tools.sortTreeItems(this);
  463. };
  464. Tree.prototype.firstNode = function () {
  465. return this.roots.length === 0 ? null : this.roots[0];
  466. };
  467. Tree.prototype.findNode = function (id) {
  468. return this.nodes[this.prefix + id];
  469. };
  470. Tree.prototype.count = function () {
  471. var iCount = 0;
  472. if (this.roots.length !== 0) {
  473. iCount += this.roots.length;
  474. this.roots.forEach(function (node) {
  475. iCount += node.posterityCount();
  476. });
  477. }
  478. return iCount;
  479. };
  480. Tree.prototype.insert = function (parentID, nextSiblingID) {
  481. var newID = this.newNodeID(), node = null, data = {};
  482. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  483. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  484. if (newID !== -1) {
  485. data = {};
  486. data[this.setting.id] = newID;
  487. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  488. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  489. node = new Node(this, data);
  490. if (nextSibling) {
  491. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  492. } else {
  493. tools.addNodes(this, parent, [node]);
  494. }
  495. this.nodes[this.prefix + newID] = node;
  496. tools.sortTreeItems(this);
  497. this.maxNodeID(newID);
  498. }
  499. return node;
  500. };
  501. Tree.prototype.getInsertData = function (parentID, nextSiblingID, uid = null) {
  502. var data = [];
  503. var newID = uid ? uuid.v1() : this.newNodeID();
  504. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  505. var nextSibling = nextSiblingID == -1 ? null: this.nodes[this.prefix + nextSiblingID];
  506. if (newID != -1) {
  507. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  508. if (nextSibling && nextSibling.preSibling) {
  509. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  510. } else if (parent && parent.children.length !== 0) {
  511. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  512. } else if (!parent && this.roots.length !== 0) {
  513. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  514. }
  515. }
  516. return data;
  517. };
  518. Tree.prototype.insertByData = function (data, parentID, nextSiblingID, uid = null) {
  519. var parent = parentID == -1 ? null : this.nodes[this.prefix + parentID];
  520. var nextSibling = nextSiblingID == -1 ? null : this.nodes[this.prefix + nextSiblingID];
  521. var node = this.nodes[this.prefix + data[this.setting.id]];
  522. if (node) {
  523. return node;
  524. } else {
  525. node = new Node(this, data);
  526. if (nextSibling) {
  527. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  528. } else {
  529. tools.addNodes(this, parent, [node]);
  530. }
  531. this.nodes[this.prefix + data[this.setting.id]] = node;
  532. tools.sortTreeItems(this);
  533. if(!uid){
  534. this.maxNodeID( data[this.setting.id]);
  535. }
  536. return node;
  537. }
  538. };
  539. //批量新增节点,节点已有树结构数据
  540. Tree.prototype.insertByDatas = function (datas) {
  541. for(let data of datas){
  542. this.nodes[this.prefix + data.ID] = new Node(this, data.ID);
  543. this.nodes[this.prefix + data.ID]['data'] = data;
  544. }
  545. for(let data of datas){
  546. let node = this.nodes[this.prefix + data.ID];
  547. let parent = data.ParentID == -1 ? null : this.nodes[this.prefix + data.ParentID];
  548. node.parent = parent;
  549. if(!parent){
  550. this.roots.push(node);
  551. }
  552. else {
  553. parent.children.push(node);
  554. }
  555. let next = data.NextSiblingID == -1 ? null : this.nodes[this.prefix + data.NextSiblingID];
  556. node.nextSibling = next;
  557. if(next){
  558. next.preSibling = node;
  559. }
  560. }
  561. //resort
  562. this.roots = tools.reSortNodes(this.roots, true);
  563. tools.sortTreeItems(this);
  564. };
  565. Tree.prototype.delete = function (node) {
  566. var success = false;
  567. success=this.cascadeRemove(node);
  568. tools.sortTreeItems(this);
  569. return success;
  570. };
  571. Tree.prototype.m_delete=function(nodes){
  572. for(let node of nodes){
  573. this.cascadeRemove(node);
  574. }
  575. tools.sortTreeItems(this);
  576. return true;
  577. };
  578. Tree.prototype.cascadeRemove = function (node){
  579. var success = false, that = this;
  580. var deleteIdIndex = function (nodes) {
  581. nodes.forEach(function (node) {
  582. delete that.nodes[that.prefix + node.getID()];
  583. deleteIdIndex(node.children);
  584. })
  585. }
  586. if (node) {
  587. deleteIdIndex([node]);
  588. //delete this.nodes[this.prefix + node.getID()];
  589. if (node.preSibling) {
  590. node.preSibling.setNextSibling(node.nextSibling);
  591. } else if (node.nextSibling) {
  592. node.nextSibling.preSibling = null;
  593. }
  594. if (node.parent) {
  595. node.parent.children.splice(node.siblingIndex(), 1);
  596. } else {
  597. this.roots.splice(node.siblingIndex(), 1);
  598. }
  599. success = true;
  600. }
  601. return success;
  602. };
  603. Tree.prototype.singleDelete = function (node) {//删除本身不删除子项
  604. let that = this;
  605. let success = false;
  606. delete that.nodes[that.prefix + node.getID()];//删除本身
  607. if (node.parent) {//从父项的子节点中移除
  608. node.parent.children.splice(node.siblingIndex(), 1);
  609. } else {
  610. this.roots.splice(node.siblingIndex(), 1);
  611. }
  612. if(node.children.length>0){
  613. if(node.preSibling){//子项变成前兄弟的子项
  614. for(let c of node.children){
  615. node.preSibling.addChild(c);
  616. }
  617. }else if(node.nextSibling){//没有前兄弟,有后兄弟
  618. let oldChild = node.parent.children;
  619. node.parent.children = [];
  620. for(let c of node.children){
  621. node.parent.addChild(c);
  622. }
  623. for(let oc of oldChild){
  624. node.parent.addChild(oc);
  625. }
  626. }else {//都没有的情况
  627. for(let c of node.children){
  628. node.parent.addChild(c);
  629. }
  630. }
  631. tools.sortTreeItems(this);
  632. success = true;
  633. }
  634. return success;
  635. };
  636. Tree.prototype.getDeleteData = function (node) {
  637. var data = [];
  638. var addUpdateDataForDelete = function (datas, nodes) {
  639. nodes.forEach(function (node) {
  640. var delData = {};
  641. delData[node.tree.setting.id] = node.getID();
  642. datas.push({type: 'delete', data: delData});
  643. addUpdateDataForDelete(datas, node.children);
  644. })
  645. };
  646. if (node) {
  647. addUpdateDataForDelete(data, [node]);
  648. if (node.preSibling) {
  649. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  650. }
  651. }
  652. return data;
  653. };
  654. //非叶子节点默认收起展开
  655. Tree.prototype.setRootExpanded = function(nodes, expanded){
  656. for(let node of nodes){
  657. if(node.children.length > 0){
  658. node.setExpanded(expanded);
  659. this.setRootExpanded(node.children, expanded);
  660. }
  661. }
  662. };
  663. /*Tree.prototype.editedData = function (field, id, newText) {
  664. var node = this.findNode(id), result = {allow: false, nodes: []};
  665. if (this.event[this.eventType.editedData]) {
  666. return this.event[this.eventType.editedData](field, node.data);
  667. } else {
  668. node.data[field] = newText;
  669. result.allow = true;
  670. return result;
  671. }
  672. };*/
  673. Tree.prototype.bind = function (eventName, eventFun) {
  674. this.event[eventName] = eventFun;
  675. };
  676. Tree.prototype.getNodeByID = function (ID) {
  677. let node = this.nodes[this.prefix+ID];
  678. return node;
  679. };
  680. return new Tree(setting);
  681. },
  682. updateType: {update: 'update', new: 'new', delete: 'delete'}
  683. };