2020-01-22 20:05:30 +02:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2019 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { EntityId } from '@shared/models/id/entity-id';
|
2020-01-24 19:05:41 +02:00
|
|
|
import { DataKey, WidgetConfig } from '@shared/models/widget.models';
|
|
|
|
|
import { getDescendantProp, isDefined } from '@core/utils';
|
|
|
|
|
import { alarmFields, AlarmInfo } from '@shared/models/alarm.models';
|
|
|
|
|
import * as tinycolor_ from 'tinycolor2';
|
|
|
|
|
|
|
|
|
|
const tinycolor = tinycolor_;
|
|
|
|
|
|
|
|
|
|
export interface TableWidgetSettings {
|
|
|
|
|
enableSearch: boolean;
|
|
|
|
|
enableSelectColumnDisplay: boolean;
|
|
|
|
|
displayPagination: boolean;
|
|
|
|
|
defaultPageSize: number;
|
|
|
|
|
defaultSortOrder: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TableWidgetDataKeySettings {
|
2020-01-30 13:03:53 +02:00
|
|
|
columnWidth?: string;
|
2020-01-24 19:05:41 +02:00
|
|
|
useCellStyleFunction: boolean;
|
|
|
|
|
cellStyleFunction: string;
|
|
|
|
|
useCellContentFunction: boolean;
|
|
|
|
|
cellContentFunction: string;
|
|
|
|
|
}
|
2020-01-22 20:05:30 +02:00
|
|
|
|
|
|
|
|
export interface EntityData {
|
|
|
|
|
id: EntityId;
|
|
|
|
|
entityName: string;
|
|
|
|
|
entityType?: string;
|
|
|
|
|
[key: string]: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface EntityColumn extends DataKey {
|
2020-01-24 19:05:41 +02:00
|
|
|
def: string;
|
2020-01-22 20:05:30 +02:00
|
|
|
title: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface DisplayColumn {
|
|
|
|
|
title: string;
|
2020-01-24 19:05:41 +02:00
|
|
|
def: string;
|
2020-01-22 20:05:30 +02:00
|
|
|
display: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:14:17 +02:00
|
|
|
export type CellContentFunction = (...args: any[]) => string;
|
|
|
|
|
|
2020-01-22 20:05:30 +02:00
|
|
|
export interface CellContentInfo {
|
|
|
|
|
useCellContentFunction: boolean;
|
2020-02-04 15:14:17 +02:00
|
|
|
cellContentFunction?: CellContentFunction;
|
2020-01-22 20:05:30 +02:00
|
|
|
units?: string;
|
|
|
|
|
decimals?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-04 15:14:17 +02:00
|
|
|
export type CellStyleFunction = (value: any) => any;
|
|
|
|
|
|
2020-01-22 20:05:30 +02:00
|
|
|
export interface CellStyleInfo {
|
|
|
|
|
useCellStyleFunction: boolean;
|
2020-02-04 15:14:17 +02:00
|
|
|
cellStyleFunction?: CellStyleFunction;
|
2020-01-22 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 19:05:41 +02:00
|
|
|
export function findColumnProperty(searchProperty: string, searchValue: string, columnProperty: string, columns: EntityColumn[]): string {
|
|
|
|
|
let res = searchValue;
|
2020-02-04 15:14:17 +02:00
|
|
|
const column = columns.find(theColumn => theColumn[searchProperty] === searchValue);
|
2020-01-24 19:05:41 +02:00
|
|
|
if (column) {
|
|
|
|
|
res = column[columnProperty];
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toEntityColumnDef(label: string, columns: EntityColumn[]): string {
|
|
|
|
|
return findColumnProperty('label', label, 'def', columns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fromEntityColumnDef(def: string, columns: EntityColumn[]): string {
|
|
|
|
|
return findColumnProperty('def', def, 'label', columns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function toAlarmColumnDef(name: string, columns: EntityColumn[]): string {
|
|
|
|
|
return findColumnProperty('name', name, 'def', columns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fromAlarmColumnDef(def: string, columns: EntityColumn[]): string {
|
|
|
|
|
return findColumnProperty('def', def, 'name', columns);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 20:05:30 +02:00
|
|
|
export function getEntityValue(entity: any, key: DataKey): any {
|
|
|
|
|
return getDescendantProp(entity, key.label);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 19:05:41 +02:00
|
|
|
export function getAlarmValue(alarm: AlarmInfo, key: EntityColumn) {
|
|
|
|
|
const alarmField = alarmFields[key.name];
|
|
|
|
|
if (alarmField) {
|
|
|
|
|
return getDescendantProp(alarm, alarmField.value);
|
|
|
|
|
} else {
|
|
|
|
|
return getDescendantProp(alarm, key.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getCellStyleInfo(keySettings: TableWidgetDataKeySettings): CellStyleInfo {
|
2020-02-04 15:14:17 +02:00
|
|
|
let cellStyleFunction: CellStyleFunction = null;
|
2020-01-24 19:05:41 +02:00
|
|
|
let useCellStyleFunction = false;
|
|
|
|
|
|
|
|
|
|
if (keySettings.useCellStyleFunction === true) {
|
|
|
|
|
if (isDefined(keySettings.cellStyleFunction) && keySettings.cellStyleFunction.length > 0) {
|
|
|
|
|
try {
|
2020-02-04 15:14:17 +02:00
|
|
|
cellStyleFunction = new Function('value', keySettings.cellStyleFunction) as CellStyleFunction;
|
2020-01-24 19:05:41 +02:00
|
|
|
useCellStyleFunction = true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
cellStyleFunction = null;
|
|
|
|
|
useCellStyleFunction = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
useCellStyleFunction,
|
|
|
|
|
cellStyleFunction
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getCellContentInfo(keySettings: TableWidgetDataKeySettings, ...args: string[]): CellContentInfo {
|
2020-02-04 15:14:17 +02:00
|
|
|
let cellContentFunction: CellContentFunction = null;
|
2020-01-24 19:05:41 +02:00
|
|
|
let useCellContentFunction = false;
|
|
|
|
|
|
|
|
|
|
if (keySettings.useCellContentFunction === true) {
|
|
|
|
|
if (isDefined(keySettings.cellContentFunction) && keySettings.cellContentFunction.length > 0) {
|
|
|
|
|
try {
|
2020-02-04 15:14:17 +02:00
|
|
|
cellContentFunction = new Function(...args, keySettings.cellContentFunction) as CellContentFunction;
|
2020-01-24 19:05:41 +02:00
|
|
|
useCellContentFunction = true;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
cellContentFunction = null;
|
|
|
|
|
useCellContentFunction = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
cellContentFunction,
|
|
|
|
|
useCellContentFunction
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getColumnWidth(keySettings: TableWidgetDataKeySettings): string {
|
|
|
|
|
return isDefined(keySettings.columnWidth) ? keySettings.columnWidth : '0px';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function constructTableCssString(widgetConfig: WidgetConfig): string {
|
|
|
|
|
const origColor = widgetConfig.color || 'rgba(0, 0, 0, 0.87)';
|
|
|
|
|
const origBackgroundColor = widgetConfig.backgroundColor || 'rgb(255, 255, 255)';
|
|
|
|
|
const currentEntityColor = 'rgba(221, 221, 221, 0.65)';
|
|
|
|
|
const currentEntityStickyColor = tinycolor.mix(origBackgroundColor,
|
|
|
|
|
tinycolor(currentEntityColor).setAlpha(1), 65).toRgbString();
|
|
|
|
|
const selectedColor = 'rgba(221, 221, 221, 0.5)';
|
|
|
|
|
const selectedStickyColor = tinycolor.mix(origBackgroundColor,
|
|
|
|
|
tinycolor(selectedColor).setAlpha(1), 50).toRgbString();
|
|
|
|
|
const hoverColor = 'rgba(221, 221, 221, 0.3)';
|
|
|
|
|
const hoverStickyColor = tinycolor.mix(origBackgroundColor,
|
|
|
|
|
tinycolor(hoverColor).setAlpha(1), 30).toRgbString();
|
|
|
|
|
const defaultColor = tinycolor(origColor);
|
|
|
|
|
const mdDark = defaultColor.setAlpha(0.87).toRgbString();
|
|
|
|
|
const mdDarkSecondary = defaultColor.setAlpha(0.54).toRgbString();
|
|
|
|
|
const mdDarkDisabled = defaultColor.setAlpha(0.26).toRgbString();
|
2020-01-30 13:03:53 +02:00
|
|
|
const mdDarkDisabled2 = defaultColor.setAlpha(0.38).toRgbString();
|
2020-01-24 19:05:41 +02:00
|
|
|
const mdDarkDivider = defaultColor.setAlpha(0.12).toRgbString();
|
|
|
|
|
|
|
|
|
|
const cssString =
|
|
|
|
|
'.mat-input-element::placeholder {\n' +
|
2020-02-04 15:14:17 +02:00
|
|
|
' color: ' + mdDarkSecondary + ';\n' +
|
2020-01-24 19:05:41 +02:00
|
|
|
'}\n' +
|
|
|
|
|
'.mat-input-element::-moz-placeholder {\n' +
|
2020-02-04 15:14:17 +02:00
|
|
|
' color: ' + mdDarkSecondary + ';\n' +
|
2020-01-24 19:05:41 +02:00
|
|
|
'}\n' +
|
|
|
|
|
'.mat-input-element::-webkit-input-placeholder {\n' +
|
2020-02-04 15:14:17 +02:00
|
|
|
' color: ' + mdDarkSecondary + ';\n' +
|
2020-01-24 19:05:41 +02:00
|
|
|
'}\n' +
|
|
|
|
|
'.mat-input-element:-ms-input-placeholder {\n' +
|
2020-02-04 15:14:17 +02:00
|
|
|
' color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'mat-toolbar.mat-table-toolbar {\n' +
|
|
|
|
|
'color: ' + mdDark + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'mat-toolbar.mat-table-toolbar:not([color="primary"]) button.mat-icon-button mat-icon {\n' +
|
|
|
|
|
'color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-tab-label {\n' +
|
|
|
|
|
'color: ' + mdDark + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-tab-header-pagination-chevron {\n' +
|
|
|
|
|
'border-color: ' + mdDark + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-tab-header-pagination-disabled .mat-tab-header-pagination-chevron {\n' +
|
|
|
|
|
'border-color: ' + mdDarkDisabled2 + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-header-row {\n' +
|
|
|
|
|
'background-color: ' + origBackgroundColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-header-cell {\n' +
|
|
|
|
|
'color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-header-cell .mat-sort-header-arrow {\n' +
|
|
|
|
|
'color: ' + mdDarkDisabled + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-cell, .mat-table .mat-header-cell {\n' +
|
|
|
|
|
'border-bottom-color: ' + mdDarkDivider + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-cell .mat-checkbox-frame, .mat-table .mat-header-cell .mat-checkbox-frame {\n' +
|
|
|
|
|
'border-color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row .mat-cell.mat-table-sticky {\n' +
|
|
|
|
|
'transition: background-color .2s;\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row.tb-current-entity {\n' +
|
|
|
|
|
'background-color: ' + currentEntityColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row.tb-current-entity .mat-cell.mat-table-sticky {\n' +
|
|
|
|
|
'background-color: ' + currentEntityStickyColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row:hover:not(.tb-current-entity) {\n' +
|
|
|
|
|
'background-color: ' + hoverColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row:hover:not(.tb-current-entity) .mat-cell.mat-table-sticky {\n' +
|
|
|
|
|
'background-color: ' + hoverStickyColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row.mat-row-select.mat-selected:not(.tb-current-entity) {\n' +
|
|
|
|
|
'background-color: ' + selectedColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row.mat-row-select.mat-selected:not(.tb-current-entity) .mat-cell.mat-table-sticky {\n' +
|
|
|
|
|
'background-color: ' + selectedStickyColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-row .mat-cell.mat-table-sticky, .mat-table .mat-header-cell.mat-table-sticky {\n' +
|
|
|
|
|
'background-color: ' + origBackgroundColor + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-cell {\n' +
|
|
|
|
|
'color: ' + mdDark + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-cell button.mat-icon-button mat-icon {\n' +
|
|
|
|
|
'color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-table .mat-cell button.mat-icon-button[disabled][disabled] mat-icon {\n' +
|
|
|
|
|
'color: ' + mdDarkDisabled + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-divider {\n' +
|
|
|
|
|
'border-top-color: ' + mdDarkDivider + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-paginator {\n' +
|
|
|
|
|
'color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-paginator button.mat-icon-button {\n' +
|
|
|
|
|
'color: ' + mdDarkSecondary + ';\n' +
|
|
|
|
|
'}\n' +
|
|
|
|
|
'.mat-paginator button.mat-icon-button[disabled][disabled] {\n' +
|
|
|
|
|
'color: ' + mdDarkDisabled + ';\n' +
|
2020-01-24 19:05:41 +02:00
|
|
|
'}\n' +
|
2020-02-04 15:14:17 +02:00
|
|
|
'.mat-paginator .mat-select-value {\n' +
|
|
|
|
|
'color: ' + mdDarkSecondary + ';\n' +
|
2020-01-24 19:05:41 +02:00
|
|
|
'}';
|
|
|
|
|
return cssString;
|
2020-01-22 20:05:30 +02:00
|
|
|
}
|