tree_sheet_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.m_insert = function (datas) {
  43. let nodes = [], that = this, sels = this.sheet.getSelections();
  44. if(this.tree){
  45. if (this.tree.selected) {
  46. nodes = this.tree.m_insert(datas,this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  47. } else {
  48. nodes = this.tree.m_insert(datas);
  49. }
  50. }
  51. let length = nodes.length;
  52. if (nodes.length > 0) {
  53. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  54. that.sheet.addRows(nodes[length - 1].serialNo(), length);
  55. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, false);
  56. that.setTreeSelected(nodes[length - 1]);
  57. that.sheet.setSelection(nodes[length - 1].serialNo(), sels[0].col, 1, 1);
  58. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  59. });
  60. }
  61. };
  62. controller.prototype.insertByID = function (ID) {
  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, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  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.insertByIDS = function (ID, ParentID, NexSiblingID) {
  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, NexSiblingID);
  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.insertToChild = function (ID, ParentID, NextSiblingID) {
  103. var newNode = null, that = this, sels = this.sheet.getSelections();
  104. if (this.tree) {
  105. if (this.tree.selected) {
  106. newNode = this.tree.insertByID(ID, ParentID, NextSiblingID);
  107. } else {
  108. newNode = this.tree.insertByID(ID);
  109. }
  110. if (newNode) {
  111. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  112. that.sheet.addRows(newNode.serialNo(), 1);
  113. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  114. that.setTreeSelected(newNode);
  115. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  116. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  117. });
  118. }
  119. }
  120. return newNode;
  121. };
  122. controller.prototype.delete = function () {
  123. var that = this, sels = this.sheet.getSelections();
  124. if (this.tree.selected) {
  125. if (this.tree.delete(this.tree.selected)) {
  126. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  127. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  128. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  129. });
  130. }
  131. }
  132. };
  133. controller.prototype.m_delete = function (nodes) {
  134. var that = this, sels = this.sheet.getSelections();
  135. if(nodes.length > 0){
  136. if(this.tree.m_delete(nodes)){
  137. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  138. let rowCount = 0;
  139. for(let node of nodes){
  140. rowCount = rowCount+node.posterityCount() + 1;
  141. }
  142. that.sheet.deleteRows(sels[0].row, rowCount);
  143. that.setTreeSelected(that.tree.items[sels[0].row]);
  144. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  145. });
  146. }
  147. }
  148. };
  149. controller.prototype.upLevel = function () {
  150. var that = this;
  151. if (this.tree.selected) {
  152. if (this.tree.selected.upLevel()) {
  153. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  154. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  155. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  156. if (that.event.refreshBaseActn) {
  157. that.event.refreshBaseActn(that.tree);
  158. }
  159. });
  160. }
  161. }
  162. };
  163. controller.prototype.downLevel = function () {
  164. var that = this;
  165. if (this.tree.selected) {
  166. if (this.tree.selected.downLevel()) {
  167. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  168. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  169. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  170. if (that.event.refreshBaseActn) {
  171. that.event.refreshBaseActn(that.tree);
  172. }
  173. });
  174. }
  175. }
  176. };
  177. controller.prototype.m_upLevel = function (nodes) { //多选升级
  178. let that = this;
  179. if (this.tree.m_upLevel(nodes)) {
  180. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  181. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0]], that.sheet, true);
  182. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  183. if (that.event.refreshBaseActn) {
  184. that.event.refreshBaseActn(that.tree);
  185. }
  186. });
  187. }
  188. };
  189. controller.prototype.m_downLevel = function (nodes) { //多选降级
  190. let that = this;
  191. if (this.tree.m_downLevel(nodes)) {
  192. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  193. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0].parent], that.sheet, true);
  194. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  195. if (that.event.refreshBaseActn) {
  196. that.event.refreshBaseActn(that.tree);
  197. }
  198. });
  199. }
  200. };
  201. controller.prototype.upMove = function () {
  202. var that = this, sels = this.sheet.getSelections();
  203. if (this.tree.selected) {
  204. if (this.tree.selected.upMove()) {
  205. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  206. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  207. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  208. if (that.event.refreshBaseActn) {
  209. that.event.refreshBaseActn(that.tree);
  210. }
  211. });
  212. }
  213. }
  214. };
  215. controller.prototype.downMove = function () {
  216. var that = this, sels = this.sheet.getSelections();
  217. if (this.tree.selected) {
  218. if (this.tree.selected.downMove()) {
  219. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  220. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  221. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  222. if (that.event.refreshBaseActn) {
  223. that.event.refreshBaseActn(that.tree);
  224. }
  225. });
  226. }
  227. }
  228. };
  229. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  230. var that = this;
  231. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  232. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  233. })
  234. }
  235. controller.prototype.setTreeSelected = function (node) {
  236. if (this.event.beforeTreeSelectedChange) {
  237. this.event.beforeTreeSelectedChange(this.tree.selected);
  238. }
  239. this.tree.selected = node;
  240. if (this.event.refreshBaseActn) {
  241. this.event.refreshBaseActn(this.tree);
  242. }
  243. if (this.event.treeSelectedChanged) {
  244. this.event.treeSelectedChanged(this.tree.selected);
  245. }
  246. }
  247. controller.prototype.bind = function (eventName, eventFun) {
  248. this.event[eventName] = eventFun;
  249. };
  250. return new controller();
  251. },
  252. eventName: {
  253. refreshBaseActn: 'refreshBaseActn',
  254. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  255. treeSelectedChanged: 'treeSelectedChanged',
  256. cellDoubleClick: 'cellDoubleClick'
  257. }
  258. };