pm_tree.js 15 KB

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