tree_sheet_controller.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. cbTools.refreshFormulaNodes();
  41. });
  42. }
  43. }
  44. };
  45. controller.prototype.delete = function () {
  46. var that = this, sels = this.sheet.getSelections();
  47. if (this.tree.selected) {
  48. if (this.tree.delete(this.tree.selected)) {
  49. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  50. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  51. that.setTreeSelected(that.tree.items[sels[0].row]);
  52. });
  53. cbTools.refreshFormulaNodes();
  54. }
  55. }
  56. };
  57. controller.prototype.m_delete = function (nodes, beginRow = null) {//删除选中的多行节点
  58. var that = this, sels = this.sheet.getSelections();
  59. if (this.tree.selected) {
  60. if (this.tree.m_delete(nodes)) {
  61. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  62. let rowCount = 0;
  63. for(let node of nodes){
  64. rowCount = rowCount+node.posterityCount() + 1;
  65. }
  66. if(beginRow){
  67. sels[0].row = beginRow;
  68. }
  69. that.sheet.deleteRows(sels[0].row, rowCount);
  70. that.setTreeSelected(that.tree.items[sels[0].row]);
  71. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  72. });
  73. cbTools.refreshFormulaNodes();
  74. }
  75. }
  76. };
  77. controller.prototype.singleDelete = function () {//只删除当前节点,不删除子节点
  78. var that = this, sels = this.sheet.getSelections();
  79. if (this.tree.selected) {
  80. if (this.tree.singleDelete(this.tree.selected)) {
  81. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  82. that.sheet.deleteRows(sels[0].row,1);
  83. that.setTreeSelected(that.tree.items[sels[0].row]);
  84. });
  85. }
  86. }
  87. };
  88. controller.prototype.deleteNode = function (node,next) {
  89. var that = this;
  90. if (node){
  91. var row = node.serialNo();
  92. if (this.tree.delete(node)) {
  93. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  94. that.sheet.deleteRows(row,1);
  95. next?that.setTreeSelected(that.tree.items[row]):"";
  96. });
  97. cbTools.refreshFormulaNodes();
  98. }
  99. }
  100. };
  101. controller.prototype.upLevel = function () {
  102. var that = this;
  103. if (this.tree.selected) {
  104. if (this.tree.selected.upLevel()) {
  105. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  106. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  107. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  108. if (that.event.refreshBaseActn) {
  109. that.event.refreshBaseActn(that.tree);
  110. }
  111. });
  112. }
  113. }
  114. };
  115. controller.prototype.downLevel = function () {
  116. var that = this;
  117. if (this.tree.selected) {
  118. if (this.tree.selected.downLevel()) {
  119. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  120. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], 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.upMove = function () {
  130. var that = this, sels = this.sheet.getSelections();
  131. if (this.tree.selected) {
  132. if (this.tree.selected.upMove()) {
  133. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  134. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  135. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  136. if (that.event.refreshBaseActn) {
  137. that.event.refreshBaseActn(that.tree);
  138. }
  139. });
  140. }
  141. }
  142. };
  143. controller.prototype.downMove = function () {
  144. var that = this, sels = this.sheet.getSelections();
  145. if (this.tree.selected) {
  146. if (this.tree.selected.downMove()) {
  147. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  148. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  149. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  150. if (that.event.refreshBaseActn) {
  151. that.event.refreshBaseActn(that.tree);
  152. }
  153. });
  154. }
  155. }
  156. };
  157. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  158. var that = this;
  159. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  160. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  161. })
  162. }
  163. controller.prototype.setTreeSelected = function (node) {
  164. if (this.event.beforeTreeSelectedChange) {
  165. this.event.beforeTreeSelectedChange(this.tree.selected);
  166. }
  167. this.tree.selected = node;
  168. if (this.event.refreshBaseActn) {
  169. this.event.refreshBaseActn(this.tree);
  170. }
  171. if (this.event.treeSelectedChanged) {
  172. this.event.treeSelectedChanged(this.tree.selected);
  173. }
  174. }
  175. controller.prototype.bind = function (eventName, eventFun) {
  176. this.event[eventName] = eventFun;
  177. };
  178. return new controller();
  179. },
  180. eventName: {
  181. refreshBaseActn: 'refreshBaseActn',
  182. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  183. treeSelectedChanged: 'treeSelectedChanged',
  184. cellDoubleClick: 'cellDoubleClick'
  185. }
  186. };