jpc_ex.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. let JV = require('./jpc_value_define');
  2. let JpcBand = require('./jpc_band');
  3. let JpcFlowTab = require('./jpc_flow_tab');
  4. let JpcBillTab = require('./jpc_bill_tab');
  5. let JpcCrossTab = require('./jpc_cross_tab');
  6. let JpcField = require('./jpc_field');
  7. let JpcParam = require('./jpc_param');
  8. let JpcFunc = require('./jpc_function');
  9. let JpcData = require('./jpc_data');
  10. let JpcCommonHelper = require('./helper/jpc_helper_common');
  11. let $JE = require('./jpc_rte'); //Important: for self-define function execution purpose
  12. let JpcExSrv = function(){};
  13. JpcExSrv.prototype.createNew = function(){
  14. function private_buildDftItems(rptTpl, dftCollection, propArray, nodeName) {
  15. let rst = {};
  16. if (dftCollection) {
  17. for (let i = 0; i < dftCollection.length; i++) {
  18. let item = {};
  19. for (let j = 0; j < propArray.length; j++) {
  20. item[propArray[j]] = dftCollection[i][propArray[j]];
  21. }
  22. rst[dftCollection[i][JV.PROP_ID]] = item;
  23. }
  24. if (rptTpl && rptTpl[nodeName] && rptTpl[nodeName].length > 0) {
  25. for (let i = 0; i < rptTpl[nodeName].length; i++) {
  26. let rptDftItem = rptTpl[nodeName][i];
  27. if (rst[rptDftItem[JV.PROP_ID]] === undefined) {
  28. let item = {};
  29. for (let j = 0; j < propArray.length; j++) {
  30. item[propArray[j]] = rptDftItem[propArray[j]];
  31. }
  32. rst[rptDftItem[JV.PROP_ID]] = item;
  33. }
  34. }
  35. }
  36. }
  37. return rst;
  38. }
  39. function private_buildDftControls(rptTpl, dftControlCollection) {
  40. return private_buildDftItems(rptTpl,dftControlCollection, JV.CONTROL_PROPS, JV.NODE_CONTROL_COLLECTION);
  41. }
  42. function private_buildDftFonts(rptTpl, dftFontCollection) {
  43. return private_buildDftItems(rptTpl,dftFontCollection, JV.FONT_PROPS, JV.NODE_FONT_COLLECTION);
  44. }
  45. function private_buildDftStyles(rptTpl, dftStyleCollection) {
  46. let rst = {};
  47. if (dftStyleCollection) {
  48. for (let i = 0; i < dftStyleCollection.length; i++) {
  49. let item = {};
  50. if (dftStyleCollection[i][JV.PROP_BORDER_STYLE] && dftStyleCollection[i][JV.PROP_BORDER_STYLE].length > 0) {
  51. for (let j = 0; j < dftStyleCollection[i][JV.PROP_BORDER_STYLE].length; j++) {
  52. let borderItem = {};
  53. for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
  54. borderItem[JV.BORDER_STYLE_PROPS[k]] = dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
  55. }
  56. item[dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
  57. }
  58. }
  59. rst[dftStyleCollection[i][JV.PROP_ID]] = item;
  60. }
  61. if (rptTpl && rptTpl[JV.NODE_STYLE_COLLECTION] && rptTpl[JV.NODE_STYLE_COLLECTION].length > 0) {
  62. for (let i = 0; i < rptTpl[JV.NODE_STYLE_COLLECTION].length; i++) {
  63. let rptDftItem = rptTpl[JV.NODE_STYLE_COLLECTION][i];
  64. if (rst[rptDftItem[JV.PROP_ID]] === undefined) {
  65. let item = {};
  66. for (let j = 0; j < rptDftItem[JV.PROP_BORDER_STYLE].length; j++) {
  67. let borderItem = {};
  68. for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
  69. borderItem[JV.BORDER_STYLE_PROPS[k]] = rptDftItem[JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
  70. }
  71. item[rptDftItem[JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
  72. }
  73. rst[rptDftItem[JV.PROP_ID]] = item;
  74. }
  75. }
  76. }
  77. }
  78. return rst;
  79. }
  80. let JpcResult = {};
  81. //JpcResult.report_title
  82. JpcResult.initialize = function(rptTpl) {
  83. let me = this;
  84. if (rptTpl[JV.NODE_FLOW_INFO]) {
  85. me.flowTab = JpcFlowTab.createNew();
  86. me.flowTab.initialize(false);
  87. me.isFollowMode = false;
  88. }
  89. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  90. me.flowTabEx = JpcFlowTab.createNew();
  91. me.flowTabEx.initialize(true);
  92. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.PROP_FLOW_EX_DISPLAY_MODE] === JV.DISPLAY_MODE_FOLLOW) {
  93. me.isFollowMode = true;
  94. }
  95. }
  96. if (rptTpl[JV.NODE_BILL_INFO]) {
  97. me.billTab = JpcBillTab.createNew();
  98. me.billTab.initialize();
  99. }
  100. //let dt1 = new Date();
  101. if (rptTpl[JV.NODE_CROSS_INFO]) {
  102. me.crossTab = JpcCrossTab.createNew();
  103. me.crossTab.initialize();
  104. }
  105. me.totalPages = 0;
  106. me.exTotalPages = 0;
  107. me.runTimePageData = {};
  108. me.fields = JpcField.createNew(rptTpl);
  109. me.params = JpcParam.createNew(rptTpl);
  110. me.formulas = JpcFunc.createNew(rptTpl);
  111. };
  112. JpcResult.analyzeData = function(rptTpl, dataObj, defProperties, option) {
  113. let me = this, dftPagingOption = option||JV.PAGING_OPTION_NORMAL;
  114. //1. data object
  115. let dataHelper = JpcData.createNew();
  116. dataHelper.analyzeData(rptTpl, dataObj);
  117. //2. tab object
  118. //pre-condition: the data should be sorted in SQL/NoSQL level!
  119. //let dt1 = new Date();
  120. if (me.flowTab) {
  121. me.flowTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  122. if (me.flowTabEx) {
  123. me.flowTabEx.sorting(rptTpl, dataObj, dataHelper.exDataSeq.slice(0));
  124. }
  125. }
  126. if (me.crossTab) {
  127. me.crossTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
  128. }
  129. //let dt2 = new Date();
  130. //alert(dt2 - dt1);
  131. //3. formulas
  132. me.executeFormulas(JV.RUN_TYPE_BEFORE_PAGING, rptTpl, dataObj, me);
  133. //4. paging
  134. me.paging(rptTpl, dataObj, defProperties, dftPagingOption);
  135. //alert('analyzeData was completed!');
  136. //for garbage collection:
  137. dataHelper = null;
  138. };
  139. JpcResult.paging = function(rptTpl, dataObj, defProperties, option) {
  140. let me = this, dftPagingOption = option||JV.PAGING_OPTION_NORMAL;
  141. if (me.flowTab) {
  142. if (me.isFollowMode) {
  143. //
  144. } else {
  145. //
  146. }
  147. me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption);
  148. if (me.flowTabEx) {
  149. me.exTotalPages = me.flowTabEx.preSetupPages(rptTpl, dataObj, defProperties, dftPagingOption);
  150. //console.log('ad-hoc flow pages: ' + me.exTotalPages);
  151. }
  152. me.totalPages += me.exTotalPages;
  153. } else if (me.crossTab) {
  154. //me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties, dftPagingOption);
  155. me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties, JV.PAGING_OPTION_NORMAL); //infinity对交叉表来说无意义
  156. } else if (me.billTab) {
  157. me.totalPages = me.billTab.paging();
  158. }
  159. };
  160. JpcResult.executeFormulas = function(runType, $CURRENT_TEMPLATE, $CURRENT_DATA, $CURRENT_RPT) {
  161. let me = this;
  162. for (let i = 0; i < me.formulas.length; i++) {
  163. if (me.formulas[i][JV.PROP_RUN_TYPE] === runType) {
  164. let expression = me.formulas[i][JV.PROP_EXPRESSION];
  165. if (expression) {
  166. let $ME = me.formulas[i];
  167. console.log(expression);
  168. eval(expression);
  169. }
  170. }
  171. }
  172. };
  173. JpcResult.outputAsSimpleJSONPageArray = function(rptTpl, dataObj, startPage, endPage, defProperties) {
  174. let me = this, rst = {};
  175. if ((startPage > 0) && (startPage <= endPage) && (endPage <= me.totalPages)) {
  176. rst[JV.NODE_CONTROL_COLLECTION] = private_buildDftControls(rptTpl, (defProperties === null)?null:defProperties.ctrls);
  177. rst[JV.NODE_STYLE_COLLECTION] = private_buildDftStyles(rptTpl, (defProperties === null)?null:defProperties.styles);
  178. rst[JV.NODE_FONT_COLLECTION] = private_buildDftFonts(rptTpl, (defProperties === null)?null:defProperties.fonts);
  179. rst[JV.NODE_PAGE_INFO] = {};
  180. rst[JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MAIN_INFO_RPT_NAME];
  181. rst[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl);
  182. rst[JV.NODE_PAGE_INFO][JV.NODE_MARGINS] = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS];
  183. rst.items = [];
  184. let bands = JpcBand.createNew(rptTpl, defProperties);
  185. try {
  186. for (let page = startPage; page <= endPage; page++) {
  187. me.runTimePageData.currentPage = page;
  188. me.executeFormulas(JV.RUN_TYPE_BEFORE_OUTPUT, rptTpl, dataObj, me);
  189. rst.items.push(me.outputAsSimpleJSONPage(rptTpl, dataObj, bands, page, rst[JV.NODE_CONTROL_COLLECTION]));
  190. }
  191. if (bands[JV.BAND_PROP_MERGE_BAND]) {
  192. let mergedBand = {}, band = bands[JV.BAND_PROP_MERGE_BAND];
  193. mergedBand[JV.PROP_LEFT] = parseInt(band[JV.PROP_LEFT].toFixed(0));
  194. mergedBand[JV.PROP_RIGHT] = parseInt(band[JV.PROP_RIGHT].toFixed(0));
  195. mergedBand[JV.PROP_TOP] = parseInt(band[JV.PROP_TOP].toFixed(0));
  196. mergedBand[JV.PROP_BOTTOM] = parseInt(band[JV.PROP_BOTTOM].toFixed(0));
  197. mergedBand[JV.BAND_PROP_STYLE] = band[JV.BAND_PROP_STYLE];
  198. rst[JV.BAND_PROP_MERGE_BAND] = mergedBand;
  199. }
  200. } catch(exception) {
  201. console.log(exception);
  202. } finally {
  203. bands = null;
  204. }
  205. }
  206. return rst;
  207. };
  208. JpcResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, bands, page, controls) {
  209. let me = this, rst = null;
  210. if (me.totalPages >= page) {
  211. rst = {};
  212. rst[JV.PROP_PAGE_SEQ] = page;
  213. //rst.cells = [];
  214. let adHocMergePos = null;
  215. if (me.flowTab) {
  216. if (me.totalPages - me.exTotalPages >= page) {
  217. if (me.flowTab.paging_option === JV.PAGING_OPTION_INFINITY) {
  218. adHocMergePos = {};
  219. }
  220. rst.cells = me.flowTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, adHocMergePos, me);
  221. if (adHocMergePos) {
  222. adHocMergePos[JV.NODE_PAGE_SIZE] = JpcCommonHelper.getPageSize(rptTpl);
  223. rst[JV.PAGE_SPECIAL_MERGE_POS] = adHocMergePos;
  224. }
  225. } else {
  226. rst.cells = me.flowTabEx.outputAsSimpleJSONPage(rptTpl, dataObj, page - (me.totalPages - me.exTotalPages), bands, controls, adHocMergePos, me);
  227. }
  228. } else if (me.crossTab) {
  229. rst.cells = me.crossTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
  230. } else if (me.billTab) {
  231. //
  232. }
  233. }
  234. return rst;
  235. };
  236. //JpcEx.rte.currentRptObj = JpcResult;
  237. return JpcResult;
  238. };
  239. export default new JpcExSrv();