tree_sheet_helper.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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]).wordWrap(col.head.wordWrap);
  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. // var getFieldText = function () {
  69. // var fields = colSetting.data.field.split('.');
  70. // var validField = fields.reduce(function (field1, field2) {
  71. // if (eval('node.data.' + field1)) {
  72. // return field1 + '.' + field2
  73. // } else {
  74. // return field1;
  75. // }
  76. // });
  77. // if (eval('node.data.' + validField)) {
  78. // return eval('node.data.' + validField);
  79. // } else {
  80. // return '';
  81. // }
  82. // };
  83. var getFieldText2 = function () {
  84. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  85. for (iField = 0; iField < fields.length; iField++) {
  86. if (data[fields[iField]]) {
  87. data = data[fields[iField]];
  88. } else {
  89. return '';
  90. }
  91. }
  92. return data;
  93. };
  94. if (colSetting.data.getText) {
  95. cell.value(colSetting.data.getText(node));
  96. } else {
  97. cell.value(getFieldText2());
  98. }
  99. });
  100. sheet.autoFitRow(node.serialNo());
  101. if (recursive) {
  102. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  103. }
  104. });
  105. },
  106. showTreeData: function (setting, sheet, tree) {
  107. var indent = 20;
  108. var halfBoxLength = 5;
  109. var halfExpandLength = 3;
  110. var TreeNodeCellType = function () {
  111. };
  112. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  113. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  114. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  115. var drawLine = function (canvas, x1, y1, x2, y2, color) {
  116. ctx.save();
  117. // ����ƫ����
  118. ctx.translate(0.5, 0.5);
  119. ctx.beginPath();
  120. ctx.moveTo(x1, y1);
  121. ctx.lineTo(x2, y2);
  122. ctx.strokeStyle = color;
  123. ctx.stroke();
  124. ctx.restore();
  125. };
  126. var drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  127. var rect = {}, h1, h2, offset = 1;
  128. rect.top = centerY - halfBoxLength;
  129. rect.bottom = centerY + halfBoxLength;
  130. rect.left = centerX - halfBoxLength;
  131. rect.right = centerX + halfBoxLength;
  132. if (rect.left < x + w) {
  133. rect.right = Math.min(rect.right, x + w);
  134. ctx.save();
  135. // ����ƫ����
  136. ctx.translate(0.5, 0.5);
  137. ctx.strokeStyle = 'black';
  138. ctx.beginPath();
  139. ctx.moveTo(rect.left, rect.top);
  140. ctx.lineTo(rect.left, rect.bottom);
  141. ctx.lineTo(rect.right, rect.bottom);
  142. ctx.lineTo(rect.right, rect.top);
  143. ctx.lineTo(rect.left, rect.top);
  144. ctx.stroke();
  145. ctx.fillStyle = 'white';
  146. ctx.fill();
  147. ctx.restore();
  148. // Draw Horizontal Line
  149. h1 = centerX - halfExpandLength;
  150. h2 = Math.min(centerX + halfExpandLength, x + w);
  151. if (h2 > h1) {
  152. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  153. }
  154. // Draw Vertical Line
  155. if (!expanded && (centerX < x + w)) {
  156. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  157. }
  158. }
  159. }
  160. var node = tree.items[options.row];
  161. var showTreeLine = true;
  162. if (!node) { return; }
  163. var iLevel = node.depth();
  164. var centerX = Math.floor(x) + node.depth() * indent + indent / 2;
  165. var x1 = centerX + indent / 2;
  166. var centerY = Math.floor((y + (y + h)) / 2);
  167. var y1;
  168. // Draw Sibling Line
  169. if (showTreeLine) {
  170. // Draw Horizontal Line
  171. if (centerX < x + w) {
  172. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  173. }
  174. // Draw Vertical Line
  175. if (centerX < x + w) {
  176. y1 = node.isLast() ? centerY : y + h;
  177. if (node.isFirst() && !node.parent) {
  178. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  179. } else {
  180. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  181. }
  182. }
  183. }
  184. // Draw Expand Box
  185. if (node.children.length > 0) {
  186. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  187. }
  188. // Draw Parent Line
  189. if (showTreeLine) {
  190. var parent = node.parent, parentCenterX = centerX - indent;
  191. while (parent) {
  192. if (!parent.isLast()) {
  193. if (parentCenterX < x + w) {
  194. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  195. }
  196. }
  197. parent = parent.parent;
  198. parentCenterX -= indent;
  199. }
  200. };
  201. // Draw Text
  202. x = x + (node.depth() + 1) * indent;
  203. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  204. };
  205. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  206. return {
  207. x: x,
  208. y: y,
  209. row: context.row,
  210. col: context.col,
  211. cellStyle: cellStyle,
  212. cellRect: cellRect,
  213. sheetArea: context.sheetArea
  214. };
  215. }
  216. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  217. var offset = -1;
  218. var node = tree.items[hitinfo.row];
  219. tree.selected = node;
  220. if (!node || node.children.length === 0) { return; }
  221. var centerX = hitinfo.cellRect.x + offset + node.depth() * indent + indent / 2;
  222. var centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  223. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  224. node.setExpanded(!node.expanded);
  225. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  226. var iCount = node.posterityCount(), i, child;
  227. for (i = 0; i < iCount; i++) {
  228. child = tree.items[hitinfo.row + i + 1];
  229. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  230. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  231. }
  232. hitinfo.sheet.invalidateLayout();
  233. });
  234. hitinfo.sheet.repaint();
  235. }
  236. };
  237. TREE_SHEET_HELPER.protectdSheet(sheet);
  238. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  239. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  240. sheet.showRowOutline(false);
  241. if (setting.defaultRowHeight) {
  242. sheet.defaults.rowHeight = setting.defaultRowHeight;
  243. }
  244. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  245. setting.cols.forEach(function (colSetting, iCol) {
  246. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  247. });
  248. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  249. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  250. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  251. });
  252. }
  253. };