tree_sheet_helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_HELPER = {
  5. createNewSpread: function (obj) {
  6. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
  7. spread.options.tabStripVisible = false;
  8. spread.options.scrollbarMaxAlign = true;
  9. spread.options.cutCopyIndicatorVisible = false;
  10. spread.options.allowCopyPasteExcelStyle = false;
  11. spread.options.allowUserDragDrop = false;
  12. spread.getActiveSheet().setRowCount(3);
  13. return spread;
  14. },
  15. getSheetCellStyle: function (setting) {
  16. var style = new GC.Spread.Sheets.Style();
  17. style.locked = setting.readOnly ? true : false;
  18. style.name = setting.id;
  19. style.font = setting.data.font;
  20. style.hAlign = setting.data.hAlign;
  21. style.vAlign = setting.data.vAlign;
  22. style.wordWrap = setting.data.wordWrap;
  23. return style;
  24. },
  25. loadSheetHeader: function (setting, sheet) {
  26. sheet.setColumnCount(setting.cols.length);
  27. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  28. setting.headRowHeight.forEach(function (rowHeight, index) {
  29. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  30. });
  31. setting.cols.forEach(function (col, index) {
  32. var i, iRow = 0, cell;
  33. for (i = 0; i < col.head.spanCols.length; i++) {
  34. if (col.head.spanCols[i] !== 0) {
  35. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  36. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(col.head.wordWrap);
  37. }
  38. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  39. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  40. }
  41. iRow += col.head.spanRows[i];
  42. };
  43. sheet.setColumnWidth(index, col.width);
  44. });
  45. },
  46. protectdSheet: function (sheet) {
  47. var option = {
  48. allowSelectLockedCells: true,
  49. allowSelectUnlockedCells: true,
  50. allowResizeRows: true,
  51. allowResizeColumns: true
  52. };
  53. sheet.options.protectionOptions = option;
  54. sheet.options.isProtected = true;
  55. },
  56. massOperationSheet: function (sheet, Operation) {
  57. sheet.suspendPaint();
  58. sheet.suspendEvent();
  59. Operation();
  60. sheet.resumeEvent();
  61. sheet.resumePaint();
  62. },
  63. refreshNodesVisible: function (nodes, sheet, recursive) {
  64. nodes.forEach(function (node) {
  65. var iRow;
  66. iRow = node.serialNo();
  67. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  68. if (recursive) {
  69. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  70. }
  71. })
  72. },
  73. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  74. nodes.forEach(function (node) {
  75. setting.cols.forEach(function (colSetting, iCol) {
  76. var iRow = node.serialNo();
  77. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  78. var getFieldText = function () {
  79. var fields = colSetting.data.field.split('.');
  80. var validField = fields.reduce(function (field1, field2) {
  81. if (eval('node.data.' + field1)) {
  82. return field1 + '.' + field2
  83. } else {
  84. return field1;
  85. }
  86. });
  87. if (eval('node.data.' + validField)) {
  88. return eval('node.data.' + validField);
  89. } else {
  90. return '';
  91. }
  92. };
  93. var getFieldText2 = function () {
  94. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  95. for (iField = 0; iField < fields.length; iField++) {
  96. if (data[fields[iField]]) {
  97. data = data[fields[iField]];
  98. } else {
  99. return '';
  100. }
  101. }
  102. return data;
  103. };
  104. if (colSetting.data.getText) {
  105. cell.value(colSetting.data.getText(node));
  106. } else {
  107. cell.value(getFieldText2());
  108. }
  109. if (colSetting.data.cellType) {
  110. cell.cellType(colSetting.data.cellType);
  111. }
  112. });
  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. };