renderer.ts 4.7 KB

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