tree_sheet_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_CONTROLLER = {
  5. createNew: function (tree, sheet, setting, loadSheetHeader = true) {
  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. if(loadSheetHeader){
  15. TREE_SHEET_HELPER.loadSheetHeader(this.setting, this.sheet);
  16. }
  17. };
  18. controller.prototype.showTreeData = function () {
  19. //
  20. var that = this;
  21. TREE_SHEET_HELPER.showTreeData(this.setting, this.sheet, this.tree);
  22. this.sheet.unbind(GC.Spread.Sheets.Events.SelectionChanged);
  23. this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  24. that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
  25. });
  26. };
  27. controller.prototype.insert = function () {
  28. var newNode = null, that = this, sels = this.sheet.getSelections();
  29. if (this.tree) {
  30. if (this.tree.selected) {
  31. newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  32. } else {
  33. newNode = this.tree.insert();
  34. }
  35. if (newNode) {
  36. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  37. that.sheet.addRows(newNode.serialNo(), 1);
  38. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  39. that.setTreeSelected(newNode);
  40. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  41. that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  42. if(typeof cbTools !== 'undefined'){
  43. cbTools.refreshFormulaNodes();
  44. }
  45. });
  46. }
  47. }
  48. };
  49. controller.prototype.insertByID = function (ID) {
  50. var newNode = null, that = this, sels = this.sheet.getSelections();
  51. if (this.tree) {
  52. if (this.tree.selected) {
  53. newNode = this.tree.insertByID(ID, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  54. } else {
  55. newNode = this.tree.insertByID(ID);
  56. }
  57. if (newNode) {
  58. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  59. that.sheet.addRows(newNode.serialNo(), 1);
  60. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  61. that.setTreeSelected(newNode);
  62. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  63. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  64. });
  65. }
  66. }
  67. return newNode;
  68. };
  69. controller.prototype.delete = function () {
  70. var that = this, sels = this.sheet.getSelections();
  71. if (this.tree.selected) {
  72. if (this.tree.delete(this.tree.selected)) {
  73. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  74. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  75. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  76. });
  77. if(typeof cbTools !== 'undefined'){
  78. cbTools.refreshFormulaNodes();
  79. }
  80. }
  81. }
  82. };
  83. controller.prototype.m_delete = function (nodes, beginRow = null,selectNew = true) {//删除选中的多行节点,selectNew 为false时删除后不改变tree的选中节点
  84. var that = this, sels = this.sheet.getSelections();
  85. if (this.tree.selected) {
  86. if (this.tree.m_delete(nodes)) {
  87. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  88. let rowCount = 0;
  89. let delRow = beginRow?beginRow:sels[0].row;
  90. for(let node of nodes){
  91. rowCount = rowCount+node.posterityCount() + 1;
  92. }
  93. that.sheet.deleteRows(delRow, rowCount);
  94. if(selectNew == true){
  95. if(beginRow) sels[0].row = beginRow;
  96. if(sels[0].row >= that.tree.items.length){
  97. sels[0].row = that.tree.items.length-1;
  98. sels[0].colCount = 1;
  99. }
  100. that.setTreeSelected(that.tree.items[sels[0].row]);
  101. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  102. }
  103. });
  104. if(typeof cbTools !== 'undefined'){
  105. cbTools.refreshFormulaNodes();
  106. }
  107. }
  108. }
  109. };
  110. controller.prototype.singleDelete = function () {//只删除当前节点,不删除子节点
  111. var that = this, sels = this.sheet.getSelections();
  112. if (this.tree.selected) {
  113. if (this.tree.singleDelete(this.tree.selected)) {
  114. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  115. that.sheet.deleteRows(sels[0].row,1);
  116. that.setTreeSelected(that.tree.items[sels[0].row]);
  117. });
  118. }
  119. }
  120. };
  121. controller.prototype.deleteNode = function (node,next) {
  122. var that = this;
  123. if (node){
  124. var row = node.serialNo();
  125. if (this.tree.delete(node)) {
  126. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  127. that.sheet.deleteRows(row,1);
  128. next?that.setTreeSelected(that.tree.items[row]):"";
  129. });
  130. if(typeof cbTools !== 'undefined'){
  131. cbTools.refreshFormulaNodes();
  132. }
  133. }
  134. }
  135. };
  136. controller.prototype.upLevel = function () {
  137. var that = this;
  138. if (this.tree.selected) {
  139. if (this.tree.selected.upLevel()) {
  140. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  141. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  142. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  143. if (that.event.refreshBaseActn) {
  144. that.event.refreshBaseActn(that.tree);
  145. }
  146. });
  147. }
  148. }
  149. };
  150. controller.prototype.downLevel = function () {
  151. var that = this;
  152. if (this.tree.selected) {
  153. if (this.tree.selected.downLevel()) {
  154. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  155. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  156. that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  157. if (that.event.refreshBaseActn) {
  158. that.event.refreshBaseActn(that.tree);
  159. }
  160. });
  161. }
  162. }
  163. };
  164. controller.prototype.upMove = function () {
  165. var that = this, sels = this.sheet.getSelections();
  166. if (this.tree.selected) {
  167. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  168. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  169. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo(),true);
  170. if (that.tree.selected.upMove()) {
  171. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  172. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  173. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo());
  174. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  175. if (that.event.refreshBaseActn) {
  176. that.event.refreshBaseActn(that.tree);
  177. }
  178. }
  179. });
  180. }
  181. };
  182. controller.prototype.downMove = function () {
  183. var that = this, sels = this.sheet.getSelections();
  184. if (this.tree.selected) {
  185. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  186. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  187. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo(),true);
  188. if (that.tree.selected.downMove()) {
  189. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  190. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  191. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo());
  192. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  193. if (that.event.refreshBaseActn) {
  194. that.event.refreshBaseActn(that.tree);
  195. }
  196. }
  197. });
  198. }
  199. };
  200. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  201. var that = this;
  202. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  203. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  204. })
  205. };
  206. controller.prototype.setTreeSelection = function (oldSelect) {
  207. let controller = this;
  208. let sel = controller.sheet.getSelections()[0];
  209. if(controller.tree.getNodeByID(oldSelect.getID())){//如果旧选中节点还存在
  210. controller.sheet.setSelection(oldSelect.serialNo(),sel.col,sel.rowCount,sel.colCount);
  211. controller.setTreeSelected(oldSelect)
  212. }else {//选中节点已被删除,自动选中新的节点
  213. controller.setTreeSelected(controller.tree.items[sel.row]);
  214. }
  215. };
  216. controller.prototype.setTreeSelected = function (node) {
  217. if (this.event.beforeTreeSelectedChange) {
  218. this.event.beforeTreeSelectedChange(this.tree.selected);
  219. }
  220. this.tree.selected = node;
  221. if (this.event.refreshBaseActn) {
  222. this.event.refreshBaseActn(this.tree);
  223. }
  224. if (this.event.treeSelectedChanged) {
  225. this.event.treeSelectedChanged(this.tree.selected);
  226. }
  227. }
  228. controller.prototype.bind = function (eventName, eventFun) {
  229. this.event[eventName] = eventFun;
  230. };
  231. return new controller();
  232. },
  233. eventName: {
  234. refreshBaseActn: 'refreshBaseActn',
  235. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  236. treeSelectedChanged: 'treeSelectedChanged',
  237. cellDoubleClick: 'cellDoubleClick'
  238. }
  239. };