tree_sheet_controller.js 7.4 KB

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