cache_tree.js 16 KB

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