renderer.ts 7.0 KB

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