tree_sheet_helper.js 12 KB

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