jpc_helper_common.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. me.commonConstant.resolution = arrDPI;
  73. } else {
  74. arrDPI = me.commonConstant.resolution;
  75. }
  76. return arrDPI;
  77. },
  78. getScreenDPI_bk: function() {
  79. let me = this, arrDPI = [];
  80. if (!me.commonConstant.resolution) {
  81. if (window) {
  82. if (window.screen.deviceXDPI != undefined) {
  83. arrDPI.push(window.screen.deviceXDPI);
  84. arrDPI.push(window.screen.deviceYDPI);
  85. } else {
  86. let tmpNode = document.createElement("DIV");
  87. tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
  88. document.body.appendChild(tmpNode);
  89. arrDPI.push(parseInt(tmpNode.offsetWidth));
  90. arrDPI.push(parseInt(tmpNode.offsetHeight));
  91. tmpNode.parentNode.removeChild(tmpNode);
  92. }
  93. } else {
  94. arrDPI = [96,96];
  95. }
  96. me.commonConstant.resolution = arrDPI;
  97. } else {
  98. arrDPI = me.commonConstant.resolution;
  99. }
  100. return arrDPI;
  101. },
  102. getUnitFactor: function(rptTpl) {
  103. let me = this;
  104. return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
  105. },
  106. translateUnit: function(unitStr) {
  107. let me = this, rst = 1.0;
  108. if (unitStr) {
  109. let resolution = me.getScreenDPI();
  110. if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
  111. rst = 1.0;
  112. } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
  113. rst = 1.0 * resolution[0] / 2.54;
  114. } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
  115. rst = 1.0 * resolution[0];
  116. }
  117. }
  118. return rst;
  119. },
  120. getPageSize: function (rptTpl) {
  121. let me = this, size = null;
  122. let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  123. let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
  124. if (sizeIdx >= 0) {
  125. size = JV.PAGES_SIZE[sizeIdx].slice(0);
  126. } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
  127. //
  128. } else {
  129. size = JV.SIZE_A4.slice(0);
  130. }
  131. let page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
  132. if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
  133. //swap x,y
  134. let tmp = size[0];
  135. size[0] = size[1];
  136. size[1] = tmp;
  137. }
  138. return size;
  139. },
  140. getReportArea: function(rptTpl, unitFactor) {
  141. let me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
  142. size[0] = resolution[0] * size[0];
  143. size[1] = resolution[0] * size[1];
  144. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
  145. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
  146. rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
  147. rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
  148. return rst;
  149. },
  150. getSegIdxByPageIdx: function(page, page_seg_map) {
  151. let rst = -1;
  152. for (let pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
  153. if (page_seg_map[pIdx][0] == page) {
  154. rst = page_seg_map[pIdx][1];
  155. break;
  156. }
  157. }
  158. return rst;
  159. }
  160. };
  161. module.exports = JpcCommonHelper;