idTree.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. //pre.nextSibling = next;
  75. }
  76. if (next) {
  77. next.preSibling = pre;
  78. }
  79. if (arguments.length === 4) {
  80. children.splice(iIndex, count);
  81. } else {
  82. children.splice(iIndex, children.length - iIndex);
  83. }
  84. },
  85. // 在parent.children/tree.roots中增加nodes, 位置从index开始
  86. addNodes: function (tree, parent, nodes, iIndex) {
  87. var children = parent ? parent.children : tree.roots;
  88. var pre, next, i;
  89. if (nodes.length === 0) { return; }
  90. if (arguments.length === 4) {
  91. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex - 1];
  92. next = pre ? pre.nextSibling : null;
  93. } else if (arguments.length === 3) {
  94. pre = children.length === 0 ? null : children[children.length - 1];
  95. next = null;
  96. }
  97. if (pre) {
  98. pre.setNextSibling(nodes[0]);
  99. }
  100. nodes[0].preSibling = pre;
  101. if (next) {
  102. next.preSibling = nodes[nodes.length - 1];
  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 (this.tree.setting.autoUpdate) {
  166. this.data[this.tree.setting.nid] = this.getNextSiblingID();
  167. }
  168. }
  169. Node.prototype.firstChild = function () {
  170. return this.children.length === 0 ? null : this.children[0];
  171. };
  172. Node.prototype.lastChild = function () {
  173. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  174. };
  175. Node.prototype.depth = function () {
  176. return this.parent ? this.parent.depth() + 1 : 0;
  177. };
  178. Node.prototype.isFirst = function () {
  179. if (this.parent) {
  180. return this.parent.children.indexOf(this) === 0 ? true : false;
  181. } else {
  182. return this.tree.roots.indexOf(this) === 0 ? true : false;
  183. }
  184. };
  185. Node.prototype.isLast = function () {
  186. if (this.parent) {
  187. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  188. } else {
  189. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  190. }
  191. };
  192. Node.prototype.siblingIndex = function () {
  193. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  194. };
  195. Node.prototype.posterityCount = function () {
  196. var iCount = 0;
  197. if (this.children.length !== 0) {
  198. iCount += this.children.length;
  199. this.children.forEach(function (child) {
  200. iCount += child.posterityCount();
  201. });
  202. }
  203. return iCount;
  204. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  205. return x.posterityCount() + y.posterityCount();
  206. }) + node.children.count;*/
  207. };
  208. Node.prototype.setExpanded = function (expanded) {
  209. var setNodesVisible = function (nodes, visible) {
  210. nodes.forEach(function (node) {
  211. node.visible = visible;
  212. setNodesVisible(node.children, visible && node.expanded);
  213. })
  214. };
  215. this.expanded = expanded;
  216. setNodesVisible(this.children, expanded);
  217. };
  218. /*Node.prototype.vis = function () {
  219. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  220. };*/
  221. Node.prototype.serialNo = function () {
  222. return this.tree.items.indexOf(this);
  223. };
  224. Node.prototype.addChild = function (node) {
  225. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  226. node.parent = this;
  227. if (preSibling) {
  228. preSibling.nextSibling = node;
  229. }
  230. node.preSibling = preSibling;
  231. this.children.push(node);
  232. };
  233. Node.prototype.removeChild = function (node) {
  234. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  235. if (preSibling) {
  236. preSibling.nextSibling = nextSibling;
  237. }
  238. if (nextSibling) {
  239. nextSibling.preSibling = preSibling;
  240. }
  241. this.children.splice(this.children.re)
  242. };
  243. Node.prototype.canUpLevel = function () {
  244. return this.parent ? true : false;
  245. };
  246. Node.prototype.getUpLevelData = function () {
  247. var data = [];
  248. if (this.canUpLevel()) {
  249. if (!this.isLast()) {
  250. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  251. }
  252. if (this.preSibling) {
  253. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  254. }
  255. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  256. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
  257. }
  258. return data;
  259. };
  260. Node.prototype.upLevel = function () {
  261. var result = {success: false, updateDatas: []};
  262. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  263. if (this.canUpLevel) {
  264. // NextSiblings become child
  265. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  266. // Orginal Parent remove node and nextSiblings
  267. tools.removeNodes(this.tree, this.parent, iIndex);
  268. // New Parent add node
  269. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  270. if (!this.expanded) {
  271. this.setExpanded(true);
  272. }
  273. result.success = true;
  274. }
  275. return result;
  276. };
  277. Node.prototype.canDownLevel = function () {
  278. return !this.isFirst();
  279. };
  280. Node.prototype.getDownLevelData = function () {
  281. var data = [];
  282. if (this.canDownLevel()) {
  283. if (this.preSibling.children.length !== 0) {
  284. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  285. }
  286. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  287. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  288. }
  289. return data;
  290. };
  291. Node.prototype.downLevel = function () {
  292. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  293. var newParent = this.preSibling;
  294. if (this.canDownLevel()) {
  295. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  296. tools.addNodes(this.tree, this.preSibling, [this]);
  297. if (!newParent.expanded) {
  298. newParent.setExpanded(true);
  299. }
  300. success = true;
  301. }
  302. return success;
  303. };
  304. Node.prototype.canUpMove = function () {
  305. return !this.isFirst();
  306. };
  307. Node.prototype.getUpMoveData = function () {
  308. var data = [];
  309. if (this.canUpMove()) {
  310. if (this.preSibling.preSibling) {
  311. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  312. }
  313. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  314. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  315. }
  316. return data;
  317. };
  318. Node.prototype.upMove = function () {
  319. var success = false;
  320. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  321. if (this.canUpMove()) {
  322. if (orgPre.preSibling) {
  323. orgPre.preSibling.setNextSibling(this);
  324. }
  325. orgPre.setNextSibling(this.nextSibling);
  326. this.preSibling = orgPre.preSibling;
  327. orgPre.preSibling = this;
  328. this.setNextSibling(orgPre);
  329. belongArray.splice(iIndex, 1);
  330. belongArray.splice(iIndex - 1, 0, this);
  331. tools.sortTreeItems(this.tree);
  332. success = true;
  333. }
  334. return success;
  335. };
  336. Node.prototype.canDownMove = function () {
  337. return !this.isLast();
  338. };
  339. Node.prototype.getDownMoveData = function () {
  340. var data = [];
  341. if (this.canDownMove()) {
  342. if (this.preSibling) {
  343. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  344. }
  345. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  346. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  347. }
  348. return data;
  349. };
  350. Node.prototype.downMove = function () {
  351. var success = false;
  352. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  353. if (this.canDownMove()) {
  354. if (this.preSibling) {
  355. this.preSibling.setNextSibling(orgNext);
  356. }
  357. orgNext.preSibling = this.preSibling;
  358. this.setNextSibling(orgNext.nextSibling);
  359. orgNext.setNextSibling(this);
  360. this.preSibling = orgNext;
  361. belongArray.splice(iIndex, 1);
  362. belongArray.splice(iIndex + 1, 0, this);
  363. tools.sortTreeItems(this.tree);
  364. success = true;
  365. }
  366. return success;
  367. };
  368. var Tree = function (setting) {
  369. this.nodes = {};
  370. this.roots = [];
  371. this.items = [];
  372. this.setting = setting;
  373. this.prefix = 'id_';
  374. this.selected = null;
  375. this.event = {};
  376. this.eventType = _eventType;
  377. };
  378. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  379. var data = {};
  380. data[this.setting.id] = id;
  381. data[this.setting.pid] = pid;
  382. data[this.setting.nid] = nid;
  383. return data;
  384. };
  385. Tree.prototype.maxNodeID = (function () {
  386. var maxID = 0;
  387. return function (ID) {
  388. if (arguments.length === 0) {
  389. return maxID;
  390. } else {
  391. maxID = Math.max(maxID, ID);
  392. }
  393. };
  394. })();
  395. Tree.prototype.rangeNodeID = (function () {
  396. var rangeID = -1;
  397. return function (ID) {
  398. if (arguments.length === 0) {
  399. return rangeID;
  400. } else {
  401. rangeID = Math.max(rangeID, ID);
  402. }
  403. }
  404. })();
  405. Tree.prototype.newNodeID = function () {
  406. if (this.rangeNodeID() === -1) {
  407. return this.maxNodeID() + 1;
  408. } else {
  409. if (this.maxNodeID() < this.rangeNodeID()) {
  410. return this.maxNodeID() + 1;
  411. } else {
  412. return -1;
  413. }
  414. }
  415. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  416. return -1;
  417. } else {
  418. return this.maxNodeID() + 1;
  419. }*/
  420. };
  421. Tree.prototype.clearNodes = function () {
  422. this.nodes = {};
  423. this.roots = [];
  424. this.items = [];
  425. };
  426. Tree.prototype.loadDatas = function (datas) {
  427. var prefix = this.prefix, i, node, parent, next, that = this;
  428. this.nodes = {};
  429. this.roots = [];
  430. this.items = [];
  431. // prepare index
  432. datas.forEach(function (data) {
  433. var node = new Node(that, data);
  434. that.nodes[prefix + data[that.setting.id]] = node;
  435. that.maxNodeID(data[that.setting.id]);
  436. });
  437. // set parent by pid, set nextSibling by nid
  438. datas.forEach(function (data) {
  439. node = that.nodes[prefix + data[that.setting.id]];
  440. if (data[that.setting.pid] === that.setting.rootId) {
  441. that.roots.push(node);
  442. } else {
  443. parent = that.nodes[prefix + data[that.setting.pid]];
  444. if (parent) {
  445. node.parent = parent;
  446. parent.children.push(node);
  447. }
  448. }
  449. if (data[that.setting.nid] !== that.setting.rootId) {
  450. next = that.nodes[prefix + data[that.setting.nid]];
  451. if (next) {
  452. node.nextSibling = next;
  453. next.preSibling = node;
  454. }
  455. }
  456. })
  457. // sort by nid
  458. this.roots = tools.reSortNodes(this.roots, true);
  459. tools.sortTreeItems(this);
  460. };
  461. Tree.prototype.firstNode = function () {
  462. return this.roots.length === 0 ? null : this.roots[0];
  463. };
  464. Tree.prototype.findNode = function (id) {
  465. return this.nodes[this.prefix + id];
  466. };
  467. Tree.prototype.count = function () {
  468. var iCount = 0;
  469. if (this.roots.length !== 0) {
  470. iCount += this.roots.length;
  471. this.roots.forEach(function (node) {
  472. iCount += node.posterityCount();
  473. });
  474. }
  475. return iCount;
  476. };
  477. Tree.prototype.insert = function (parentID, nextSiblingID) {
  478. var newID = this.newNodeID(), node = null, data = {};
  479. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  480. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  481. if (newID !== -1) {
  482. data = {};
  483. data[this.setting.id] = newID;
  484. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  485. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  486. node = new Node(this, data);
  487. if (nextSibling) {
  488. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  489. } else {
  490. tools.addNodes(this, parent, [node]);
  491. }
  492. this.nodes[this.prefix + newID] = node;
  493. tools.sortTreeItems(this);
  494. this.maxNodeID(newID);
  495. }
  496. return node;
  497. };
  498. Tree.prototype.getInsertData = function (parentID, nextSiblingID) {
  499. var data = [];
  500. var newID = this.newNodeID();
  501. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  502. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  503. if (newID !== -1) {
  504. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  505. if (nextSibling && nextSibling.preSibling) {
  506. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  507. } else if (parent && parent.children.length !== 0) {
  508. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  509. } else if (!parent && this.roots.length !== 0) {
  510. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  511. }
  512. }
  513. return data;
  514. };
  515. Tree.prototype.insertByData = function (data, parentID, nextSiblingID) {
  516. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  517. var nextSibling = nextSiblingID === -1 ? null : this.nodes[this.prefix + nextSiblingID];
  518. var node = this.nodes[this.prefix + data[this.setting.id]];
  519. if (node) {
  520. return node;
  521. } else {
  522. node = new Node(this, data);
  523. if (nextSibling) {
  524. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  525. } else {
  526. tools.addNodes(this, parent, [node]);
  527. }
  528. this.nodes[this.prefix + data[this.setting.id]] = node;
  529. tools.sortTreeItems(this);
  530. this.maxNodeID( data[this.setting.id]);
  531. return node;
  532. }
  533. };
  534. Tree.prototype.delete = function (node) {
  535. var success = false, that = this;
  536. var deleteIdIndex = function (nodes) {
  537. nodes.forEach(function (node) {
  538. delete that.nodes[that.prefix + node.getID()];
  539. deleteIdIndex(node.children);
  540. })
  541. }
  542. if (node) {
  543. deleteIdIndex([node]);
  544. //delete this.nodes[this.prefix + node.getID()];
  545. if (node.preSibling) {
  546. node.preSibling.setNextSibling(node.nextSibling);
  547. }
  548. if (node.nextSibling) {
  549. node.nextSibling.preSibling = node.preSibling;
  550. }
  551. if (node.parent) {
  552. node.parent.children.splice(node.siblingIndex(), 1);
  553. } else {
  554. this.roots.splice(node.siblingIndex(), 1);
  555. }
  556. tools.sortTreeItems(this);
  557. success = true;
  558. }
  559. return success;
  560. };
  561. Tree.prototype.getDeleteData = function (node) {
  562. var data = [];
  563. var addUpdateDataForDelete = function (datas, nodes) {
  564. nodes.forEach(function (node) {
  565. var delData = {};
  566. delData[node.tree.setting.id] = node.getID();
  567. datas.push({type: 'delete', data: delData});
  568. addUpdateDataForDelete(datas, node.children);
  569. })
  570. };
  571. if (node) {
  572. addUpdateDataForDelete(data, [node]);
  573. if (node.preSibling) {
  574. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  575. }
  576. }
  577. return data;
  578. };
  579. /*Tree.prototype.editedData = function (field, id, newText) {
  580. var node = this.findNode(id), result = {allow: false, nodes: []};
  581. if (this.event[this.eventType.editedData]) {
  582. return this.event[this.eventType.editedData](field, node.data);
  583. } else {
  584. node.data[field] = newText;
  585. result.allow = true;
  586. return result;
  587. }
  588. };*/
  589. Tree.prototype.bind = function (eventName, eventFun) {
  590. this.event[eventName] = eventFun;
  591. };
  592. return new Tree(setting);
  593. }
  594. };