renderer.ts 6.9 KB

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