jpc_helper_common_output.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. let JV = require('../jpc_value_define');
  2. let JpcFieldHelper = require('./jpc_helper_field');
  3. let JpcCommonOutputHelper = {
  4. createCommonOutputWithoutDecorate: function (node, value, forceCombine) {
  5. let me = this, rst = {};
  6. //1. font/style/control
  7. rst[JV.PROP_FONT] = node[[JV.PROP_FONT]];
  8. rst[JV.PROP_CONTROL] = node[[JV.PROP_CONTROL]];
  9. rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]];
  10. //2. value
  11. rst[JV.PROP_VALUE] = value;
  12. me.formatCell(node[JV.PROP_FORMAT], rst);
  13. // innerFormat(node[JV.PROP_FORMAT], rst);
  14. if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null) {
  15. rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
  16. } else if (node[JV.PROP_PREFIX] && forceCombine) {
  17. rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX];
  18. }
  19. if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null) {
  20. rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
  21. } else if (node[JV.PROP_SUFFIX] && forceCombine) {
  22. rst[JV.PROP_VALUE] = node[JV.PROP_SUFFIX];
  23. }
  24. return rst;
  25. },
  26. createCommonOutput: function (node, value, controls) {
  27. let me = this, rst = {};
  28. //1. font/style/control
  29. rst[JV.PROP_FONT] = node[[JV.PROP_FONT]];
  30. rst[JV.PROP_CONTROL] = node[[JV.PROP_CONTROL]];
  31. rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]];
  32. //2. value
  33. rst[JV.PROP_VALUE] = value;
  34. JpcFieldHelper.decorateValue(rst, controls);
  35. me.formatCell(node[JV.PROP_FORMAT], rst);
  36. // innerFormat(node[JV.PROP_FORMAT], rst);
  37. if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== "") {
  38. rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
  39. }
  40. if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== "") {
  41. rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
  42. }
  43. return rst;
  44. },
  45. formatCell: function (formatStr, rstCell) {
  46. if (formatStr) {
  47. if (!(isNaN(parseFloat(rstCell[JV.PROP_VALUE])))) {
  48. let dotIdx = formatStr.indexOf(".");
  49. if (dotIdx >= 0) {
  50. let tmpStr = parseFloat(rstCell[JV.PROP_VALUE]).toFixed(formatStr.length - dotIdx - 1);
  51. let digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
  52. for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
  53. if (digStr[sIdx] === '#') {
  54. if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
  55. tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  56. } else {
  57. break;
  58. }
  59. } else {
  60. break;
  61. }
  62. }
  63. if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  64. rstCell[JV.PROP_VALUE] = tmpStr;
  65. } else {
  66. rstCell[JV.PROP_VALUE] = parseFloat(rstCell[JV.PROP_VALUE]).toFixed(0);
  67. }
  68. let commaIdx = formatStr.indexOf(",");
  69. if (commaIdx >= 0) {
  70. rstCell[JV.PROP_VALUE] = comdify(rstCell[JV.PROP_VALUE].toString());
  71. }
  72. }
  73. }
  74. }
  75. };
  76. function comdify(numStr){
  77. let re = /\d{1,3}(?=(\d{3})+$)/g;
  78. return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
  79. }
  80. module.exports = JpcCommonOutputHelper;