Jpc_Helper_Common.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var JV = require('../Jpc_ValueDefine');
  2. var JpcCommonHelper = {
  3. commonConstant: {},
  4. getResultByID: function (KeyID, collectionList) {
  5. var rst = null;
  6. if (KeyID) {
  7. for (var i = 0; i < collectionList.length; i++) {
  8. var collection = collectionList[i];
  9. if (collection && collection instanceof Array) {
  10. for (var 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. var 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. var 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. var 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. var rst = JV.LAYOUT.indexOf(alignStr);
  45. if (rst < 0) rst = JV.LAYOUT_FULFILL;
  46. return rst;
  47. },
  48. getPosCalculationType: function (typeStr) {
  49. var rst = JV.CAL_TYPE.indexOf(typeStr);
  50. if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT;
  51. return rst;
  52. },
  53. getScreenDPI: function() {
  54. var me = this, arrDPI = [];
  55. if (!me.commonConstant.resolution) {
  56. arrDPI = [96,96];
  57. me.commonConstant.resolution = arrDPI;
  58. } else {
  59. arrDPI = me.commonConstant.resolution;
  60. }
  61. return arrDPI;
  62. },
  63. getScreenDPI_bk: function() {
  64. var me = this, arrDPI = [];
  65. if (!me.commonConstant.resolution) {
  66. if (window) {
  67. if (window.screen.deviceXDPI != undefined) {
  68. arrDPI.push(window.screen.deviceXDPI);
  69. arrDPI.push(window.screen.deviceYDPI);
  70. } else {
  71. var tmpNode = document.createElement("DIV");
  72. tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
  73. document.body.appendChild(tmpNode);
  74. arrDPI.push(parseInt(tmpNode.offsetWidth));
  75. arrDPI.push(parseInt(tmpNode.offsetHeight));
  76. tmpNode.parentNode.removeChild(tmpNode);
  77. }
  78. } else {
  79. arrDPI = [96,96];
  80. }
  81. me.commonConstant.resolution = arrDPI;
  82. } else {
  83. arrDPI = me.commonConstant.resolution;
  84. }
  85. return arrDPI;
  86. },
  87. getUnitFactor: function(rptTpl) {
  88. var me = this;
  89. return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
  90. },
  91. translateUnit: function(unitStr) {
  92. var me = this, rst = 1.0;
  93. if (unitStr) {
  94. var resolution = me.getScreenDPI();
  95. if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
  96. rst = 1.0;
  97. } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
  98. rst = 1.0 * resolution[0] / 2.54;
  99. } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
  100. rst = 1.0 * resolution[0];
  101. }
  102. }
  103. return rst;
  104. },
  105. getPageSize: function (rptTpl) {
  106. var me = this, size = null;
  107. var sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
  108. var sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
  109. if (sizeIdx >= 0) {
  110. size = JV.PAGES_SIZE[sizeIdx].slice(0);
  111. } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
  112. //
  113. } else {
  114. size = JV.SIZE_A4.slice(0);
  115. }
  116. var page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
  117. if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
  118. //swap x,y
  119. var tmp = size[0];
  120. size[0] = size[1];
  121. size[1] = tmp;
  122. }
  123. return size;
  124. },
  125. getReportArea: function(rptTpl, unitFactor) {
  126. var me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
  127. size[0] = resolution[0] * size[0];
  128. size[1] = resolution[0] * size[1];
  129. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
  130. rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
  131. rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
  132. rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
  133. return rst;
  134. },
  135. getSegIdxByPageIdx: function(page, page_seg_map) {
  136. var rst = -1;
  137. for (var pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
  138. if (page_seg_map[pIdx][0] == page) {
  139. rst = page_seg_map[pIdx][1];
  140. break;
  141. }
  142. }
  143. return rst;
  144. }
  145. };
  146. module.exports = JpcCommonHelper;