cache_tree.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. sortTreeItems: function (tree) {
  104. var addItems = function (items) {
  105. var i;
  106. for (i = 0; i < items.length; i++) {
  107. tree.items.push(items[i]);
  108. addItems(items[i].children);
  109. }
  110. };
  111. tree.items.splice(0, tree.items.length);
  112. addItems(tree.roots);
  113. }
  114. };
  115. var Node = function (tree, id) {
  116. var ID = id;
  117. // 以下的属性,本单元外均不可直接修改
  118. this.tree = tree;
  119. this.children = [];
  120. this.parent = null;
  121. this.nextSibling = null;
  122. this.preSibling = null;
  123. this.expanded = true;
  124. this.visible = true;
  125. this.nodeType = null;
  126. this.source = null;
  127. this.getID = function () {
  128. return ID;
  129. }
  130. };
  131. Node.prototype.getParentID = function () {
  132. return this.parent ? this.parent.getID() : -1;
  133. };
  134. Node.prototype.getNextSiblingID = function () {
  135. return this.nextSibling ? this.nextSibling.getID() : -1;
  136. };
  137. Node.prototype.firstChild = function () {
  138. return this.children.length === 0 ? null : this.children[0];
  139. };
  140. Node.prototype.depth = function () {
  141. return this.parent ? this.parent.depth() + 1 : 0;
  142. };
  143. Node.prototype.isFirst = function () {
  144. if (this.parent) {
  145. return this.parent.children.indexOf(this) === 0 ? true : false;
  146. } else {
  147. return this.tree.roots.indexOf(this) === 0 ? true : false;
  148. }
  149. };
  150. Node.prototype.isLast = function () {
  151. if (this.parent) {
  152. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  153. } else {
  154. return this.tree.roots.indexOf(this) === this.tree.roots.length - 1 ? true : false;
  155. }
  156. };
  157. Node.prototype.siblingIndex = function () {
  158. return this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  159. }
  160. Node.prototype.posterityCount = function () {
  161. var iCount = 0;
  162. if (this.children.length !== 0) {
  163. iCount += this.children.length;
  164. this.children.forEach(function (child) {
  165. iCount += child.posterityCount();
  166. });
  167. }
  168. return iCount;
  169. };
  170. Node.prototype.setExpanded = function (expanded) {
  171. var setNodesVisible = function (nodes, visible) {
  172. nodes.forEach(function (node) {
  173. node.visible = visible;
  174. setNodesVisible(node.children, visible && node.expanded);
  175. })
  176. };
  177. this.expanded = expanded;
  178. setNodesVisible(this.children, expanded);
  179. };
  180. Node.prototype.serialNo = function () {
  181. return this.tree.items.indexOf(this);
  182. }
  183. Node.prototype.addChild = function (node) {
  184. var preSibling = this.children.length === 0 ? null : this.children[this.children.length - 1];
  185. node.parent = this;
  186. if (preSibling) {
  187. preSibling.nextSibling = node;
  188. }
  189. node.preSibling = preSibling;
  190. this.children.push(node);
  191. };
  192. Node.prototype.removeChild = function (node) {
  193. var preSibling = node.preSibling, nextSibling = node.nextSibling;
  194. if (preSibling) {
  195. preSibling.nextSibling = nextSibling;
  196. }
  197. if (nextSibling) {
  198. nextSibling.preSibling = preSibling;
  199. }
  200. this.children.splice(this.children.re)
  201. };
  202. Node.prototype.canUpLevel = function () {
  203. if (this.nodeType === this.tree.nodeType.bills) {
  204. return this.parent ? true : false;
  205. } else {
  206. return false;
  207. }
  208. };
  209. Node.prototype.canDownLevel = function () {
  210. return this.nodeType === this.tree.nodeType.bills ? !this.isFirst() : false;
  211. };
  212. Node.prototype.canUpMove = function () {
  213. return !this.isFirst();
  214. };
  215. Node.prototype.canDownMove = function () {
  216. return !this.isLast();
  217. }
  218. Node.prototype.upLevel = function () {
  219. var success = false,
  220. iIndex = this.parent.children.indexOf(this), orgParent = this.parent, newNextSibling = this.parent.nextSibling;
  221. if (this.canUpLevel) {
  222. // NextSiblings become child
  223. tools.addNodes(this.tree, this, this.parent.children.slice(iIndex + 1));
  224. // Orginal Parent remove node and nextSiblings
  225. tools.removeNodes(this.tree, this.parent, iIndex);
  226. // New Parent add node
  227. tools.addNodes(this.tree, this.parent.parent, [this], this.parent.siblingIndex() + 1);
  228. if (!this.expanded) {
  229. this.setExpanded(true);
  230. }
  231. success = true;
  232. }
  233. return success;
  234. };
  235. Node.prototype.downLevel = function () {
  236. var success = false, iIndex = this.parent ? this.parent.children.indexOf(this) : this.tree.roots.indexOf(this);
  237. var newParent = this.preSibling;
  238. if (this.canDownLevel()) {
  239. tools.removeNodes(this.tree, this.parent, this.siblingIndex(), 1);
  240. tools.addNodes(this.tree, this.preSibling, [this]);
  241. if (!newParent.expanded) {
  242. newParent.setExpanded(true);
  243. }
  244. success = true;
  245. }
  246. return success;
  247. };
  248. Node.prototype.upMove = function () {
  249. var success = false;
  250. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgPre = this.preSibling;
  251. if (this.canUpMove()) {
  252. orgPre.nextSibling = this.nextSibling;
  253. this.preSibling = orgPre.preSibling;
  254. orgPre.preSibling = this;
  255. this.nextSibling = orgPre;
  256. belongArray.splice(iIndex, 1);
  257. belongArray.splice(iIndex - 1, 0, this);
  258. tools.sortTreeItems(this.tree);
  259. success = true;
  260. }
  261. return success;
  262. };
  263. Node.prototype.downMove = function () {
  264. var success = false;
  265. var iIndex = this.siblingIndex(), belongArray = this.parent ? this.parent.children : this.tree.roots, orgNext = this.nextSibling;
  266. if (this.canDownMove()) {
  267. orgNext.preSibling = this.preSibling;
  268. this.nextSibling = orgNext.nextSibling;
  269. orgNext.nextSibling = this;
  270. this.preSibling = orgNext;
  271. belongArray.splice(iIndex, 1);
  272. belongArray.splice(iIndex + 1, 0, this);
  273. tools.sortTreeItems(this.tree);
  274. success = true;
  275. }
  276. return success;
  277. };
  278. var Tree = function (owner) {
  279. this.nodes = {};
  280. this.roots = [];
  281. this.items = [];
  282. this.prefix = 'id_';
  283. this.selected = null;
  284. this.nodeType = {bills: 'bills', ration: 'ration', zc: 'zhuCai'};
  285. this.masterField = {ration: 'BillsID'};
  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.createNewNode = function () {
  301. var node = new Node(this, this.newNodeID());
  302. this.nodes[this.prefix + node.getID()] = node;
  303. return node;
  304. };
  305. Tree.prototype.loadDatas = function (idTree, rations) {
  306. var that = this;
  307. var loadRationNode = function (rations, cacheNode) {
  308. var newNode;
  309. rations.forEach(function (ration) {
  310. if (ration[that.masterField.ration] && ration[that.masterField.ration] === cacheNode.source.getID()) {
  311. newNode = that.createNewNode();
  312. newNode.source = ration;
  313. newNode.nodeType = that.nodeType.ration;
  314. newNode.data = ration;
  315. tools.addNodes(that, cacheNode, [newNode]);
  316. }
  317. });
  318. };
  319. var loadIdTreeNode = function (nodes, parent) {
  320. var newNode, i;
  321. for (i = 0; i < nodes.length; i++) {
  322. newNode = that.createNewNode();
  323. newNode.source = nodes[i];
  324. newNode.nodeType = that.nodeType.bills;
  325. newNode.data = nodes[i].data;
  326. newNode.parent = parent;
  327. tools.addNodes(that, parent, [newNode]);
  328. if (nodes[i].children.length === 0) {
  329. loadRationNode(rations, newNode);
  330. };
  331. loadIdTreeNode(nodes[i].children, newNode);
  332. }
  333. };
  334. loadIdTreeNode(idTree.roots, null);
  335. tools.sortTreeItems(this);
  336. };
  337. Tree.prototype.firstNode = function () {
  338. return this.roots.length === 0 ? null : this.roots[0];
  339. };
  340. Tree.prototype.findNode = function (id) {
  341. return this.nodes[this.prefix + id];
  342. };
  343. Tree.prototype.count = function () {
  344. var iCount = 0;
  345. if (this.roots.length !== 0) {
  346. iCount += this.roots.length;
  347. this.roots.forEach(function (node) {
  348. iCount += node.posterityCount();
  349. });
  350. }
  351. return iCount;
  352. };
  353. Tree.prototype.insert = function (parentID, nextSiblingID) {
  354. var newID = this.newNodeID(), node = null, data = {};
  355. var parent = parentID === -1 ? null : this.nodes[this.prefix + parentID];
  356. var nextSibling = nextSiblingID === -1 ? null: this.nodes[this.prefix + nextSiblingID];
  357. if (newID !== -1) {
  358. data = {};
  359. data[this.setting.id] = newID;
  360. data[this.setting.pid] = parentID;
  361. data[this.setting.nid] = nextSiblingID;
  362. node = new Node(this, data);
  363. if (nextSibling) {
  364. tools.addNodes(this, parent, [node], nextSibling.siblingIndex());
  365. } else {
  366. tools.addNodes(this, parent, [node]);
  367. }
  368. this.nodes[this.prefix + newID] = node;
  369. tools.sortTreeItems(this);
  370. this.maxNodeID(newID);
  371. }
  372. return node;
  373. };
  374. Tree.prototype.delete = function (node) {
  375. var success = false;
  376. if (node) {
  377. delete this.nodes[this.prefix + node.getID()];
  378. if (node.preSibling) {
  379. node.preSibling.nextSibling = node.nextSibling;
  380. }
  381. if (node.nextSibling) {
  382. node.nextSibling.preSibling = node.preSibling;
  383. }
  384. if (node.parent) {
  385. node.parent.children.splice(node.siblingIndex(), 1);
  386. } else {
  387. this.roots.splice(node.siblingIndex(), 1);
  388. }
  389. tools.sortTreeItems(this);
  390. success = true;
  391. }
  392. return success;
  393. };
  394. Tree.prototype.editedData = function (field, id, newText) {
  395. var node = this.findNode(id);
  396. }
  397. return new Tree(owner);
  398. }
  399. };