jpc_helper_common.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. let JV = require('../jpc_value_define');
  2. let JpcCommonHelper = {
  3. commonConstant: {},
  4. getResultByID: function (KeyID, collectionList) {
  5. let rst = null;
  6. if (KeyID) {
  7. for (let i = 0; i < collectionList.length; i++) {
  8. let collection = collectionList[i];
  9. if (collection && collection instanceof Array) {
  10. for (let j = 0; j < collection.length; j++) {
  11. if (collection[j][JV.PROP_ID] === KeyID) {
  12. rst = collection[j];
  13. break;
  14. }
  15. }
  16. if (rst) break;
  17. }
  18. }
  19. }
  20. return rst;
  21. },
  22. getFont: function(fontName, dftFonts, rptTpl) {
  23. let me = this, rst = null, list = [];
  24. if (rptTpl) list.push(rptTpl[JV.NODE_FONT_COLLECTION]);
  25. list.push(dftFonts);
  26. rst = me.getResultByID(fontName, list);
  27. return rst;
  28. },
  29. getStyle: function(styleName, dftStyles, rptTpl) {
  30. let me = this, rst = null, list = [];
  31. if (rptTpl) list.push(rptTpl[JV.NODE_STYLE_COLLECTION]);
  32. list.push(dftStyles);
  33. rst = me.getResultByID(styleName, list);
  34. return rst;
  35. },
  36. getControl: function(controlName, dftControls, rptTpl) {
  37. let me = this, rst = null, list = [];
  38. if (rptTpl) list.push(rptTpl[JV.NODE_CONTROL_COLLECTION]);
  39. list.push(dftControls);
  40. rst = me.getResultByID(controlName, list);
  41. return rst;
  42. },
  43. getLayoutAlignment: function(alignStr) {
  44. let rst = JV.LAYOUT.indexOf(alignStr);
  45. if (rst < 0) rst = JV.LAYOUT_FULFILL;
  46. return rst;
  47. },
  48. getPosCalculationType: function (typeStr) {
  49. let rst = JV.CAL_TYPE.indexOf(typeStr);
  50. if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT;
  51. return rst;
  52. },
  53. getBoolean: function(bStr) {
  54. let rst = false;
  55. if (bStr !== null && bStr !== undefined) {
  56. let valType = typeof(bStr);
  57. if (valType === "boolean") {
  58. rst = bStr;
  59. } else if (valType === "string") {
  60. let tS = bStr.toUpperCase();
  61. rst = (tS === "T" || tS === "TRUE" || tS === "YES" || tS === "Y");
  62. } else if (valType === "number") {
  63. rst = (bStr === 1);
  64. }
  65. }
  66. return rst;
  67. },
  68. getScreenDPI: function() {
  69. let me = this, arrDPI = [];
  70. if (!me.commonConstant.resolution) {
  71. arrDPI = [96,96];
  72. // arrDPI = [100,100];
  73. me.commonConstant.resolution = arrDPI;
  74. } else {
  75. arrDPI = me.commonConstant.resolution;
  76. }
  77. return arrDPI;
  78. },
  79. getScreenDPI_bk: function() {
  80. let me = this, arrDPI = [];
  81. if (!me.commonConstant.resolution) {
  82. if (window) {
  83. if (window.screen.deviceXDPI != undefined) {
  84. arrDPI.push(window.screen.deviceXDPI);
  85. arrDPI.push(window.screen.deviceYDPI);
  86. } else {
  87. let tmpNode = document.createElement("DIV");
  88. tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
  89. document.body.appendChild(tmpNode);
  90. arrDPI.push(parseInt(tmpNode.offsetWidth));
  91. arrDPI.push(parseInt(tmpNode.offsetHeight));
  92. tmpNode.parentNode.removeChild(tmpNode);
  93. }
  94. } else {
  95. arrDPI = [96,96];
  96. }
  97. me.commonConstant.resolution = arrDPI;
  98. } else {
  99. arrDPI = me.commonConstant.resolution;
  100. }
  101. return arrDPI;
  102. },
  103. getUnitFactor: function(rptTpl) {
  104. let me = this;
  105. return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
  106. },
  107. translateUnit: function(unitStr) {
  108. let me = this, rst = 1.0;
  109. if (unitStr) {
  110. let resolution = me.getScreenDPI();
  111. if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
  112. rst = 1.0;
  113. } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
  114. rst = 1.0 * resolution[0] / 2.54;
  115. } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
  116. rst = 1.0 * resolution[0];
  117. }
  118. }
  119. return rst;
  120. },
  121. getPageSize: function (rptTpl) {
  122. let me = this, size = null;
  123. let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  124. let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
  125. if (sizeIdx >= 0) {
  126. size = JV.PAGES_SIZE[sizeIdx].slice(0);
  127. } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
  128. //
  129. } else {
  130. size = JV.SIZE_A4.slice(0);
  131. }
  132. let page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
  133. if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
  134. //swap x,y
  135. let tmp = size[0];
  136. size[0] = size[1];
  137. size[1] = tmp;
  138. }
  139. return size;
  140. },
  141. getReportArea: function(rptTpl, unitFactor) {
  142. let me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
  143. size[0] = resolution[0] * size[0];
  144. size[1] = resolution[0] * size[1];
  145. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
  146. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
  147. rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
  148. rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
  149. return rst;
  150. },
  151. getSegIdxByPageIdx: function(page, page_seg_map) {
  152. let rst = -1;
  153. for (let pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
  154. if (page_seg_map[pIdx][0] == page) {
  155. rst = page_seg_map[pIdx][1];
  156. break;
  157. }
  158. }
  159. return rst;
  160. }
  161. };
  162. module.exports = JpcCommonHelper;