tree_sheet_helper.js 10 KB

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