tree_sheet_helper.js 12 KB

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