tree_sheet_helper.js 12 KB

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