tree_sheet_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.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. debugger;
  180. if (nodes.length > 0) {
  181. if (this.tree.m_delete(nodes)) {
  182. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  183. let rowCount = 0;
  184. for (let node of nodes) {
  185. rowCount = rowCount + node.posterityCount() + 1;
  186. }
  187. that.sheet.deleteRows(sels[0].row, rowCount);
  188. const locateRow = that.tree.items.length <= sels[0].row ? that.tree.items.length - 1 : sels[0].row;
  189. that.setTreeSelected(that.tree.items[locateRow]);
  190. that.sheet.setSelection(locateRow, sels[0].col, 1, sels[0].colCount);
  191. });
  192. }
  193. }
  194. };
  195. controller.prototype.upLevel = function () {
  196. var that = this;
  197. if (this.tree.selected) {
  198. if (this.tree.selected.upLevel()) {
  199. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  200. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
  201. // that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  202. if (that.event.refreshBaseActn) {
  203. that.event.refreshBaseActn(that.tree);
  204. }
  205. });
  206. }
  207. }
  208. };
  209. controller.prototype.downLevel = function () {
  210. debugger;
  211. var that = this;
  212. if (this.tree.selected) {
  213. if (this.tree.selected.downLevel()) {
  214. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  215. TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
  216. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  217. if (that.event.refreshBaseActn) {
  218. that.event.refreshBaseActn(that.tree);
  219. }
  220. });
  221. }
  222. }
  223. };
  224. controller.prototype.m_upLevel = function (nodes) { //多选升级
  225. let that = this;
  226. if (this.tree.m_upLevel(nodes)) {
  227. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  228. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0]], that.sheet, true);
  229. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  230. if (that.event.refreshBaseActn) {
  231. that.event.refreshBaseActn(that.tree);
  232. }
  233. });
  234. }
  235. };
  236. controller.prototype.m_downLevel = function (nodes) { //多选降级
  237. let that = this;
  238. if (this.tree.m_downLevel(nodes)) {
  239. TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
  240. TREE_SHEET_HELPER.refreshNodesVisible([nodes[0].parent], that.sheet, true);
  241. //that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  242. if (that.event.refreshBaseActn) {
  243. that.event.refreshBaseActn(that.tree);
  244. }
  245. });
  246. }
  247. };
  248. controller.prototype.upMove = function () {
  249. var that = this, sels = this.sheet.getSelections();
  250. if (this.tree.selected) {
  251. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  252. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo(), true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  253. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.preSibling, that.tree.selected.preSibling.serialNo(), true);
  254. if (that.tree.selected.upMove()) {
  255. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
  256. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo());
  257. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.nextSibling, that.tree.selected.nextSibling.serialNo());
  258. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  259. if (that.event.refreshBaseActn) {
  260. that.event.refreshBaseActn(that.tree);
  261. }
  262. }
  263. });
  264. }
  265. };
  266. controller.prototype.downMove = function () {
  267. var that = this, sels = this.sheet.getSelections();
  268. if (this.tree.selected) {
  269. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  270. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo(), true);//为了处理移动前子项是隐藏的情况,先把所有的列设置为显示
  271. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.nextSibling, that.tree.selected.nextSibling.serialNo(), true);
  272. if (that.tree.selected.downMove()) {
  273. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
  274. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected, that.tree.selected.serialNo());
  275. TREE_SHEET_HELPER.refreshChildrenVisiable(that.sheet, that.tree, that.tree.selected.preSibling, that.tree.selected.preSibling.serialNo());
  276. that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
  277. if (that.event.refreshBaseActn) {
  278. that.event.refreshBaseActn(that.tree);
  279. }
  280. }
  281. });
  282. }
  283. };
  284. controller.prototype.refreshTreeNode = function (nodes, recursive) {
  285. var that = this;
  286. TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
  287. TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
  288. })
  289. }
  290. controller.prototype.setTreeSelected = function (node) {
  291. if (this.event.beforeTreeSelectedChange) {
  292. this.event.beforeTreeSelectedChange(this.tree.selected);
  293. }
  294. this.tree.selected = node;
  295. console.log(node);
  296. if (this.event.refreshBaseActn) {
  297. this.event.refreshBaseActn(this.tree);
  298. }
  299. if (this.event.treeSelectedChanged) {
  300. this.event.treeSelectedChanged(this.tree.selected);
  301. }
  302. }
  303. controller.prototype.bind = function (eventName, eventFun) {
  304. this.event[eventName] = eventFun;
  305. };
  306. return new controller();
  307. },
  308. eventName: {
  309. refreshBaseActn: 'refreshBaseActn',
  310. beforeTreeSelectedChange: 'beforeTreeSelectedChange',
  311. treeSelectedChanged: 'treeSelectedChanged',
  312. cellDoubleClick: 'cellDoubleClick'
  313. }
  314. };