idTree.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. };
  12. var _eventType = {
  13. editedData: 'editedData'
  14. };
  15. var tools = {
  16. findNode: function (nodes, check) {
  17. for (var i = 0; i < nodes.length; i++) {
  18. if (check(nodes[i])) {
  19. return nodes[i];
  20. }
  21. }
  22. return null;
  23. },
  24. reSortNodes: function (nodes, recursive) {
  25. var temp = [], first;
  26. var findFirstNode = function (nodes) {
  27. return tools.findNode(nodes, function (node) {
  28. return node.preSibling === null;
  29. });
  30. };
  31. var moveNode = function (node, orgArray, newArray, newIndex) {
  32. var next;
  33. orgArray.splice(orgArray.indexOf(node), 1);
  34. newArray.splice(newIndex, 0, node);
  35. if (node.getNextSiblingID() !== -1) {
  36. next = node.nextSibling;
  37. if (next && (orgArray.indexOf(next) >= 0)) {
  38. moveNode(next, orgArray, newArray, newIndex + 1);
  39. }
  40. }
  41. };
  42. if (nodes.length === 0) {
  43. return nodes;
  44. }
  45. if (recursive) {
  46. nodes.forEach(function (node) {
  47. node.children = tools.reSortNodes(node.children, recursive);
  48. });
  49. }
  50. while (nodes.length > 0) {
  51. first = findFirstNode(nodes);
  52. first = first ? first : nodes[0];
  53. moveNode(first, nodes, temp, temp.length);
  54. }
  55. nodes = null;
  56. tools.reSiblingNodes(temp);
  57. return temp;
  58. },
  59. reSiblingNodes: function (nodes) {
  60. var i;
  61. for (i = 0; i < nodes.length; i++) {
  62. nodes[i].preSibling = (i === 0) ? null : nodes[i - 1];
  63. nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
  64. }
  65. },
  66. // ��nodes�У���iIndex����������ʼȫ���Ƴ�
  67. removeNodes: function (tree, parent, iIndex, count) {
  68. var children = parent ? parent.children : tree.roots;
  69. var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
  70. var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
  71. if (pre) {
  72. pre.nextSibling = next;
  73. }
  74. if (next) {
  75. next.preSibling = pre;
  76. }
  77. if (arguments.length === 4) {
  78. children.splice(iIndex, count);
  79. } else {
  80. children.splice(iIndex, children.length - iIndex);
  81. }
  82. },
  83. // ��nodes������addNodes, λ�ô�index��ʼ
  84. addNodes: function (tree, parent, nodes, iIndex) {
  85. var children = parent ? parent.children : tree.roots;
  86. var pre, next, i;
  87. if (nodes.length === 0) { return; }
  88. if (arguments.length === 4) {
  89. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex - 1];
  90. next = pre ? pre.nextSibling : null;
  91. } else if (arguments.length === 3) {
  92. pre = children.length === 0 ? null : children[children.length - 1];
  93. next = null;
  94. }
  95. if (pre) {
  96. pre.nextSibling = nodes[0];
  97. }
  98. nodes[0].preSibling = pre;
  99. if (next) {
  100. next.preSibling = nodes[nodes.length - 1];
  101. }
  102. nodes[nodes.length - 1].nextSibling = next;
  103. for (i = 0; i < nodes.length; i++) {
  104. if (arguments.length === 4) {
  105. children.splice(iIndex + i, 0, nodes[i]);
  106. } else if (arguments.length === 3) {
  107. children.push(nodes[i]);
  108. }
  109. nodes[i].parent = parent ? parent : null;
  110. }
  111. },
  112. sortTreeItems: function (tree) {
  113. var addItems = function (items) {
  114. var i;
  115. for (i = 0; i < items.length; i++) {
  116. tree.items.push(items[i]);
  117. addItems(items[i].children);
  118. }
  119. };
  120. tree.items.splice(0, tree.items.length);
  121. addItems(tree.roots);
  122. },
  123. addUpdateDataForParent: function (datas, nodes, pid) {
  124. nodes.forEach(function (node) {
  125. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), pid, node.getNextSiblingID())});
  126. });
  127. },
  128. addUpdateDataForNextSibling: function (datas, node, nid) {
  129. if (node) {
  130. datas.push({type: 'update', data: node.tree.getDataTemplate(node.getID(), node.getParentID(), nid)});
  131. }
  132. }
  133. };
  134. var Node = function (tree, data) {
  135. // ���µ����ԣ�����Ԫ�������ֱ���޸�
  136. this.tree = tree;
  137. this.data = data;
  138. this.children = [];
  139. this.parent = null;
  140. this.nextSibling = null;
  141. this.preSibling = null;
  142. this.expanded = true;
  143. this.visible = true;
  144. this.visible = true;
  145. this.jobs = [];
  146. this.items = [];
  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.firstChild = function () {
  158. return this.children.length === 0 ? null : this.children[0];
  159. };
  160. Node.prototype.lastChild = function () {
  161. return this.children.length === 0 ? null : this.children[this.children.length - 1];
  162. };
  163. Node.prototype.depth = function () {
  164. return this.parent ? this.parent.depth() + 1 : 0;
  165. };
  166. Node.prototype.isFirst = function () {
  167. if (this.parent) {
  168. return this.parent.children.indexOf(this) === 0 ? true : false;
  169. } else {
  170. return this.tree.roots.indexOf(this) === 0 ? true : false;
  171. }
  172. };
  173. Node.prototype.isLast = function () {
  174. if (this.parent) {
  175. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  176. } else {
  177. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  178. }
  179. };
  180. Node.prototype.siblingIndex = function () {
  181. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  182. };
  183. Node.prototype.posterityCount = function () {
  184. var iCount = 0;
  185. if (this.children.length !== 0) {
  186. iCount += this.children.length;
  187. this.children.forEach(function (child) {
  188. iCount += child.posterityCount();
  189. });
  190. }
  191. return iCount;
  192. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  193. return x.posterityCount() + y.posterityCount();
  194. }) + node.children.count;*/
  195. };
  196. Node.prototype.setExpanded = function (expanded) {
  197. var setNodesVisible = function (nodes, visible) {
  198. nodes.forEach(function (node) {
  199. node.visible = visible;
  200. setNodesVisible(node.children, visible && node.expanded);
  201. })
  202. };
  203. this.expanded = expanded;
  204. setNodesVisible(this.children, expanded);
  205. };
  206. /*Node.prototype.vis = function () {
  207. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  208. };*/
  209. Node.prototype.serialNo = function () {
  210. return this.tree.items.indexOf(this);
  211. };
  212. Node.prototype.addChild = function (node) {
  213. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  214. node.parent = this;
  215. if (preSibling) {
  216. preSibling.nextSibling = node;
  217. }
  218. node.preSibling = preSibling;
  219. this.children.push(node);
  220. };
  221. Node.prototype.removeChild = function (node) {
  222. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  223. if (preSibling) {
  224. preSibling.nextSibling = nextSibling;
  225. }
  226. if (nextSibling) {
  227. nextSibling.preSibling = preSibling;
  228. }
  229. this.children.splice(this.children.re)
  230. };
  231. Node.prototype.canUpLevel = function () {
  232. return this.parent ? true : false;
  233. };
  234. Node.prototype.getUpLevelData = function () {
  235. var data = [];
  236. if (this.canUpLevel()) {
  237. if (!this.isLast()) {
  238. tools.addUpdateDataForParent(data, this.parent.children.slice(this.siblingIndex() + 1), this.getID());
  239. }
  240. if (this.preSibling) {
  241. tools.addUpdateDataForNextSibling(data, this.preSibling, this.tree.setting.rootId);
  242. }
  243. tools.addUpdateDataForNextSibling(data, this.parent, this.getID());
  244. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.parent.getParentID(), this.parent.getNextSiblingID())});
  245. }
  246. return data;
  247. };
  248. Node.prototype.upLevel = function () {
  249. var result = {success: false, updateDatas: []};
  250. var iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  251. if (this.canUpLevel) {
  252. // NextSiblings become child
  253. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  254. // Orginal Parent remove node and nextSiblings
  255. tools.removeNodes(this.tree, this.parent, iIndex);
  256. // New Parent add node
  257. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  258. if (!this.expanded) {
  259. this.setExpanded(true);
  260. }
  261. result.success = true;
  262. }
  263. return result;
  264. };
  265. Node.prototype.canDownLevel = function () {
  266. return !this.isFirst();
  267. };
  268. Node.prototype.getDownLevelData = function () {
  269. var data = [];
  270. if (this.canDownLevel()) {
  271. if (this.preSibling.children.length !== 0) {
  272. tools.addUpdateDataForNextSibling(data, this.preSibling.lastChild(), this.getID());
  273. }
  274. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  275. data.push({type: 'update', data: this.tree.getDataTemplate(this.getID(), this.preSibling.getID(), this.tree.setting.rootId)});
  276. }
  277. return data;
  278. };
  279. Node.prototype.downLevel = function () {
  280. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  281. var newParent = this.preSibling;
  282. if (this.canDownLevel()) {
  283. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  284. tools.addNodes(this.tree, this.preSibling, [this]);
  285. if (!newParent.expanded) {
  286. newParent.setExpanded(true);
  287. }
  288. success = true;
  289. }
  290. return success;
  291. };
  292. Node.prototype.canUpMove = function () {
  293. return !this.isFirst();
  294. };
  295. Node.prototype.getUpMoveData = function () {
  296. var data = [];
  297. if (this.canUpMove()) {
  298. if (this.preSibling.preSibling) {
  299. tools.addUpdateDataForNextSibling(data, this.preSibling.preSibling, this.getID());
  300. }
  301. tools.addUpdateDataForNextSibling(data, this.preSibling, this.getNextSiblingID());
  302. tools.addUpdateDataForNextSibling(data, this, this.preSibling.getID());
  303. }
  304. return data;
  305. };
  306. Node.prototype.upMove = function () {
  307. var success = false;
  308. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  309. if (this.canUpMove()) {
  310. if (orgPre.preSibling) {
  311. orgPre.preSibling.nextSibling = this;
  312. }
  313. orgPre.nextSibling = this.nextSibling;
  314. this.preSibling = orgPre.preSibling;
  315. orgPre.preSibling = this;
  316. this.nextSibling = orgPre;
  317. belongArray.splice(iIndex, 1);
  318. belongArray.splice(iIndex - 1, 0, this);
  319. tools.sortTreeItems(this.tree);
  320. success = true;
  321. }
  322. return success;
  323. };
  324. Node.prototype.canDownMove = function () {
  325. return !this.isLast();
  326. };
  327. Node.prototype.getDownMoveData = function () {
  328. var data = [];
  329. if (this.canDownMove()) {
  330. if (this.preSibling) {
  331. tools.addUpdateDataForNextSibling(data, this.preSibling, this.nextSibling.getID());
  332. }
  333. tools.addUpdateDataForNextSibling(data, this, this.nextSibling.getNextSiblingID());
  334. tools.addUpdateDataForNextSibling(data, this.nextSibling, this.getID());
  335. }
  336. return data;
  337. };
  338. Node.prototype.downMove = function () {
  339. var success = false;
  340. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  341. if (this.canDownMove()) {
  342. if (this.preSibling) {
  343. this.preSibling.nextSibling = orgNext;
  344. }
  345. orgNext.preSibling = this.preSibling;
  346. this.nextSibling = orgNext.nextSibling;
  347. orgNext.nextSibling = this;
  348. this.preSibling = orgNext;
  349. belongArray.splice(iIndex, 1);
  350. belongArray.splice(iIndex + 1, 0, this);
  351. tools.sortTreeItems(this.tree);
  352. success = true;
  353. }
  354. return success;
  355. };
  356. var Tree = function (setting) {
  357. this.nodes = {};
  358. this.roots = [];
  359. this.items = [];
  360. this.setting = setting;
  361. this.prefix = 'id_';
  362. this.selected = null;
  363. this.event = {};
  364. this.eventType = _eventType;
  365. };
  366. Tree.prototype.getDataTemplate = function (id, pid, nid) {
  367. var data = {};
  368. data[this.setting.id] = id;
  369. data[this.setting.pid] = pid;
  370. data[this.setting.nid] = nid;
  371. return data;
  372. };
  373. Tree.prototype.maxNodeID = (function () {
  374. var maxID = 0;
  375. return function (ID) {
  376. if (arguments.length === 0) {
  377. return maxID;
  378. } else {
  379. maxID = Math.max(maxID, ID);
  380. }
  381. };
  382. })();
  383. Tree.prototype.rangeNodeID = (function () {
  384. var rangeID = -1;
  385. return function (ID) {
  386. if (arguments.length === 0) {
  387. return rangeID;
  388. } else {
  389. rangeID = Math.max(rangeID, ID);
  390. }
  391. }
  392. })();
  393. Tree.prototype.newNodeID = function () {
  394. if (this.rangeNodeID() === -1) {
  395. return this.maxNodeID() + 1;
  396. } else {
  397. if (this.maxNodeID() < this.rangeNodeID()) {
  398. return this.maxNodeID() + 1;
  399. } else {
  400. return -1;
  401. }
  402. }
  403. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  404. return -1;
  405. } else {
  406. return this.maxNodeID() + 1;
  407. }*/
  408. };
  409. Tree.prototype.loadDatas = function (datas) {
  410. var prefix = this.prefix, i, node, parent, next, that = this;
  411. // prepare index
  412. datas.forEach(function (data) {
  413. var node = new Node(that, data);
  414. that.nodes[prefix + data[that.setting.id]] = node;
  415. that.maxNodeID(data[that.setting.id]);
  416. });
  417. // set parent by pid, set nextSibling by nid
  418. datas.forEach(function (data) {
  419. node = that.nodes[prefix + data[that.setting.id]];
  420. if (data[that.setting.pid] === that.setting.rootId) {
  421. that.roots.push(node);
  422. } else {
  423. parent = that.nodes[prefix + data[that.setting.pid]];
  424. if (parent) {
  425. node.parent = parent;
  426. parent.children.push(node);
  427. }
  428. }
  429. if (data[that.setting.nid] !== that.setting.rootId) {
  430. next = that.nodes[prefix + data[that.setting.nid]];
  431. if (next) {
  432. node.nextSibling = next;
  433. next.preSibling = node;
  434. }
  435. }
  436. })
  437. // sort by nid
  438. this.roots = tools.reSortNodes(this.roots, true);
  439. tools.sortTreeItems(this);
  440. };
  441. Tree.prototype.firstNode = function () {
  442. return this.roots.length === 0 ? null : this.roots[0];
  443. };
  444. Tree.prototype.findNode = function (id) {
  445. return this.nodes[this.prefix + id];
  446. };
  447. Tree.prototype.count = function () {
  448. var iCount = 0;
  449. if (this.roots.length !== 0) {
  450. iCount += this.roots.length;
  451. this.roots.forEach(function (node) {
  452. iCount += node.posterityCount();
  453. });
  454. }
  455. return iCount;
  456. };
  457. Tree.prototype.insert = function (parentID, nextSiblingID) {
  458. var newID = this.newNodeID(), node = null, data = {};
  459. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  460. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  461. if (newID !== -1) {
  462. data = {};
  463. data[this.setting.id] = newID;
  464. data[this.setting.pid] = parent ? parent.getID() : this.setting.rootId;
  465. data[this.setting.nid] = nextSibling ? nextSibling.getID() : this.setting.rootId;
  466. node = new Node(this, data);
  467. if (nextSibling) {
  468. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  469. } else {
  470. tools.addNodes(this, parent, [node]);
  471. }
  472. this.nodes[this.prefix + newID] = node;
  473. tools.sortTreeItems(this);
  474. this.maxNodeID(newID);
  475. }
  476. return node;
  477. };
  478. Tree.prototype.getInsertData = function (parentID, nextSiblingID) {
  479. var data = [];
  480. var newID = this.newNodeID();
  481. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  482. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  483. if (newID !== -1) {
  484. data.push({type: 'new', data: this.getDataTemplate(newID, parent ? parent.getID() : this.setting.rootId, nextSibling ? nextSibling.getID() : this.setting.rootId)});
  485. if (nextSibling && nextSibling.preSibling) {
  486. tools.addUpdateDataForNextSibling(data, nextSibling.preSibling, newID);
  487. } else if (parent && parent.children.length !== 0) {
  488. tools.addUpdateDataForNextSibling(data, parent.lastChild(), newID);
  489. } else if (!parent && this.roots.length !== 0) {
  490. tools.addUpdateDataForNextSibling(data, this.roots[this.roots.length - 1], newID);
  491. }
  492. }
  493. return data;
  494. };
  495. Tree.prototype.delete = function (node) {
  496. var success = false, that = this;
  497. var deleteIdIndex = function (nodes) {
  498. nodes.forEach(function (node) {
  499. delete that.nodes[that.prefix + node.getID()];
  500. deleteIdIndex(node.children);
  501. })
  502. }
  503. if (node) {
  504. deleteIdIndex([node]);
  505. //delete this.nodes[this.prefix + node.getID()];
  506. if (node.preSibling) {
  507. node.preSibling.nextSibling = node.nextSibling;
  508. }
  509. if (node.nextSibling) {
  510. node.nextSibling.preSibling = node.preSibling;
  511. }
  512. if (node.parent) {
  513. node.parent.children.splice(node.siblingIndex(), 1);
  514. } else {
  515. this.roots.splice(node.siblingIndex(), 1);
  516. }
  517. tools.sortTreeItems(this);
  518. success = true;
  519. }
  520. return success;
  521. };
  522. Tree.prototype.getDeleteData = function (node) {
  523. var data = [];
  524. var addUpdateDataForDelete = function (datas, nodes) {
  525. nodes.forEach(function (node) {
  526. var delData = {};
  527. delData[node.tree.setting.id] = node.getID();
  528. datas.push({type: 'delete', data: delData});
  529. addUpdateDataForDelete(datas, node.children);
  530. })
  531. };
  532. if (node) {
  533. addUpdateDataForDelete(data, [node]);
  534. if (node.preSibling) {
  535. tools.addUpdateDataForNextSibling(data, node.preSibling, node.getNextSiblingID());
  536. }
  537. }
  538. return data;
  539. }
  540. /*Tree.prototype.editedData = function (field, id, newText) {
  541. var node = this.findNode(id), result = {allow: false, nodes: []};
  542. if (this.event[this.eventType.editedData]) {
  543. return this.event[this.eventType.editedData](field, node.data);
  544. } else {
  545. node.data[field] = newText;
  546. result.allow = true;
  547. return result;
  548. }
  549. };*/
  550. Tree.prototype.bind = function (eventName, eventFun) {
  551. this.event[eventName] = eventFun;
  552. };
  553. return new Tree(setting);
  554. }
  555. };