idTree.js 23 KB

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