cache_tree.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * Created by Mai on 2017/4/5.
  3. */
  4. var cacheTree = {
  5. createNew: function (owner) {
  6. var tools = {
  7. findNode: function (nodes, check) {
  8. for (var i = 0; i < nodes.length; i++) {
  9. if (check(nodes[i])) {
  10. return nodes[i];
  11. }
  12. }
  13. return null;
  14. },
  15. reSortNodes: function (nodes, recursive) {
  16. var temp = [], first;
  17. var findFirstNode = function (nodes) {
  18. return tools.findNode(nodes, function (node) {
  19. return node.preSibling === null;
  20. });
  21. };
  22. var moveNode = function (node, orgArray, newArray, newIndex) {
  23. var next;
  24. orgArray.splice(orgArray.indexOf(node), 1);
  25. newArray.splice(newIndex, 0, node);
  26. if (node.getNextSiblingID() !== -1) {
  27. next = node.nextSibling;
  28. if (next && (orgArray.indexOf(next) >= 0)) {
  29. moveNode(next, orgArray, newArray, newIndex + 1);
  30. }
  31. }
  32. };
  33. if (nodes.length === 0) {
  34. return nodes;
  35. }
  36. if (recursive) {
  37. nodes.forEach(function (node) {
  38. node.children = tools.reSortNodes(node.children, recursive);
  39. });
  40. }
  41. while (nodes.length > 0) {
  42. first = findFirstNode(nodes);
  43. first = first ? first : nodes[0];
  44. moveNode(first, nodes, temp, temp.length);
  45. }
  46. nodes = null;
  47. tools.reSiblingNodes(temp);
  48. return temp;
  49. },
  50. reSiblingNodes: function (nodes) {
  51. var i;
  52. for (i = 0; i < nodes.length; i++) {
  53. nodes[i].preSibling = (i === 0) ? null : nodes[i - 1];
  54. nodes[i].nextSibling = (i === nodes.length - 1) ? null : nodes[i + 1];
  55. }
  56. },
  57. // ��nodes�У���iIndex����������ʼȫ���Ƴ�
  58. removeNodes: function (tree, parent, iIndex, count) {
  59. var children = parent ? parent.children : tree.roots;
  60. var pre = (iIndex < 0 || iIndex >= children.length) ? null : children[iIndex].preSibling;
  61. var next = (pre && iIndex + count - 1 < children.length) ? children[iIndex + count] : null;
  62. if (pre) {
  63. pre.setNextSibling(next);
  64. } else if (next) {
  65. next.preSibling = null;
  66. }
  67. if (arguments.length === 4) {
  68. children.splice(iIndex, count);
  69. } else {
  70. children.splice(iIndex, children.length - iIndex);
  71. }
  72. },
  73. // ��nodes������addNodes, λ�ô�index��ʼ
  74. addNodes: function (tree, parent, nodes, iIndex) {
  75. var children = parent ? parent.children : tree.roots;
  76. var pre, next, i;
  77. if (nodes.length === 0) { return; }
  78. if (arguments.length === 4) {
  79. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex - 1];
  80. next = pre ? pre.nextSibling : null;
  81. } else if (arguments.length === 3) {
  82. pre = children.length === 0 ? null : children[children.length - 1];
  83. next = null;
  84. }
  85. if (pre) {
  86. pre.setNextSibling(nodes[0]);
  87. } else {
  88. nodes[0].preSibling = null;
  89. }
  90. nodes[nodes.length - 1].setNextSibling(next);
  91. for (i = 0; i < nodes.length; i++) {
  92. if (arguments.length === 4) {
  93. children.splice(iIndex + i, 0, nodes[i]);
  94. } else if (arguments.length === 3) {
  95. children.push(nodes[i]);
  96. }
  97. nodes[i].parent = parent;
  98. }
  99. }
  100. };
  101. var Node = function (tree, id) {
  102. var ID = id;
  103. // ���µ����ԣ�����Ԫ��������ֱ���޸�
  104. this.tree = tree;
  105. this.children = [];
  106. this.parent = null;
  107. this.nextSibling = null;
  108. this.preSibling = null;
  109. this.expanded = true;
  110. this.visible = true;
  111. this.sourceType = null;
  112. this.source = null;
  113. this.getID = function () {
  114. return ID;
  115. }
  116. };
  117. Node.prototype.getParentID = function () {
  118. return this.parent ? this.parent.getID() : -1;
  119. };
  120. Node.prototype.getNextSiblingID = function () {
  121. return this.nextSibling ? this.nextSibling.getID() : -1;
  122. };
  123. Node.prototype.setParent = function (parent) {
  124. this.parent = parent;
  125. };
  126. Node.prototype.setNextSibling = function (nextSibling) {
  127. this.nextSibling = nextSibling;
  128. if (nextSibling) {
  129. nextSibling.preSibling = this;
  130. }
  131. }
  132. Node.prototype.firstChild = function () {
  133. return this.children.length === 0 ? null : this.children[0];
  134. };
  135. Node.prototype.depth = function () {
  136. return this.parent ? this.parent.depth() + 1 : 0;
  137. };
  138. Node.prototype.isFirst = function () {
  139. if (this.parent) {
  140. return this.parent.children.indexOf(this) === 0 ? true : false;
  141. } else {
  142. return this.tree.roots.indexOf(this) === 0 ? true : false;
  143. }
  144. };
  145. Node.prototype.isLast = function () {
  146. if (this.parent) {
  147. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  148. } else {
  149. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  150. }
  151. };
  152. Node.prototype.siblingIndex = function () {
  153. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  154. }
  155. Node.prototype.posterityCount = function () {
  156. var iCount = 0;
  157. if (this.children.length !== 0) {
  158. iCount += this.children.length;
  159. this.children.forEach(function (child) {
  160. iCount += child.posterityCount();
  161. });
  162. }
  163. return iCount;
  164. };
  165. Node.prototype.setExpanded = function (expanded) {
  166. var setNodesVisible = function (nodes, visible) {
  167. nodes.forEach(function (node) {
  168. node.visible = visible;
  169. setNodesVisible(node.children, visible && node.expanded);
  170. })
  171. };
  172. this.expanded = expanded;
  173. setNodesVisible(this.children, expanded);
  174. };
  175. Node.prototype.serialNo = function () {
  176. return this.tree.items.indexOf(this);
  177. }
  178. Node.prototype.addChild = function (node) {
  179. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  180. node.parent = this;
  181. if (preSibling) {
  182. preSibling.nextSibling = node;
  183. }
  184. node.preSibling = preSibling;
  185. this.children.push(node);
  186. };
  187. Node.prototype.removeChild = function (node) {
  188. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  189. if (preSibling) {
  190. preSibling.nextSibling = nextSibling;
  191. }
  192. if (nextSibling) {
  193. nextSibling.preSibling = preSibling;
  194. }
  195. this.children.splice(this.children.re)
  196. };
  197. Node.prototype.canUpLevel = function () {
  198. if (this.sourceType === this.tree.owner.Bills.getSourceType()) {
  199. return this.parent ? true : false;
  200. } else {
  201. return false;
  202. }
  203. };
  204. Node.prototype.canDownLevel = function () {
  205. return this.sourceType === this.tree.owner.Bills.getSourceType() ? !this.isFirst() : false;
  206. };
  207. Node.prototype.canUpMove = function () {
  208. return !this.isFirst();
  209. };
  210. Node.prototype.canDownMove = function () {
  211. return !this.isLast();
  212. }
  213. Node.prototype.upLevel = function () {
  214. var success = false,
  215. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  216. if (this.canUpLevel) {
  217. // NextSiblings become child
  218. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  219. // Orginal Parent remove node and nextSiblings
  220. tools.removeNodes(this.tree, this.parent, iIndex);
  221. // New Parent add node
  222. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  223. if (!this.expanded) {
  224. this.setExpanded(true);
  225. }
  226. success = true;
  227. }
  228. return success;
  229. };
  230. Node.prototype.downLevel = function () {
  231. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  232. var newParent = this.preSibling;
  233. if (this.canDownLevel()) {
  234. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  235. tools.addNodes(this.tree, this.preSibling, [this]);
  236. if (!newParent.expanded) {
  237. newParent.setExpanded(true);
  238. }
  239. success = true;
  240. }
  241. return success;
  242. };
  243. Node.prototype.upMove = function () {
  244. var success = false;
  245. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  246. if (this.canUpMove()) {
  247. if (orgPre.preSibling) {
  248. orgPre.preSibling.setNextSibling(this);
  249. } else {
  250. this.preSibling = null;
  251. }
  252. orgPre.setNextSibling(this.nextSibling);
  253. this.setNextSibling(orgPre);
  254. belongArray.splice(iIndex, 1);
  255. belongArray.splice(iIndex - 1, 0, this);
  256. this.tree.sortTreeItems();
  257. success = true;
  258. }
  259. return success;
  260. };
  261. Node.prototype.downMove = function () {
  262. var success = false;
  263. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  264. if (this.canDownMove()) {
  265. if (this.preSibling) {
  266. this.preSibling.setNextSibling(orgNext);
  267. } else if (orgNext) {
  268. orgNext.preSibling = null;
  269. }
  270. this.setNextSibling(orgNext.nextSibling);
  271. orgNext.setNextSibling(this);
  272. belongArray.splice(iIndex, 1);
  273. belongArray.splice(iIndex + 1, 0, this);
  274. this.tree.sortTreeItems();
  275. success = true;
  276. }
  277. return success;
  278. };
  279. var Tree = function (owner) {
  280. this.owner = owner;
  281. this.nodes = {};
  282. this.roots = [];
  283. this.items = [];
  284. this.prefix = 'id_';
  285. this.selected = null;
  286. var _MaxID = 0;
  287. this.newNodeID = function (id) {
  288. if (arguments.length === 0) {
  289. _MaxID += 1;
  290. return _MaxID;
  291. } else {
  292. _MaxID = Math.max(_MaxID, id);
  293. }
  294. };
  295. var rootId = -1;
  296. this.rootID = function () {
  297. return rootId;
  298. }
  299. };
  300. Tree.prototype.addNode = function (parent, nextSibling) {
  301. var newNode = new Node(this, this.newNodeID());
  302. this.nodes[this.prefix + newNode.getID()] = newNode;
  303. if (nextSibling) {
  304. tools.addNodes(this, parent, [newNode], nextSibling.siblingIndex());
  305. } else {
  306. tools.addNodes(this, parent, [newNode]);
  307. }
  308. return newNode;
  309. };
  310. Tree.prototype.sortTreeItems = function () {
  311. var that = this;
  312. var addItems = function (nodes) {
  313. var i;
  314. for (i = 0; i < nodes.length; i++) {
  315. that.items.push(nodes[i]);
  316. addItems(nodes[i].children);
  317. }
  318. };
  319. this.items.splice(0, this.items.length);
  320. addItems(this.roots);
  321. };
  322. Tree.prototype.firstNode = function () {
  323. return this.roots.length === 0 ? null : this.roots[0];
  324. };
  325. Tree.prototype.findNode = function (id) {
  326. return this.nodes[this.prefix + id];
  327. };
  328. Tree.prototype.count = function () {
  329. var iCount = 0;
  330. if (this.roots.length !== 0) {
  331. iCount += this.roots.length;
  332. this.roots.forEach(function (node) {
  333. iCount += node.posterityCount();
  334. });
  335. }
  336. return iCount;
  337. };
  338. Tree.prototype.insert = function (parentID, nextSiblingID) {
  339. var parent = !parentID || parentID === -1 ? null : this.nodes[this.prefix + parentID];
  340. var nextSibling = !nextSiblingID || nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  341. var newNode = this.addNode(parent, nextSibling);
  342. this.sortTreeItems();
  343. return newNode;
  344. };
  345. Tree.prototype.delete = function (node) {
  346. var success = false;
  347. if (node) {
  348. delete this.nodes[this.prefix + node.getID()];
  349. if (node.preSibling) {
  350. node.preSibling.setNextSibling(node.nextSibling);
  351. } else if (node.nextSibling) {
  352. node.nextSibling.preSibling = null;
  353. }
  354. if (node.parent) {
  355. node.parent.children.splice(node.siblingIndex(), 1);
  356. } else {
  357. this.roots.splice(node.siblingIndex(), 1);
  358. }
  359. this.sortTreeItems();
  360. success = true;
  361. }
  362. return success;
  363. };
  364. return new Tree(owner);
  365. }
  366. };