Commit 9a9379d1857b3edd32cff843bf7ad435ec74e565

Authored by Vladyslav_Prykhodko
1 parent 5865dd28

UI: Added validation of the obtained value from the cell style function

@@ -35,7 +35,7 @@ import { DataKey, WidgetActionDescriptor, WidgetConfig } from '@shared/models/wi @@ -35,7 +35,7 @@ import { DataKey, WidgetActionDescriptor, WidgetConfig } from '@shared/models/wi
35 import { IWidgetSubscription } from '@core/api/widget-api.models'; 35 import { IWidgetSubscription } from '@core/api/widget-api.models';
36 import { UtilsService } from '@core/services/utils.service'; 36 import { UtilsService } from '@core/services/utils.service';
37 import { TranslateService } from '@ngx-translate/core'; 37 import { TranslateService } from '@ngx-translate/core';
38 -import { createLabelFromDatasource, deepClone, hashCode, isDefined, isNumber } from '@core/utils'; 38 +import { createLabelFromDatasource, deepClone, hashCode, isDefined, isNumber, isObject } from '@core/utils';
39 import cssjs from '@core/css/css'; 39 import cssjs from '@core/css/css';
40 import { sortItems } from '@shared/models/page/page-link'; 40 import { sortItems } from '@shared/models/page/page-link';
41 import { Direction } from '@shared/models/page/sort-order'; 41 import { Direction } from '@shared/models/page/sort-order';
@@ -598,8 +598,16 @@ export class AlarmsTableWidgetComponent extends PageComponent implements OnInit, @@ -598,8 +598,16 @@ export class AlarmsTableWidgetComponent extends PageComponent implements OnInit,
598 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) { 598 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) {
599 try { 599 try {
600 style = styleInfo.cellStyleFunction(value); 600 style = styleInfo.cellStyleFunction(value);
  601 + if (!isObject(style)) {
  602 + throw new TypeError(`${style === null ? 'null' : typeof style} instead of style object`);
  603 + }
  604 + if (Array.isArray(style)) {
  605 + throw new TypeError(`Array instead of style object`);
  606 + }
601 } catch (e) { 607 } catch (e) {
602 style = {}; 608 style = {};
  609 + console.warn(`Cell style function for data key '${key.label}' in widget '${this.ctx.widgetTitle}' ` +
  610 + `returns '${e}'. Please check your cell style function.`);
603 } 611 }
604 } else { 612 } else {
605 style = this.defaultStyle(key, value); 613 style = this.defaultStyle(key, value);
@@ -40,7 +40,7 @@ import { @@ -40,7 +40,7 @@ import {
40 import { IWidgetSubscription } from '@core/api/widget-api.models'; 40 import { IWidgetSubscription } from '@core/api/widget-api.models';
41 import { UtilsService } from '@core/services/utils.service'; 41 import { UtilsService } from '@core/services/utils.service';
42 import { TranslateService } from '@ngx-translate/core'; 42 import { TranslateService } from '@ngx-translate/core';
43 -import { createLabelFromDatasource, deepClone, hashCode, isDefined, isNumber } from '@core/utils'; 43 +import { createLabelFromDatasource, deepClone, hashCode, isDefined, isNumber, isObject } from '@core/utils';
44 import cssjs from '@core/css/css'; 44 import cssjs from '@core/css/css';
45 import { CollectionViewer, DataSource } from '@angular/cdk/collections'; 45 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
46 import { DataKeyType } from '@shared/models/telemetry/telemetry.models'; 46 import { DataKeyType } from '@shared/models/telemetry/telemetry.models';
@@ -515,8 +515,16 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni @@ -515,8 +515,16 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni
515 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) { 515 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) {
516 try { 516 try {
517 style = styleInfo.cellStyleFunction(value); 517 style = styleInfo.cellStyleFunction(value);
  518 + if (!isObject(style)) {
  519 + throw new TypeError(`${style === null ? 'null' : typeof style} instead of style object`);
  520 + }
  521 + if (Array.isArray(style)) {
  522 + throw new TypeError(`Array instead of style object`);
  523 + }
518 } catch (e) { 524 } catch (e) {
519 style = {}; 525 style = {};
  526 + console.warn(`Cell style function for data key '${key.label}' in widget '${this.ctx.widgetTitle}' ` +
  527 + `returns '${e}'. Please check your cell style function.`);
520 } 528 }
521 } else { 529 } else {
522 style = {}; 530 style = {};
@@ -538,7 +546,7 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni @@ -538,7 +546,7 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni
538 try { 546 try {
539 content = contentInfo.cellContentFunction(value, entity, this.ctx); 547 content = contentInfo.cellContentFunction(value, entity, this.ctx);
540 } catch (e) { 548 } catch (e) {
541 - content = '' + value; 549 + content = '' + value;
542 } 550 }
543 } else { 551 } else {
544 content = this.defaultContent(key, contentInfo, value); 552 content = this.defaultContent(key, contentInfo, value);
@@ -40,7 +40,7 @@ import { @@ -40,7 +40,7 @@ import {
40 } from '@shared/models/widget.models'; 40 } from '@shared/models/widget.models';
41 import { UtilsService } from '@core/services/utils.service'; 41 import { UtilsService } from '@core/services/utils.service';
42 import { TranslateService } from '@ngx-translate/core'; 42 import { TranslateService } from '@ngx-translate/core';
43 -import {hashCode, isDefined, isDefinedAndNotNull, isNumber} from '@core/utils'; 43 +import { hashCode, isDefined, isNumber, isObject } from '@core/utils';
44 import cssjs from '@core/css/css'; 44 import cssjs from '@core/css/css';
45 import { PageLink } from '@shared/models/page/page-link'; 45 import { PageLink } from '@shared/models/page/page-link';
46 import { Direction, SortOrder, sortOrderFromString } from '@shared/models/page/sort-order'; 46 import { Direction, SortOrder, sortOrderFromString } from '@shared/models/page/sort-order';
@@ -385,8 +385,16 @@ export class TimeseriesTableWidgetComponent extends PageComponent implements OnI @@ -385,8 +385,16 @@ export class TimeseriesTableWidgetComponent extends PageComponent implements OnI
385 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) { 385 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) {
386 try { 386 try {
387 style = styleInfo.cellStyleFunction(value); 387 style = styleInfo.cellStyleFunction(value);
  388 + if (!isObject(style)) {
  389 + throw new TypeError(`${style === null ? 'null' : typeof style} instead of style object`);
  390 + }
  391 + if (Array.isArray(style)) {
  392 + throw new TypeError(`Array instead of style object`);
  393 + }
388 } catch (e) { 394 } catch (e) {
389 style = {}; 395 style = {};
  396 + console.warn(`Cell style function for data key '${source.header[index - 1].dataKey.label}' in widget ` +
  397 + `'${this.ctx.widgetConfig.title}' returns '${e}'. Please check your cell style function.`);
390 } 398 }
391 } 399 }
392 } 400 }