tree_sheet_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.m_delete = function (nodes) {
  114. var that = this, sels = this.sheet.getSelections();
  115. if(nodes.length > 0){
  116. if(this.tree.m_delete(nodes)){
  117. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  118. let rowCount = 0;
  119. for(let node of nodes){
  120. rowCount = rowCount+node.posterityCount() + 1;
  121. }
  122. that.sheet.deleteRows(sels[0].row, rowCount);
  123. that.setTreeSelected(that.tree.items[sels[0].row]);
  124. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  125. });
  126. }
  127. }
  128. };
  129. controller.prototype.upLevel = function () {
  130. var that = this;
  131. if (this.tree.selected) {
  132. if (this.tree.selected.upLevel()) {
  133. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  134. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  135. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  136. if (that.event.refreshBaseActn) {
  137. that.event.refreshBaseActn(that.tree);
  138. }
  139. });
  140. }
  141. }
  142. };
  143. controller.prototype.downLevel = function () {
  144. var that = this;
  145. if (this.tree.selected) {
  146. if (this.tree.selected.downLevel()) {
  147. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  148. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  149. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  150. if (that.event.refreshBaseActn) {
  151. that.event.refreshBaseActn(that.tree);
  152. }
  153. });
  154. }
  155. }
  156. };
  157. controller.prototype.m_upLevel = function (nodes) { //多选升级
  158. let that = this;
  159. if (this.tree.m_upLevel(nodes)) {
  160. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  161. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0]], that.sheet, true);
  162. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  163. if (that.event.refreshBaseActn) {
  164. that.event.refreshBaseActn(that.tree);
  165. }
  166. });
  167. }
  168. };
  169. controller.prototype.m_downLevel = function (nodes) { //多选降级
  170. let that = this;
  171. if (this.tree.m_downLevel(nodes)) {
  172. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  173. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0].parent], that.sheet, true);
  174. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  175. if (that.event.refreshBaseActn) {
  176. that.event.refreshBaseActn(that.tree);
  177. }
  178. });
  179. }
  180. };
  181. controller.prototype.upMove = function () {
  182. var that = this, sels = this.sheet.getSelections();
  183. if (this.tree.selected) {
  184. if (this.tree.selected.upMove()) {
  185. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  186. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  187. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  188. if (that.event.refreshBaseActn) {
  189. that.event.refreshBaseActn(that.tree);
  190. }
  191. });
  192. }
  193. }
  194. };
  195. controller.prototype.downMove = function () {
  196. var that = this, sels = this.sheet.getSelections();
  197. if (this.tree.selected) {
  198. if (this.tree.selected.downMove()) {
  199. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  200. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  201. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  202. if (that.event.refreshBaseActn) {
  203. that.event.refreshBaseActn(that.tree);
  204. }
  205. });
  206. }
  207. }
  208. };
  209. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  210. var that = this;
  211. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  212. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  213. })
  214. }
  215. controller.prototype.setTreeSelected = function (node) {
  216. if (this.event.beforeTreeSelectedChange) {
  217. this.event.beforeTreeSelectedChange(this.tree.selected);
  218. }
  219. this.tree.selected = node;
  220. if (this.event.refreshBaseActn) {
  221. this.event.refreshBaseActn(this.tree);
  222. }
  223. if (this.event.treeSelectedChanged) {
  224. this.event.treeSelectedChanged(this.tree.selected);
  225. }
  226. }
  227. controller.prototype.bind = function (eventName, eventFun) {
  228. this.event[eventName] = eventFun;
  229. };
  230. return new controller();
  231. },
  232. eventName: {
  233. refreshBaseActn: 'refreshBaseActn',
  234. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  235. treeSelectedChanged: 'treeSelectedChanged',
  236. cellDoubleClick: 'cellDoubleClick'
  237. }
  238. };