tree_sheet_controller.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_CONTROLLER = {
  5. createNew: function (tree, sheet, setting, loadSheetHeader = true) {
  6. var controller = function () {
  7. this.tree = tree;
  8. this.sheet = sheet;
  9. this.setting = setting;
  10. this.event = {
  11. refreshBaseActn: null,
  12. treeSelectedChanged: null
  13. };
  14. if(loadSheetHeader){
  15. TREE_SHEET_HELPER.loadSheetHeader(this.setting, this.sheet);
  16. }
  17. };
  18. controller.prototype.showTreeData = function () {
  19. //
  20. var that = this;
  21. TREE_SHEET_HELPER.showTreeData(this.setting, this.sheet, this.tree);
  22. this.sheet.unbind(GC.Spread.Sheets.Events.SelectionChanged);
  23. this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  24. that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
  25. });
  26. };
  27. controller.prototype.insert = function () {
  28. var newNode = null, that = this, sels = this.sheet.getSelections();
  29. if (this.tree) {
  30. if (this.tree.selected) {
  31. newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  32. } else {
  33. newNode = this.tree.insert();
  34. }
  35. if (newNode) {
  36. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  37. that.sheet.addRows(newNode.serialNo(), 1);
  38. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  39. that.setTreeSelected(newNode);
  40. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  41. that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  42. if(typeof cbTools !== 'undefined'){
  43. cbTools.refreshFormulaNodes();
  44. }
  45. });
  46. }
  47. }
  48. };
  49. controller.prototype.insertByID = function (ID) {
  50. var newNode = null, that = this, sels = this.sheet.getSelections();
  51. if (this.tree) {
  52. if (this.tree.selected) {
  53. newNode = this.tree.insertByID(ID, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  54. } else {
  55. newNode = this.tree.insertByID(ID);
  56. }
  57. if (newNode) {
  58. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  59. that.sheet.addRows(newNode.serialNo(), 1);
  60. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  61. that.setTreeSelected(newNode);
  62. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  63. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  64. });
  65. }
  66. }
  67. return newNode;
  68. };
  69. controller.prototype.delete = function () {
  70. var that = this, sels = this.sheet.getSelections();
  71. if (this.tree.selected) {
  72. if (this.tree.delete(this.tree.selected)) {
  73. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  74. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  75. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  76. });
  77. if(typeof cbTools !== 'undefined'){
  78. cbTools.refreshFormulaNodes();
  79. }
  80. }
  81. }
  82. };
  83. controller.prototype.m_delete = function (nodes, beginRow = null) {//删除选中的多行节点
  84. var that = this, sels = this.sheet.getSelections();
  85. if (this.tree.selected) {
  86. if (this.tree.m_delete(nodes)) {
  87. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  88. let rowCount = 0;
  89. for(let node of nodes){
  90. rowCount = rowCount+node.posterityCount() + 1;
  91. }
  92. if(beginRow){
  93. sels[0].row = beginRow;
  94. }
  95. that.sheet.deleteRows(sels[0].row, rowCount);
  96. if(sels[0].row >= that.tree.items.length){
  97. sels[0].row = that.tree.items.length-1;
  98. sels[0].colCount = 1;
  99. }
  100. that.setTreeSelected(that.tree.items[sels[0].row]);
  101. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  102. });
  103. if(typeof cbTools !== 'undefined'){
  104. cbTools.refreshFormulaNodes();
  105. }
  106. }
  107. }
  108. };
  109. controller.prototype.singleDelete = function () {//只删除当前节点,不删除子节点
  110. var that = this, sels = this.sheet.getSelections();
  111. if (this.tree.selected) {
  112. if (this.tree.singleDelete(this.tree.selected)) {
  113. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  114. that.sheet.deleteRows(sels[0].row,1);
  115. that.setTreeSelected(that.tree.items[sels[0].row]);
  116. });
  117. }
  118. }
  119. };
  120. controller.prototype.deleteNode = function (node,next) {
  121. var that = this;
  122. if (node){
  123. var row = node.serialNo();
  124. if (this.tree.delete(node)) {
  125. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  126. that.sheet.deleteRows(row,1);
  127. next?that.setTreeSelected(that.tree.items[row]):"";
  128. });
  129. if(typeof cbTools !== 'undefined'){
  130. cbTools.refreshFormulaNodes();
  131. }
  132. }
  133. }
  134. };
  135. controller.prototype.upLevel = function () {
  136. var that = this;
  137. if (this.tree.selected) {
  138. if (this.tree.selected.upLevel()) {
  139. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  140. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  141. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  142. if (that.event.refreshBaseActn) {
  143. that.event.refreshBaseActn(that.tree);
  144. }
  145. });
  146. }
  147. }
  148. };
  149. controller.prototype.multiUpLevel = function (nodes) {
  150. nodes.forEach(node => node.upLevel());
  151. TREE_SHEET_HELPER.massOperationSheet(this.sheet, () => {
  152. TREE_SHEET_HELPER.refreshNodesVisible(nodes, this.sheet, true);
  153. this.sheet.showRow(nodes[0].serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  154. if (this.event.refreshBaseActn) {
  155. this.event.refreshBaseActn(this.tree);
  156. }
  157. });
  158. };
  159. controller.prototype.downLevel = function () {
  160. var that = this;
  161. if (this.tree.selected) {
  162. if (this.tree.selected.downLevel()) {
  163. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  164. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  165. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  166. if (that.event.refreshBaseActn) {
  167. that.event.refreshBaseActn(that.tree);
  168. }
  169. });
  170. }
  171. }
  172. };
  173. controller.prototype.multiDownLevel = function (nodes) {
  174. nodes.forEach(node => node.downLevel());
  175. const firstNode = nodes[0];
  176. TREE_SHEET_HELPER.massOperationSheet(this.sheet, () => {
  177. TREE_SHEET_HELPER.refreshNodesVisible([firstNode.parent], this.sheet, true);
  178. this.sheet.showRow(firstNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  179. if (this.event.refreshBaseActn) {
  180. this.event.refreshBaseActn(this.tree);
  181. }
  182. });
  183. };
  184. controller.prototype.upMove = function () {
  185. var that = this, sels = this.sheet.getSelections();
  186. if (this.tree.selected) {
  187. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  188. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  189. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo(),true);
  190. if (that.tree.selected.upMove()) {
  191. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  192. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  193. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo());
  194. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  195. if (that.event.refreshBaseActn) {
  196. that.event.refreshBaseActn(that.tree);
  197. }
  198. }
  199. });
  200. }
  201. };
  202. controller.prototype.multiUpMove = function (nodes) {
  203. TREE_SHEET_HELPER.massOperationSheet(this.sheet, () => {
  204. const { row, col, rowCount, colCount } = this.sheet.getSelections()[0];
  205. nodes.forEach(node => {
  206. TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, node, node.serialNo(), true); //为了处理移动前子项是隐藏的情况
  207. });
  208. const preNode = nodes[0].preSibling;
  209. TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, preNode, preNode.serialNo(), true);
  210. nodes.forEach(node => node.upMove());
  211. TREE_SHEET_HELPER.refreshTreeNodeData(this.setting, this.sheet, [...nodes, preNode], true);
  212. nodes.forEach(node => TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, node, node.serialNo()));
  213. TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, preNode, preNode.serialNo());
  214. this.sheet.setSelection(row - preNode.posterityCount() - 1, col, rowCount, colCount);
  215. if (this.event.refreshBaseActn) {
  216. this.event.refreshBaseActn(this.tree);
  217. }
  218. });
  219. };
  220. controller.prototype.multiDownMove = function (nodes) {
  221. TREE_SHEET_HELPER.massOperationSheet(this.sheet, () => {
  222. const { row, col, rowCount, colCount } = this.sheet.getSelections()[0];
  223. nodes.forEach(node => {
  224. TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, node, node.serialNo(), true); //为了处理移动前子项是隐藏的情况
  225. });
  226. const nextNode = nodes[nodes.length - 1].nextSibling;
  227. TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, nextNode, nextNode.serialNo(), true);
  228. for (let i = nodes.length - 1; i >= 0; i--) {
  229. nodes[i].downMove();
  230. }
  231. TREE_SHEET_HELPER.refreshTreeNodeData(this.setting, this.sheet, [...nodes, nextNode], true);
  232. nodes.forEach(node => TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, node, node.serialNo()));
  233. TREE_SHEET_HELPER.refreshChildrenVisiable(this.sheet, this.tree, nextNode, nextNode.serialNo());
  234. this.sheet.setSelection(row + nextNode.posterityCount() + 1, col, rowCount, colCount);
  235. if (this.event.refreshBaseActn) {
  236. this.event.refreshBaseActn(this.tree);
  237. }
  238. });
  239. };
  240. controller.prototype.downMove = function () {
  241. var that = this, sels = this.sheet.getSelections();
  242. if (this.tree.selected) {
  243. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  244. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  245. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo(),true);
  246. if (that.tree.selected.downMove()) {
  247. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  248. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  249. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo());
  250. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  251. if (that.event.refreshBaseActn) {
  252. that.event.refreshBaseActn(that.tree);
  253. }
  254. }
  255. });
  256. }
  257. };
  258. controller.prototype.refreshTreeNode = function (nodes,recursive,autoFit) {
  259. var that = this;
  260. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  261. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive,autoFit)
  262. })
  263. }
  264. controller.prototype.setTreeSelected = function (node) {
  265. if (this.event.beforeTreeSelectedChange) {
  266. this.event.beforeTreeSelectedChange(this.tree.selected);
  267. }
  268. this.tree.selected = node;
  269. if (this.event.refreshBaseActn) {
  270. this.event.refreshBaseActn(this.tree);
  271. }
  272. if (this.event.treeSelectedChanged) {
  273. this.event.treeSelectedChanged(this.tree.selected);
  274. }
  275. }
  276. controller.prototype.bind = function (eventName, eventFun) {
  277. this.event[eventName] = eventFun;
  278. };
  279. // 获取有效的选中同层节点
  280. // 如果选中的节点内,有比第一个选中节点深度小的节点,则此选中范围内无有效节点
  281. // 如果没用上述的情况,则有效选中节点为,跟第一个选中节点深度相同的节点
  282. controller.prototype.getValidNodesWithinSelection = function () {
  283. const selection = this.sheet.getSelections()[0];
  284. if (!selection || !commonUtil.isDef(selection.row)) {
  285. return [];
  286. }
  287. const nodes = [];
  288. let firstNode;
  289. let firstNodeDepth;
  290. const maxRow = selection.row + selection.rowCount;
  291. for (let row = selection.row; row < maxRow; row++) {
  292. const node = this.tree.items[row];
  293. if (!node) {
  294. return [];
  295. }
  296. const depth = node.depth();
  297. if (!firstNode) {
  298. firstNode = node;
  299. firstNodeDepth = firstNode.depth();
  300. }
  301. if (depth < firstNodeDepth) {
  302. return [];
  303. }
  304. if (depth === firstNodeDepth) {
  305. nodes.push(node);
  306. }
  307. }
  308. return nodes;
  309. };
  310. return new controller();
  311. },
  312. eventName: {
  313. refreshBaseActn: 'refreshBaseActn',
  314. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  315. treeSelectedChanged: 'treeSelectedChanged',
  316. cellDoubleClick: 'cellDoubleClick'
  317. }
  318. };