renderer.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, private sanitize) {
  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, style) {
  22. if (v === null || v === void 0 || v === undefined) {
  23. return '';
  24. }
  25. if (_.isArray(v)) {
  26. v = v.join(', ');
  27. }
  28. if (style && style.sanitize) {
  29. return this.sanitize(v);
  30. } else {
  31. return _.escape(v);
  32. }
  33. }
  34. createColumnFormater(style, column) {
  35. if (!style) {
  36. return this.defaultCellFormater;
  37. }
  38. if (style.type === 'date') {
  39. return v => {
  40. if (_.isArray(v)) { v = v[0]; }
  41. var date = moment(v);
  42. if (this.isUtc) {
  43. date = date.utc();
  44. }
  45. return date.format(style.dateFormat);
  46. };
  47. }
  48. if (style.type === 'number') {
  49. let valueFormater = kbn.valueFormats[column.unit || style.unit];
  50. return v => {
  51. if (v === null || v === void 0) {
  52. return '-';
  53. }
  54. if (_.isString(v)) {
  55. return this.defaultCellFormater(v, style);
  56. }
  57. if (style.colorMode) {
  58. this.colorState[style.colorMode] = this.getColorForValue(v, style);
  59. }
  60. return valueFormater(v, style.decimals, null);
  61. };
  62. }
  63. return (value) => {
  64. return this.defaultCellFormater(value, style);
  65. };
  66. }
  67. formatColumnValue(colIndex, value) {
  68. if (this.formaters[colIndex]) {
  69. return this.formaters[colIndex](value);
  70. }
  71. for (let i = 0; i < this.panel.styles.length; i++) {
  72. let style = this.panel.styles[i];
  73. let column = this.table.columns[colIndex];
  74. var regex = kbn.stringToJsRegex(style.pattern);
  75. if (column.text.match(regex)) {
  76. this.formaters[colIndex] = this.createColumnFormater(style, column);
  77. return this.formaters[colIndex](value);
  78. }
  79. }
  80. this.formaters[colIndex] = this.defaultCellFormater;
  81. return this.formaters[colIndex](value);
  82. }
  83. renderCell(columnIndex, value, addWidthHack = false) {
  84. value = this.formatColumnValue(columnIndex, value);
  85. var style = '';
  86. if (this.colorState.cell) {
  87. style = ' style="background-color:' + this.colorState.cell + ';color: white"';
  88. this.colorState.cell = null;
  89. } else if (this.colorState.value) {
  90. style = ' style="color:' + this.colorState.value + '"';
  91. this.colorState.value = null;
  92. }
  93. // because of the fixed table headers css only solution
  94. // there is an issue if header cell is wider the cell
  95. // this hack adds header content to cell (not visible)
  96. var widthHack = '';
  97. if (addWidthHack) {
  98. widthHack = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].text + '</div>';
  99. }
  100. return '<td' + style + '>' + value + widthHack + '</td>';
  101. }
  102. render(page) {
  103. let pageSize = this.panel.pageSize || 100;
  104. let startPos = page * pageSize;
  105. let endPos = Math.min(startPos + pageSize, this.table.rows.length);
  106. var html = "";
  107. for (var y = startPos; y < endPos; y++) {
  108. let row = this.table.rows[y];
  109. let cellHtml = '';
  110. let rowStyle = '';
  111. for (var i = 0; i < this.table.columns.length; i++) {
  112. cellHtml += this.renderCell(i, row[i], y === startPos);
  113. }
  114. if (this.colorState.row) {
  115. rowStyle = ' style="background-color:' + this.colorState.row + ';color: white"';
  116. this.colorState.row = null;
  117. }
  118. html += '<tr ' + rowStyle + '>' + cellHtml + '</tr>';
  119. }
  120. return html;
  121. }
  122. render_values() {
  123. let rows = [];
  124. for (var y = 0; y < this.table.rows.length; y++) {
  125. let row = this.table.rows[y];
  126. let new_row = [];
  127. for (var i = 0; i < this.table.columns.length; i++) {
  128. new_row.push(this.formatColumnValue(i, row[i]));
  129. }
  130. rows.push(new_row);
  131. }
  132. return {
  133. columns: this.table.columns,
  134. rows: rows,
  135. };
  136. }
  137. }