Browse Source

Code improvements

pull/15549/head
Maksym Tsymbarov 4 months ago
parent
commit
8a3f73ab5b
  1. 6
      ui-ngx/src/app/modules/home/components/widget/lib/alarm/alarms-table-widget.component.ts
  2. 6
      ui-ngx/src/app/modules/home/components/widget/lib/entity/entities-table-widget.component.ts
  3. 37
      ui-ngx/src/app/modules/home/components/widget/lib/table-widget.models.ts
  4. 3
      ui-ngx/src/app/modules/home/components/widget/lib/timeseries-table-widget.component.ts

6
ui-ngx/src/app/modules/home/components/widget/lib/alarm/alarms-table-widget.component.ts

@ -80,8 +80,8 @@ import {
isValidPageStepIncrement, isValidPageStepIncrement,
noDataMessage, noDataMessage,
prepareTableCellButtonActions, prepareTableCellButtonActions,
resetPaginationOnStateChange,
RowStyleInfo, RowStyleInfo,
setupPaginationResets,
TableCellButtonActionDescriptor, TableCellButtonActionDescriptor,
TableWidgetDataKeySettings, TableWidgetDataKeySettings,
TableWidgetSettings, TableWidgetSettings,
@ -331,9 +331,7 @@ export class AlarmsTableWidgetComponent extends PageComponent implements OnInit,
}); });
if (this.displayPagination) { if (this.displayPagination) {
this.sort.sortChange.pipe(takeUntil(this.destroy$)).subscribe(() => this.paginator.pageIndex = 0); setupPaginationResets(this.ctx, this.paginator, this.sort, this.destroy$);
resetPaginationOnStateChange(this.ctx, this.paginator, this.destroy$);
this.ctx.aliasController?.filtersChanged.pipe( this.ctx.aliasController?.filtersChanged.pipe(
takeUntil(this.destroy$) takeUntil(this.destroy$)

6
ui-ngx/src/app/modules/home/components/widget/lib/entity/entities-table-widget.component.ts

@ -79,8 +79,8 @@ import {
isValidPageStepIncrement, isValidPageStepIncrement,
noDataMessage, noDataMessage,
prepareTableCellButtonActions, prepareTableCellButtonActions,
resetPaginationOnStateChange,
RowStyleInfo, RowStyleInfo,
setupPaginationResets,
TableCellButtonActionDescriptor, TableCellButtonActionDescriptor,
TableWidgetDataKeySettings, TableWidgetDataKeySettings,
TableWidgetSettings, TableWidgetSettings,
@ -266,9 +266,7 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni
}); });
if (this.displayPagination) { if (this.displayPagination) {
this.sort.sortChange.pipe(takeUntil(this.destroy$)).subscribe(() => this.paginator.pageIndex = 0); setupPaginationResets(this.ctx, this.paginator, this.sort, this.destroy$);
resetPaginationOnStateChange(this.ctx, this.paginator, this.destroy$);
this.ctx.aliasController?.filtersChanged.pipe( this.ctx.aliasController?.filtersChanged.pipe(
takeUntil(this.destroy$) takeUntil(this.destroy$)

37
ui-ngx/src/app/modules/home/components/widget/lib/table-widget.models.ts

@ -16,7 +16,7 @@
import { EntityId } from '@shared/models/id/entity-id'; import { EntityId } from '@shared/models/id/entity-id';
import { DataKey, FormattedData, WidgetActionDescriptor, WidgetConfig } from '@shared/models/widget.models'; import { DataKey, FormattedData, WidgetActionDescriptor, WidgetConfig } from '@shared/models/widget.models';
import { deepClone, getDescendantProp, isDefined, isEqual, isNotEmptyStr } from '@core/utils'; import { getDescendantProp, isDefined, isEqual, isNotEmptyStr } from '@core/utils';
import { AlarmDataInfo, alarmFields } from '@shared/models/alarm.models'; import { AlarmDataInfo, alarmFields } from '@shared/models/alarm.models';
import tinycolor from 'tinycolor2'; import tinycolor from 'tinycolor2';
import { Direction } from '@shared/models/page/sort-order'; import { Direction } from '@shared/models/page/sort-order';
@ -32,9 +32,10 @@ import {
isNotEmptyTbFunction, isNotEmptyTbFunction,
TbFunction TbFunction
} from '@shared/models/js-function.models'; } from '@shared/models/js-function.models';
import { forkJoin, Observable, of, ReplaySubject, Subject } from 'rxjs'; import { forkJoin, Observable, of, ReplaySubject, Subscription } from 'rxjs';
import { catchError, distinctUntilChanged, map, share, skip, startWith, takeUntil } from 'rxjs/operators'; import { catchError, distinctUntilChanged, map, share, skip, startWith, takeUntil } from 'rxjs/operators';
import { MatPaginator } from '@angular/material/paginator'; import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import type { ValueFormatProcessor } from '@shared/models/widget-settings.models'; import type { ValueFormatProcessor } from '@shared/models/widget-settings.models';
type ColumnVisibilityOptions = 'visible' | 'hidden' | 'hidden-mobile'; type ColumnVisibilityOptions = 'visible' | 'hidden' | 'hidden-mobile';
@ -518,14 +519,26 @@ export function isValidPageStepCount(value: number): boolean {
return Number.isInteger(value) && value > 0 && value <= 100; return Number.isInteger(value) && value > 0 && value <= 100;
} }
export function resetPaginationOnStateChange(ctx: WidgetContext, export function setupPaginationResets(ctx: WidgetContext,
paginator: MatPaginator, paginator: MatPaginator,
destroy$: Subject<void>): void { sort: MatSort,
ctx.stateController?.stateChanged().pipe( destroyNotifier$: Observable<void>): Subscription {
map(() => deepClone(ctx.stateController.getStateParams())), const subscription = new Subscription();
startWith(deepClone(ctx.stateController?.getStateParams())),
distinctUntilChanged(isEqual), subscription.add(
skip(1), sort.sortChange.pipe(
takeUntil(destroy$) takeUntil(destroyNotifier$)
).subscribe(() => paginator.firstPage()); ).subscribe(() => paginator.pageIndex = 0)
);
subscription.add(
ctx.stateController.stateChanged().pipe(
map(() => ctx.stateController.getStateParams()),
startWith(ctx.stateController.getStateParams()),
distinctUntilChanged(isEqual),
skip(1),
takeUntil(destroyNotifier$)
).subscribe(() => paginator.firstPage())
);
return subscription;
} }

3
ui-ngx/src/app/modules/home/components/widget/lib/timeseries-table-widget.component.ts

@ -89,6 +89,7 @@ import {
noDataMessage, noDataMessage,
prepareTableCellButtonActions, prepareTableCellButtonActions,
RowStyleInfo, RowStyleInfo,
setupPaginationResets,
TableCellButtonActionDescriptor, TableCellButtonActionDescriptor,
TableWidgetDataKeySettings, TableWidgetDataKeySettings,
TableWidgetSettings TableWidgetSettings
@ -653,7 +654,7 @@ export class TimeseriesTableWidgetComponent extends PageComponent implements OnI
if (this.displayPagination) { if (this.displayPagination) {
paginator = this.paginators.toArray()[index]; paginator = this.paginators.toArray()[index];
this.subscriptions.push( this.subscriptions.push(
sort.sortChange.subscribe(() => paginator.pageIndex = 0) setupPaginationResets(this.ctx, paginator, sort, this.destroy$)
); );
observables.push(paginator.page); observables.push(paginator.page);
} }

Loading…
Cancel
Save