tree_sheet_helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_HELPER = {
  5. createNewSpread: function (obj) {
  6. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
  7. spread.options.tabStripVisible = false;
  8. spread.options.scrollbarMaxAlign = true;
  9. spread.options.cutCopyIndicatorVisible = false;
  10. spread.options.allowCopyPasteExcelStyle = false;
  11. spread.options.allowUserDragDrop = false;
  12. spread.getActiveSheet().setRowCount(3);
  13. return spread;
  14. },
  15. getSheetCellStyle: function (setting) {
  16. var style = new GC.Spread.Sheets.Style();
  17. //style.locked = setting.readOnly ? true : false;
  18. style.name = setting.id;
  19. style.font = setting.data.font;
  20. style.hAlign = setting.data.hAlign;
  21. style.vAlign = setting.data.vAlign;
  22. style.wordWrap = setting.data.wordWrap;
  23. if (setting.data.formatter) {
  24. style.formatter = setting.data.formatter;
  25. }
  26. return style;
  27. },
  28. loadSheetHeader: function (setting, sheet) {
  29. sheet.setColumnCount(setting.cols.length);
  30. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  31. setting.headRowHeight.forEach(function (rowHeight, index) {
  32. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  33. });
  34. setting.cols.forEach(function (col, index) {
  35. var i, iRow = 0, cell;
  36. for (i = 0; i < col.head.spanCols.length; i++) {
  37. if (col.head.spanCols[i] !== 0) {
  38. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  39. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(col.head.wordWrap);
  40. }
  41. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  42. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  43. }
  44. iRow += col.head.spanRows[i];
  45. };
  46. sheet.setColumnWidth(index, col.width);
  47. });
  48. },
  49. protectdSheet: function (sheet) {
  50. var option = {
  51. allowSelectLockedCells: true,
  52. allowSelectUnlockedCells: true,
  53. allowResizeRows: true,
  54. allowResizeColumns: true
  55. };
  56. sheet.options.protectionOptions = option;
  57. sheet.options.isProtected = true;
  58. },
  59. massOperationSheet: function (sheet, Operation) {
  60. sheet.suspendPaint();
  61. sheet.suspendEvent();
  62. Operation();
  63. sheet.resumeEvent();
  64. sheet.resumePaint();
  65. },
  66. refreshNodesVisible: function (nodes, sheet, recursive) {
  67. nodes.forEach(function (node) {
  68. var iRow;
  69. iRow = node.serialNo();
  70. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  71. if (recursive) {
  72. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  73. }
  74. })
  75. },
  76. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  77. nodes.forEach(function (node) {
  78. setting.cols.forEach(function (colSetting, iCol) {
  79. var iRow = node.serialNo();
  80. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  81. // var getFieldText = function () {
  82. // var fields = colSetting.data.field.split('.');
  83. // var validField = fields.reduce(function (field1, field2) {
  84. // if (eval('node.data.' + field1)) {
  85. // return field1 + '.' + field2
  86. // } else {
  87. // return field1;
  88. // }
  89. // });
  90. // if (eval('node.data.' + validField)) {
  91. // return eval('node.data.' + validField);
  92. // } else {
  93. // return '';
  94. // }
  95. // };
  96. var getFieldText2 = function () {
  97. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  98. for (iField = 0; iField < fields.length; iField++) {
  99. if (data[fields[iField]]) {
  100. data = data[fields[iField]];
  101. } else {
  102. return '';
  103. }
  104. }
  105. return data;
  106. };
  107. if (colSetting.data.getText) {
  108. cell.value(colSetting.data.getText(node));
  109. } else {
  110. cell.value(getFieldText2());
  111. }
  112. if (colSetting.readOnly) {
  113. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  114. cell.locked(colSetting.readOnly(node));
  115. } else {
  116. cell.locked(true);
  117. }
  118. } else {
  119. cell.locked(false);
  120. }
  121. });
  122. sheet.autoFitRow(node.serialNo());
  123. if (recursive) {
  124. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  125. }
  126. });
  127. },
  128. showTreeData: function (setting, sheet, tree) {
  129. var indent = 20;
  130. var halfBoxLength = 5;
  131. var halfExpandLength = 3;
  132. var TreeNodeCellType = function () {
  133. };
  134. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  135. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  136. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  137. var drawLine = function (canvas, x1, y1, x2, y2, color) {
  138. ctx.save();
  139. // ����ƫ����
  140. ctx.translate(0.5, 0.5);
  141. ctx.beginPath();
  142. ctx.moveTo(x1, y1);
  143. ctx.lineTo(x2, y2);
  144. ctx.strokeStyle = color;
  145. ctx.stroke();
  146. ctx.restore();
  147. };
  148. var drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  149. var rect = {}, h1, h2, offset = 1;
  150. rect.top = centerY - halfBoxLength;
  151. rect.bottom = centerY + halfBoxLength;
  152. rect.left = centerX - halfBoxLength;
  153. rect.right = centerX + halfBoxLength;
  154. if (rect.left < x + w) {
  155. rect.right = Math.min(rect.right, x + w);
  156. ctx.save();
  157. // ����ƫ����
  158. ctx.translate(0.5, 0.5);
  159. ctx.strokeStyle = 'black';
  160. ctx.beginPath();
  161. ctx.moveTo(rect.left, rect.top);
  162. ctx.lineTo(rect.left, rect.bottom);
  163. ctx.lineTo(rect.right, rect.bottom);
  164. ctx.lineTo(rect.right, rect.top);
  165. ctx.lineTo(rect.left, rect.top);
  166. ctx.stroke();
  167. ctx.fillStyle = 'white';
  168. ctx.fill();
  169. ctx.restore();
  170. // Draw Horizontal Line
  171. h1 = centerX - halfExpandLength;
  172. h2 = Math.min(centerX + halfExpandLength, x + w);
  173. if (h2 > h1) {
  174. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  175. }
  176. // Draw Vertical Line
  177. if (!expanded && (centerX < x + w)) {
  178. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  179. }
  180. }
  181. }
  182. var node = tree.items[options.row];
  183. var showTreeLine = true;
  184. if (!node) { return; }
  185. var iLevel = node.depth();
  186. var centerX = Math.floor(x) + node.depth() * indent + indent / 2;
  187. var x1 = centerX + indent / 2;
  188. var centerY = Math.floor((y + (y + h)) / 2);
  189. var y1;
  190. // Draw Sibling Line
  191. if (showTreeLine) {
  192. // Draw Horizontal Line
  193. if (centerX < x + w) {
  194. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  195. }
  196. // Draw Vertical Line
  197. if (centerX < x + w) {
  198. y1 = node.isLast() ? centerY : y + h;
  199. if (node.isFirst() && !node.parent) {
  200. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  201. } else {
  202. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  203. }
  204. }
  205. }
  206. // Draw Expand Box
  207. if (node.children.length > 0) {
  208. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  209. }
  210. // Draw Parent Line
  211. if (showTreeLine) {
  212. var parent = node.parent, parentCenterX = centerX - indent;
  213. while (parent) {
  214. if (!parent.isLast()) {
  215. if (parentCenterX < x + w) {
  216. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  217. }
  218. }
  219. parent = parent.parent;
  220. parentCenterX -= indent;
  221. }
  222. };
  223. // Draw Text
  224. x = x + (node.depth() + 1) * indent;
  225. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  226. };
  227. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  228. return {
  229. x: x,
  230. y: y,
  231. row: context.row,
  232. col: context.col,
  233. cellStyle: cellStyle,
  234. cellRect: cellRect,
  235. sheetArea: context.sheetArea
  236. };
  237. }
  238. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  239. var offset = -1;
  240. var node = tree.items[hitinfo.row];
  241. tree.selected = node;
  242. if (!node || node.children.length === 0) { return; }
  243. var centerX = hitinfo.cellRect.x + offset + node.depth() * indent + indent / 2;
  244. var centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  245. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  246. node.setExpanded(!node.expanded);
  247. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  248. var iCount = node.posterityCount(), i, child;
  249. for (i = 0; i < iCount; i++) {
  250. child = tree.items[hitinfo.row + i + 1];
  251. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  252. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  253. }
  254. hitinfo.sheet.invalidateLayout();
  255. });
  256. hitinfo.sheet.repaint();
  257. }
  258. };
  259. TREE_SHEET_HELPER.protectdSheet(sheet);
  260. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  261. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  262. sheet.showRowOutline(false);
  263. if (setting.defaultRowHeight) {
  264. sheet.defaults.rowHeight = setting.defaultRowHeight;
  265. }
  266. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  267. setting.cols.forEach(function (colSetting, iCol) {
  268. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  269. });
  270. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  271. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  272. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  273. });
  274. }
  275. };