cache_tree.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.nextSibling = next;
  64. }
  65. if (next) {
  66. next.preSibling = pre;
  67. }
  68. if (arguments.length === 4) {
  69. children.splice(iIndex, count);
  70. } else {
  71. children.splice(iIndex, children.length - iIndex);
  72. }
  73. },
  74. // 在nodes中增加addNodes, 位置从index开始
  75. addNodes: function (tree, parent, nodes, iIndex) {
  76. var children = parent ? parent.children : tree.roots;
  77. var pre, next, i;
  78. if (nodes.length === 0) { return; }
  79. if (arguments.length === 4) {
  80. pre = (iIndex <= 0 || iIndex > children.length) ? null : children[iIndex - 1];
  81. next = pre ? pre.nextSibling : null;
  82. } else if (arguments.length === 3) {
  83. pre = children.length === 0 ? null : children[children.length - 1];
  84. next = null;
  85. }
  86. if (pre) {
  87. pre.nextSibling = nodes[0];
  88. }
  89. nodes[0].preSibling = pre;
  90. if (next) {
  91. next.preSibling = nodes[nodes.length - 1];
  92. }
  93. nodes[nodes.length - 1].nextSibling = next;
  94. for (i = 0; i < nodes.length; i++) {
  95. if (arguments.length === 4) {
  96. children.splice(iIndex + i, 0, nodes[i]);
  97. } else if (arguments.length === 3) {
  98. children.push(nodes[i]);
  99. }
  100. nodes[i].parent = parent;
  101. }
  102. }
  103. };
  104. var Node = function (tree, id) {
  105. var ID = id;
  106. // 以下的属性,本单元外均不可直接修改
  107. this.tree = tree;
  108. this.children = [];
  109. this.parent = null;
  110. this.nextSibling = null;
  111. this.preSibling = null;
  112. this.expanded = true;
  113. this.visible = true;
  114. this.sourceType = null;
  115. this.source = null;
  116. this.getID = function () {
  117. return ID;
  118. }
  119. };
  120. Node.prototype.getParentID = function () {
  121. return this.parent ? this.parent.getID() : -1;
  122. };
  123. Node.prototype.getNextSiblingID = function () {
  124. return this.nextSibling ? this.nextSibling.getID() : -1;
  125. };
  126. Node.prototype.firstChild = function () {
  127. return this.children.length === 0 ? null : this.children[0];
  128. };
  129. Node.prototype.depth = function () {
  130. return this.parent ? this.parent.depth() + 1 : 0;
  131. };
  132. Node.prototype.isFirst = function () {
  133. if (this.parent) {
  134. return this.parent.children.indexOf(this) === 0 ? true : false;
  135. } else {
  136. return this.tree.roots.indexOf(this) === 0 ? true : false;
  137. }
  138. };
  139. Node.prototype.isLast = function () {
  140. if (this.parent) {
  141. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  142. } else {
  143. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  144. }
  145. };
  146. Node.prototype.siblingIndex = function () {
  147. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  148. }
  149. Node.prototype.posterityCount = function () {
  150. var iCount = 0;
  151. if (this.children.length !== 0) {
  152. iCount += this.children.length;
  153. this.children.forEach(function (child) {
  154. iCount += child.posterityCount();
  155. });
  156. }
  157. return iCount;
  158. };
  159. Node.prototype.setExpanded = function (expanded) {
  160. var setNodesVisible = function (nodes, visible) {
  161. nodes.forEach(function (node) {
  162. node.visible = visible;
  163. setNodesVisible(node.children, visible && node.expanded);
  164. })
  165. };
  166. this.expanded = expanded;
  167. setNodesVisible(this.children, expanded);
  168. };
  169. Node.prototype.serialNo = function () {
  170. return this.tree.items.indexOf(this);
  171. }
  172. Node.prototype.addChild = function (node) {
  173. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  174. node.parent = this;
  175. if (preSibling) {
  176. preSibling.nextSibling = node;
  177. }
  178. node.preSibling = preSibling;
  179. this.children.push(node);
  180. };
  181. Node.prototype.removeChild = function (node) {
  182. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  183. if (preSibling) {
  184. preSibling.nextSibling = nextSibling;
  185. }
  186. if (nextSibling) {
  187. nextSibling.preSibling = preSibling;
  188. }
  189. this.children.splice(this.children.re)
  190. };
  191. Node.prototype.canUpLevel = function () {
  192. if (this.sourceType === this.tree.owner.Bills.getSourceType()) {
  193. return this.parent ? true : false;
  194. } else {
  195. return false;
  196. }
  197. };
  198. Node.prototype.canDownLevel = function () {
  199. return this.sourceType === this.tree.owner.Bills.getSourceType() ? !this.isFirst() : false;
  200. };
  201. Node.prototype.canUpMove = function () {
  202. return !this.isFirst();
  203. };
  204. Node.prototype.canDownMove = function () {
  205. return !this.isLast();
  206. }
  207. Node.prototype.upLevel = function () {
  208. var success = false,
  209. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  210. if (this.canUpLevel) {
  211. // NextSiblings become child
  212. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  213. // Orginal Parent remove node and nextSiblings
  214. tools.removeNodes(this.tree, this.parent, iIndex);
  215. // New Parent add node
  216. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  217. if (!this.expanded) {
  218. this.setExpanded(true);
  219. }
  220. success = true;
  221. }
  222. return success;
  223. };
  224. Node.prototype.downLevel = function () {
  225. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  226. var newParent = this.preSibling;
  227. if (this.canDownLevel()) {
  228. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  229. tools.addNodes(this.tree, this.preSibling, [this]);
  230. if (!newParent.expanded) {
  231. newParent.setExpanded(true);
  232. }
  233. success = true;
  234. }
  235. return success;
  236. };
  237. Node.prototype.upMove = function () {
  238. var success = false;
  239. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  240. if (this.canUpMove()) {
  241. orgPre.nextSibling = this.nextSibling;
  242. this.preSibling = orgPre.preSibling;
  243. orgPre.preSibling = this;
  244. this.nextSibling = orgPre;
  245. belongArray.splice(iIndex, 1);
  246. belongArray.splice(iIndex - 1, 0, this);
  247. this.tree.sortTreeItems();
  248. success = true;
  249. }
  250. return success;
  251. };
  252. Node.prototype.downMove = function () {
  253. var success = false;
  254. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  255. if (this.canDownMove()) {
  256. orgNext.preSibling = this.preSibling;
  257. this.nextSibling = orgNext.nextSibling;
  258. orgNext.nextSibling = this;
  259. this.preSibling = orgNext;
  260. belongArray.splice(iIndex, 1);
  261. belongArray.splice(iIndex + 1, 0, this);
  262. this.tree.sortTreeItems();
  263. success = true;
  264. }
  265. return success;
  266. };
  267. var Tree = function (owner) {
  268. this.owner = owner;
  269. this.nodes = {};
  270. this.roots = [];
  271. this.items = [];
  272. this.prefix = 'id_';
  273. this.selected = null;
  274. var _MaxID = 0;
  275. this.newNodeID = function (id) {
  276. if (arguments.length === 0) {
  277. _MaxID += 1;
  278. return _MaxID;
  279. } else {
  280. _MaxID = Math.max(_MaxID, id);
  281. }
  282. };
  283. var rootId = -1;
  284. this.rootID = function () {
  285. return rootId;
  286. }
  287. };
  288. Tree.prototype.addNode = function (parent, nextSibling) {
  289. var newNode = new Node(this, this.newNodeID());
  290. this.nodes[this.prefix + newNode.getID()] = newNode;
  291. if (nextSibling) {
  292. tools.addNodes(this, parent, [newNode], nextSibling.siblingIndex());
  293. } else {
  294. tools.addNodes(this, parent, [newNode]);
  295. }
  296. return newNode;
  297. };
  298. Tree.prototype.sortTreeItems = function () {
  299. var that = this;
  300. var addItems = function (nodes) {
  301. var i;
  302. for (i = 0; i < nodes.length; i++) {
  303. that.items.push(nodes[i]);
  304. addItems(nodes[i].children);
  305. }
  306. };
  307. this.items.splice(0, this.items.length);
  308. addItems(this.roots);
  309. };
  310. Tree.prototype.firstNode = function () {
  311. return this.roots.length === 0 ? null : this.roots[0];
  312. };
  313. Tree.prototype.findNode = function (id) {
  314. return this.nodes[this.prefix + id];
  315. };
  316. Tree.prototype.count = function () {
  317. var iCount = 0;
  318. if (this.roots.length !== 0) {
  319. iCount += this.roots.length;
  320. this.roots.forEach(function (node) {
  321. iCount += node.posterityCount();
  322. });
  323. }
  324. return iCount;
  325. };
  326. Tree.prototype.insert = function (parentID, nextSiblingID) {
  327. var parent = !parentID || parentID === -1 ? null : this.nodes[this.prefix + parentID];
  328. var nextSibling = !nextSiblingID || nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  329. var newNode = this.addNode(parent, nextSibling);
  330. this.sortTreeItems();
  331. return newNode;
  332. };
  333. Tree.prototype.delete = function (node) {
  334. var success = false;
  335. if (node) {
  336. delete this.nodes[this.prefix + node.getID()];
  337. if (node.preSibling) {
  338. node.preSibling.nextSibling = node.nextSibling;
  339. }
  340. if (node.nextSibling) {
  341. node.nextSibling.preSibling = node.preSibling;
  342. }
  343. if (node.parent) {
  344. node.parent.children.splice(node.siblingIndex(), 1);
  345. } else {
  346. this.roots.splice(node.siblingIndex(), 1);
  347. }
  348. this.sortTreeItems();
  349. success = true;
  350. }
  351. return success;
  352. };
  353. return new Tree(owner);
  354. }
  355. };