|
|
@@ -2,6 +2,7 @@ import {
|
|
|
ChangeDetectionStrategy,
|
|
|
Component,
|
|
|
HostBinding,
|
|
|
+ Injector,
|
|
|
Input,
|
|
|
OnChanges,
|
|
|
OnInit,
|
|
|
@@ -9,12 +10,16 @@ import {
|
|
|
} from '@angular/core';
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
import { assertNever } from '@vendure/common/lib/shared-utils';
|
|
|
-import { BehaviorSubject, combineLatest, Observable, of } from 'rxjs';
|
|
|
-import { filter, map } from 'rxjs/operators';
|
|
|
+import { BehaviorSubject, combineLatest, mergeAll, Observable, of } from 'rxjs';
|
|
|
+import { map, tap } from 'rxjs/operators';
|
|
|
|
|
|
import { ActionBarLocationId } from '../../../common/component-registry-types';
|
|
|
import { DataService } from '../../../data/providers/data.service';
|
|
|
-import { ActionBarItem } from '../../../providers/nav-builder/nav-builder-types';
|
|
|
+import {
|
|
|
+ ActionBarButtonState,
|
|
|
+ ActionBarContext,
|
|
|
+ ActionBarItem,
|
|
|
+} from '../../../providers/nav-builder/nav-builder-types';
|
|
|
import { NavBuilderService } from '../../../providers/nav-builder/nav-builder.service';
|
|
|
import { NotificationService } from '../../../providers/notification/notification.service';
|
|
|
|
|
|
@@ -30,6 +35,7 @@ export class ActionBarItemsComponent implements OnInit, OnChanges {
|
|
|
locationId: ActionBarLocationId;
|
|
|
|
|
|
items$: Observable<ActionBarItem[]>;
|
|
|
+ buttonStates: { [id: string]: Observable<ActionBarButtonState> } = {};
|
|
|
private locationId$ = new BehaviorSubject<string>('');
|
|
|
|
|
|
constructor(
|
|
|
@@ -37,11 +43,25 @@ export class ActionBarItemsComponent implements OnInit, OnChanges {
|
|
|
private route: ActivatedRoute,
|
|
|
private dataService: DataService,
|
|
|
private notificationService: NotificationService,
|
|
|
+ private injector: Injector,
|
|
|
) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
this.items$ = combineLatest(this.navBuilderService.actionBarConfig$, this.locationId$).pipe(
|
|
|
map(([items, locationId]) => items.filter(config => config.locationId === locationId)),
|
|
|
+ tap(items => {
|
|
|
+ const context = this.createContext();
|
|
|
+ for (const item of items) {
|
|
|
+ const buttonState$ =
|
|
|
+ typeof item.buttonState === 'function'
|
|
|
+ ? item.buttonState(context)
|
|
|
+ : of({
|
|
|
+ disabled: false,
|
|
|
+ visible: true,
|
|
|
+ });
|
|
|
+ this.buttonStates[item.id] = buttonState$;
|
|
|
+ }
|
|
|
+ }),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@@ -53,11 +73,7 @@ export class ActionBarItemsComponent implements OnInit, OnChanges {
|
|
|
|
|
|
handleClick(event: MouseEvent, item: ActionBarItem) {
|
|
|
if (typeof item.onClick === 'function') {
|
|
|
- item.onClick(event, {
|
|
|
- route: this.route,
|
|
|
- dataService: this.dataService,
|
|
|
- notificationService: this.notificationService,
|
|
|
- });
|
|
|
+ item.onClick(event, this.createContext());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -90,4 +106,13 @@ export class ActionBarItemsComponent implements OnInit, OnChanges {
|
|
|
return '';
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private createContext(): ActionBarContext {
|
|
|
+ return {
|
|
|
+ route: this.route,
|
|
|
+ injector: this.injector,
|
|
|
+ dataService: this.dataService,
|
|
|
+ notificationService: this.notificationService,
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|