Browse Source

feat(admin-ui): Expose services to ActionBarItem onClick function

Relates to #247

BREAKING CHANGE: This relates to Admin UI extensions. The `onClick` function signature of any custom ActionBarItems has changed - the second parameter used to be the `ActivatedRoute` - it is now an object containing `ActivatedRoute` plus an instance of `DataService` and `NotificationService`.
Michael Bromley 6 years ago
parent
commit
e44d372bd1

+ 13 - 1
packages/admin-ui/src/app/core/providers/nav-builder/nav-builder-types.ts

@@ -1,6 +1,9 @@
 import { ActivatedRoute } from '@angular/router';
 import { Observable } from 'rxjs';
 
+import { DataService } from '../../../data/providers/data.service';
+import { NotificationService } from '../notification/notification.service';
+
 /**
  * A NavMenuItem is a menu item in the main (left-hand side) nav
  * bar.
@@ -27,6 +30,15 @@ export interface NavMenuSection {
     collapsedByDefault?: boolean;
 }
 
+/**
+ * Utilities available to the onClick handler of an ActionBarItem.
+ */
+export interface OnClickContext {
+    route: ActivatedRoute;
+    dataService: DataService;
+    notificationService: NotificationService;
+}
+
 /**
  * A button in the ActionBar area at the top of one of the list or detail views.
  */
@@ -35,7 +47,7 @@ export interface ActionBarItem {
     label: string;
     locationId: string;
     disabled?: Observable<boolean>;
-    onClick?: (event: MouseEvent, route: ActivatedRoute) => void;
+    onClick?: (event: MouseEvent, context: OnClickContext) => void;
     routerLink?: RouterLinkDefinition;
     buttonColor?: 'primary' | 'success' | 'warning';
     buttonStyle?: 'solid' | 'outline' | 'link';

+ 13 - 2
packages/admin-ui/src/app/shared/components/action-bar-items/action-bar-items.component.ts

@@ -14,6 +14,8 @@ import { assertNever } from 'shared/shared-utils';
 
 import { ActionBarItem } from '../../../core/providers/nav-builder/nav-builder-types';
 import { NavBuilderService } from '../../../core/providers/nav-builder/nav-builder.service';
+import { NotificationService } from '../../../core/providers/notification/notification.service';
+import { DataService } from '../../../data/providers/data.service';
 
 @Component({
     selector: 'vdr-action-bar-items',
@@ -29,7 +31,12 @@ export class ActionBarItemsComponent implements OnInit, OnChanges {
     items$: Observable<ActionBarItem[]>;
     private locationId$ = new BehaviorSubject<string>('');
 
-    constructor(private navBuilderService: NavBuilderService, private route: ActivatedRoute) {}
+    constructor(
+        private navBuilderService: NavBuilderService,
+        private route: ActivatedRoute,
+        private dataService: DataService,
+        private notificationService: NotificationService,
+    ) {}
 
     ngOnInit() {
         this.items$ = combineLatest(this.navBuilderService.actionBarConfig$, this.locationId$).pipe(
@@ -45,7 +52,11 @@ export class ActionBarItemsComponent implements OnInit, OnChanges {
 
     handleClick(event: MouseEvent, item: ActionBarItem) {
         if (typeof item.onClick === 'function') {
-            item.onClick(event, this.route);
+            item.onClick(event, {
+                route: this.route,
+                dataService: this.dataService,
+                notificationService: this.notificationService,
+            });
         }
     }