tree_sheet_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_CONTROLLER = {
  5. createNew: function (tree, sheet, setting, loadHeader = 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 (loadHeader) {
  15. TREE_SHEET_HELPER.loadSheetHeader(this.setting, this.sheet);
  16. }
  17. };
  18. controller.prototype.showTreeData = function () {
  19. var that = this;
  20. TREE_SHEET_HELPER.showTreeData(this.setting, this.sheet, this.tree);
  21. this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  22. that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
  23. });
  24. };
  25. controller.prototype.syncDisplayNewNodes = function (newNodes) {
  26. let me = this;
  27. TREE_SHEET_HELPER.massOperationSheet(me.sheet, function () {
  28. var sels = me.sheet.getSelections();
  29. newNodes.sort(function (a, b) {
  30. let rst = 0;
  31. if(a.serialNo() > b.serialNo()){
  32. rst = 1;
  33. }
  34. else {
  35. rst = -1;
  36. }
  37. return rst;
  38. });
  39. for(let newNode of newNodes){
  40. me.sheet.addRows(newNode.serialNo(), 1);
  41. }
  42. for(let newNode of newNodes){
  43. me.setTreeSelected(newNode);
  44. TREE_SHEET_HELPER.refreshTreeNodeData(me.setting, me.sheet, [newNode], false);
  45. me.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  46. }
  47. });
  48. };
  49. controller.prototype.insert = function () {
  50. var newNode = null, that = this, sels = this.sheet.getSelections();
  51. if (this.tree) {
  52. if (this.tree.selected) {
  53. newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  54. } else {
  55. newNode = this.tree.insert();
  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. };
  68. controller.prototype.m_insert = function (datas) {
  69. let nodes = [], that = this, sels = this.sheet.getSelections();
  70. if(this.tree){
  71. if (this.tree.selected) {
  72. nodes = this.tree.m_insert(datas,this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  73. } else {
  74. nodes = this.tree.m_insert(datas);
  75. }
  76. }
  77. let length = nodes.length;
  78. if (nodes.length > 0) {
  79. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  80. that.sheet.addRows(nodes[length - 1].serialNo(), length);
  81. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, false);
  82. that.setTreeSelected(nodes[length - 1]);
  83. that.sheet.setSelection(nodes[length - 1].serialNo(), sels[0].col, 1, 1);
  84. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  85. });
  86. }
  87. };
  88. controller.prototype.insertByID = function (ID) {
  89. var newNode = null, that = this, sels = this.sheet.getSelections();
  90. if (this.tree) {
  91. if (this.tree.selected) {
  92. newNode = this.tree.insertByID(ID, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  93. } else {
  94. newNode = this.tree.insertByID(ID);
  95. }
  96. if (newNode) {
  97. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  98. that.sheet.addRows(newNode.serialNo(), 1);
  99. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  100. that.setTreeSelected(newNode);
  101. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  102. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  103. });
  104. }
  105. }
  106. return newNode;
  107. };
  108. controller.prototype.insertByIDS = function (ID, ParentID, NexSiblingID) {
  109. var newNode = null, that = this, sels = this.sheet.getSelections();
  110. if (this.tree) {
  111. if (this.tree.selected) {
  112. newNode = this.tree.insertByID(ID, ParentID, NexSiblingID);
  113. } else {
  114. newNode = this.tree.insertByID(ID);
  115. }
  116. if (newNode) {
  117. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  118. that.sheet.addRows(newNode.serialNo(), 1);
  119. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  120. that.setTreeSelected(newNode);
  121. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  122. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  123. });
  124. }
  125. }
  126. return newNode;
  127. };
  128. controller.prototype.insertToChild = function (ID, ParentID, NextSiblingID) {
  129. var newNode = null, that = this, sels = this.sheet.getSelections();
  130. if (this.tree) {
  131. if (this.tree.selected) {
  132. newNode = this.tree.insertByID(ID, ParentID, NextSiblingID);
  133. } else {
  134. newNode = this.tree.insertByID(ID);
  135. }
  136. if (newNode) {
  137. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  138. that.sheet.addRows(newNode.serialNo(), 1);
  139. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  140. that.setTreeSelected(newNode);
  141. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  142. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  143. });
  144. }
  145. }
  146. return newNode;
  147. };
  148. controller.prototype.delete = function () {
  149. var that = this, sels = this.sheet.getSelections();
  150. if (this.tree.selected) {
  151. if (this.tree.delete(this.tree.selected)) {
  152. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  153. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  154. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  155. });
  156. }
  157. }
  158. };
  159. controller.prototype.m_delete = function (nodes) {
  160. var that = this, sels = this.sheet.getSelections();
  161. if(nodes.length > 0){
  162. if(this.tree.m_delete(nodes)){
  163. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  164. let rowCount = 0;
  165. for(let node of nodes){
  166. rowCount = rowCount+node.posterityCount() + 1;
  167. }
  168. that.sheet.deleteRows(sels[0].row, rowCount);
  169. that.setTreeSelected(that.tree.items[sels[0].row]);
  170. that.sheet.setSelection(sels[0].row,sels[0].col,1,sels[0].colCount);
  171. });
  172. }
  173. }
  174. };
  175. controller.prototype.upLevel = function () {
  176. var that = this;
  177. if (this.tree.selected) {
  178. if (this.tree.selected.upLevel()) {
  179. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  180. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  181. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  182. if (that.event.refreshBaseActn) {
  183. that.event.refreshBaseActn(that.tree);
  184. }
  185. });
  186. }
  187. }
  188. };
  189. controller.prototype.downLevel = function () {
  190. var that = this;
  191. if (this.tree.selected) {
  192. if (this.tree.selected.downLevel()) {
  193. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  194. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  195. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  196. if (that.event.refreshBaseActn) {
  197. that.event.refreshBaseActn(that.tree);
  198. }
  199. });
  200. }
  201. }
  202. };
  203. controller.prototype.m_upLevel = function (nodes) { //多选升级
  204. let that = this;
  205. if (this.tree.m_upLevel(nodes)) {
  206. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  207. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0]], that.sheet, true);
  208. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  209. if (that.event.refreshBaseActn) {
  210. that.event.refreshBaseActn(that.tree);
  211. }
  212. });
  213. }
  214. };
  215. controller.prototype.m_downLevel = function (nodes) { //多选降级
  216. let that = this;
  217. if (this.tree.m_downLevel(nodes)) {
  218. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  219. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0].parent], that.sheet, true);
  220. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  221. if (that.event.refreshBaseActn) {
  222. that.event.refreshBaseActn(that.tree);
  223. }
  224. });
  225. }
  226. };
  227. controller.prototype.upMove = function () {
  228. var that = this, sels = this.sheet.getSelections();
  229. if (this.tree.selected) {
  230. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  231. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  232. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo(),true);
  233. if (that.tree.selected.upMove()) {
  234. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  235. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  236. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo());
  237. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  238. if (that.event.refreshBaseActn) {
  239. that.event.refreshBaseActn(that.tree);
  240. }
  241. }
  242. });
  243. }
  244. };
  245. controller.prototype.downMove = function () {
  246. var that = this, sels = this.sheet.getSelections();
  247. if (this.tree.selected) {
  248. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  249. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo(),true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  250. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.nextSibling,that.tree.selected.nextSibling.serialNo(),true);
  251. if (that.tree.selected.downMove()) {
  252. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  253. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected,that.tree.selected.serialNo());
  254. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet,that.tree,that.tree.selected.preSibling,that.tree.selected.preSibling.serialNo());
  255. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  256. if (that.event.refreshBaseActn) {
  257. that.event.refreshBaseActn(that.tree);
  258. }
  259. }
  260. });
  261. }
  262. };
  263. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  264. var that = this;
  265. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  266. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  267. })
  268. }
  269. controller.prototype.setTreeSelected = function (node) {
  270. if (this.event.beforeTreeSelectedChange) {
  271. this.event.beforeTreeSelectedChange(this.tree.selected);
  272. }
  273. this.tree.selected = node;
  274. if (this.event.refreshBaseActn) {
  275. this.event.refreshBaseActn(this.tree);
  276. }
  277. if (this.event.treeSelectedChanged) {
  278. this.event.treeSelectedChanged(this.tree.selected);
  279. }
  280. }
  281. controller.prototype.bind = function (eventName, eventFun) {
  282. this.event[eventName] = eventFun;
  283. };
  284. return new controller();
  285. },
  286. eventName: {
  287. refreshBaseActn: 'refreshBaseActn',
  288. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  289. treeSelectedChanged: 'treeSelectedChanged',
  290. cellDoubleClick: 'cellDoubleClick'
  291. }
  292. };