tree_sheet_helper.js 15 KB

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