renderer.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ = require('lodash');
  3. import kbn = require('app/core/utils/kbn');
  4. import moment = require('moment');
  5. export class TableRenderer {
  6. formaters: any[];
  7. colorState: any;
  8. constructor(private panel, private table, private timezone) {
  9. this.formaters = [];
  10. this.colorState = {};
  11. }
  12. getColorForValue(value, style) {
  13. if (!style.thresholds) { return null; }
  14. for (var i = style.thresholds.length - 1; i >= 0 ; i--) {
  15. if (value >= style.thresholds[i]) {
  16. return style.colors[i];
  17. }
  18. }
  19. return null;
  20. }
  21. createColumnFormater(style) {
  22. if (!style) {
  23. return v => v;
  24. }
  25. if (style.type === 'date') {
  26. return v => {
  27. if (_.isArray(v)) { v = v[0]; }
  28. var date = moment(v);
  29. if (this.timezone === 'utc') {
  30. date = date.utc();
  31. }
  32. return date.format(style.dateFormat);
  33. };
  34. }
  35. if (style.type === 'number') {
  36. let valueFormater = kbn.valueFormats[style.unit];
  37. return v => {
  38. if (v === null || v === void 0) {
  39. return '-';
  40. }
  41. if (_.isString(v)) {
  42. return v;
  43. }
  44. if (style.colorMode) {
  45. this.colorState[style.colorMode] = this.getColorForValue(v, style);
  46. }
  47. return valueFormater(v, style.decimals, null);
  48. };
  49. }
  50. return v => {
  51. if (v === null || v === void 0) {
  52. return '-';
  53. }
  54. if (_.isArray(v)) {
  55. v = v.join(',&nbsp;');
  56. }
  57. return v;
  58. };
  59. }
  60. formatColumnValue(colIndex, value) {
  61. if (this.formaters[colIndex]) {
  62. return this.formaters[colIndex](value);
  63. }
  64. for (let i = 0; i < this.panel.columns.length; i++) {
  65. let style = this.panel.columns[i];
  66. let column = this.table.columns[colIndex];
  67. var regex = kbn.stringToJsRegex(style.pattern);
  68. if (column.text.match(regex)) {
  69. this.formaters[colIndex] = this.createColumnFormater(style);
  70. return this.formaters[colIndex](value);
  71. }
  72. }
  73. this.formaters[colIndex] = function(v) {
  74. return v;
  75. };
  76. return this.formaters[colIndex](value);
  77. }
  78. renderCell(columnIndex, value) {
  79. var value = this.formatColumnValue(columnIndex, value);
  80. var style = '';
  81. if (this.colorState.cell) {
  82. style = ' style="background-color:' + this.colorState.cell + ';color: white"';
  83. this.colorState.cell = null;
  84. }
  85. else if (this.colorState.value) {
  86. style = ' style="color:' + this.colorState.value + '"';
  87. this.colorState.value = null;
  88. }
  89. return '<td' + style + '>' + value + '</td>';
  90. }
  91. render(page) {
  92. let endPos = Math.min(this.panel.pageSize, this.table.rows.length);
  93. let startPos = 0;
  94. var html = "";
  95. for (var y = startPos; y < endPos; y++) {
  96. let row = this.table.rows[y];
  97. html += '<tr>';
  98. for (var i = 0; i < this.table.columns.length; i++) {
  99. html += this.renderCell(i, row[i]);
  100. }
  101. html += '</tr>';
  102. }
  103. return html;
  104. }
  105. }