rpt_tpl_virtual_common.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Created by Tony on 2018/9/4.
  3. */
  4. let virtualCommonOprObj = {
  5. getActPos: function (textNode, caclStr, typeStr, baseMea) {
  6. let rst = 0;
  7. if (caclStr === JV.CAL_TYPE[0]) {
  8. //percentage
  9. rst = Math.round(baseMea * textNode[JV.PROP_AREA][typeStr] / 100);
  10. } else {
  11. //abstract
  12. rst = Math.round(textNode[JV.PROP_AREA][typeStr] / 2.54 * 96);
  13. }
  14. return rst;
  15. },
  16. getActPosEx: function (textNode, caclObj, typeStr, baseMea) {
  17. let me = this;
  18. if (typeof caclObj === 'string') {
  19. return me.getActPos(textNode, caclObj, typeStr, baseMea);
  20. } else {
  21. return me.getActPos(textNode, caclObj[typeStr], typeStr, baseMea);
  22. }
  23. },
  24. pushPos: function (textNode, caclObj, typeStr, baseMea, posArr) {
  25. let me = this, pos = me.getActPosEx(textNode, caclObj, typeStr, baseMea);
  26. if (posArr.indexOf(pos) < 0) posArr.push(pos);
  27. },
  28. getBandEx: function (bandName, rptTpl) {
  29. let me = this, rst;
  30. for (let band of rptTpl[JV.NODE_BAND_COLLECTION]) {
  31. rst = me.getBand(bandName, band);
  32. if (rst !== null) {
  33. break;
  34. }
  35. }
  36. return rst;
  37. },
  38. getBand: function (bandName, parentBand) {
  39. let me = this, rst = null;
  40. if (parentBand[JV.PROP_NAME] === bandName) {
  41. rst = parentBand;
  42. } else {
  43. if (parentBand[JV.BAND_PROP_SUB_BANDS] && parentBand[JV.BAND_PROP_SUB_BANDS].length > 0) {
  44. for (let subBand of parentBand[JV.BAND_PROP_SUB_BANDS]) {
  45. rst = me.getBand(bandName, subBand);
  46. if (rst !== null) {
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. return rst;
  53. },
  54. checkInSpan: function (spans, row, col) {
  55. let rst = false;
  56. for (let span of spans) {
  57. if (span.row <= row && row < (span.row + span.rowCount) && span.col <= col && col < (span.col + span.colCount)) {
  58. rst = true;
  59. break;
  60. }
  61. }
  62. return rst;
  63. },
  64. setupHeightWidth: function (dest, text, colWidthArr, rowHeightArr) {
  65. let width = colWidthArr[colWidthArr.length - 1], height = rowHeightArr[rowHeightArr.length - 1];
  66. dest[JV.PROP_AREA][JV.PROP_RIGHT] = (colWidthArr[text.col + text.colCount - 1] / width * 100).toFixed(2);
  67. if (text.col > 0) {
  68. dest[JV.PROP_AREA][JV.PROP_LEFT] = (colWidthArr[text.col - 1] / width * 100).toFixed(2);
  69. } else {
  70. dest[JV.PROP_AREA][JV.PROP_LEFT] = 0;
  71. }
  72. dest[JV.PROP_AREA][JV.PROP_BOTTOM] = (rowHeightArr[text.row + text.rowCount - 1] / height * 100).toFixed(2);
  73. if (text.row > 0) {
  74. dest[JV.PROP_AREA][JV.PROP_TOP] = (rowHeightArr[text.row - 1] / height * 100).toFixed(2);
  75. } else {
  76. dest[JV.PROP_AREA][JV.PROP_TOP] = 0;
  77. }
  78. },
  79. createTxtNode: function (text, parentNode, colWidthArr, rowHeightArr) {
  80. let me = this, rst = dataInfoMapTreeOprObj.getDummyTextNode(parentNode);
  81. me.setupHeightWidth(rst, text, colWidthArr, rowHeightArr);
  82. if (stringUtil.isEmptyString(text[`text`])) {
  83. rst[JV.PROP_NAME] = '';
  84. } else {
  85. rst[JV.PROP_NAME] = stringUtil.replaceAll(stringUtil.replaceAll(text[`text`].toString(), '\n', '|'), '\r', '');
  86. }
  87. rst[JV.PROP_LABEL] = rst[JV.PROP_NAME];
  88. return rst;
  89. },
  90. collectSheetTxt: function (sheet, texts, reserveRows, colWidthArr, rowHeightArr) {
  91. let spans= sheet.getSpans();
  92. let private_build_pre_text = function (row, col, rowCount, colCount) {
  93. let cell = sheet.getCell(row, col);
  94. let textValue = sheet.getValue(row, col);
  95. if (textValue && cell.cellType().typeName === '7') {
  96. textValue = `{` + textValue + `}`;
  97. }
  98. texts.push({"row": row, "col": col, "rowCount": rowCount, "colCount": colCount, "text": textValue});
  99. };
  100. for (let span of spans) {
  101. private_build_pre_text(span.row, span.col, span.rowCount, span.colCount);
  102. }
  103. for (let iRow = 0; iRow < sheet.getRowCount() - reserveRows; iRow++) {
  104. rowHeightArr.push(sheet.getRowHeight(iRow));
  105. if (iRow > 0) {
  106. rowHeightArr[iRow] = rowHeightArr[iRow] + rowHeightArr[iRow - 1];
  107. }
  108. for (let jCol = 0; jCol < sheet.getColumnCount(); jCol++) {
  109. if (iRow === 0) {
  110. colWidthArr.push(sheet.getColumnWidth(jCol));
  111. if (jCol > 0) {
  112. colWidthArr[jCol] = colWidthArr[jCol] + colWidthArr[jCol - 1];
  113. }
  114. }
  115. if (!virtualCommonOprObj.checkInSpan(spans, iRow, jCol)) {
  116. private_build_pre_text(iRow, jCol, 1, 1);
  117. }
  118. }
  119. }
  120. texts.sort(function(t1, t2){
  121. if (t1.col === t2.col) {
  122. return t1.row - t2.row;
  123. } else {
  124. return t1.col - t2.col;
  125. }
  126. });
  127. },
  128. addSpan: function (itemNode, sheet, bandW, bandH, xPos, yPos, sheetAreaType) {
  129. let me = this;
  130. let idx1 = xPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_LEFT, bandW, xPos));
  131. let idx2 = xPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_RIGHT, bandW, xPos));
  132. let idy1 = yPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_TOP, bandH, yPos));
  133. let idy2 = yPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_BOTTOM, bandH, yPos));
  134. if (idx2 - idx1 > 1 || idy2 - idy1 > 1) {
  135. sheet.addSpan(idy1, idx1, idy2 - idy1, idx2 - idx1, sheetAreaType);
  136. }
  137. let cell = sheet.getCell(idy1, idx1, sheetAreaType);
  138. if (sheetAreaType === GC.Spread.Sheets.SheetArea.viewport) {
  139. if (itemNode.FieldID) {
  140. me.changeCellType(cell, 'field');
  141. } else {
  142. me.changeCellType(cell, 'text');
  143. }
  144. }
  145. me.setupCell(cell, itemNode);
  146. },
  147. changeBandHeight: function (bandName, newHeight) {
  148. let nodes = bandTreeOprObj.treeObj.getNodes()[0][JV.BAND_PROP_SUB_BANDS];
  149. let private_set_band_height = function(bNode) {
  150. let rst = false;
  151. if (bNode[JV.PROP_NAME] === bandName) {
  152. bNode[JV.BAND_PROP_HEIGHT] = newHeight;
  153. rst = true;
  154. } else if (bNode[JV.BAND_PROP_SUB_BANDS] && bNode[JV.BAND_PROP_SUB_BANDS].length > 0){
  155. for (let subNode of bNode[JV.BAND_PROP_SUB_BANDS]) {
  156. rst = private_set_band_height(subNode);
  157. if (rst) break;
  158. }
  159. }
  160. return rst;
  161. };
  162. for (let bandNode of nodes) {
  163. if (private_set_band_height(bandNode)) break;
  164. }
  165. },
  166. changeCellType: function (cell, newType) {
  167. switch (newType) {
  168. case `field`:
  169. let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
  170. let cellType = new GC.Spread.Sheets.CellTypes.ComboBox();
  171. let selectableFields = virtualCommonOprObj.getSelectedFields(rptTpl);
  172. cellType.items(selectableFields);
  173. cell.cellType(cellType);
  174. cell.backColor("LightCyan");
  175. break;
  176. case `text`:
  177. default:
  178. cell.cellType(new GC.Spread.Sheets.CellTypes.Text());
  179. cell.value(``);
  180. cell.backColor(undefined);
  181. break;
  182. }
  183. },
  184. setupCell: function (cell, itemNode) {
  185. let me = this;
  186. me.private_setCellFont(cell, itemNode);
  187. me.private_setCellControl(cell, itemNode);
  188. me.private_setCellStyle(cell, itemNode);
  189. let value = itemNode[JV.PROP_NAME];
  190. if (itemNode[JV.PROP_NAME].indexOf(`|`) >= 0) {
  191. value = itemNode[JV.PROP_NAME].split('|').join('\n');
  192. }
  193. cell.wordWrap(true);
  194. cell.value(value);
  195. },
  196. private_setCellControl: function (cell, textNode) {
  197. let ctrl = null;
  198. if (typeof textNode[JV.PROP_CONTROL] === 'string') {
  199. let idx = rpt_tpl_cfg_helper.reportCfg.controlArr.indexOf(textNode[JV.PROP_CONTROL]);
  200. ctrl = rpt_tpl_cfg_helper.reportCfg.ctrls[idx];
  201. } else {
  202. ctrl = textNode[JV.PROP_CONTROL];
  203. }
  204. if (ctrl) {
  205. switch (ctrl.Horizon) {
  206. case `center`:
  207. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  208. break;
  209. case `right`:
  210. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  211. break;
  212. default:
  213. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  214. break;
  215. }
  216. switch (ctrl.Vertical) {
  217. case `center`:
  218. cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  219. break;
  220. case `bottom`:
  221. cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  222. break;
  223. default:
  224. cell.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  225. break;
  226. }
  227. }
  228. },
  229. private_setCellStyle: function (cell, textNode) {
  230. //默认是normal,暂时不管
  231. },
  232. private_setCellFont: function (cell, textNode) {
  233. for (let font of rpt_tpl_cfg_helper.reportCfg.fonts) {
  234. if (font.ID === textNode[JV.PROP_FONT]) {
  235. cell.font(Math.round(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]] * 3 / 4) + 'pt ' + font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]]);
  236. break;
  237. }
  238. }
  239. },
  240. getSelectedFields: function (rptTpl) {
  241. let rst = [];
  242. if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].length > 0) {
  243. for (let field of rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]) {
  244. rst.push(field[JV.PROP_NAME]);
  245. }
  246. }
  247. if (rptTpl[JV.NODE_NO_MAPPING_FIELDS] !== undefined && rptTpl[JV.NODE_NO_MAPPING_FIELDS].length > 0) {
  248. for (let field of rptTpl[JV.NODE_NO_MAPPING_FIELDS]) {
  249. rst.push(field[JV.PROP_NAME]);
  250. }
  251. }
  252. return rst;
  253. },
  254. setupCellDft: function (cell) {
  255. cell.font(`9pt 宋体`);
  256. cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  257. cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  258. cell.wordWrap(true);
  259. cell.cellType(new GC.Spread.Sheets.CellTypes.Text());
  260. cell.value(``);
  261. }
  262. };