tree_sheet_controller.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. return newNode;
  61. };
  62. controller.prototype.insertByIDS = function (ID, ParentID, NexSiblingID) {
  63. var newNode = null, that = this, sels = this.sheet.getSelections();
  64. if (this.tree) {
  65. if (this.tree.selected) {
  66. newNode = this.tree.insertByID(ID, ParentID, NexSiblingID);
  67. } else {
  68. newNode = this.tree.insertByID(ID);
  69. }
  70. if (newNode) {
  71. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  72. that.sheet.addRows(newNode.serialNo(), 1);
  73. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  74. that.setTreeSelected(newNode);
  75. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  76. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  77. });
  78. }
  79. }
  80. return newNode;
  81. };
  82. controller.prototype.insertToChild = function (ID, ParentID, NextSiblingID) {
  83. var newNode = null, that = this, sels = this.sheet.getSelections();
  84. if (this.tree) {
  85. if (this.tree.selected) {
  86. newNode = this.tree.insertByID(ID, ParentID, NextSiblingID);
  87. } else {
  88. newNode = this.tree.insertByID(ID);
  89. }
  90. if (newNode) {
  91. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  92. that.sheet.addRows(newNode.serialNo(), 1);
  93. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  94. that.setTreeSelected(newNode);
  95. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  96. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  97. });
  98. }
  99. }
  100. return newNode;
  101. };
  102. controller.prototype.delete = function () {
  103. var that = this, sels = this.sheet.getSelections();
  104. if (this.tree.selected) {
  105. if (this.tree.delete(this.tree.selected)) {
  106. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  107. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  108. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  109. });
  110. }
  111. }
  112. };
  113. controller.prototype.upLevel = function () {
  114. var that = this;
  115. if (this.tree.selected) {
  116. if (this.tree.selected.upLevel()) {
  117. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  118. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  119. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  120. if (that.event.refreshBaseActn) {
  121. that.event.refreshBaseActn(that.tree);
  122. }
  123. });
  124. }
  125. }
  126. };
  127. controller.prototype.downLevel = function () {
  128. var that = this;
  129. if (this.tree.selected) {
  130. if (this.tree.selected.downLevel()) {
  131. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  132. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  133. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  134. if (that.event.refreshBaseActn) {
  135. that.event.refreshBaseActn(that.tree);
  136. }
  137. });
  138. }
  139. }
  140. };
  141. controller.prototype.upMove = function () {
  142. var that = this, sels = this.sheet.getSelections();
  143. if (this.tree.selected) {
  144. if (this.tree.selected.upMove()) {
  145. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  146. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  147. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  148. if (that.event.refreshBaseActn) {
  149. that.event.refreshBaseActn(that.tree);
  150. }
  151. });
  152. }
  153. }
  154. };
  155. controller.prototype.downMove = function () {
  156. var that = this, sels = this.sheet.getSelections();
  157. if (this.tree.selected) {
  158. if (this.tree.selected.downMove()) {
  159. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  160. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  161. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  162. if (that.event.refreshBaseActn) {
  163. that.event.refreshBaseActn(that.tree);
  164. }
  165. });
  166. }
  167. }
  168. };
  169. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  170. var that = this;
  171. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  172. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  173. })
  174. }
  175. controller.prototype.setTreeSelected = function (node) {
  176. if (this.event.beforeTreeSelectedChange) {
  177. this.event.beforeTreeSelectedChange(this.tree.selected);
  178. }
  179. this.tree.selected = node;
  180. if (this.event.refreshBaseActn) {
  181. this.event.refreshBaseActn(this.tree);
  182. }
  183. if (this.event.treeSelectedChanged) {
  184. this.event.treeSelectedChanged(this.tree.selected);
  185. }
  186. }
  187. controller.prototype.bind = function (eventName, eventFun) {
  188. this.event[eventName] = eventFun;
  189. };
  190. return new controller();
  191. },
  192. eventName: {
  193. refreshBaseActn: 'refreshBaseActn',
  194. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  195. treeSelectedChanged: 'treeSelectedChanged',
  196. cellDoubleClick: 'cellDoubleClick'
  197. }
  198. };