idTree.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. // 在nodes中,从iIndex(包括)开始全部移除
  69. removeNodes: function (tree, parent, iIndex, count) {
  70. var children = parent ? parent.children : tree.roots;
  71. var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
  72. var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
  73. if (pre) {
  74. pre.setNextSibling(next);
  75. //pre.nextSibling = next;
  76. }
  77. if (next) {
  78. next.preSibling = pre;
  79. }
  80. if (arguments.length === 4) {
  81. children.splice(iIndex, count);
  82. } else {
  83. children.splice(iIndex, children.length - iIndex);
  84. }
  85. },
  86. // ��nodes������addNodes, λ�ô�index��ʼ
  87. // 在parent.children/tree.roots中增加nodes, 位置从index开始
  88. addNodes: function (tree, parent, nodes, iIndex) {
  89. var children = parent ? parent.children : tree.roots;
  90. var pre, next, i;
  91. if (nodes.length === 0) { return; }
  92. if (arguments.length === 4) {
  93. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex - 1];
  94. next = pre ? pre.nextSibling : null;
  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. }
  102. nodes[0].preSibling = pre;
  103. if (next) {
  104. next.preSibling = nodes[nodes.length - 1];
  105. }
  106. nodes[nodes.length - 1].setNextSibling(next);
  107. for (i = 0; i < nodes.length; i++) {
  108. if (arguments.length === 4) {
  109. children.splice(iIndex + i, 0, nodes[i]);
  110. } else if (arguments.length === 3) {
  111. children.push(nodes[i]);
  112. }
  113. nodes[i].setParent(parent ? parent : null);
  114. }
  115. },
  116. sortTreeItems: function (tree) {
  117. var addItems = function (items) {
  118. var i;
  119. for (i = 0; i < items.length; i++) {
  120. tree.items.push(items[i]);
  121. addItems(items[i].children);
  122. }
  123. };
  124. tree.items.splice(0, tree.items.length);
  125. addItems(tree.roots);
  126. },
  127. addUpdateDataForParent: function (datas, nodes, pid) {
  128. nodes.forEach(function (node) {
  129. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), pid, node.getNextSiblingID())});
  130. });
  131. },
  132. addUpdateDataForNextSibling: function (datas, node, nid) {
  133. if (node) {
  134. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), node.getParentID(), nid)});
  135. }
  136. }
  137. };
  138. var Node = function (tree, data) {
  139. // ���µ����ԣ�����Ԫ�������ֱ���޸�
  140. // 以下的属性,本单元外均不可直接修改
  141. this.tree = tree;
  142. this.data = data;
  143. this.children = [];
  144. this.parent = null;
  145. this.nextSibling = null;
  146. this.preSibling = null;
  147. this.expanded = true;
  148. this.visible = true;
  149. this.visible = true;
  150. };
  151. Node.prototype.getID = function () {
  152. return this.data[this.tree.setting.id];
  153. };
  154. Node.prototype.getParentID = function () {
  155. return this.parent ? this.parent.getID() : -1;
  156. };
  157. Node.prototype.getNextSiblingID = function () {
  158. return this.nextSibling ? this.nextSibling.getID() : -1;
  159. };
  160. Node.prototype.setParent = function (parent) {
  161. this.parent = parent;
  162. if (this.tree.setting.autoUpdate) {
  163. this.data[this.tree.setting.pid] = this.getParentID();
  164. }
  165. };
  166. Node.prototype.setNextSibling = function (nextSibling) {
  167. this.nextSibling = nextSibling;
  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(this.children.re)
  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. }
  328. orgPre.setNextSibling(this.nextSibling);
  329. this.preSibling = orgPre.preSibling;
  330. orgPre.preSibling = this;
  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. }
  360. orgNext.preSibling = this.preSibling;
  361. this.setNextSibling(orgNext.nextSibling);
  362. orgNext.setNextSibling(this);
  363. this.preSibling = orgNext;
  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.loadDatas = function (datas) {
  425. var prefix = this.prefix, i, node, parent, next, that = this;
  426. // prepare index
  427. datas.forEach(function (data) {
  428. var node = new Node(that, data);
  429. that.nodes[prefix + data[that.setting.id]] = node;
  430. that.maxNodeID(data[that.setting.id]);
  431. });
  432. // set parent by pid, set nextSibling by nid
  433. datas.forEach(function (data) {
  434. node = that.nodes[prefix + data[that.setting.id]];
  435. if (data[that.setting.pid] === that.setting.rootId) {
  436. that.roots.push(node);
  437. } else {
  438. parent = that.nodes[prefix + data[that.setting.pid]];
  439. if (parent) {
  440. node.parent = parent;
  441. parent.children.push(node);
  442. }
  443. }
  444. if (data[that.setting.nid] !== that.setting.rootId) {
  445. next = that.nodes[prefix + data[that.setting.nid]];
  446. if (next) {
  447. node.nextSibling = next;
  448. next.preSibling = node;
  449. }
  450. }
  451. })
  452. // sort by nid
  453. this.roots = tools.reSortNodes(this.roots, true);
  454. tools.sortTreeItems(this);
  455. };
  456. Tree.prototype.firstNode = function () {
  457. return this.roots.length === 0 ? null : this.roots[0];
  458. };
  459. Tree.prototype.findNode = function (id) {
  460. return this.nodes[this.prefix + id];
  461. };
  462. Tree.prototype.count = function () {
  463. var iCount = 0;
  464. if (this.roots.length !== 0) {
  465. iCount += this.roots.length;
  466. this.roots.forEach(function (node) {
  467. iCount += node.posterityCount();
  468. });
  469. }
  470. return iCount;
  471. };
  472. Tree.prototype.insert = function (parentID, nextSiblingID) {
  473. var newID = this.newNodeID(), node = null, data = {};
  474. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  475. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  476. if (newID !== -1) {
  477. data = {};
  478. data[this.setting.id] = newID;
  479. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  480. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  481. node = new Node(this, data);
  482. if (nextSibling) {
  483. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  484. } else {
  485. tools.addNodes(this, parent, [node]);
  486. }
  487. this.nodes[this.prefix + newID] = node;
  488. tools.sortTreeItems(this);
  489. this.maxNodeID(newID);
  490. }
  491. return node;
  492. };
  493. Tree.prototype.getInsertData = function (parentID, nextSiblingID) {
  494. var data = [];
  495. var newID = this.newNodeID();
  496. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  497. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  498. if (newID !== -1) {
  499. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  500. if (nextSibling && nextSibling.preSibling) {
  501. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  502. } else if (parent && parent.children.length !== 0) {
  503. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  504. } else if (!parent && this.roots.length !== 0) {
  505. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  506. }
  507. }
  508. return data;
  509. };
  510. Tree.prototype.insertByData = function (data, parentID, nextSiblingID) {
  511. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  512. var nextSibling = nextSiblingID === -1 ? null : this.nodes[this.prefix + nextSiblingID];
  513. var node = this.nodes[this.prefix + data[this.setting.id]];
  514. if (node) {
  515. return node;
  516. } else {
  517. node = new Node(this, data);
  518. if (nextSibling) {
  519. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  520. } else {
  521. tools.addNodes(this, parent, [node]);
  522. }
  523. this.nodes[this.prefix + data[this.setting.id]] = node;
  524. tools.sortTreeItems(this);
  525. this.maxNodeID( data[this.setting.id]);
  526. return node;
  527. }
  528. };
  529. Tree.prototype.delete = function (node) {
  530. var success = false, that = this;
  531. var deleteIdIndex = function (nodes) {
  532. nodes.forEach(function (node) {
  533. delete that.nodes[that.prefix + node.getID()];
  534. deleteIdIndex(node.children);
  535. })
  536. }
  537. if (node) {
  538. deleteIdIndex([node]);
  539. //delete this.nodes[this.prefix + node.getID()];
  540. if (node.preSibling) {
  541. node.preSibling.setNextSibling(node.nextSibling);
  542. }
  543. if (node.nextSibling) {
  544. node.nextSibling.preSibling = node.preSibling;
  545. }
  546. if (node.parent) {
  547. node.parent.children.splice(node.siblingIndex(), 1);
  548. } else {
  549. this.roots.splice(node.siblingIndex(), 1);
  550. }
  551. tools.sortTreeItems(this);
  552. success = true;
  553. }
  554. return success;
  555. };
  556. Tree.prototype.getDeleteData = function (node) {
  557. var data = [];
  558. var addUpdateDataForDelete = function (datas, nodes) {
  559. nodes.forEach(function (node) {
  560. var delData = {};
  561. delData[node.tree.setting.id] = node.getID();
  562. datas.push({type: 'delete', data: delData});
  563. addUpdateDataForDelete(datas, node.children);
  564. })
  565. };
  566. if (node) {
  567. addUpdateDataForDelete(data, [node]);
  568. if (node.preSibling) {
  569. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  570. }
  571. }
  572. return data;
  573. }
  574. /*Tree.prototype.editedData = function (field, id, newText) {
  575. var node = this.findNode(id), result = {allow: false, nodes: []};
  576. if (this.event[this.eventType.editedData]) {
  577. return this.event[this.eventType.editedData](field, node.data);
  578. } else {
  579. node.data[field] = newText;
  580. result.allow = true;
  581. return result;
  582. }
  583. };*/
  584. Tree.prototype.bind = function (eventName, eventFun) {
  585. this.event[eventName] = eventFun;
  586. };
  587. return new Tree(setting);
  588. }
  589. };