tree_sheet_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.delete = function () {
  50. var that = this, sels = this.sheet.getSelections();
  51. if (this.tree.selected) {
  52. if (this.tree.delete(this.tree.selected)) {
  53. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  54. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  55. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  56. });
  57. if(typeof cbTools !== 'undefined'){
  58. cbTools.refreshFormulaNodes();
  59. }
  60. }
  61. }
  62. };
  63. controller.prototype.m_delete = function (nodes, beginRow = null) {//删除选中的多行节点
  64. var that = this, sels = this.sheet.getSelections();
  65. if (this.tree.selected) {
  66. if (this.tree.m_delete(nodes)) {
  67. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  68. let rowCount = 0;
  69. for(let node of nodes){
  70. rowCount = rowCount+node.posterityCount() + 1;
  71. }
  72. if(beginRow){
  73. sels[0].row = beginRow;
  74. }
  75. that.sheet.deleteRows(sels[0].row, rowCount);
  76. if(sels[0].row >= that.tree.items.length){
  77. sels[0].row = that.tree.items.length-1;
  78. sels[0].colCount = 1;
  79. }
  80. that.setTreeSelected(that.tree.items[sels[0].row]);
  81. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  82. });
  83. if(typeof cbTools !== 'undefined'){
  84. cbTools.refreshFormulaNodes();
  85. }
  86. }
  87. }
  88. };
  89. controller.prototype.singleDelete = function () {//只删除当前节点,不删除子节点
  90. var that = this, sels = this.sheet.getSelections();
  91. if (this.tree.selected) {
  92. if (this.tree.singleDelete(this.tree.selected)) {
  93. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  94. that.sheet.deleteRows(sels[0].row,1);
  95. that.setTreeSelected(that.tree.items[sels[0].row]);
  96. });
  97. }
  98. }
  99. };
  100. controller.prototype.deleteNode = function (node,next) {
  101. var that = this;
  102. if (node){
  103. var row = node.serialNo();
  104. if (this.tree.delete(node)) {
  105. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  106. that.sheet.deleteRows(row,1);
  107. next?that.setTreeSelected(that.tree.items[row]):"";
  108. });
  109. if(typeof cbTools !== 'undefined'){
  110. cbTools.refreshFormulaNodes();
  111. }
  112. }
  113. }
  114. };
  115. controller.prototype.upLevel = function () {
  116. var that = this;
  117. if (this.tree.selected) {
  118. if (this.tree.selected.upLevel()) {
  119. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  120. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  121. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  122. if (that.event.refreshBaseActn) {
  123. that.event.refreshBaseActn(that.tree);
  124. }
  125. });
  126. }
  127. }
  128. };
  129. controller.prototype.downLevel = function () {
  130. var that = this;
  131. if (this.tree.selected) {
  132. if (this.tree.selected.downLevel()) {
  133. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  134. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  135. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  136. if (that.event.refreshBaseActn) {
  137. that.event.refreshBaseActn(that.tree);
  138. }
  139. });
  140. }
  141. }
  142. };
  143. controller.prototype.upMove = function () {
  144. var that = this, sels = this.sheet.getSelections();
  145. if (this.tree.selected) {
  146. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  147. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  148. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo(),true);
  149. if (that.tree.selected.upMove()) {
  150. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  151. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  152. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo());
  153. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  154. if (that.event.refreshBaseActn) {
  155. that.event.refreshBaseActn(that.tree);
  156. }
  157. }
  158. });
  159. }
  160. };
  161. controller.prototype.downMove = function () {
  162. var that = this, sels = this.sheet.getSelections();
  163. if (this.tree.selected) {
  164. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  165. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  166. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo(),true);
  167. if (that.tree.selected.downMove()) {
  168. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  169. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  170. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo());
  171. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  172. if (that.event.refreshBaseActn) {
  173. that.event.refreshBaseActn(that.tree);
  174. }
  175. }
  176. });
  177. }
  178. };
  179. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  180. var that = this;
  181. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  182. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  183. })
  184. }
  185. controller.prototype.setTreeSelected = function (node) {
  186. if (this.event.beforeTreeSelectedChange) {
  187. this.event.beforeTreeSelectedChange(this.tree.selected);
  188. }
  189. this.tree.selected = node;
  190. if (this.event.refreshBaseActn) {
  191. this.event.refreshBaseActn(this.tree);
  192. }
  193. if (this.event.treeSelectedChanged) {
  194. this.event.treeSelectedChanged(this.tree.selected);
  195. }
  196. }
  197. controller.prototype.bind = function (eventName, eventFun) {
  198. this.event[eventName] = eventFun;
  199. };
  200. return new controller();
  201. },
  202. eventName: {
  203. refreshBaseActn: 'refreshBaseActn',
  204. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  205. treeSelectedChanged: 'treeSelectedChanged',
  206. cellDoubleClick: 'cellDoubleClick'
  207. }
  208. };