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 35 import { IWidgetSubscription } from '@core/api/widget-api.models';
36 36 import { UtilsService } from '@core/services/utils.service';
37 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 39 import cssjs from '@core/css/css';
40 40 import { sortItems } from '@shared/models/page/page-link';
41 41 import { Direction } from '@shared/models/page/sort-order';
... ... @@ -598,8 +598,16 @@ export class AlarmsTableWidgetComponent extends PageComponent implements OnInit,
598 598 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) {
599 599 try {
600 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 607 } catch (e) {
602 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 612 } else {
605 613 style = this.defaultStyle(key, value);
... ...
... ... @@ -40,7 +40,7 @@ import {
40 40 import { IWidgetSubscription } from '@core/api/widget-api.models';
41 41 import { UtilsService } from '@core/services/utils.service';
42 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 44 import cssjs from '@core/css/css';
45 45 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
46 46 import { DataKeyType } from '@shared/models/telemetry/telemetry.models';
... ... @@ -515,8 +515,16 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni
515 515 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) {
516 516 try {
517 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 524 } catch (e) {
519 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 529 } else {
522 530 style = {};
... ... @@ -538,7 +546,7 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni
538 546 try {
539 547 content = contentInfo.cellContentFunction(value, entity, this.ctx);
540 548 } catch (e) {
541   - content = '' + value;
  549 + content = '' + value;
542 550 }
543 551 } else {
544 552 content = this.defaultContent(key, contentInfo, value);
... ...
... ... @@ -40,7 +40,7 @@ import {
40 40 } from '@shared/models/widget.models';
41 41 import { UtilsService } from '@core/services/utils.service';
42 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 44 import cssjs from '@core/css/css';
45 45 import { PageLink } from '@shared/models/page/page-link';
46 46 import { Direction, SortOrder, sortOrderFromString } from '@shared/models/page/sort-order';
... ... @@ -385,8 +385,16 @@ export class TimeseriesTableWidgetComponent extends PageComponent implements OnI
385 385 if (styleInfo.useCellStyleFunction && styleInfo.cellStyleFunction) {
386 386 try {
387 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 394 } catch (e) {
389 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 }
... ...