2015-11-21 06:46:18 -06:00
|
|
|
///<reference path="../../../headers/common.d.ts" />
|
2015-11-05 11:42:47 -06:00
|
|
|
|
2015-12-22 06:59:11 -06:00
|
|
|
import _ from 'lodash';
|
2015-12-17 12:18:30 -06:00
|
|
|
import moment from 'moment';
|
2015-12-22 06:59:11 -06:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
2015-11-05 08:55:42 -06:00
|
|
|
|
|
|
|
export class TableRenderer {
|
2015-11-05 11:42:47 -06:00
|
|
|
formaters: any[];
|
2015-11-06 06:16:17 -06:00
|
|
|
colorState: any;
|
2015-11-05 11:42:47 -06:00
|
|
|
|
2016-07-11 02:38:06 -05:00
|
|
|
constructor(private panel, private table, private isUtc, private sanitize) {
|
2015-11-05 11:42:47 -06:00
|
|
|
this.formaters = [];
|
2015-11-06 06:16:17 -06:00
|
|
|
this.colorState = {};
|
2015-11-05 08:55:42 -06:00
|
|
|
}
|
|
|
|
|
2015-11-06 06:16:17 -06:00
|
|
|
getColorForValue(value, style) {
|
|
|
|
if (!style.thresholds) { return null; }
|
|
|
|
|
2016-02-05 03:03:08 -06:00
|
|
|
for (var i = style.thresholds.length; i > 0; i--) {
|
|
|
|
if (value >= style.thresholds[i - 1]) {
|
2015-11-06 06:16:17 -06:00
|
|
|
return style.colors[i];
|
2015-11-05 11:42:47 -06:00
|
|
|
}
|
2015-11-06 06:16:17 -06:00
|
|
|
}
|
2016-02-05 03:03:08 -06:00
|
|
|
return _.first(style.colors);
|
2015-11-06 06:16:17 -06:00
|
|
|
}
|
|
|
|
|
2016-07-11 02:38:06 -05:00
|
|
|
defaultCellFormater(v, style) {
|
2016-02-07 13:17:41 -06:00
|
|
|
if (v === null || v === void 0 || v === undefined) {
|
2016-02-07 10:08:48 -06:00
|
|
|
return '';
|
|
|
|
}
|
2015-11-10 09:15:23 -06:00
|
|
|
|
2016-02-07 13:17:41 -06:00
|
|
|
if (_.isArray(v)) {
|
2016-04-22 07:46:32 -05:00
|
|
|
v = v.join(', ');
|
2016-02-07 10:08:48 -06:00
|
|
|
}
|
2015-11-10 09:15:23 -06:00
|
|
|
|
2016-07-11 02:38:06 -05:00
|
|
|
if (style && style.sanitize) {
|
|
|
|
return this.sanitize(v);
|
|
|
|
} else {
|
|
|
|
return _.escape(v);
|
|
|
|
}
|
2016-02-05 10:58:51 -06:00
|
|
|
}
|
2015-11-10 09:15:23 -06:00
|
|
|
|
2016-05-03 04:29:51 -05:00
|
|
|
createColumnFormater(style, column) {
|
2015-11-06 06:16:17 -06:00
|
|
|
if (!style) {
|
2016-02-07 10:08:48 -06:00
|
|
|
return this.defaultCellFormater;
|
2015-11-06 06:16:17 -06:00
|
|
|
}
|
2015-11-05 11:42:47 -06:00
|
|
|
|
2016-07-29 15:21:59 -05:00
|
|
|
if (style.type === 'hidden') {
|
|
|
|
return v => {
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-11-06 06:16:17 -06:00
|
|
|
if (style.type === 'date') {
|
|
|
|
return v => {
|
2015-11-05 11:42:47 -06:00
|
|
|
if (_.isArray(v)) { v = v[0]; }
|
|
|
|
var date = moment(v);
|
2016-04-03 08:05:43 -05:00
|
|
|
if (this.isUtc) {
|
2015-11-05 11:42:47 -06:00
|
|
|
date = date.utc();
|
|
|
|
}
|
|
|
|
return date.format(style.dateFormat);
|
2015-11-06 06:16:17 -06:00
|
|
|
};
|
|
|
|
}
|
2015-11-05 11:42:47 -06:00
|
|
|
|
2015-11-06 06:16:17 -06:00
|
|
|
if (style.type === 'number') {
|
2016-05-03 04:29:51 -05:00
|
|
|
let valueFormater = kbn.valueFormats[column.unit || style.unit];
|
2015-11-06 06:16:17 -06:00
|
|
|
|
|
|
|
return v => {
|
|
|
|
if (v === null || v === void 0) {
|
|
|
|
return '-';
|
|
|
|
}
|
|
|
|
|
2015-11-09 02:46:49 -06:00
|
|
|
if (_.isString(v)) {
|
2016-07-11 02:38:06 -05:00
|
|
|
return this.defaultCellFormater(v, style);
|
2015-11-09 02:46:49 -06:00
|
|
|
}
|
|
|
|
|
2015-11-06 06:16:17 -06:00
|
|
|
if (style.colorMode) {
|
|
|
|
this.colorState[style.colorMode] = this.getColorForValue(v, style);
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueFormater(v, style.decimals, null);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-11 02:38:06 -05:00
|
|
|
return (value) => {
|
|
|
|
return this.defaultCellFormater(value, style);
|
|
|
|
};
|
2015-11-05 11:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
formatColumnValue(colIndex, value) {
|
|
|
|
if (this.formaters[colIndex]) {
|
|
|
|
return this.formaters[colIndex](value);
|
|
|
|
}
|
|
|
|
|
2015-11-13 03:32:01 -06:00
|
|
|
for (let i = 0; i < this.panel.styles.length; i++) {
|
|
|
|
let style = this.panel.styles[i];
|
2015-11-05 11:42:47 -06:00
|
|
|
let column = this.table.columns[colIndex];
|
|
|
|
var regex = kbn.stringToJsRegex(style.pattern);
|
|
|
|
if (column.text.match(regex)) {
|
2016-05-03 04:29:51 -05:00
|
|
|
this.formaters[colIndex] = this.createColumnFormater(style, column);
|
2015-11-05 11:42:47 -06:00
|
|
|
return this.formaters[colIndex](value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 10:08:48 -06:00
|
|
|
this.formaters[colIndex] = this.defaultCellFormater;
|
2015-11-05 11:42:47 -06:00
|
|
|
return this.formaters[colIndex](value);
|
2015-11-05 08:55:42 -06:00
|
|
|
}
|
|
|
|
|
2015-11-12 05:39:16 -06:00
|
|
|
renderCell(columnIndex, value, addWidthHack = false) {
|
2016-01-13 14:22:39 -06:00
|
|
|
value = this.formatColumnValue(columnIndex, value);
|
2015-11-06 06:16:17 -06:00
|
|
|
var style = '';
|
|
|
|
if (this.colorState.cell) {
|
|
|
|
style = ' style="background-color:' + this.colorState.cell + ';color: white"';
|
|
|
|
this.colorState.cell = null;
|
2016-01-13 14:22:39 -06:00
|
|
|
} else if (this.colorState.value) {
|
2015-11-06 06:16:17 -06:00
|
|
|
style = ' style="color:' + this.colorState.value + '"';
|
|
|
|
this.colorState.value = null;
|
|
|
|
}
|
|
|
|
|
2015-11-12 05:39:16 -06:00
|
|
|
// because of the fixed table headers css only solution
|
|
|
|
// there is an issue if header cell is wider the cell
|
|
|
|
// this hack adds header content to cell (not visible)
|
|
|
|
var widthHack = '';
|
|
|
|
if (addWidthHack) {
|
2015-12-04 08:38:49 -06:00
|
|
|
widthHack = '<div class="table-panel-width-hack">' + this.table.columns[columnIndex].text + '</div>';
|
2015-11-12 05:39:16 -06:00
|
|
|
}
|
|
|
|
|
2016-07-29 15:21:59 -05:00
|
|
|
if (value === undefined) {
|
|
|
|
style = ' style="display:none;"';
|
|
|
|
this.table.columns[columnIndex].hidden = true;
|
|
|
|
} else {
|
|
|
|
this.table.columns[columnIndex].hidden = false;
|
|
|
|
}
|
|
|
|
|
2015-11-12 05:39:16 -06:00
|
|
|
return '<td' + style + '>' + value + widthHack + '</td>';
|
2015-11-05 08:55:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
render(page) {
|
2015-11-20 09:26:44 -06:00
|
|
|
let pageSize = this.panel.pageSize || 100;
|
|
|
|
let startPos = page * pageSize;
|
|
|
|
let endPos = Math.min(startPos + pageSize, this.table.rows.length);
|
2015-11-05 08:55:42 -06:00
|
|
|
var html = "";
|
|
|
|
|
|
|
|
for (var y = startPos; y < endPos; y++) {
|
|
|
|
let row = this.table.rows[y];
|
2015-11-12 05:39:16 -06:00
|
|
|
let cellHtml = '';
|
|
|
|
let rowStyle = '';
|
2015-11-05 08:55:42 -06:00
|
|
|
for (var i = 0; i < this.table.columns.length; i++) {
|
2015-11-12 09:30:15 -06:00
|
|
|
cellHtml += this.renderCell(i, row[i], y === startPos);
|
2015-11-05 08:55:42 -06:00
|
|
|
}
|
2015-11-12 05:39:16 -06:00
|
|
|
|
|
|
|
if (this.colorState.row) {
|
|
|
|
rowStyle = ' style="background-color:' + this.colorState.row + ';color: white"';
|
|
|
|
this.colorState.row = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
html += '<tr ' + rowStyle + '>' + cellHtml + '</tr>';
|
2015-11-05 08:55:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
2016-06-27 20:11:42 -05:00
|
|
|
|
|
|
|
render_values() {
|
|
|
|
let rows = [];
|
|
|
|
|
|
|
|
for (var y = 0; y < this.table.rows.length; y++) {
|
|
|
|
let row = this.table.rows[y];
|
|
|
|
let new_row = [];
|
|
|
|
for (var i = 0; i < this.table.columns.length; i++) {
|
|
|
|
new_row.push(this.formatColumnValue(i, row[i]));
|
|
|
|
}
|
|
|
|
rows.push(new_row);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
columns: this.table.columns,
|
|
|
|
rows: rows,
|
|
|
|
};
|
|
|
|
}
|
2016-02-05 10:58:51 -06:00
|
|
|
}
|