DashboardFormatter.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017, 2019
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['../lib/@waca/core-client/js/core-client/i18n/Formatter', '../widgets/livewidget/nls/StringResources'], function (Formatter, StringResources) {
  13. 'use strict';
  14. var DashboardFormatter = function DashboardFormatter() {
  15. return {
  16. /**
  17. * Invert the given color by inverting the brightness of the color
  18. * @param value
  19. * @param format
  20. */
  21. format: function format(value, _format) {
  22. if (_format) {
  23. _format = _format.validFormatSpec || _format.formatSpec || _format;
  24. }
  25. if (value === undefined || value === null || value === '') {
  26. // If value is null or undefined or empty
  27. // and defaultValue is defined in spec,
  28. // return defaultValue
  29. if (_format && _format.defaultValue) {
  30. return _format.defaultValue;
  31. }
  32. return StringResources.get('nullValueContent');
  33. }
  34. if (value === 'ERROR') {
  35. return StringResources.get('value_is_not_available');
  36. }
  37. if (!_format || value === StringResources.get('value_is_not_available')) {
  38. return value;
  39. }
  40. return Formatter.format(value, _format);
  41. },
  42. /**
  43. * Format null value as '(null)'
  44. * @param value
  45. * @param format
  46. */
  47. formatNull: function formatNull(value, format) {
  48. if (format) {
  49. format = format.validFormatSpec || format.formatSpec || format;
  50. }
  51. // Per OM decision re: Story 280739, we'll start with option 1 '(null)' and add localization later if necessary.
  52. if (value === null) {
  53. // If value is null,
  54. // and defaultValue is defined in spec,
  55. // return defaultValue
  56. if (format && format.defaultValue) {
  57. return format.defaultValue;
  58. }
  59. return '(null)';
  60. } else {
  61. return this.format(value, format);
  62. }
  63. },
  64. /**
  65. * @description Returns a Number from a formatted string
  66. * @param {String} formattedValue - A number that was formatted by calling the format method
  67. * @returns {Number} - Returns a number or NaN
  68. */
  69. getNumberFromFormattedValue: function getNumberFromFormattedValue(formattedValue) {
  70. var groupingSeperator = Formatter.formatNumber(1000).substring(1, 2);
  71. var decimalSeperator = Formatter.formatNumber(0.1).substring(1, 2);
  72. var patternGrp = new RegExp('\\' + groupingSeperator, 'g');
  73. var patternDecimal = new RegExp('\\' + decimalSeperator, 'g');
  74. var valueString = formattedValue + '';
  75. return Number(valueString.replace(patternGrp, '').replace(patternDecimal, '.'));
  76. }
  77. };
  78. };
  79. return new DashboardFormatter();
  80. });
  81. //# sourceMappingURL=DashboardFormatter.js.map