pm_tree.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * Created by Zhong on 2018/3/21.
  3. */
  4. const pmTree = {
  5. createNew: function (setting, arrData) {
  6. function sortTreeItems(tree) {
  7. var addItems = function (items) {
  8. var i;
  9. for (i = 0; i < items.length; i++) {
  10. tree.items.push(items[i]);
  11. addItems(items[i].children);
  12. }
  13. };
  14. tree.items.splice(0, tree.items.length);
  15. addItems(tree._root.children);
  16. }
  17. var Node = (function () {
  18. function Node(tree, data, nodeState = null) {
  19. this.parent = null;
  20. this.nextSibling = null;
  21. this.children = [];
  22. this.tree = tree;
  23. this.data = data;
  24. this.setting = tree.setting;
  25. this.expanded = true;
  26. this.row = null;
  27. this.expandBtn = null;
  28. if (nodeState) {
  29. for (const attr in nodeState) {
  30. this[attr] = nodeState[attr];
  31. }
  32. }
  33. }
  34. Node.prototype.firstChild = function() {
  35. return (this.children.length === 0) ? null : this.children[0];
  36. };
  37. Node.prototype.lastChild = function () {
  38. return (this.children.length === 0) ? null : this.children[this.children.length - 1];
  39. };
  40. Node.prototype.deepestRow = function () {
  41. return (this.children.length === 0) ? this.row : this.lastChild().deepestRow();
  42. };
  43. Node.prototype.depth = function () {
  44. return this.parent ? this.parent.depth() + 1 : 0;
  45. };
  46. Node.prototype.isFirst = function () {
  47. if (this.parent) {
  48. return this.parent.children.indexOf(this) === 0 ? true : false;
  49. } else {
  50. return this.tree._root.children.indexOf(this) === 0 ? true : false;
  51. }
  52. };
  53. Node.prototype.isLast = function () {
  54. if (this.parent) {
  55. return this.parent.children.indexOf(this) === this.parent.children.length - 1 ? true : false;
  56. } else {
  57. return this.tree._root.children.indexOf(this) === this.tree._root.children.length - 1 ? true : false;
  58. }
  59. };
  60. Node.prototype.setExpanded = function (expanded) {
  61. var setNodesVisible = function (nodes, visible) {
  62. nodes.forEach(function (node) {
  63. node.visible = visible;
  64. setNodesVisible(node.children, visible && node.expanded);
  65. })
  66. };
  67. this.expanded = expanded;
  68. setNodesVisible(this.children, expanded);
  69. };
  70. Node.prototype.posterityCount = function () {
  71. var iCount = 0;
  72. if (this.children.length !== 0) {
  73. iCount += this.children.length;
  74. this.children.forEach(function (child) {
  75. iCount += child.posterityCount();
  76. });
  77. }
  78. return iCount;
  79. };
  80. Node.prototype.addChild = function (child, childNext) {
  81. if (childNext){
  82. this.children.push(child);
  83. } else {
  84. if (this.childIndex(childNext) > -1){
  85. this.children.splice(this.childIndex(childNext) - 1, 0, child);
  86. } else {
  87. this.children.push(child);
  88. }
  89. }
  90. return child;
  91. };
  92. Node.prototype.childIndex = function (child) {
  93. return this.children.indexOf(child);
  94. };
  95. Node.prototype.depth = function () {
  96. return this.parent ? this.parent.depth() + 1 : 0;
  97. };
  98. Node.prototype.domId = function () {
  99. return this.data ? this.tree.domId + '_' + this.data[this.setting.tree.id] : '';
  100. };
  101. Node.prototype.expand = function (bool) {
  102. this.expanded = bool;
  103. _view._refreshTreeBtn(this);
  104. if (this.expanded) {
  105. _view._showNodes(this.children);
  106. } else {
  107. _view._hideNodes(this.children);
  108. }
  109. };
  110. Node.prototype.serialNo = function () {
  111. return this.tree.items.indexOf(this);
  112. };
  113. Node.prototype.preSibling = function () {
  114. var iIndex = this.parent.childIndex(this);
  115. if (iIndex === -1){
  116. return null;
  117. } else {
  118. return iIndex > 0 ? this.parent.children[iIndex-1] : null;
  119. }
  120. };
  121. Node.prototype.setParent = function (parent) {
  122. if (parent && this.parent !== parent) {
  123. this.parent = parent;
  124. this.data[this.setting.tree.pid] = this.pid();
  125. }
  126. };
  127. Node.prototype.setNextSibling = function (nextSibling) {
  128. if (this.nextSibling !== nextSibling) {
  129. this.nextSibling = nextSibling;
  130. this.data[this.setting.tree.nid] = this.nid();
  131. }
  132. }
  133. Node.prototype.id = function () {
  134. return this.data ? this.data[this.setting.tree.id] : -1;
  135. };
  136. Node.prototype.pid = function () {
  137. return this.parent ? this.parent.id() : -1;
  138. };
  139. Node.prototype.nid = function () {
  140. return this.nextSibling ? this.nextSibling.id() : -1;
  141. };
  142. Node.prototype.propertyJoin = function (dataName) {
  143. return this.parent ? this.parent.propertyJoin(dataName) + ';' + this.data[dataName] : this.data[dataName];
  144. };
  145. Node.prototype.getAllChildren = function () {
  146. let childrenList = [];
  147. getChildren(this);
  148. function getChildren(node) {
  149. for(let c of node.children){
  150. childrenList.push(c);
  151. getChildren(c)
  152. }
  153. }
  154. return childrenList;
  155. };
  156. return Node;
  157. })();
  158. var Tree = (function () {
  159. function Tree(setting) {
  160. this._root = new Node(this);
  161. this.items = [];
  162. this.selected = null;
  163. this.setting = setting;
  164. var _maxNodeId = 0;
  165. this.newNodeId = function (id) {
  166. if (arguments.length > 0){
  167. _maxNodeId = (id > _maxNodeId) ? id : _maxNodeId;
  168. } else {
  169. _maxNodeId += 1;
  170. return _maxNodeId;
  171. }
  172. };
  173. this.maxNodeId = function (id){
  174. if (arguments.length > 0) {
  175. _maxNodeId = Math.max(id, _maxNodeId);
  176. } else {
  177. return _maxNodeId;
  178. }
  179. }
  180. };
  181. Tree.prototype.firstNode = function (){
  182. return this._root.firstChild();
  183. };
  184. Tree.prototype.traverseDF = function(callback){
  185. var recurse = function (node) {
  186. var i;
  187. if (node !== this._root) {
  188. callback(node);
  189. }
  190. for (i = 0; i < node.children.length; i++){
  191. recurse(node.children[i]);
  192. }
  193. }
  194. recurse(this._root);
  195. };
  196. Tree.prototype.findNode = function (id){
  197. var treenode = null,
  198. callback = function (node) {
  199. if (node.data && node.data[node.setting.tree.id] === id){
  200. treenode = node;
  201. }
  202. };
  203. this.traverseDF.call(this, callback);
  204. return treenode;
  205. };
  206. Tree.prototype.findNodeByNid = function (nid) {
  207. let treenode = null,
  208. callback = function (node) {
  209. if(node.data && node.data[node.setting.tree.nid] === nid){
  210. treenode = node;
  211. }
  212. };
  213. this.traverseDF.call(this, callback);
  214. return treenode;
  215. };
  216. Tree.prototype.removeNode = function (node) {
  217. var iIndex;
  218. if (node) {
  219. iIndex = node.parent.childIndex(node);
  220. if (iIndex > 0) {
  221. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  222. }
  223. node.parent.children.splice(iIndex, 1);
  224. }
  225. sortTreeItems(this);
  226. };
  227. Tree.prototype.loadData = function (arrData) {
  228. let i, that = this;
  229. let nodesIndex = {};
  230. function getPreNode(id){
  231. for(let index in nodesIndex){
  232. let node = nodesIndex[index];
  233. if(node['data'][node.setting.tree.nid] === id){
  234. return node;
  235. }
  236. }
  237. return null;
  238. }
  239. function loadNode (data, setting) {//mark
  240. var node = nodesIndex[data[setting.tree.id]] || null,
  241. parent = nodesIndex[data[setting.tree.pid]] || that._root,
  242. next = nodesIndex[data[setting.tree.nid]] || null,
  243. pre = getPreNode(data[setting.tree.id]) || null,
  244. tempData;
  245. if (!node) {
  246. node = new Node(that, data);
  247. }
  248. that.maxNodeId(node.id());
  249. if(parent.childIndex(node) === -1){
  250. if (!pre) {
  251. parent.children.unshift(node);
  252. }
  253. else if(pre && parent.childIndex(pre) !== -1){
  254. parent.children.splice(parent.childIndex(pre) + 1, 0, node);
  255. }
  256. else if(next && parent.childIndex(next) !== -1){
  257. parent.children.splice(parent.childIndex(next), 0, node);
  258. }
  259. else {
  260. parent.children.push(node);
  261. }
  262. }
  263. if(pre && parent.childIndex(pre) === -1){
  264. parent.children.splice(parent.childIndex(node), 0, pre);
  265. }
  266. if(next && parent.childIndex(next) === -1){
  267. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  268. }
  269. if(pre && parent.childIndex(pre) !== parent.childIndex(node) - 1){
  270. parent.children.splice(parent.childIndex(pre), 1);
  271. parent.children.splice(parent.childIndex(node), 0, pre);
  272. }
  273. if(next && parent.childIndex(next) !== parent.childIndex(node) + 1){
  274. parent.children.splice(parent.childIndex(next), 1);
  275. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  276. }
  277. node.parent = parent;
  278. node.nextSibling = next;
  279. }
  280. //建立索引
  281. for(let data of arrData){
  282. nodesIndex[data.ID] = new Node(that, data);
  283. }
  284. for (i = 0; i < arrData.length; i++){
  285. loadNode(arrData[i], this.setting);
  286. }
  287. //set items
  288. sortTreeItems(this);
  289. };
  290. Tree.prototype.addNodeData = function (data, parent, nextSibling, nodeState = null) {
  291. var node = null;
  292. var pNode = parent ? parent : this._root;
  293. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  294. node = new Node(this, data, nodeState);
  295. this.maxNodeId(data[this.setting.tree.id]);
  296. this.move(node, pNode, nextSibling);
  297. }
  298. return node;
  299. };
  300. Tree.prototype.move = function(node, parent, nextSibling) {
  301. var iIndex = -1, pre;
  302. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  303. if (node) {
  304. if (node.parent) {
  305. iIndex = node.parent.childIndex(node);
  306. if (iIndex > 0) {
  307. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  308. }
  309. node.parent.children.splice(iIndex, 1);
  310. }
  311. if (nextSibling) {
  312. iIndex = parent.childIndex(nextSibling);
  313. if (iIndex > 0){
  314. pre = parent.children[iIndex - 1];
  315. pre.setNextSibling(node);
  316. //parent.children.splice(iIndex - 1, 0, node);
  317. parent.children.splice(iIndex, 0, node);
  318. } else {
  319. parent.children.splice(0, 0, node);
  320. }
  321. } else {
  322. if (parent.children.length > 0){
  323. pre = parent.lastChild();
  324. pre.setNextSibling(node);
  325. }
  326. parent.children.push(node);
  327. }
  328. node.setParent(parent);
  329. node.setNextSibling(nextSibling);
  330. //sort items
  331. sortTreeItems(this);
  332. }
  333. } else {
  334. this.e.throw('Error: information of moving node has mistake.');
  335. }
  336. };
  337. Tree.prototype.insert = function (sheet, items, node) {
  338. const rIdx = items.indexOf(node);
  339. const cIdx = sheet.getActiveColumnIndex();
  340. sheet.addRows(rIdx, 1);
  341. //set selection selected
  342. sheet.setSelection(rIdx, cIdx, 1, 1);
  343. this.selected = node;
  344. sheet.getCell(rIdx, 0).cellType(this);
  345. };
  346. Tree.prototype.refreshData = function () {
  347. };
  348. return Tree;
  349. })()
  350. let tree = new Tree(setting);
  351. tree.loadData(arrData);
  352. return tree;
  353. }
  354. };