tree_sheet_controller.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.delete = function () {
  43. var that = this, sels = this.sheet.getSelections();
  44. if (this.tree.selected) {
  45. if (this.tree.delete(this.tree.selected)) {
  46. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  47. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  48. that.setTreeSelected(that.tree.items[sels[0].row]);
  49. });
  50. }
  51. }
  52. };
  53. controller.prototype.upLevel = function () {
  54. var that = this;
  55. if (this.tree.selected) {
  56. if (this.tree.selected.upLevel()) {
  57. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  58. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  59. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  60. if (that.event.refreshBaseActn) {
  61. that.event.refreshBaseActn(that.tree);
  62. }
  63. });
  64. }
  65. }
  66. };
  67. controller.prototype.downLevel = function () {
  68. var that = this;
  69. if (this.tree.selected) {
  70. if (this.tree.selected.downLevel()) {
  71. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  72. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  73. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  74. if (that.event.refreshBaseActn) {
  75. that.event.refreshBaseActn(that.tree);
  76. }
  77. });
  78. }
  79. }
  80. };
  81. controller.prototype.upMove = function () {
  82. var that = this, sels = this.sheet.getSelections();
  83. if (this.tree.selected) {
  84. if (this.tree.selected.upMove()) {
  85. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  86. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  87. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  88. if (that.event.refreshBaseActn) {
  89. that.event.refreshBaseActn(that.tree);
  90. }
  91. });
  92. }
  93. }
  94. };
  95. controller.prototype.downMove = function () {
  96. var that = this, sels = this.sheet.getSelections();
  97. if (this.tree.selected) {
  98. if (this.tree.selected.downMove()) {
  99. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  100. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  101. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  102. if (that.event.refreshBaseActn) {
  103. that.event.refreshBaseActn(that.tree);
  104. }
  105. });
  106. }
  107. }
  108. };
  109. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  110. var that = this;
  111. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  112. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  113. })
  114. }
  115. controller.prototype.setTreeSelected = function (node) {
  116. this.tree.selected = node;
  117. if (this.event.refreshBaseActn) {
  118. this.event.refreshBaseActn(this.tree);
  119. }
  120. if (this.event.treeSelectedChanged) {
  121. this.event.treeSelectedChanged(this.tree.selected);
  122. }
  123. }
  124. controller.prototype.bind = function (eventName, eventFun) {
  125. this.event[eventName] = eventFun;
  126. };
  127. return new controller();
  128. },
  129. eventName: {
  130. refreshBaseActn: 'refreshBaseActn',
  131. treeSelectedChanged: 'treeSelectedChanged'
  132. }
  133. };