idTree.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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;
  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. };
  124. var Node = function (tree, data) {
  125. // 以下的属性,本单元外均不可直接修改
  126. this.tree = tree;
  127. this.data = data;
  128. this.children = [];
  129. this.parent = null;
  130. this.nextSibling = null;
  131. this.preSibling = null;
  132. this.expanded = true;
  133. this.visible = true;
  134. this.visible = true;
  135. this.isUpdate = false;
  136. this.isNew = false;
  137. };
  138. Node.prototype.getID = function () {
  139. return this.data[this.tree.setting.id];
  140. };
  141. Node.prototype.getParentID = function () {
  142. return this.parent ? this.parent.getID() : -1;
  143. };
  144. Node.prototype.getNextSiblingID = function () {
  145. return this.nextSibling ? this.nextSibling.getID() : -1;
  146. };
  147. Node.prototype.firstChild = function () {
  148. return this.children.length === 0 ? null : this.children[0];
  149. };
  150. Node.prototype.depth = function () {
  151. return this.parent ? this.parent.depth() + 1 : 0;
  152. };
  153. Node.prototype.isFirst = function () {
  154. if (this.parent) {
  155. return this.parent.children.indexOf(this) === 0 ? true : false;
  156. } else {
  157. return this.tree.roots.indexOf(this) === 0 ? true : false;
  158. }
  159. };
  160. Node.prototype.isLast = function () {
  161. if (this.parent) {
  162. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  163. } else {
  164. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  165. }
  166. };
  167. Node.prototype.siblingIndex = function () {
  168. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  169. }
  170. Node.prototype.posterityCount = function () {
  171. var iCount = 0;
  172. if (this.children.length !== 0) {
  173. iCount += this.children.length;
  174. this.children.forEach(function (child) {
  175. iCount += child.posterityCount();
  176. });
  177. }
  178. return iCount;
  179. /*return (node.children.length === 0) ? 0 : node.children.reduce(function (x, y) {
  180. return x.posterityCount() + y.posterityCount();
  181. }) + node.children.count;*/
  182. };
  183. Node.prototype.setExpanded = function (expanded) {
  184. var setNodesVisible = function (nodes, visible) {
  185. nodes.forEach(function (node) {
  186. node.visible = visible;
  187. setNodesVisible(node.children, visible && node.expanded);
  188. })
  189. };
  190. this.expanded = expanded;
  191. setNodesVisible(this.children, expanded);
  192. };
  193. /*Node.prototype.vis = function () {
  194. return this.parent ? this.parent.vis() && this.parent.expanded() : true;
  195. };*/
  196. Node.prototype.serialNo = function () {
  197. return this.tree.items.indexOf(this);
  198. }
  199. /*Node.prototype.serialNo = function () {
  200. if (this.preSibling) {
  201. return this.preSibling.serialNo() + this.preSibling.posterityCount() + 1;
  202. } else if (this.parent) {
  203. return this.parent.serialNo() + 1;
  204. } else {
  205. return 0;
  206. }
  207. };*/
  208. Node.prototype.addChild = function (node) {
  209. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  210. node.parent = this;
  211. if (preSibling) {
  212. preSibling.nextSibling = node;
  213. }
  214. node.preSibling = preSibling;
  215. this.children.push(node);
  216. };
  217. Node.prototype.removeChild = function (node) {
  218. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  219. if (preSibling) {
  220. preSibling.nextSibling = nextSibling;
  221. }
  222. if (nextSibling) {
  223. nextSibling.preSibling = preSibling;
  224. }
  225. this.children.splice(this.children.re)
  226. };
  227. Node.prototype.canUpLevel = function () {
  228. return this.parent ? true : false;
  229. };
  230. Node.prototype.canDownLevel = function () {
  231. return !this.isFirst();
  232. };
  233. Node.prototype.canUpMove = function () {
  234. return !this.isFirst();
  235. };
  236. Node.prototype.canDownMove = function () {
  237. return !this.isLast();
  238. }
  239. Node.prototype.upLevel = function () {
  240. var success = false,
  241. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  242. if (this.canUpLevel) {
  243. // NextSiblings become child
  244. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  245. // Orginal Parent remove node and nextSiblings
  246. tools.removeNodes(this.tree, this.parent, iIndex);
  247. // New Parent add node
  248. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  249. if (!this.expanded) {
  250. this.setExpanded(true);
  251. }
  252. success = true;
  253. }
  254. return success;
  255. };
  256. Node.prototype.downLevel = function () {
  257. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  258. var newParent = this.preSibling;
  259. if (this.canDownLevel()) {
  260. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  261. tools.addNodes(this.tree, this.preSibling, [this]);
  262. if (!newParent.expanded) {
  263. newParent.setExpanded(true);
  264. }
  265. success = true;
  266. }
  267. return success;
  268. };
  269. Node.prototype.upMove = function () {
  270. var success = false;
  271. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  272. if (this.canUpMove()) {
  273. orgPre.nextSibling = this.nextSibling;
  274. this.preSibling = orgPre.preSibling;
  275. orgPre.preSibling = this;
  276. this.nextSibling = orgPre;
  277. belongArray.splice(iIndex, 1);
  278. belongArray.splice(iIndex - 1, 0, this);
  279. tools.sortTreeItems(this.tree);
  280. success = true;
  281. }
  282. return success;
  283. };
  284. Node.prototype.downMove = function () {
  285. var success = false;
  286. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  287. if (this.canDownMove()) {
  288. orgNext.preSibling = this.preSibling;
  289. this.nextSibling = orgNext.nextSibling;
  290. orgNext.nextSibling = this;
  291. this.preSibling = orgNext;
  292. belongArray.splice(iIndex, 1);
  293. belongArray.splice(iIndex + 1, 0, this);
  294. tools.sortTreeItems(this.tree);
  295. success = true;
  296. }
  297. return success;
  298. }
  299. var Tree = function (setting) {
  300. this.nodes = {};
  301. this.roots = [];
  302. this.items = [];
  303. this.setting = setting;
  304. this.prefix = 'id_';
  305. this.selected = null;
  306. this.event = {};
  307. this.eventType = _eventType;
  308. };
  309. Tree.prototype.maxNodeID = (function () {
  310. var maxID = 0;
  311. return function (ID) {
  312. if (arguments.length === 0) {
  313. return maxID;
  314. } else {
  315. maxID = Math.max(maxID, ID);
  316. }
  317. };
  318. })();
  319. Tree.prototype.rangeNodeID = (function () {
  320. var rangeID = -1;
  321. return function (ID) {
  322. if (arguments.length === 0) {
  323. return rangeID;
  324. } else {
  325. rangeID = Math.max(rangeID, ID);
  326. }
  327. }
  328. })();
  329. Tree.prototype.newNodeID = function () {
  330. if (this.rangeNodeID() === -1) {
  331. return this.maxNodeID() + 1;
  332. } else {
  333. if (this.maxNodeID() >= this.rangeNodeID()) {
  334. return this.maxNodeID() + 1;
  335. } else {
  336. return -1;
  337. }
  338. }
  339. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  340. return -1;
  341. } else {
  342. return this.maxNodeID() + 1;
  343. }*/
  344. };
  345. Tree.prototype.loadDatas = function (datas) {
  346. var prefix = this.prefix, i, node, parent, next, that = this;
  347. // prepare index
  348. datas.forEach(function (data) {
  349. var node = new Node(that, data);
  350. that.nodes[prefix + data[that.setting.id]] = node;
  351. that.maxNodeID(data[that.setting.id]);
  352. });
  353. // set parent by pid, set nextSibling by nid
  354. datas.forEach(function (data) {
  355. node = that.nodes[prefix + data[that.setting.id]];
  356. if (data[that.setting.pid] === that.setting.rootId) {
  357. that.roots.push(node);
  358. } else {
  359. parent = that.nodes[prefix + data[that.setting.pid]];
  360. if (parent) {
  361. node.parent = parent;
  362. parent.children.push(node);
  363. }
  364. }
  365. if (data[that.setting.nid] !== that.setting.rootId) {
  366. next = that.nodes[prefix + data[that.setting.nid]];
  367. if (next) {
  368. node.nextSibling = next;
  369. next.preSibling = node;
  370. }
  371. }
  372. })
  373. // sort by nid
  374. this.roots = tools.reSortNodes(this.roots, true);
  375. tools.sortTreeItems(this);
  376. };
  377. Tree.prototype.firstNode = function () {
  378. return this.roots.length === 0 ? null : this.roots[0];
  379. };
  380. Tree.prototype.findNode = function (id) {
  381. return this.nodes[this.prefix + id];
  382. };
  383. Tree.prototype.count = function () {
  384. var iCount = 0;
  385. if (this.roots.length !== 0) {
  386. iCount += this.roots.length;
  387. this.roots.forEach(function (node) {
  388. iCount += node.posterityCount();
  389. });
  390. }
  391. return iCount;
  392. };
  393. Tree.prototype.insert = function (parentID, nextSiblingID) {
  394. var newID = this.newNodeID(), node = null, data = {};
  395. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  396. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  397. if (newID !== -1) {
  398. data = {};
  399. data[this.setting.id] = newID;
  400. data[this.setting.pid] = parentID;
  401. data[this.setting.nid] = nextSiblingID;
  402. node = new Node(this, data);
  403. if (nextSibling) {
  404. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  405. } else {
  406. tools.addNodes(this, parent, [node]);
  407. }
  408. this.nodes[this.prefix + newID] = node;
  409. tools.sortTreeItems(this);
  410. this.maxNodeID(newID);
  411. }
  412. return node;
  413. };
  414. Tree.prototype.delete = function (node) {
  415. var success = false;
  416. if (node) {
  417. delete this.nodes[this.prefix + node.getID()];
  418. if (node.preSibling) {
  419. node.preSibling.nextSibling = node.nextSibling;
  420. }
  421. if (node.nextSibling) {
  422. node.nextSibling.preSibling = node.preSibling;
  423. }
  424. if (node.parent) {
  425. node.parent.children.splice(node.siblingIndex(), 1);
  426. } else {
  427. this.roots.splice(node.siblingIndex(), 1);
  428. }
  429. tools.sortTreeItems(this);
  430. success = true;
  431. }
  432. return success;
  433. };
  434. Tree.prototype.editedData = function (field, id, newText) {
  435. var node = this.findNode(id), result = {allow: false, nodes: []};
  436. if (this.event[this.eventType.editedData]) {
  437. return this.event[this.eventType.editedData](field, node.data);
  438. } else {
  439. node.data[field] = newText;
  440. result.allow = true;
  441. return result;
  442. }
  443. };
  444. Tree.prototype.bind = function (eventName, eventFun) {
  445. this.event[eventName] = eventFun;
  446. };
  447. return new Tree(setting);
  448. }
  449. };