tree_sheet_helper.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var TREE_SHEET_HELPER = {
  5. getObjPos: function (obj) {
  6. let target = obj;
  7. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  8. target = obj.offsetParent;
  9. while (target) {
  10. pos.x += target.offsetLeft;
  11. pos.y += target.offsetTop;
  12. target = target.offsetParent;
  13. }
  14. return pos;
  15. },
  16. /**
  17. * 初始化setting,需要提示单元格text时,必须先初始化setting
  18. * @param obj
  19. * @param setting
  20. */
  21. initSetting: function (obj, setting) {
  22. setting.pos = this.getObjPos(obj);
  23. },
  24. createNewSpread: function (obj) {
  25. var spread = new GC.Spread.Sheets.Workbook(obj, {sheetCount: 1});
  26. spread.options.tabStripVisible = false;
  27. spread.options.scrollbarMaxAlign = true;
  28. spread.options.cutCopyIndicatorVisible = false;
  29. spread.options.allowCopyPasteExcelStyle = false;
  30. spread.options.allowUserDragDrop = false;
  31. spread.options.
  32. spread.getActiveSheet().setRowCount(3);
  33. return spread;
  34. },
  35. getSheetCellStyle: function (setting) {
  36. var style = new GC.Spread.Sheets.Style();
  37. //style.locked = setting.readOnly ? true : false;
  38. style.name = setting.id;
  39. style.font = setting.data.font;
  40. style.hAlign = setting.data.hAlign;
  41. style.vAlign = setting.data.vAlign;
  42. style.wordWrap = setting.data.wordWrap;
  43. if (setting.data.formatter) {
  44. style.formatter = setting.data.formatter;
  45. }
  46. return style;
  47. },
  48. loadSheetHeader: function (setting, sheet) {
  49. if (setting.frozenCols) {
  50. sheet.frozenColumnCount(setting.frozenCols);
  51. }
  52. sheet.setColumnCount(setting.cols.length);
  53. sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
  54. setting.headRowHeight.forEach(function (rowHeight, index) {
  55. sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
  56. });
  57. setting.cols.forEach(function (col, index) {
  58. var i, iRow = 0, cell;
  59. for (i = 0; i < col.head.spanCols.length; i++) {
  60. if (col.head.spanCols[i] !== 0) {
  61. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  62. cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(col.head.wordWrap);
  63. }
  64. if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
  65. sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  66. }
  67. iRow += col.head.spanRows[i];
  68. };
  69. sheet.setColumnWidth(index, col.width);
  70. });
  71. },
  72. protectdSheet: function (sheet) {
  73. var option = {
  74. allowSelectLockedCells: true,
  75. allowSelectUnlockedCells: true,
  76. allowResizeRows: true,
  77. allowResizeColumns: true
  78. };
  79. sheet.options.protectionOptions = option;
  80. sheet.options.isProtected = true;
  81. },
  82. massOperationSheet: function (sheet, Operation) {
  83. sheet.suspendPaint();
  84. sheet.suspendEvent();
  85. Operation();
  86. sheet.resumeEvent();
  87. sheet.resumePaint();
  88. },
  89. refreshNodesVisible: function (nodes, sheet, recursive) {
  90. nodes.forEach(function (node) {
  91. var iRow;
  92. iRow = node.serialNo();
  93. sheet.setRowVisible(iRow, node.visible, GC.Spread.Sheets.SheetArea.viewport);
  94. if (recursive) {
  95. TREE_SHEET_HELPER.refreshNodesVisible(node.children, sheet, recursive);
  96. }
  97. })
  98. },
  99. refreshTreeNodeData: function (setting, sheet, nodes, recursive) {
  100. nodes.forEach(function (node) {
  101. setting.cols.forEach(function (colSetting, iCol) {
  102. var iRow = node.serialNo();
  103. var cell = sheet.getCell(iRow, iCol, GC.Spread.Sheets.SheetArea.viewport);
  104. // var getFieldText = function () {
  105. // var fields = colSetting.data.field.split('.');
  106. // var validField = fields.reduce(function (field1, field2) {
  107. // if (eval('node.data.' + field1)) {
  108. // return field1 + '.' + field2
  109. // } else {
  110. // return field1;
  111. // }
  112. // });
  113. // if (eval('node.data.' + validField)) {
  114. // return eval('node.data.' + validField);
  115. // } else {
  116. // return '';
  117. // }
  118. // };
  119. var getFieldText2 = function () {
  120. var fields = colSetting.data.field.split('.'), iField, data = node.data;
  121. for (iField = 0; iField < fields.length; iField++) {
  122. if (data[fields[iField]]) {
  123. data = data[fields[iField]];
  124. } else {
  125. return '';
  126. }
  127. }
  128. return data;
  129. };
  130. if (colSetting.data.getText) {
  131. cell.value(colSetting.data.getText(node));
  132. } else {
  133. cell.value(getFieldText2());
  134. }
  135. if (colSetting.data.cellType) {
  136. cell.cellType(colSetting.data.cellType);
  137. }
  138. if (colSetting.readOnly) {
  139. if (Object.prototype.toString.apply(colSetting.readOnly) === "[object Function]") {
  140. cell.locked(colSetting.readOnly(node));
  141. } else {
  142. cell.locked(true);
  143. }
  144. } else {
  145. cell.locked(false);
  146. }
  147. });
  148. sheet.autoFitRow(node.serialNo());
  149. if (recursive) {
  150. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, node.children, recursive);
  151. }
  152. });
  153. },
  154. showTreeData: function (setting, sheet, tree) {
  155. var indent = 20;
  156. var halfBoxLength = 5;
  157. var halfExpandLength = 3;
  158. var TreeNodeCellType = function () {
  159. };
  160. TreeNodeCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  161. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  162. // ������(x1, y1)���(��, ��), (x2, y2)�յ�(��, ��), ��ɫ
  163. var drawLine = function (canvas, x1, y1, x2, y2, color) {
  164. ctx.save();
  165. // ����ƫ����
  166. ctx.translate(0.5, 0.5);
  167. ctx.beginPath();
  168. ctx.moveTo(x1, y1);
  169. ctx.lineTo(x2, y2);
  170. ctx.strokeStyle = color;
  171. ctx.stroke();
  172. ctx.restore();
  173. };
  174. var drawExpandBox = function (ctx, x, y, w, h, centerX, centerY, expanded) {
  175. var rect = {}, h1, h2, offset = 1;
  176. rect.top = centerY - halfBoxLength;
  177. rect.bottom = centerY + halfBoxLength;
  178. rect.left = centerX - halfBoxLength;
  179. rect.right = centerX + halfBoxLength;
  180. if (rect.left < x + w) {
  181. rect.right = Math.min(rect.right, x + w);
  182. ctx.save();
  183. // ����ƫ����
  184. ctx.translate(0.5, 0.5);
  185. ctx.strokeStyle = 'black';
  186. ctx.beginPath();
  187. ctx.moveTo(rect.left, rect.top);
  188. ctx.lineTo(rect.left, rect.bottom);
  189. ctx.lineTo(rect.right, rect.bottom);
  190. ctx.lineTo(rect.right, rect.top);
  191. ctx.lineTo(rect.left, rect.top);
  192. ctx.stroke();
  193. ctx.fillStyle = 'white';
  194. ctx.fill();
  195. ctx.restore();
  196. // Draw Horizontal Line
  197. h1 = centerX - halfExpandLength;
  198. h2 = Math.min(centerX + halfExpandLength, x + w);
  199. if (h2 > h1) {
  200. drawLine(ctx, h1, centerY, h2, centerY, 'black');
  201. }
  202. // Draw Vertical Line
  203. if (!expanded && (centerX < x + w)) {
  204. drawLine(ctx, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, 'black');
  205. }
  206. }
  207. }
  208. var node = tree.items[options.row];
  209. var showTreeLine = true;
  210. if (!node) { return; }
  211. var iLevel = node.depth();
  212. var centerX = Math.floor(x) + node.depth() * indent + indent / 2;
  213. var x1 = centerX + indent / 2;
  214. var centerY = Math.floor((y + (y + h)) / 2);
  215. var y1;
  216. // Draw Sibling Line
  217. if (showTreeLine) {
  218. // Draw Horizontal Line
  219. if (centerX < x + w) {
  220. drawLine(ctx, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  221. }
  222. // Draw Vertical Line
  223. if (centerX < x + w) {
  224. y1 = node.isLast() ? centerY : y + h;
  225. if (node.isFirst() && !node.parent) {
  226. drawLine(ctx, centerX, centerY, centerX, y1, 'gray');
  227. } else {
  228. drawLine(ctx, centerX, y, centerX, y1, 'gray');
  229. }
  230. }
  231. }
  232. // Draw Expand Box
  233. if (node.children.length > 0) {
  234. drawExpandBox(ctx, x, y, w, h, centerX, centerY, node.expanded);
  235. }
  236. // Draw Parent Line
  237. if (showTreeLine) {
  238. var parent = node.parent, parentCenterX = centerX - indent;
  239. while (parent) {
  240. if (!parent.isLast()) {
  241. if (parentCenterX < x + w) {
  242. drawLine(ctx, parentCenterX, y, parentCenterX, y + h, 'gray');
  243. }
  244. }
  245. parent = parent.parent;
  246. parentCenterX -= indent;
  247. }
  248. };
  249. // Draw Text
  250. x = x + (node.depth() + 1) * indent;
  251. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  252. };
  253. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  254. return {
  255. x: x,
  256. y: y,
  257. row: context.row,
  258. col: context.col,
  259. cellStyle: cellStyle,
  260. cellRect: cellRect,
  261. sheetArea: context.sheetArea
  262. };
  263. };
  264. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  265. var offset = -1;
  266. var node = tree.items[hitinfo.row];
  267. tree.selected = node;
  268. if (!node || node.children.length === 0) { return; }
  269. var centerX = hitinfo.cellRect.x + offset + node.depth() * indent + indent / 2;
  270. var centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  271. if (hitinfo.x > centerX - halfBoxLength && hitinfo.x < centerX + halfBoxLength && hitinfo.y > centerY - halfBoxLength && hitinfo.y < centerY + halfBoxLength) {
  272. node.setExpanded(!node.expanded);
  273. TREE_SHEET_HELPER.massOperationSheet(hitinfo.sheet, function () {
  274. var iCount = node.posterityCount(), i, child;
  275. for (i = 0; i < iCount; i++) {
  276. child = tree.items[hitinfo.row + i + 1];
  277. hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.visible, hitinfo.sheetArea);
  278. //hitinfo.sheet.setRowVisible(hitinfo.row + i + 1, child.vis(), hitinfo.sheetArea);
  279. }
  280. hitinfo.sheet.invalidateLayout();
  281. });
  282. hitinfo.sheet.repaint();
  283. }
  284. };
  285. let TipCellType = function () {};
  286. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  287. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  288. return {
  289. x: x,
  290. y: y,
  291. row: context.row,
  292. col: context.col,
  293. cellStyle: cellStyle,
  294. cellRect: cellRect,
  295. sheet: context.sheet,
  296. sheetArea: context.sheetArea
  297. };
  298. };
  299. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  300. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  301. if (setting.pos && text && text !== '') {
  302. if (!this._toolTipElement) {
  303. var div = document.createElement("div");
  304. $(div).css("position", "absolute")
  305. .css("border", "1px #C0C0C0 solid")
  306. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  307. .css("font", "9pt Arial")
  308. .css("background", "white")
  309. .css("padding", 5);
  310. this._toolTipElement = div;
  311. }
  312. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  313. $(this._toolTipElement).hide();
  314. document.body.insertBefore(this._toolTipElement, null);
  315. $(this._toolTipElement).show("fast");
  316. }
  317. };
  318. TipCellType.prototype.processMouseLeave = function (hininfo) {
  319. if (this._toolTipElement) {
  320. document.body.removeChild(this._toolTipElement);
  321. this._toolTipElement = null;
  322. }
  323. }
  324. TREE_SHEET_HELPER.protectdSheet(sheet);
  325. TREE_SHEET_HELPER.massOperationSheet(sheet, function () {
  326. sheet.rowOutlines.direction(GC.Spread.Sheets.Outlines.OutlineDirection.backward);
  327. sheet.showRowOutline(false);
  328. if (setting.defaultRowHeight) {
  329. sheet.defaults.rowHeight = setting.defaultRowHeight;
  330. }
  331. sheet.setRowCount(tree.count() + setting.emptyRows, GC.Spread.Sheets.SheetArea.viewport);
  332. setting.cols.forEach(function (colSetting, iCol) {
  333. sheet.setStyle(-1, iCol, TREE_SHEET_HELPER.getSheetCellStyle(colSetting));
  334. if (colSetting.showHint) {
  335. sheet.getRange(-1, iCol, -1, 1).cellType(new TipCellType());
  336. }
  337. });
  338. sheet.getRange(-1, setting.treeCol, -1, 1).cellType(new TreeNodeCellType());
  339. TREE_SHEET_HELPER.refreshTreeNodeData(setting, sheet, tree.roots, true);
  340. TREE_SHEET_HELPER.refreshNodesVisible(tree.roots, sheet, true);
  341. });
  342. }
  343. };