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 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.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. var style = '';
  80. if (this.colorState.cell) {
  81. style = ' style="background-color:' + this.colorState.cell + ';color: white"';
  82. this.colorState.cell = null;
  83. } else if (this.colorState.value) {
  84. style = ' style="color:' + this.colorState.value + '"';
  85. this.colorState.value = null;
  86. }
  87. // because of the fixed table headers css only solution
  88. // there is an issue if header cell is wider the cell
  89. // this hack adds header content to cell (not visible)
  90. var widthHack = '';
  91. if (addWidthHack) {
  92. widthHack = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].text + '</div>';
  93. }
  94. return '<td' + style + '>' + value + widthHack + '</td>';
  95. }
  96. render(page) {
  97. let pageSize = this.panel.pageSize || 100;
  98. let startPos = page * pageSize;
  99. let endPos = Math.min(startPos + pageSize, this.table.rows.length);
  100. var html = "";
  101. for (var y = startPos; y < endPos; y++) {
  102. let row = this.table.rows[y];
  103. let cellHtml = '';
  104. let rowStyle = '';
  105. for (var i = 0; i < this.table.columns.length; i++) {
  106. cellHtml += this.renderCell(i, row[i], y === startPos);
  107. }
  108. if (this.colorState.row) {
  109. rowStyle = ' style="background-color:' + this.colorState.row + ';color: white"';
  110. this.colorState.row = null;
  111. }
  112. html += '<tr ' + rowStyle + '>' + cellHtml + '</tr>';
  113. }
  114. return html;
  115. }
  116. }