renderer.ts 3.1 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. defaultCellFormater(v) {
  22. if (v === null || v === void 0) {
  23. return '';
  24. }
  25. if (_.isArray(v)) {
  26. v = v.join(',&nbsp;');
  27. }
  28. return v;
  29. }
  30. createColumnFormater(style) {
  31. if (!style) {
  32. return this.defaultCellFormater;
  33. }
  34. if (style.type === 'date') {
  35. return v => {
  36. if (_.isArray(v)) { v = v[0]; }
  37. var date = moment(v);
  38. if (this.timezone === 'utc') {
  39. date = date.utc();
  40. }
  41. return date.format(style.dateFormat);
  42. };
  43. }
  44. if (style.type === 'number') {
  45. let valueFormater = kbn.valueFormats[style.unit];
  46. return v => {
  47. if (v === null || v === void 0) {
  48. return '-';
  49. }
  50. if (_.isString(v)) {
  51. return v;
  52. }
  53. if (style.colorMode) {
  54. this.colorState[style.colorMode] = this.getColorForValue(v, style);
  55. }
  56. return valueFormater(v, style.decimals, null);
  57. };
  58. }
  59. return this.defaultCellFormater;
  60. }
  61. formatColumnValue(colIndex, value) {
  62. if (this.formaters[colIndex]) {
  63. return this.formaters[colIndex](value);
  64. }
  65. for (let i = 0; i < this.panel.columns.length; i++) {
  66. let style = this.panel.columns[i];
  67. let column = this.table.columns[colIndex];
  68. var regex = kbn.stringToJsRegex(style.pattern);
  69. if (column.text.match(regex)) {
  70. this.formaters[colIndex] = this.createColumnFormater(style);
  71. return this.formaters[colIndex](value);
  72. }
  73. }
  74. this.formaters[colIndex] = this.defaultCellFormater;
  75. return this.formaters[colIndex](value);
  76. }
  77. renderCell(columnIndex, value) {
  78. var value = this.formatColumnValue(columnIndex, value);
  79. var style = '';
  80. if (this.colorState.cell) {
  81. style = ' style="background-color:' + this.colorState.cell + ';color: white"';
  82. this.colorState.cell = null;
  83. }
  84. else if (this.colorState.value) {
  85. style = ' style="color:' + this.colorState.value + '"';
  86. this.colorState.value = null;
  87. }
  88. return '<td' + style + '>' + value + '</td>';
  89. }
  90. render(page) {
  91. let startPos = page * this.panel.pageSize;
  92. let endPos = Math.min(startPos + this.panel.pageSize, this.table.rows.length);
  93. var html = "";
  94. for (var y = startPos; y < endPos; y++) {
  95. let row = this.table.rows[y];
  96. html += '<tr>';
  97. for (var i = 0; i < this.table.columns.length; i++) {
  98. html += this.renderCell(i, row[i]);
  99. }
  100. html += '</tr>';
  101. }
  102. return html;
  103. }
  104. }