renderer.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import kbn from 'app/core/utils/kbn';
  4. export class TableRenderer {
  5. formatters: any[];
  6. colorState: any;
  7. constructor(private panel, private table, private isUtc, private sanitize, private templateSrv) {
  8. this.initColumns();
  9. }
  10. setTable(table) {
  11. this.table = table;
  12. this.initColumns();
  13. }
  14. initColumns() {
  15. this.formatters = [];
  16. this.colorState = {};
  17. for (let colIndex = 0; colIndex < this.table.columns.length; colIndex++) {
  18. let column = this.table.columns[colIndex];
  19. column.title = column.text;
  20. for (let i = 0; i < this.panel.styles.length; i++) {
  21. let style = this.panel.styles[i];
  22. var regex = kbn.stringToJsRegex(style.pattern);
  23. if (column.text.match(regex)) {
  24. column.style = style;
  25. if (style.alias) {
  26. column.title = column.text.replace(regex, style.alias);
  27. }
  28. break;
  29. }
  30. }
  31. this.formatters[colIndex] = this.createColumnFormatter(column);
  32. }
  33. }
  34. getColorForValue(value, style) {
  35. if (!style.thresholds) { return null; }
  36. for (var i = style.thresholds.length; i > 0; i--) {
  37. if (value >= style.thresholds[i - 1]) {
  38. return style.colors[i];
  39. }
  40. }
  41. return _.first(style.colors);
  42. }
  43. defaultCellFormatter(v, style) {
  44. if (v === null || v === void 0 || v === undefined) {
  45. return '';
  46. }
  47. if (_.isArray(v)) {
  48. v = v.join(', ');
  49. }
  50. if (style && style.sanitize) {
  51. return this.sanitize(v);
  52. } else {
  53. return _.escape(v);
  54. }
  55. }
  56. createColumnFormatter(column) {
  57. if (!column.style) {
  58. return this.defaultCellFormatter;
  59. }
  60. if (column.style.type === 'hidden') {
  61. return v => {
  62. return undefined;
  63. };
  64. }
  65. if (column.style.type === 'date') {
  66. return v => {
  67. if (v === undefined || v === null) {
  68. return '-';
  69. }
  70. if (_.isArray(v)) { v = v[0]; }
  71. var date = moment(v);
  72. if (this.isUtc) {
  73. date = date.utc();
  74. }
  75. return date.format(column.style.dateFormat);
  76. };
  77. }
  78. if (column.style.type === 'number') {
  79. let valueFormatter = kbn.valueFormats[column.unit || column.style.unit];
  80. return v => {
  81. if (v === null || v === void 0) {
  82. return '-';
  83. }
  84. if (_.isString(v) || _.isArray(v)) {
  85. return this.defaultCellFormatter(v, column.style);
  86. }
  87. if (column.style.colorMode) {
  88. this.colorState[column.style.colorMode] = this.getColorForValue(v, column.style);
  89. }
  90. return valueFormatter(v, column.style.decimals, null);
  91. };
  92. }
  93. return (value) => {
  94. return this.defaultCellFormatter(value, column.style);
  95. };
  96. }
  97. renderRowVariables(rowIndex) {
  98. let scopedVars = {};
  99. let cell_variable;
  100. let row = this.table.rows[rowIndex];
  101. for (let i = 0; i < row.length; i++) {
  102. cell_variable = `__cell_${i}`;
  103. scopedVars[cell_variable] = { value: row[i] };
  104. }
  105. return scopedVars;
  106. }
  107. formatColumnValue(colIndex, value) {
  108. return this.formatters[colIndex] ? this.formatters[colIndex](value) : value;
  109. }
  110. renderCell(columnIndex, rowIndex, value, addWidthHack = false) {
  111. value = this.formatColumnValue(columnIndex, value);
  112. var column = this.table.columns[columnIndex];
  113. var style = '';
  114. var cellClasses = [];
  115. var cellClass = '';
  116. if (this.colorState.cell) {
  117. style = ' style="background-color:' + this.colorState.cell + ';color: white"';
  118. this.colorState.cell = null;
  119. } else if (this.colorState.value) {
  120. style = ' style="color:' + this.colorState.value + '"';
  121. this.colorState.value = null;
  122. }
  123. // because of the fixed table headers css only solution
  124. // there is an issue if header cell is wider the cell
  125. // this hack adds header content to cell (not visible)
  126. var columnHtml = '';
  127. if (addWidthHack) {
  128. columnHtml = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].title + '</div>';
  129. }
  130. if (value === undefined) {
  131. style = ' style="display:none;"';
  132. column.hidden = true;
  133. } else {
  134. column.hidden = false;
  135. }
  136. if (column.style && column.style.preserveFormat) {
  137. cellClasses.push("table-panel-cell-pre");
  138. }
  139. if (column.style && column.style.link) {
  140. // Render cell as link
  141. var scopedVars = this.renderRowVariables(rowIndex);
  142. scopedVars['__cell'] = { value: value };
  143. var cellLink = this.templateSrv.replace(column.style.linkUrl, scopedVars);
  144. var cellLinkTooltip = this.templateSrv.replace(column.style.linkTooltip, scopedVars);
  145. var cellTarget = column.style.linkTargetBlank ? '_blank' : '';
  146. cellClasses.push("table-panel-cell-link");
  147. columnHtml += `
  148. <a href="${cellLink}" target="${cellTarget}" data-link-tooltip data-original-title="${cellLinkTooltip}" data-placement="right">
  149. ${value}
  150. </a>
  151. `;
  152. } else {
  153. columnHtml += value;
  154. }
  155. if (column.filterable) {
  156. cellClasses.push("table-panel-cell-filterable");
  157. columnHtml += `
  158. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter out value" data-placement="bottom"
  159. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="!=">
  160. <i class="fa fa-search-minus"></i>
  161. </a>
  162. <a class="table-panel-filter-link" data-link-tooltip data-original-title="Filter for value" data-placement="bottom"
  163. data-row="${rowIndex}" data-column="${columnIndex}" data-operator="=">
  164. <i class="fa fa-search-plus"></i>
  165. </a>`;
  166. }
  167. if (cellClasses.length) {
  168. cellClass = ' class="' + cellClasses.join(' ') + '"';
  169. }
  170. columnHtml = '<td' + cellClass + style + '>' + columnHtml + '</td>';
  171. return columnHtml;
  172. }
  173. render(page) {
  174. let pageSize = this.panel.pageSize || 100;
  175. let startPos = page * pageSize;
  176. let endPos = Math.min(startPos + pageSize, this.table.rows.length);
  177. var html = "";
  178. for (var y = startPos; y < endPos; y++) {
  179. let row = this.table.rows[y];
  180. let cellHtml = '';
  181. let rowStyle = '';
  182. for (var i = 0; i < this.table.columns.length; i++) {
  183. cellHtml += this.renderCell(i, y, row[i], y === startPos);
  184. }
  185. if (this.colorState.row) {
  186. rowStyle = ' style="background-color:' + this.colorState.row + ';color: white"';
  187. this.colorState.row = null;
  188. }
  189. html += '<tr ' + rowStyle + '>' + cellHtml + '</tr>';
  190. }
  191. return html;
  192. }
  193. render_values() {
  194. let rows = [];
  195. for (var y = 0; y < this.table.rows.length; y++) {
  196. let row = this.table.rows[y];
  197. let new_row = [];
  198. for (var i = 0; i < this.table.columns.length; i++) {
  199. new_row.push(this.formatColumnValue(i, row[i]));
  200. }
  201. rows.push(new_row);
  202. }
  203. return {
  204. columns: this.table.columns,
  205. rows: rows,
  206. };
  207. }
  208. }