tree_sheet_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. return nodes;
  88. };
  89. controller.prototype.insertByDatas = function (datas) {
  90. let nodes = [], that = this, sels = this.sheet.getSelections();
  91. if (this.tree) {
  92. nodes = this.tree.insertByDatas(datas);
  93. }
  94. let length = nodes.length;
  95. if (nodes.length > 0) {
  96. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  97. that.sheet.addRows(nodes[0].serialNo(), length);
  98. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, false);
  99. that.setTreeSelected(nodes[0]);
  100. that.sheet.setSelection(nodes[0].serialNo(), sels[0].col, 1, 1);
  101. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  102. });
  103. }
  104. return nodes;
  105. };
  106. controller.prototype.insertByID = function (ID) {
  107. var newNode = null, that = this, sels = this.sheet.getSelections();
  108. if (this.tree) {
  109. if (this.tree.selected) {
  110. newNode = this.tree.insertByID(ID, this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
  111. } else {
  112. newNode = this.tree.insertByID(ID);
  113. }
  114. if (newNode) {
  115. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  116. that.sheet.addRows(newNode.serialNo(), 1);
  117. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  118. that.setTreeSelected(newNode);
  119. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  120. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  121. });
  122. }
  123. }
  124. return newNode;
  125. };
  126. controller.prototype.insertByIDS = function (ID, ParentID, NexSiblingID) {
  127. var newNode = null, that = this, sels = this.sheet.getSelections();
  128. if (this.tree) {
  129. if (this.tree.selected) {
  130. newNode = this.tree.insertByID(ID, ParentID, NexSiblingID);
  131. } else {
  132. newNode = this.tree.insertByID(ID);
  133. }
  134. if (newNode) {
  135. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  136. that.sheet.addRows(newNode.serialNo(), 1);
  137. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  138. that.setTreeSelected(newNode);
  139. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  140. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  141. });
  142. }
  143. }
  144. return newNode;
  145. };
  146. controller.prototype.insertToChild = function (ID, ParentID, NextSiblingID) {
  147. var newNode = null, that = this, sels = this.sheet.getSelections();
  148. if (this.tree) {
  149. if (this.tree.selected) {
  150. newNode = this.tree.insertByID(ID, ParentID, NextSiblingID);
  151. } else {
  152. newNode = this.tree.insertByID(ID);
  153. }
  154. if (newNode) {
  155. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  156. that.sheet.addRows(newNode.serialNo(), 1);
  157. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
  158. that.setTreeSelected(newNode);
  159. that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  160. //that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  161. });
  162. }
  163. }
  164. return newNode;
  165. };
  166. controller.prototype.delete = function () {
  167. var that = this, sels = this.sheet.getSelections();
  168. if (this.tree.selected) {
  169. if (this.tree.delete(this.tree.selected)) {
  170. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  171. that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
  172. that.setTreeSelected(that.tree.items[sels[0].row] ? that.tree.items[sels[0].row] : that.tree.items[that.tree.items.length - 1]);
  173. });
  174. }
  175. }
  176. };
  177. controller.prototype.m_delete = function (nodes) {
  178. var that = this, sels = this.sheet.getSelections();
  179. if (nodes.length > 0) {
  180. if (this.tree.m_delete(nodes)) {
  181. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  182. let rowCount = 0;
  183. for (let node of nodes) {
  184. rowCount = rowCount + node.posterityCount() + 1;
  185. }
  186. that.sheet.deleteRows(sels[0].row, rowCount);
  187. const locateRow = that.tree.items.length <= sels[0].row ? that.tree.items.length - 1 : sels[0].row;
  188. that.setTreeSelected(that.tree.items[locateRow]);
  189. that.sheet.setSelection(locateRow, sels[0].col, 1, sels[0].colCount);
  190. });
  191. }
  192. }
  193. };
  194. controller.prototype.upLevel = function () {
  195. var that = this;
  196. if (this.tree.selected) {
  197. if (this.tree.selected.upLevel()) {
  198. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  199. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  200. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  201. if (that.event.refreshBaseActn) {
  202. that.event.refreshBaseActn(that.tree);
  203. }
  204. });
  205. }
  206. }
  207. };
  208. controller.prototype.downLevel = function () {
  209. var that = this;
  210. if (this.tree.selected) {
  211. if (this.tree.selected.downLevel()) {
  212. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  213. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  214. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  215. if (that.event.refreshBaseActn) {
  216. that.event.refreshBaseActn(that.tree);
  217. }
  218. });
  219. }
  220. }
  221. };
  222. controller.prototype.m_upLevel = function (nodes) { //多选升级
  223. let that = this;
  224. if (this.tree.m_upLevel(nodes)) {
  225. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  226. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0]], that.sheet, true);
  227. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  228. if (that.event.refreshBaseActn) {
  229. that.event.refreshBaseActn(that.tree);
  230. }
  231. });
  232. }
  233. };
  234. controller.prototype.m_downLevel = function (nodes) { //多选降级
  235. let that = this;
  236. if (this.tree.m_downLevel(nodes)) {
  237. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  238. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0].parent], that.sheet, true);
  239. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  240. if (that.event.refreshBaseActn) {
  241. that.event.refreshBaseActn(that.tree);
  242. }
  243. });
  244. }
  245. };
  246. controller.prototype.upMove = function () {
  247. var that = this, sels = this.sheet.getSelections();
  248. if (this.tree.selected) {
  249. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  250. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo(), true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  251. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.preSibling, that.tree.selected.preSibling.serialNo(), true);
  252. if (that.tree.selected.upMove()) {
  253. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  254. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo());
  255. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.nextSibling, that.tree.selected.nextSibling.serialNo());
  256. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  257. if (that.event.refreshBaseActn) {
  258. that.event.refreshBaseActn(that.tree);
  259. }
  260. }
  261. });
  262. }
  263. };
  264. controller.prototype.downMove = function () {
  265. var that = this, sels = this.sheet.getSelections();
  266. if (this.tree.selected) {
  267. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  268. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo(), true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  269. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.nextSibling, that.tree.selected.nextSibling.serialNo(), true);
  270. if (that.tree.selected.downMove()) {
  271. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  272. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo());
  273. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.preSibling, that.tree.selected.preSibling.serialNo());
  274. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  275. if (that.event.refreshBaseActn) {
  276. that.event.refreshBaseActn(that.tree);
  277. }
  278. }
  279. });
  280. }
  281. };
  282. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  283. var that = this;
  284. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  285. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  286. })
  287. }
  288. controller.prototype.setTreeSelected = function (node) {
  289. if (this.event.beforeTreeSelectedChange) {
  290. this.event.beforeTreeSelectedChange(this.tree.selected);
  291. }
  292. this.tree.selected = node;
  293. console.log(node);
  294. if (this.event.refreshBaseActn) {
  295. this.event.refreshBaseActn(this.tree);
  296. }
  297. if (this.event.treeSelectedChanged) {
  298. this.event.treeSelectedChanged(this.tree.selected);
  299. }
  300. }
  301. controller.prototype.bind = function (eventName, eventFun) {
  302. this.event[eventName] = eventFun;
  303. };
  304. return new controller();
  305. },
  306. eventName: {
  307. refreshBaseActn: 'refreshBaseActn',
  308. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  309. treeSelectedChanged: 'treeSelectedChanged',
  310. cellDoubleClick: 'cellDoubleClick'
  311. }
  312. };