Browse Source

[PROD-3656] [FIX] refactoring

pull/11012/head
mpetrov 2 years ago
parent
commit
858b2eab0c
  1. 2
      ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-service-rpc-connector-templates.component.html
  2. 16
      ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts
  3. 58
      ui-ngx/src/app/shared/pipe/key-value-not-empty.pipe.ts
  4. 3
      ui-ngx/src/app/shared/shared.module.ts

2
ui-ngx/src/app/modules/home/components/widget/lib/gateway/gateway-service-rpc-connector-templates.component.html

@ -32,7 +32,7 @@
</mat-expansion-panel-header> </mat-expansion-panel-header>
<ng-container <ng-container
*ngFor="let config of template.config | keyValueIsNotEmpty : originalOrder" *ngFor="let config of template.config | keyValueIsNotEmpty"
[ngTemplateOutlet]="RPCTemplateRef" [ngTemplateOutlet]="RPCTemplateRef"
[ngTemplateOutletContext]="{ $implicit: config, innerValue: false }"> [ngTemplateOutletContext]="{ $implicit: config, innerValue: false }">
</ng-container> </ng-container>

16
ui-ngx/src/app/modules/home/components/widget/widget-components.module.ts

@ -100,6 +100,7 @@ import { BarChartWidgetComponent } from '@home/components/widget/lib/chart/bar-c
import { PolarAreaWidgetComponent } from '@home/components/widget/lib/chart/polar-area-widget.component'; import { PolarAreaWidgetComponent } from '@home/components/widget/lib/chart/polar-area-widget.component';
import { RadarChartWidgetComponent } from '@home/components/widget/lib/chart/radar-chart-widget.component'; import { RadarChartWidgetComponent } from '@home/components/widget/lib/chart/radar-chart-widget.component';
import { MobileAppQrcodeWidgetComponent } from '@home/components/widget/lib/mobile-app-qrcode-widget.component'; import { MobileAppQrcodeWidgetComponent } from '@home/components/widget/lib/mobile-app-qrcode-widget.component';
import { KeyValueIsNotEmptyPipe } from "@shared/pipe/key-value-not-empty.pipe";
@NgModule({ @NgModule({
declarations: declarations:
@ -168,13 +169,14 @@ import { MobileAppQrcodeWidgetComponent } from '@home/components/widget/lib/mobi
PolarAreaWidgetComponent, PolarAreaWidgetComponent,
RadarChartWidgetComponent RadarChartWidgetComponent
], ],
imports: [ imports: [
CommonModule, CommonModule,
SharedModule, SharedModule,
RpcWidgetsModule, RpcWidgetsModule,
HomePageWidgetsModule, HomePageWidgetsModule,
SharedHomeComponentsModule SharedHomeComponentsModule,
], KeyValueIsNotEmptyPipe
],
exports: [ exports: [
EntitiesTableWidgetComponent, EntitiesTableWidgetComponent,
AlarmsTableWidgetComponent, AlarmsTableWidgetComponent,

58
ui-ngx/src/app/shared/pipe/key-value-not-empty.pipe.ts

@ -14,31 +14,53 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { KeyValueChanges, KeyValueDiffer, KeyValueDiffers, Pipe, PipeTransform } from '@angular/core'; import {
import { KeyValue, KeyValuePipe } from "@angular/common"; inject,
KeyValueChangeRecord,
KeyValueChanges,
KeyValueDiffer,
KeyValueDiffers,
Pipe,
PipeTransform
} from '@angular/core';
import { KeyValue } from "@angular/common";
@Pipe({ @Pipe({
name: 'keyValueIsNotEmpty', name: 'keyValueIsNotEmpty',
pure: false,
standalone: true,
}) })
export class KeyValueIsNotEmptyPipe extends KeyValuePipe implements PipeTransform { export class KeyValueIsNotEmptyPipe implements PipeTransform {
private difference!: KeyValueDiffer<any, any>; private differs: KeyValueDiffers = inject(KeyValueDiffers);
private differ!: KeyValueDiffer<string, unknown>;
private keyValues: Array<KeyValue<string, unknown>> = [];
constructor(private readonly keyValueDiffers: KeyValueDiffers) { // This is a custom implementation of angular keyvalue pipe
super(keyValueDiffers); // https://github.com/angular/angular/blob/main/packages/common/src/pipes/keyvalue_pipe.ts
} transform(
input: Record<string, unknown>,
): Array<KeyValue<string, unknown>> {
if (!input || (!(input instanceof Map) && typeof input !== 'object')) {
return null;
}
this.differ ??= this.differs.find(input).create();
const differChanges: KeyValueChanges<string, unknown> | null = this.differ.diff(input);
transform<ValueType>(
input: Record<string, ValueType>,
compareFn?: (a: KeyValue<string, ValueType>, b: KeyValue<string, ValueType>) => number
): KeyValue<string, ValueType>[] & null {
super.transform(input, compareFn);
this.difference ??= this.keyValueDiffers.find(input).create();
const differChanges: KeyValueChanges<string, ValueType> | null = this.difference.diff(input as any);
if (differChanges) { if (differChanges) {
console.log(differChanges) this.keyValues = [];
const filteredEntries = [...Object.entries(input)] differChanges.forEachItem((r: KeyValueChangeRecord<string, unknown>) => {
.filter(([_, value]) => value !== null && value !== undefined); if (r.currentValue !== null && r.currentValue !== undefined) {
return super.transform(new Map<string, ValueType>(filteredEntries), compareFn) as KeyValue<string, ValueType>[] & null; this.keyValues.push(this.makeKeyValuePair(r.key, r.currentValue!));
}
});
} }
return this.keyValues;
}
private makeKeyValuePair(key: string, value: unknown): KeyValue<string, unknown> {
return {key: key, value: value};
} }
} }

3
ui-ngx/src/app/shared/shared.module.ts

@ -219,7 +219,6 @@ import { ImageGalleryDialogComponent } from '@shared/components/image/image-gall
import { RuleChainSelectPanelComponent } from '@shared/components/rule-chain/rule-chain-select-panel.component'; import { RuleChainSelectPanelComponent } from '@shared/components/rule-chain/rule-chain-select-panel.component';
import { WidgetButtonComponent } from '@shared/components/button/widget-button.component'; import { WidgetButtonComponent } from '@shared/components/button/widget-button.component';
import { HexInputComponent } from '@shared/components/color-picker/hex-input.component'; import { HexInputComponent } from '@shared/components/color-picker/hex-input.component';
import { KeyValueIsNotEmptyPipe } from "@shared/pipe/key-value-not-empty.pipe";
export function MarkedOptionsFactory(markedOptionsService: MarkedOptionsService) { export function MarkedOptionsFactory(markedOptionsService: MarkedOptionsService) {
return markedOptionsService; return markedOptionsService;
@ -367,7 +366,6 @@ export function MarkedOptionsFactory(markedOptionsService: MarkedOptionsService)
ShortNumberPipe, ShortNumberPipe,
SelectableColumnsPipe, SelectableColumnsPipe,
KeyboardShortcutPipe, KeyboardShortcutPipe,
KeyValueIsNotEmptyPipe,
TbJsonToStringDirective, TbJsonToStringDirective,
JsonObjectEditDialogComponent, JsonObjectEditDialogComponent,
HistorySelectorComponent, HistorySelectorComponent,
@ -621,7 +619,6 @@ export function MarkedOptionsFactory(markedOptionsService: MarkedOptionsService)
SafePipe, SafePipe,
ShortNumberPipe, ShortNumberPipe,
SelectableColumnsPipe, SelectableColumnsPipe,
KeyValueIsNotEmptyPipe,
RouterModule, RouterModule,
TranslateModule, TranslateModule,
JsonObjectEditDialogComponent, JsonObjectEditDialogComponent,

Loading…
Cancel
Save