2019-10-31 10:06:57 +02:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
2019-10-31 10:06:57 +02:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation } from '@angular/core';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { WidgetsBundle } from '@shared/models/widgets-bundle.model';
|
|
|
|
|
import { IAliasController } from '@core/api/widget-api.models';
|
|
|
|
|
import { NULL_UUID } from '@shared/models/id/has-uuid';
|
|
|
|
|
import { WidgetService } from '@core/http/widget.service';
|
2023-09-28 18:16:20 +03:00
|
|
|
import {
|
|
|
|
|
DeprecatedFilter,
|
|
|
|
|
fullWidgetTypeFqn,
|
|
|
|
|
WidgetInfo,
|
|
|
|
|
widgetType,
|
|
|
|
|
WidgetTypeInfo
|
|
|
|
|
} from '@shared/models/widget.models';
|
|
|
|
|
import { debounceTime, distinctUntilChanged, map, skip } from 'rxjs/operators';
|
|
|
|
|
import { BehaviorSubject, combineLatest } from 'rxjs';
|
2023-11-17 17:12:47 +02:00
|
|
|
import { isObject } from '@core/utils';
|
2023-09-04 18:04:20 +03:00
|
|
|
import { PageLink } from '@shared/models/page/page-link';
|
|
|
|
|
import { Direction } from '@shared/models/page/sort-order';
|
2023-11-28 11:30:46 +02:00
|
|
|
import { GridEntitiesFetchFunction, ScrollGridColumns } from '@shared/components/grid/scroll-grid-datasource';
|
|
|
|
|
import { ItemSizeStrategy } from '@shared/components/grid/scroll-grid.component';
|
2024-07-08 16:28:40 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2023-08-28 18:54:12 +03:00
|
|
|
|
2023-09-04 18:04:20 +03:00
|
|
|
type selectWidgetMode = 'bundles' | 'allWidgets';
|
|
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
interface WidgetsFilter {
|
|
|
|
|
search: string;
|
|
|
|
|
filter: widgetType[];
|
|
|
|
|
deprecatedFilter: DeprecatedFilter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface BundleWidgetsFilter extends WidgetsFilter {
|
|
|
|
|
widgetsBundleId: string;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-dashboard-widget-select',
|
|
|
|
|
templateUrl: './dashboard-widget-select.component.html',
|
2023-09-28 18:16:20 +03:00
|
|
|
styleUrls: ['./dashboard-widget-select.component.scss'],
|
|
|
|
|
encapsulation: ViewEncapsulation.None
|
2019-10-31 10:06:57 +02:00
|
|
|
})
|
2021-03-03 18:41:01 +02:00
|
|
|
export class DashboardWidgetSelectComponent implements OnInit {
|
2019-10-31 10:06:57 +02:00
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
private searchSubject = new BehaviorSubject<string>('');
|
|
|
|
|
private search$ = this.searchSubject.asObservable().pipe(
|
|
|
|
|
debounceTime(150));
|
|
|
|
|
|
2021-03-04 17:11:55 +02:00
|
|
|
private filterWidgetTypes$ = new BehaviorSubject<Array<widgetType>>(null);
|
2023-09-28 18:16:20 +03:00
|
|
|
private deprecatedFilter$ = new BehaviorSubject<DeprecatedFilter>(DeprecatedFilter.ACTUAL);
|
2023-09-04 18:04:20 +03:00
|
|
|
private selectWidgetMode$ = new BehaviorSubject<selectWidgetMode>('bundles');
|
2023-09-28 18:16:20 +03:00
|
|
|
private widgetsBundle$ = new BehaviorSubject<WidgetsBundle>(null);
|
|
|
|
|
|
2021-03-11 14:54:35 +02:00
|
|
|
widgetTypes = new Set<widgetType>();
|
2023-08-28 18:54:12 +03:00
|
|
|
hasDeprecated = false;
|
2021-03-03 18:41:01 +02:00
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
@Input()
|
|
|
|
|
aliasController: IAliasController;
|
|
|
|
|
|
2024-07-08 16:28:40 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
scadaFirst = false;
|
|
|
|
|
|
2021-03-03 15:41:47 +02:00
|
|
|
@Input()
|
2023-09-28 18:16:20 +03:00
|
|
|
set search(search: string) {
|
|
|
|
|
this.searchSubject.next(search);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get search(): string {
|
|
|
|
|
return this.searchSubject.value;
|
2021-03-03 15:41:47 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-04 17:11:55 +02:00
|
|
|
@Input()
|
|
|
|
|
set filterWidgetTypes(widgetTypes: Array<widgetType>) {
|
|
|
|
|
this.filterWidgetTypes$.next(widgetTypes);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-31 16:49:56 +03:00
|
|
|
get filterWidgetTypes(): Array<widgetType> {
|
|
|
|
|
return this.filterWidgetTypes$.value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 18:04:20 +03:00
|
|
|
@Input()
|
|
|
|
|
set selectWidgetMode(mode: selectWidgetMode) {
|
2023-09-28 18:16:20 +03:00
|
|
|
if (this.selectWidgetMode$.value !== mode) {
|
|
|
|
|
if (mode === 'bundles' && this.widgetsBundle$.value === null) {
|
|
|
|
|
this.widgetTypes.clear();
|
|
|
|
|
this.hasDeprecated = false;
|
|
|
|
|
} else {
|
|
|
|
|
this.widgetTypes = new Set<widgetType>(Object.keys(widgetType).map(t => t as widgetType));
|
|
|
|
|
this.hasDeprecated = true;
|
|
|
|
|
}
|
|
|
|
|
this.filterWidgetTypes$.next(null);
|
|
|
|
|
this.deprecatedFilter$.next(DeprecatedFilter.ACTUAL);
|
|
|
|
|
this.selectWidgetMode$.next(mode);
|
|
|
|
|
}
|
2023-09-04 18:04:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get selectWidgetMode(): selectWidgetMode {
|
|
|
|
|
return this.selectWidgetMode$.value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 18:54:12 +03:00
|
|
|
@Input()
|
2023-09-28 18:16:20 +03:00
|
|
|
set deprecatedFilter(filter: DeprecatedFilter) {
|
|
|
|
|
this.deprecatedFilter$.next(filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get deprecatedFilter(): DeprecatedFilter {
|
|
|
|
|
return this.deprecatedFilter$.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set widgetsBundle(widgetBundle: WidgetsBundle) {
|
|
|
|
|
if (this.widgetsBundle$.value !== widgetBundle) {
|
|
|
|
|
if (widgetBundle === null && this.selectWidgetMode$.value !== 'allWidgets') {
|
|
|
|
|
this.widgetTypes.clear();
|
|
|
|
|
this.hasDeprecated = false;
|
|
|
|
|
} else {
|
|
|
|
|
this.widgetTypes = new Set<widgetType>(Object.keys(widgetType).map(t => t as widgetType));
|
|
|
|
|
this.hasDeprecated = true;
|
|
|
|
|
}
|
|
|
|
|
this.filterWidgetTypes$.next(null);
|
|
|
|
|
this.deprecatedFilter$.next(DeprecatedFilter.ACTUAL);
|
|
|
|
|
this.widgetsBundle$.next(widgetBundle);
|
|
|
|
|
}
|
2023-08-28 18:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
get widgetsBundle(): WidgetsBundle {
|
|
|
|
|
return this.widgetsBundle$.value;
|
2023-08-28 18:54:12 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
@Output()
|
2021-03-03 18:41:01 +02:00
|
|
|
widgetSelected: EventEmitter<WidgetInfo> = new EventEmitter<WidgetInfo>();
|
2019-10-31 10:06:57 +02:00
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
columns: ScrollGridColumns = {
|
2023-11-01 19:07:25 +02:00
|
|
|
columns: 2,
|
2023-09-28 18:16:20 +03:00
|
|
|
breakpoints: {
|
2023-11-01 19:07:25 +02:00
|
|
|
'screen and (min-width: 2000px)': 5,
|
2023-11-07 18:15:35 +02:00
|
|
|
'screen and (min-width: 1097px)': 4,
|
|
|
|
|
'gt-sm': 3,
|
2023-11-07 10:52:31 +02:00
|
|
|
'screen and (min-width: 721px)': 4,
|
|
|
|
|
'screen and (min-width: 485px)': 3
|
2023-09-28 18:16:20 +03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-22 14:49:46 +02:00
|
|
|
gridWidgetsItemSizeStrategy: ItemSizeStrategy = {
|
|
|
|
|
defaultItemSize: 160,
|
|
|
|
|
itemSizeFunction: itemWidth => (itemWidth - 24) * 0.8 + 76
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
widgetBundlesFetchFunction: GridEntitiesFetchFunction<WidgetsBundle, string>;
|
|
|
|
|
allWidgetsFetchFunction: GridEntitiesFetchFunction<WidgetTypeInfo, WidgetsFilter>;
|
|
|
|
|
widgetsFetchFunction: GridEntitiesFetchFunction<WidgetTypeInfo, BundleWidgetsFilter>;
|
|
|
|
|
|
|
|
|
|
widgetsBundleFilter = '';
|
|
|
|
|
allWidgetsFilter: WidgetsFilter = {search: '', filter: null, deprecatedFilter: DeprecatedFilter.ACTUAL};
|
|
|
|
|
widgetsFilter: BundleWidgetsFilter = {search: '', filter: null, deprecatedFilter: DeprecatedFilter.ACTUAL, widgetsBundleId: null};
|
2021-02-04 19:00:28 +02:00
|
|
|
|
2021-02-25 15:37:20 +02:00
|
|
|
constructor(private widgetsService: WidgetService,
|
2023-11-17 17:12:47 +02:00
|
|
|
private cd: ChangeDetectorRef) {
|
2023-09-28 18:16:20 +03:00
|
|
|
|
|
|
|
|
this.widgetBundlesFetchFunction = (pageSize, page, filter) => {
|
|
|
|
|
const pageLink = new PageLink(pageSize, page, filter, {
|
|
|
|
|
property: 'title',
|
|
|
|
|
direction: Direction.ASC
|
|
|
|
|
});
|
2024-07-08 16:28:40 +03:00
|
|
|
return this.widgetsService.getWidgetBundles(pageLink, true, false, this.scadaFirst);
|
2023-09-28 18:16:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.allWidgetsFetchFunction = (pageSize, page, filter) => {
|
|
|
|
|
const pageLink = new PageLink(pageSize, page, filter.search, {
|
|
|
|
|
property: 'name',
|
|
|
|
|
direction: Direction.ASC
|
|
|
|
|
});
|
2024-07-08 16:28:40 +03:00
|
|
|
return this.widgetsService.getWidgetTypes(pageLink, false, true, this.scadaFirst,
|
|
|
|
|
filter.deprecatedFilter, filter.filter);
|
2023-09-28 18:16:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.widgetsFetchFunction = (pageSize, page, filter) => {
|
|
|
|
|
const pageLink = new PageLink(pageSize, page, filter.search, {
|
|
|
|
|
property: 'name',
|
|
|
|
|
direction: Direction.ASC
|
|
|
|
|
});
|
|
|
|
|
return this.widgetsService.getBundleWidgetTypeInfos(pageLink, filter.widgetsBundleId,
|
|
|
|
|
true, filter.deprecatedFilter, filter.filter);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.search$.pipe(
|
|
|
|
|
distinctUntilChanged(),
|
|
|
|
|
skip(1)
|
|
|
|
|
).subscribe(
|
|
|
|
|
(search) => {
|
|
|
|
|
this.widgetsBundleFilter = search;
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
2023-09-04 18:04:20 +03:00
|
|
|
);
|
2023-09-28 18:16:20 +03:00
|
|
|
|
|
|
|
|
combineLatest({search: this.search$, filter: this.filterWidgetTypes$.asObservable(),
|
|
|
|
|
deprecatedFilter: this.deprecatedFilter$.asObservable()}).pipe(
|
|
|
|
|
distinctUntilChanged((oldValue, newValue) => JSON.stringify(oldValue) === JSON.stringify(newValue)),
|
|
|
|
|
skip(1)
|
|
|
|
|
).subscribe(
|
|
|
|
|
(filter) => {
|
|
|
|
|
this.allWidgetsFilter = filter;
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
2021-03-03 15:41:47 +02:00
|
|
|
);
|
2023-09-28 18:16:20 +03:00
|
|
|
|
|
|
|
|
combineLatest({search: this.search$, widgetsBundleId: this.widgetsBundle$.pipe(map(wb => wb !== null ? wb.id.id : null)),
|
|
|
|
|
filter: this.filterWidgetTypes$.asObservable(), deprecatedFilter: this.deprecatedFilter$.asObservable()}).pipe(
|
2021-03-04 17:11:55 +02:00
|
|
|
distinctUntilChanged((oldValue, newValue) => JSON.stringify(oldValue) === JSON.stringify(newValue)),
|
2023-09-28 18:16:20 +03:00
|
|
|
skip(1)
|
|
|
|
|
).subscribe(
|
|
|
|
|
(filter) => {
|
|
|
|
|
if (filter.widgetsBundleId) {
|
|
|
|
|
this.widgetsFilter = filter;
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-04 19:00:28 +02:00
|
|
|
);
|
2019-10-31 10:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-11 14:54:35 +02:00
|
|
|
ngOnInit(): void {
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
onWidgetClicked($event: Event, widget: WidgetTypeInfo): void {
|
|
|
|
|
this.widgetSelected.emit(this.toWidgetInfo(widget));
|
2019-10-31 10:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 19:00:28 +02:00
|
|
|
isSystem(item: WidgetsBundle): boolean {
|
|
|
|
|
return item && item.tenantId.id === NULL_UUID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectBundle($event: Event, bundle: WidgetsBundle) {
|
|
|
|
|
$event.preventDefault();
|
|
|
|
|
this.widgetsBundle = bundle;
|
2023-09-28 18:16:20 +03:00
|
|
|
if (bundle.title?.toLowerCase().includes(this.search.toLowerCase()) ||
|
|
|
|
|
bundle.description?.toLowerCase().includes(this.search.toLowerCase())) {
|
|
|
|
|
this.searchSubject.next('');
|
|
|
|
|
}
|
2021-02-04 19:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-28 18:16:20 +03:00
|
|
|
isObject(value: any): boolean {
|
|
|
|
|
return isObject(value);
|
2023-09-04 18:04:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private toWidgetInfo(widgetTypeInfo: WidgetTypeInfo): WidgetInfo {
|
|
|
|
|
return {
|
|
|
|
|
typeFullFqn: fullWidgetTypeFqn(widgetTypeInfo),
|
|
|
|
|
type: widgetTypeInfo.widgetType,
|
|
|
|
|
title: widgetTypeInfo.name,
|
|
|
|
|
image: widgetTypeInfo.image,
|
|
|
|
|
description: widgetTypeInfo.description,
|
|
|
|
|
deprecated: widgetTypeInfo.deprecated
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-10-31 10:06:57 +02:00
|
|
|
}
|