tree_sheet_controller.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  20. that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
  21. });
  22. };
  23. controller.prototype.insert = function () {
  24. var newNode = null, that = this, sels = this.sheet.getSelections();
  25. if (this.tree) {
  26. if (this.tree.selected) {
  27. newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  28. } else {
  29. newNode = this.tree.insert();
  30. }
  31. if (newNode) {
  32. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  33. that.sheet.addRows(newNode.serialNo(), 1);
  34. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  35. that.setTreeSelected(newNode);
  36. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  37. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  38. });
  39. }
  40. }
  41. };
  42. controller.prototype.insertByID = function (ID) {
  43. var newNode = null, that = this, sels = this.sheet.getSelections();
  44. if (this.tree) {
  45. if (this.tree.selected) {
  46. newNode = this.tree.insertByID(ID, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  47. } else {
  48. newNode = this.tree.insertByID(ID);
  49. }
  50. if (newNode) {
  51. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  52. that.sheet.addRows(newNode.serialNo(), 1);
  53. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  54. that.setTreeSelected(newNode);
  55. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  56. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  57. });
  58. }
  59. }
  60. };
  61. controller.prototype.delete = function () {
  62. var that = this, sels = this.sheet.getSelections();
  63. if (this.tree.selected) {
  64. if (this.tree.delete(this.tree.selected)) {
  65. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  66. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  67. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  68. });
  69. }
  70. }
  71. };
  72. controller.prototype.upLevel = function () {
  73. var that = this;
  74. if (this.tree.selected) {
  75. if (this.tree.selected.upLevel()) {
  76. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  77. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  78. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  79. if (that.event.refreshBaseActn) {
  80. that.event.refreshBaseActn(that.tree);
  81. }
  82. });
  83. }
  84. }
  85. };
  86. controller.prototype.downLevel = function () {
  87. var that = this;
  88. if (this.tree.selected) {
  89. if (this.tree.selected.downLevel()) {
  90. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  91. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  92. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  93. if (that.event.refreshBaseActn) {
  94. that.event.refreshBaseActn(that.tree);
  95. }
  96. });
  97. }
  98. }
  99. };
  100. controller.prototype.upMove = function () {
  101. var that = this, sels = this.sheet.getSelections();
  102. if (this.tree.selected) {
  103. if (this.tree.selected.upMove()) {
  104. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  105. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  106. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  107. if (that.event.refreshBaseActn) {
  108. that.event.refreshBaseActn(that.tree);
  109. }
  110. });
  111. }
  112. }
  113. };
  114. controller.prototype.downMove = function () {
  115. var that = this, sels = this.sheet.getSelections();
  116. if (this.tree.selected) {
  117. if (this.tree.selected.downMove()) {
  118. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  119. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  120. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  121. if (that.event.refreshBaseActn) {
  122. that.event.refreshBaseActn(that.tree);
  123. }
  124. });
  125. }
  126. }
  127. };
  128. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  129. var that = this;
  130. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  131. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  132. })
  133. }
  134. controller.prototype.setTreeSelected = function (node) {
  135. if (this.event.beforeTreeSelectedChange) {
  136. this.event.beforeTreeSelectedChange(this.tree.selected);
  137. }
  138. this.tree.selected = node;
  139. if (this.event.refreshBaseActn) {
  140. this.event.refreshBaseActn(this.tree);
  141. }
  142. if (this.event.treeSelectedChanged) {
  143. this.event.treeSelectedChanged(this.tree.selected);
  144. }
  145. }
  146. controller.prototype.bind = function (eventName, eventFun) {
  147. this.event[eventName] = eventFun;
  148. };
  149. return new controller();
  150. },
  151. eventName: {
  152. refreshBaseActn: 'refreshBaseActn',
  153. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  154. treeSelectedChanged: 'treeSelectedChanged',
  155. cellDoubleClick: 'cellDoubleClick'
  156. }
  157. };