renderer.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 (style.colorMode) {
  42. this.colorState[style.colorMode] = this.getColorForValue(v, style);
  43. }
  44. return valueFormater(v, style.decimals, null);
  45. };
  46. }
  47. return v => {
  48. if (v === null || v === void 0) {
  49. return '-';
  50. }
  51. if (_.isArray(v)) {
  52. v = v.join(',&nbsp;');
  53. }
  54. return v;
  55. };
  56. }
  57. formatColumnValue(colIndex, value) {
  58. if (this.formaters[colIndex]) {
  59. return this.formaters[colIndex](value);
  60. }
  61. for (let i = 0; i < this.panel.columns.length; i++) {
  62. let style = this.panel.columns[i];
  63. let column = this.table.columns[colIndex];
  64. var regex = kbn.stringToJsRegex(style.pattern);
  65. if (column.text.match(regex)) {
  66. this.formaters[colIndex] = this.createColumnFormater(style);
  67. return this.formaters[colIndex](value);
  68. }
  69. }
  70. this.formaters[colIndex] = function(v) {
  71. return v;
  72. };
  73. return this.formaters[colIndex](value);
  74. }
  75. renderCell(columnIndex, value) {
  76. var value = this.formatColumnValue(columnIndex, value);
  77. var style = '';
  78. if (this.colorState.cell) {
  79. style = ' style="background-color:' + this.colorState.cell + ';color: white"';
  80. this.colorState.cell = null;
  81. }
  82. else if (this.colorState.value) {
  83. style = ' style="color:' + this.colorState.value + '"';
  84. this.colorState.value = null;
  85. }
  86. return '<td' + style + '>' + value + '</td>';
  87. }
  88. render(page) {
  89. let endPos = Math.min(this.panel.pageSize, this.table.rows.length);
  90. let startPos = 0;
  91. var html = "";
  92. for (var y = startPos; y < endPos; y++) {
  93. let row = this.table.rows[y];
  94. html += '<tr>';
  95. for (var i = 0; i < this.table.columns.length; i++) {
  96. html += this.renderCell(i, row[i]);
  97. }
  98. html += '</tr>';
  99. }
  100. return html;
  101. }
  102. }