Browse Source

Code improvements

pull/15549/head
Maksym Tsymbarov 4 weeks 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,
noDataMessage,
prepareTableCellButtonActions,
resetPaginationOnStateChange,
RowStyleInfo,
setupPaginationResets,
TableCellButtonActionDescriptor,
TableWidgetDataKeySettings,
TableWidgetSettings,
@ -331,9 +331,7 @@ export class AlarmsTableWidgetComponent extends PageComponent implements OnInit,
});
if (this.displayPagination) {
this.sort.sortChange.pipe(takeUntil(this.destroy$)).subscribe(() => this.paginator.pageIndex = 0);
resetPaginationOnStateChange(this.ctx, this.paginator, this.destroy$);
setupPaginationResets(this.ctx, this.paginator, this.sort, this.destroy$);
this.ctx.aliasController?.filtersChanged.pipe(
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,
noDataMessage,
prepareTableCellButtonActions,
resetPaginationOnStateChange,
RowStyleInfo,
setupPaginationResets,
TableCellButtonActionDescriptor,
TableWidgetDataKeySettings,
TableWidgetSettings,
@ -266,9 +266,7 @@ export class EntitiesTableWidgetComponent extends PageComponent implements OnIni
});
if (this.displayPagination) {
this.sort.sortChange.pipe(takeUntil(this.destroy$)).subscribe(() => this.paginator.pageIndex = 0);
resetPaginationOnStateChange(this.ctx, this.paginator, this.destroy$);
setupPaginationResets(this.ctx, this.paginator, this.sort, this.destroy$);
this.ctx.aliasController?.filtersChanged.pipe(
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 { 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 tinycolor from 'tinycolor2';
import { Direction } from '@shared/models/page/sort-order';
@ -32,9 +32,10 @@ import {
isNotEmptyTbFunction,
TbFunction
} 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 { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import type { ValueFormatProcessor } from '@shared/models/widget-settings.models';
type ColumnVisibilityOptions = 'visible' | 'hidden' | 'hidden-mobile';
@ -518,14 +519,26 @@ export function isValidPageStepCount(value: number): boolean {
return Number.isInteger(value) && value > 0 && value <= 100;
}
export function resetPaginationOnStateChange(ctx: WidgetContext,
paginator: MatPaginator,
destroy$: Subject<void>): void {
ctx.stateController?.stateChanged().pipe(
map(() => deepClone(ctx.stateController.getStateParams())),
startWith(deepClone(ctx.stateController?.getStateParams())),
distinctUntilChanged(isEqual),
skip(1),
takeUntil(destroy$)
).subscribe(() => paginator.firstPage());
export function setupPaginationResets(ctx: WidgetContext,
paginator: MatPaginator,
sort: MatSort,
destroyNotifier$: Observable<void>): Subscription {
const subscription = new Subscription();
subscription.add(
sort.sortChange.pipe(
takeUntil(destroyNotifier$)
).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,
prepareTableCellButtonActions,
RowStyleInfo,
setupPaginationResets,
TableCellButtonActionDescriptor,
TableWidgetDataKeySettings,
TableWidgetSettings
@ -653,7 +654,7 @@ export class TimeseriesTableWidgetComponent extends PageComponent implements OnI
if (this.displayPagination) {
paginator = this.paginators.toArray()[index];
this.subscriptions.push(
sort.sortChange.subscribe(() => paginator.pageIndex = 0)
setupPaginationResets(this.ctx, paginator, sort, this.destroy$)
);
observables.push(paginator.page);
}

Loading…
Cancel
Save