pm_tree.js 14 KB

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