tree_sheet_helper.js 12 KB

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