renderer.ts 4.6 KB

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