import { Component } from '@angular/core'; import { Store } from '@ngrx/store'; import { TranslateService } from '@ngx-translate/core'; import { CatalogEntry, ConfigActions, ConfigSelectors, DashboardActions, MIME_TYPE_WIDGET_MANIFEST, MIME_TYPE_WIDGET_TYPE, ToolboxManifestService, ToolboxMenuService, ViewMode, Widget } from '@onepoint-yap/dashboard'; import { BehaviorSubject, combineLatest, map, Observable } from 'rxjs'; @Component({ selector: 'yap-catalog', templateUrl: './catalog.component.html' }) export class YapCatalogComponent { search$ = new BehaviorSubject(''); type$ = new BehaviorSubject(undefined); filteredWidgets$: Observable; types$: Observable; selectedRestitution: Widget | null = null; mode = ViewMode; selectedMode: ViewMode = ViewMode.CATALOG; catalog: Widget[] | undefined; constructor( private toolboxMenuService: ToolboxMenuService, private translateService: TranslateService, private toolboxManifestService: ToolboxManifestService, private store: Store ) { this.store.dispatch(ConfigActions.loadWidgets()); this.filteredWidgets$ = combineLatest([this.toolboxMenuService.menuList$, this.search$, this.type$]).pipe( map(([menus, search, type]) => menus.filter( (menu) => (search.length === 0 || this.translateService.instant(menu.label).toLocaleLowerCase().includes(search.toLocaleLowerCase())) && (!type || this.toolboxManifestService.get(menu.type).type === type) ) ) ); this.types$ = this.toolboxManifestService.types$; this.store.select(ConfigSelectors.catalog).subscribe((catalog: Widget[]) => (this.catalog = catalog)); } dragStart($event: DragEvent, type: string) { if (!!$event && !!$event.dataTransfer && !$event.dataTransfer.types.includes(MIME_TYPE_WIDGET_MANIFEST)) { const manifest = this.toolboxManifestService.get(type); $event.dataTransfer.dropEffect = 'copy'; $event.dataTransfer.effectAllowed = 'copy'; $event.dataTransfer.setData(MIME_TYPE_WIDGET_MANIFEST, JSON.stringify(manifest)); $event.dataTransfer.setData(MIME_TYPE_WIDGET_TYPE + manifest.type, 'unused'); } } selectMode(newMode: ViewMode) { this.selectedMode = newMode; } selectRestitution(catalogEntry: CatalogEntry) { if (catalogEntry.isCustom) { this.catalog?.forEach((widget: Widget) => { if (widget.id === catalogEntry.type) { this.selectedRestitution = widget; this.selectMode(this.mode.CONSULT); } }); } } closeRestitution($event: void) { this.selectedRestitution = null; this.selectMode(this.mode.CATALOG); } viewInDashboard(widget: CatalogEntry) { this.store.dispatch(DashboardActions.addWidgetToPresentation({ widgetType: widget.type })); } }