renderer.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import moment from 'moment';
  4. import kbn from 'app/core/utils/kbn';
  5. export class TableRenderer {
  6. formaters: any[];
  7. colorState: any;
  8. constructor(private panel, private table, private isUtc) {
  9. this.formaters = [];
  10. this.colorState = {};
  11. }
  12. getColorForValue(value, style) {
  13. if (!style.thresholds) { return null; }
  14. for (var i = style.thresholds.length; i > 0; i--) {
  15. if (value >= style.thresholds[i - 1]) {
  16. return style.colors[i];
  17. }
  18. }
  19. return _.first(style.colors);
  20. }
  21. defaultCellFormater(v) {
  22. if (v === null || v === void 0 || v === undefined) {
  23. return '';
  24. }
  25. if (_.isArray(v)) {
  26. v = v.join(', ');
  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.isUtc) {
  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.styles.length; i++) {
  66. let style = this.panel.styles[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, addWidthHack = false) {
  78. value = this.formatColumnValue(columnIndex, value);
  79. value = _.escape(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. } else if (this.colorState.value) {
  85. style = ' style="color:' + this.colorState.value + '"';
  86. this.colorState.value = null;
  87. }
  88. // because of the fixed table headers css only solution
  89. // there is an issue if header cell is wider the cell
  90. // this hack adds header content to cell (not visible)
  91. var widthHack = '';
  92. if (addWidthHack) {
  93. widthHack = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].text + '</div>';
  94. }
  95. return '<td' + style + '>' + value + widthHack + '</td>';
  96. }
  97. render(page) {
  98. let pageSize = this.panel.pageSize || 100;
  99. let startPos = page * pageSize;
  100. let endPos = Math.min(startPos + pageSize, this.table.rows.length);
  101. var html = "";
  102. for (var y = startPos; y < endPos; y++) {
  103. let row = this.table.rows[y];
  104. let cellHtml = '';
  105. let rowStyle = '';
  106. for (var i = 0; i < this.table.columns.length; i++) {
  107. cellHtml += this.renderCell(i, row[i], y === startPos);
  108. }
  109. if (this.colorState.row) {
  110. rowStyle = ' style="background-color:' + this.colorState.row + ';color: white"';
  111. this.colorState.row = null;
  112. }
  113. html += '<tr ' + rowStyle + '>' + cellHtml + '</tr>';
  114. }
  115. return html;
  116. }
  117. }