tree_sheet_controller.js 15 KB

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