tree_sheet_helper.js 11 KB

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