-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Add support for dashboard widgets
Relates to #334
- Loading branch information
1 parent
70e14f2
commit aa835e8
Showing
44 changed files
with
729 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/admin-ui/src/lib/core/src/providers/dashboard-widget/dashboard-widget-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Type } from '@angular/core'; | ||
|
||
export interface DashboardWidgetConfig { | ||
/** | ||
* Used to specify the widget component. Supports both eager- and lazy-loading. | ||
* @example | ||
* ```TypeScript | ||
* // eager-loading | ||
* loadComponent: () => MyWidgetComponent, | ||
* | ||
* // lazy-loading | ||
* loadComponent: () => import('./path-to/widget.component').then(m => m.MyWidgetComponent), | ||
* ``` | ||
*/ | ||
loadComponent: () => Promise<Type<any>> | Type<any>; | ||
/** | ||
* The title of the widget. Can be a translation token as it will get passed | ||
* through the `translate` pipe. | ||
*/ | ||
title?: string; | ||
/** | ||
* The width of the widget, in terms of a Bootstrap-style 12-column grid. | ||
*/ | ||
width: 3 | 4 | 6 | 12; | ||
/** | ||
* If set, the widget will only be displayed if the current user has all the | ||
* specified permissions. | ||
*/ | ||
requiresPermissions?: string[]; | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/admin-ui/src/lib/core/src/providers/dashboard-widget/dashboard-widget.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils'; | ||
|
||
import { DashboardWidgetConfig } from './dashboard-widget-types'; | ||
|
||
/** | ||
* Registers a component to be used as a dashboard widget. | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class DashboardWidgetService { | ||
private registry = new Map<string, DashboardWidgetConfig>(); | ||
private layout: ReadonlyArray<string> = []; | ||
|
||
registerWidget(id: string, config: DashboardWidgetConfig) { | ||
if (this.registry.has(id)) { | ||
throw new Error(`A dashboard widget with the id "${id}" already exists`); | ||
} | ||
|
||
this.registry.set(id, config); | ||
} | ||
|
||
setDefaultLayout(ids: string[] | ReadonlyArray<string>) { | ||
this.layout = ids; | ||
} | ||
|
||
getDefaultLayout(): ReadonlyArray<string> { | ||
return this.layout; | ||
} | ||
|
||
getWidgets(): DashboardWidgetConfig[] { | ||
return this.layout | ||
.map(id => { | ||
const config = this.registry.get(id); | ||
if (!config) { | ||
// tslint:disable-next-line:no-console | ||
console.error( | ||
`No dashboard widget was found with the id "${id}"\nAvailable ids: ${[ | ||
...this.registry.keys(), | ||
] | ||
.map(_id => `"${_id}"`) | ||
.join(', ')}`, | ||
); | ||
} | ||
return config; | ||
}) | ||
.filter(notNullOrUndefined); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/admin-ui/src/lib/core/src/providers/dashboard-widget/register-dashboard-widget.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { APP_INITIALIZER, FactoryProvider } from '@angular/core'; | ||
|
||
import { DashboardWidgetConfig } from './dashboard-widget-types'; | ||
import { DashboardWidgetService } from './dashboard-widget.service'; | ||
|
||
export function registerDashboardWidget(id: string, config: DashboardWidgetConfig): FactoryProvider { | ||
return { | ||
provide: APP_INITIALIZER, | ||
multi: true, | ||
useFactory: (dashboardWidgetService: DashboardWidgetService) => () => { | ||
dashboardWidgetService.registerWidget(id, config); | ||
}, | ||
deps: [DashboardWidgetService], | ||
}; | ||
} | ||
|
||
export function setDashboardWidgetLayout(ids: string[] | ReadonlyArray<string>): FactoryProvider { | ||
return { | ||
provide: APP_INITIALIZER, | ||
multi: true, | ||
useFactory: (dashboardWidgetService: DashboardWidgetService) => () => { | ||
dashboardWidgetService.setDefaultLayout(ids); | ||
}, | ||
deps: [DashboardWidgetService], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.