tree_sheet_controller.js 15 KB

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