jpc_helper_common_output.js 3.4 KB

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