pm_tree.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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.preSibling = function () {
  106. var iIndex = this.parent.childIndex(this);
  107. if (iIndex === -1){
  108. return null;
  109. } else {
  110. return iIndex > 0 ? this.parent.children[iIndex-1] : null;
  111. }
  112. };
  113. Node.prototype.setParent = function (parent) {
  114. if (parent && this.parent !== parent) {
  115. this.parent = parent;
  116. this.data[this.setting.tree.pid] = this.pid();
  117. }
  118. };
  119. Node.prototype.setNextSibling = function (nextSibling) {
  120. if (this.nextSibling !== nextSibling) {
  121. this.nextSibling = nextSibling;
  122. this.data[this.setting.tree.nid] = this.nid();
  123. }
  124. }
  125. Node.prototype.id = function () {
  126. return this.data ? this.data[this.setting.tree.id] : -1;
  127. };
  128. Node.prototype.pid = function () {
  129. return this.parent ? this.parent.id() : -1;
  130. };
  131. Node.prototype.nid = function () {
  132. return this.nextSibling ? this.nextSibling.id() : -1;
  133. };
  134. Node.prototype.propertyJoin = function (dataName) {
  135. return this.parent ? this.parent.propertyJoin(dataName) + ';' + this.data[dataName] : this.data[dataName];
  136. }
  137. return Node;
  138. })();
  139. var Tree = (function () {
  140. function Tree(setting) {
  141. this._root = new Node(this);
  142. this.items = [];
  143. this.selected = null;
  144. this.setting = setting;
  145. var _maxNodeId = 0;
  146. this.newNodeId = function (id) {
  147. if (arguments.length > 0){
  148. _maxNodeId = (id > _maxNodeId) ? id : _maxNodeId;
  149. } else {
  150. _maxNodeId += 1;
  151. return _maxNodeId;
  152. }
  153. };
  154. this.maxNodeId = function (id){
  155. if (arguments.length > 0) {
  156. _maxNodeId = Math.max(id, _maxNodeId);
  157. } else {
  158. return _maxNodeId;
  159. }
  160. }
  161. };
  162. Tree.prototype.firstNode = function (){
  163. return this._root.firstChild();
  164. };
  165. Tree.prototype.traverseDF = function(callback){
  166. var recurse = function (node) {
  167. var i;
  168. if (node !== this._root) {
  169. callback(node);
  170. }
  171. for (i = 0; i < node.children.length; i++){
  172. recurse(node.children[i]);
  173. }
  174. }
  175. recurse(this._root);
  176. };
  177. Tree.prototype.findNode = function (id){
  178. var treenode = null,
  179. callback = function (node) {
  180. if (node.data && node.data[node.setting.tree.id] === id){
  181. treenode = node;
  182. }
  183. };
  184. this.traverseDF.call(this, callback);
  185. return treenode;
  186. };
  187. Tree.prototype.findNodeByNid = function (nid) {
  188. let treenode = null,
  189. callback = function (node) {
  190. if(node.data && node.data[node.setting.tree.nid] === nid){
  191. treenode = node;
  192. }
  193. };
  194. this.traverseDF.call(this, callback);
  195. return treenode;
  196. };
  197. Tree.prototype.removeNode = function (node) {
  198. var iIndex;
  199. if (node) {
  200. iIndex = node.parent.childIndex(node);
  201. if (iIndex > 0) {
  202. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  203. }
  204. node.parent.children.splice(iIndex, 1);
  205. }
  206. sortTreeItems(this);
  207. };
  208. Tree.prototype.loadData = function (arrData) {
  209. let i, that = this;
  210. let nodesIndex = {};
  211. function getPreNode(id){
  212. for(let index in nodesIndex){
  213. let node = nodesIndex[index];
  214. if(node['data'][node.setting.tree.nid] === id){
  215. return node;
  216. }
  217. }
  218. return null;
  219. }
  220. function loadNode (data, setting) {//mark
  221. var node = nodesIndex[data[setting.tree.id]] || null,
  222. parent = nodesIndex[data[setting.tree.pid]] || that._root,
  223. next = nodesIndex[data[setting.tree.nid]] || null,
  224. pre = getPreNode(data[setting.tree.id]) || null,
  225. tempData;
  226. if (!node) {
  227. node = new Node(that, data);
  228. }
  229. that.maxNodeId(node.id());
  230. if(parent.childIndex(node) === -1){
  231. if(pre && parent.childIndex(pre) !== -1){
  232. parent.children.splice(parent.childIndex(pre) + 1, 0, node);
  233. }
  234. else if(next && parent.childIndex(next) !== -1){
  235. parent.children.splice(parent.childIndex(next), 0, node);
  236. }
  237. else {
  238. parent.children.push(node);
  239. }
  240. }
  241. if(pre && parent.childIndex(pre) === -1){
  242. parent.children.splice(parent.childIndex(node), 0, pre);
  243. }
  244. if(next && parent.childIndex(next) === -1){
  245. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  246. }
  247. if(pre && parent.childIndex(pre) !== parent.childIndex(node) - 1){
  248. parent.children.splice(parent.childIndex(pre), 1);
  249. parent.children.splice(parent.childIndex(node), 0, pre);
  250. }
  251. if(next && parent.childIndex(next) !== parent.childIndex(node) + 1){
  252. parent.children.splice(parent.childIndex(next), 1);
  253. parent.children.splice(parent.childIndex(node) + 1, 0, next);
  254. }
  255. node.parent = parent;
  256. node.nextSibling = next;
  257. }
  258. //建立索引
  259. for(let data of arrData){
  260. nodesIndex[data.ID] = new Node(that, data);
  261. }
  262. for (i = 0; i < arrData.length; i++){
  263. loadNode(arrData[i], this.setting);
  264. }
  265. //set items
  266. sortTreeItems(this);
  267. };
  268. Tree.prototype.addNodeData = function (data, parent, nextSibling) {
  269. var node = null;
  270. var pNode = parent ? parent : this._root;
  271. if (!nextSibling || (nextSibling.parent === pNode && pNode.childIndex(nextSibling) > -1)) {
  272. node = new Node(this, data);
  273. this.maxNodeId(data[this.setting.tree.id]);
  274. this.move(node, pNode, nextSibling);
  275. }
  276. return node;
  277. };
  278. Tree.prototype.move = function(node, parent, nextSibling) {
  279. var iIndex = -1, pre;
  280. if (parent && (!nextSibling || (nextSibling.parent === parent && parent.childIndex(nextSibling) > -1))) {
  281. if (node) {
  282. if (node.parent) {
  283. iIndex = node.parent.childIndex(node);
  284. if (iIndex > 0) {
  285. node.parent.children[iIndex - 1].setNextSibling(node.nextSibling);
  286. }
  287. node.parent.children.splice(iIndex, 1);
  288. this.refreshNodesDom([node.parent], false);
  289. }
  290. if (nextSibling) {
  291. iIndex = parent.childIndex(nextSibling);
  292. if (iIndex > 0){
  293. pre = parent.children[iIndex - 1];
  294. pre.setNextSibling(node);
  295. //parent.children.splice(iIndex - 1, 0, node);
  296. parent.children.splice(iIndex, 0, node);
  297. } else {
  298. parent.children.splice(0, 0, node);
  299. }
  300. } else {
  301. if (parent.children.length > 0){
  302. pre = parent.lastChild();
  303. pre.setNextSibling(node);
  304. }
  305. parent.children.push(node);
  306. }
  307. node.setParent(parent);
  308. node.setNextSibling(nextSibling);
  309. //sort items
  310. sortTreeItems(this);
  311. }
  312. } else {
  313. this.e.throw('Error: information of moving node has mistake.');
  314. }
  315. };
  316. Tree.prototype.insert = function (sheet, items, node) {
  317. const rIdx = items.indexOf(node);
  318. const cIdx = sheet.getActiveColumnIndex();
  319. sheet.addRows(rIdx, 1);
  320. //set selection selected
  321. sheet.setSelection(rIdx, cIdx, 1, 1);
  322. this.selected = node;
  323. sheet.getCell(rIdx, 0).cellType(this);
  324. };
  325. Tree.prototype.refreshData = function () {
  326. };
  327. return Tree;
  328. })()
  329. let tree = new Tree(setting);
  330. tree.loadData(arrData);
  331. return tree;
  332. }
  333. };