tree_sheet_controller.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_CONTROLLER = {
  5. createNew: function (tree, sheet, setting) {
  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. TREE_SHEET_HELPER.loadSheetHeader(this.setting, this.sheet);
  15. };
  16. controller.prototype.showTreeData = function () {
  17. //
  18. var that = this;
  19. TREE_SHEET_HELPER.showTreeData(this.setting, this.sheet, this.tree);
  20. this.sheet.unbind(GC.Spread.Sheets.Events.SelectionChanged);
  21. this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  22. that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
  23. });
  24. };
  25. controller.prototype.insert = function () {
  26. var newNode = null, that = this, sels = this.sheet.getSelections();
  27. if (this.tree) {
  28. if (this.tree.selected) {
  29. newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  30. } else {
  31. newNode = this.tree.insert();
  32. }
  33. if (newNode) {
  34. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  35. that.sheet.addRows(newNode.serialNo(), 1);
  36. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  37. that.setTreeSelected(newNode);
  38. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  39. that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  40. if(typeof cbTools !== 'undefined'){
  41. cbTools.refreshFormulaNodes();
  42. }
  43. });
  44. }
  45. }
  46. };
  47. controller.prototype.delete = function () {
  48. var that = this, sels = this.sheet.getSelections();
  49. if (this.tree.selected) {
  50. if (this.tree.delete(this.tree.selected)) {
  51. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  52. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  53. that.setTreeSelected(that.tree.items[sels[0].row]);
  54. });
  55. if(typeof cbTools !== 'undefined'){
  56. cbTools.refreshFormulaNodes();
  57. }
  58. }
  59. }
  60. };
  61. controller.prototype.m_delete = function (nodes, beginRow = null) {//删除选中的多行节点
  62. var that = this, sels = this.sheet.getSelections();
  63. if (this.tree.selected) {
  64. if (this.tree.m_delete(nodes)) {
  65. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  66. let rowCount = 0;
  67. for(let node of nodes){
  68. rowCount = rowCount+node.posterityCount() + 1;
  69. }
  70. if(beginRow){
  71. sels[0].row = beginRow;
  72. }
  73. that.sheet.deleteRows(sels[0].row, rowCount);
  74. that.setTreeSelected(that.tree.items[sels[0].row]);
  75. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  76. });
  77. if(typeof cbTools !== 'undefined'){
  78. cbTools.refreshFormulaNodes();
  79. }
  80. }
  81. }
  82. };
  83. controller.prototype.singleDelete = function () {//只删除当前节点,不删除子节点
  84. var that = this, sels = this.sheet.getSelections();
  85. if (this.tree.selected) {
  86. if (this.tree.singleDelete(this.tree.selected)) {
  87. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  88. that.sheet.deleteRows(sels[0].row,1);
  89. that.setTreeSelected(that.tree.items[sels[0].row]);
  90. });
  91. }
  92. }
  93. };
  94. controller.prototype.deleteNode = function (node,next) {
  95. var that = this;
  96. if (node){
  97. var row = node.serialNo();
  98. if (this.tree.delete(node)) {
  99. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  100. that.sheet.deleteRows(row,1);
  101. next?that.setTreeSelected(that.tree.items[row]):"";
  102. });
  103. if(typeof cbTools !== 'undefined'){
  104. cbTools.refreshFormulaNodes();
  105. }
  106. }
  107. }
  108. };
  109. controller.prototype.upLevel = function () {
  110. var that = this;
  111. if (this.tree.selected) {
  112. if (this.tree.selected.upLevel()) {
  113. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  114. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  115. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  116. if (that.event.refreshBaseActn) {
  117. that.event.refreshBaseActn(that.tree);
  118. }
  119. });
  120. }
  121. }
  122. };
  123. controller.prototype.downLevel = function () {
  124. var that = this;
  125. if (this.tree.selected) {
  126. if (this.tree.selected.downLevel()) {
  127. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  128. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  129. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  130. if (that.event.refreshBaseActn) {
  131. that.event.refreshBaseActn(that.tree);
  132. }
  133. });
  134. }
  135. }
  136. };
  137. controller.prototype.upMove = function () {
  138. var that = this, sels = this.sheet.getSelections();
  139. if (this.tree.selected) {
  140. if (this.tree.selected.upMove()) {
  141. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  142. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  143. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  144. if (that.event.refreshBaseActn) {
  145. that.event.refreshBaseActn(that.tree);
  146. }
  147. });
  148. }
  149. }
  150. };
  151. controller.prototype.downMove = function () {
  152. var that = this, sels = this.sheet.getSelections();
  153. if (this.tree.selected) {
  154. if (this.tree.selected.downMove()) {
  155. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  156. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  157. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  158. if (that.event.refreshBaseActn) {
  159. that.event.refreshBaseActn(that.tree);
  160. }
  161. });
  162. }
  163. }
  164. };
  165. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  166. var that = this;
  167. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  168. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  169. })
  170. }
  171. controller.prototype.setTreeSelected = function (node) {
  172. if (this.event.beforeTreeSelectedChange) {
  173. this.event.beforeTreeSelectedChange(this.tree.selected);
  174. }
  175. this.tree.selected = node;
  176. if (this.event.refreshBaseActn) {
  177. this.event.refreshBaseActn(this.tree);
  178. }
  179. if (this.event.treeSelectedChanged) {
  180. this.event.treeSelectedChanged(this.tree.selected);
  181. }
  182. }
  183. controller.prototype.bind = function (eventName, eventFun) {
  184. this.event[eventName] = eventFun;
  185. };
  186. return new controller();
  187. },
  188. eventName: {
  189. refreshBaseActn: 'refreshBaseActn',
  190. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  191. treeSelectedChanged: 'treeSelectedChanged',
  192. cellDoubleClick: 'cellDoubleClick'
  193. }
  194. };