idTree.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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.addChild = function (node) {
  200. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  201. node.parent = this;
  202. if (preSibling) {
  203. preSibling.nextSibling = node;
  204. }
  205. node.preSibling = preSibling;
  206. this.children.push(node);
  207. };
  208. Node.prototype.removeChild = function (node) {
  209. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  210. if (preSibling) {
  211. preSibling.nextSibling = nextSibling;
  212. }
  213. if (nextSibling) {
  214. nextSibling.preSibling = preSibling;
  215. }
  216. this.children.splice(this.children.re)
  217. };
  218. Node.prototype.canUpLevel = function () {
  219. return this.parent ? true : false;
  220. };
  221. Node.prototype.canDownLevel = function () {
  222. return !this.isFirst();
  223. };
  224. Node.prototype.canUpMove = function () {
  225. return !this.isFirst();
  226. };
  227. Node.prototype.canDownMove = function () {
  228. return !this.isLast();
  229. }
  230. Node.prototype.upLevel = function () {
  231. var success = false,
  232. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  233. if (this.canUpLevel) {
  234. // NextSiblings become child
  235. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  236. // Orginal Parent remove node and nextSiblings
  237. tools.removeNodes(this.tree, this.parent, iIndex);
  238. // New Parent add node
  239. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  240. if (!this.expanded) {
  241. this.setExpanded(true);
  242. }
  243. success = true;
  244. }
  245. return success;
  246. };
  247. Node.prototype.downLevel = function () {
  248. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  249. var newParent = this.preSibling;
  250. if (this.canDownLevel()) {
  251. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  252. tools.addNodes(this.tree, this.preSibling, [this]);
  253. if (!newParent.expanded) {
  254. newParent.setExpanded(true);
  255. }
  256. success = true;
  257. }
  258. return success;
  259. };
  260. Node.prototype.upMove = function () {
  261. var success = false;
  262. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  263. if (this.canUpMove()) {
  264. orgPre.nextSibling = this.nextSibling;
  265. this.preSibling = orgPre.preSibling;
  266. orgPre.preSibling = this;
  267. this.nextSibling = orgPre;
  268. belongArray.splice(iIndex, 1);
  269. belongArray.splice(iIndex - 1, 0, this);
  270. tools.sortTreeItems(this.tree);
  271. success = true;
  272. }
  273. return success;
  274. };
  275. Node.prototype.downMove = function () {
  276. var success = false;
  277. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  278. if (this.canDownMove()) {
  279. orgNext.preSibling = this.preSibling;
  280. this.nextSibling = orgNext.nextSibling;
  281. orgNext.nextSibling = this;
  282. this.preSibling = orgNext;
  283. belongArray.splice(iIndex, 1);
  284. belongArray.splice(iIndex + 1, 0, this);
  285. tools.sortTreeItems(this.tree);
  286. success = true;
  287. }
  288. return success;
  289. }
  290. var Tree = function (setting) {
  291. this.nodes = {};
  292. this.roots = [];
  293. this.items = [];
  294. this.setting = setting;
  295. this.prefix = 'id_';
  296. this.selected = null;
  297. this.event = {};
  298. this.eventType = _eventType;
  299. };
  300. Tree.prototype.maxNodeID = (function () {
  301. var maxID = 0;
  302. return function (ID) {
  303. if (arguments.length === 0) {
  304. return maxID;
  305. } else {
  306. maxID = Math.max(maxID, ID);
  307. }
  308. };
  309. })();
  310. Tree.prototype.rangeNodeID = (function () {
  311. var rangeID = -1;
  312. return function (ID) {
  313. if (arguments.length === 0) {
  314. return rangeID;
  315. } else {
  316. rangeID = Math.max(rangeID, ID);
  317. }
  318. }
  319. })();
  320. Tree.prototype.newNodeID = function () {
  321. if (this.rangeNodeID() === -1) {
  322. return this.maxNodeID() + 1;
  323. } else {
  324. if (this.maxNodeID() >= this.rangeNodeID()) {
  325. return this.maxNodeID() + 1;
  326. } else {
  327. return -1;
  328. }
  329. }
  330. /*if (this.maxID >= this.rangeNodeID() || this.rangeNodeID === -1) {
  331. return -1;
  332. } else {
  333. return this.maxNodeID() + 1;
  334. }*/
  335. };
  336. Tree.prototype.loadDatas = function (datas) {
  337. var prefix = this.prefix, i, node, parent, next, that = this;
  338. // prepare index
  339. datas.forEach(function (data) {
  340. var node = new Node(that, data);
  341. that.nodes[prefix + data[that.setting.id]] = node;
  342. that.maxNodeID(data[that.setting.id]);
  343. });
  344. // set parent by pid, set nextSibling by nid
  345. datas.forEach(function (data) {
  346. node = that.nodes[prefix + data[that.setting.id]];
  347. if (data[that.setting.pid] === that.setting.rootId) {
  348. that.roots.push(node);
  349. } else {
  350. parent = that.nodes[prefix + data[that.setting.pid]];
  351. if (parent) {
  352. node.parent = parent;
  353. parent.children.push(node);
  354. }
  355. }
  356. if (data[that.setting.nid] !== that.setting.rootId) {
  357. next = that.nodes[prefix + data[that.setting.nid]];
  358. if (next) {
  359. node.nextSibling = next;
  360. next.preSibling = node;
  361. }
  362. }
  363. })
  364. // sort by nid
  365. this.roots = tools.reSortNodes(this.roots, true);
  366. tools.sortTreeItems(this);
  367. };
  368. Tree.prototype.firstNode = function () {
  369. return this.roots.length === 0 ? null : this.roots[0];
  370. };
  371. Tree.prototype.findNode = function (id) {
  372. return this.nodes[this.prefix + id];
  373. };
  374. Tree.prototype.count = function () {
  375. var iCount = 0;
  376. if (this.roots.length !== 0) {
  377. iCount += this.roots.length;
  378. this.roots.forEach(function (node) {
  379. iCount += node.posterityCount();
  380. });
  381. }
  382. return iCount;
  383. };
  384. Tree.prototype.insert = function (parentID, nextSiblingID) {
  385. var newID = this.newNodeID(), node = null, data = {};
  386. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  387. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  388. if (newID !== -1) {
  389. data = {};
  390. data[this.setting.id] = newID;
  391. data[this.setting.pid] = parentID;
  392. data[this.setting.nid] = nextSiblingID;
  393. node = new Node(this, data);
  394. if (nextSibling) {
  395. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  396. } else {
  397. tools.addNodes(this, parent, [node]);
  398. }
  399. this.nodes[this.prefix + newID] = node;
  400. tools.sortTreeItems(this);
  401. this.maxNodeID(newID);
  402. }
  403. return node;
  404. };
  405. Tree.prototype.delete = function (node) {
  406. var success = false;
  407. if (node) {
  408. delete this.nodes[this.prefix + node.getID()];
  409. if (node.preSibling) {
  410. node.preSibling.nextSibling = node.nextSibling;
  411. }
  412. if (node.nextSibling) {
  413. node.nextSibling.preSibling = node.preSibling;
  414. }
  415. if (node.parent) {
  416. node.parent.children.splice(node.siblingIndex(), 1);
  417. } else {
  418. this.roots.splice(node.siblingIndex(), 1);
  419. }
  420. tools.sortTreeItems(this);
  421. success = true;
  422. }
  423. return success;
  424. };
  425. /*Tree.prototype.editedData = function (field, id, newText) {
  426. var node = this.findNode(id), result = {allow: false, nodes: []};
  427. if (this.event[this.eventType.editedData]) {
  428. return this.event[this.eventType.editedData](field, node.data);
  429. } else {
  430. node.data[field] = newText;
  431. result.allow = true;
  432. return result;
  433. }
  434. };*/
  435. Tree.prototype.bind = function (eventName, eventFun) {
  436. this.event[eventName] = eventFun;
  437. };
  438. return new Tree(setting);
  439. }
  440. };