tree_sheet_controller.js 17 KB

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