tree_sheet_helper.js 17 KB

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