tree_sheet_controller.js 8.5 KB

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