|
|
@@ -1,9 +1,14 @@
|
|
|
+import { CdkDragDrop } from '@angular/cdk/drag-drop';
|
|
|
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
|
|
|
-import { DashboardWidgetConfig, DashboardWidgetService, DashboardWidgetWidth } from '@vendure/admin-ui/core';
|
|
|
+import {
|
|
|
+ DashboardWidgetConfig,
|
|
|
+ DashboardWidgetService,
|
|
|
+ DashboardWidgetWidth,
|
|
|
+ WidgetLayout,
|
|
|
+ WidgetLayoutDefinition,
|
|
|
+} from '@vendure/admin-ui/core';
|
|
|
import { assertNever } from '@vendure/common/lib/shared-utils';
|
|
|
|
|
|
-type WidgetLayout = Array<Array<{ width: DashboardWidgetWidth; config: DashboardWidgetConfig }>>;
|
|
|
-
|
|
|
@Component({
|
|
|
selector: 'vdr-dashboard',
|
|
|
templateUrl: './dashboard.component.html',
|
|
|
@@ -12,11 +17,14 @@ type WidgetLayout = Array<Array<{ width: DashboardWidgetWidth; config: Dashboard
|
|
|
})
|
|
|
export class DashboardComponent implements OnInit {
|
|
|
widgetLayout: WidgetLayout;
|
|
|
+ availableWidgetIds: string[];
|
|
|
+ private readonly deletionMarker = '__delete__';
|
|
|
|
|
|
constructor(private dashboardWidgetService: DashboardWidgetService) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
this.widgetLayout = this.dashboardWidgetService.getWidgetLayout();
|
|
|
+ this.availableWidgetIds = this.dashboardWidgetService.getAvailableIds();
|
|
|
}
|
|
|
|
|
|
getClassForWidth(width: DashboardWidgetWidth): string {
|
|
|
@@ -35,4 +43,63 @@ export class DashboardComponent implements OnInit {
|
|
|
assertNever(width);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ getSupportedWidths(config: DashboardWidgetConfig): DashboardWidgetWidth[] {
|
|
|
+ return config.supportedWidths || [3, 4, 6, 8, 12];
|
|
|
+ }
|
|
|
+
|
|
|
+ setWidgetWidth(widget: WidgetLayout[number][number], width: DashboardWidgetWidth) {
|
|
|
+ widget.width = width;
|
|
|
+ this.recalculateLayout();
|
|
|
+ }
|
|
|
+
|
|
|
+ trackRowItem(index: number, item: WidgetLayout[number][number]) {
|
|
|
+ return item.config;
|
|
|
+ }
|
|
|
+
|
|
|
+ addWidget(id: string) {
|
|
|
+ const config = this.dashboardWidgetService.getWidgetById(id);
|
|
|
+ if (config) {
|
|
|
+ const width = this.getSupportedWidths(config)[0];
|
|
|
+ const widget: WidgetLayout[number][number] = {
|
|
|
+ id,
|
|
|
+ config,
|
|
|
+ width,
|
|
|
+ };
|
|
|
+ let targetRow: WidgetLayout[number];
|
|
|
+ if (this.widgetLayout.length) {
|
|
|
+ targetRow = this.widgetLayout[this.widgetLayout.length - 1];
|
|
|
+ } else {
|
|
|
+ targetRow = [];
|
|
|
+ this.widgetLayout.push(targetRow);
|
|
|
+ }
|
|
|
+ targetRow.push(widget);
|
|
|
+ this.recalculateLayout();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ removeWidget(widget: WidgetLayout[number][number]) {
|
|
|
+ widget.id = this.deletionMarker;
|
|
|
+ this.recalculateLayout();
|
|
|
+ }
|
|
|
+
|
|
|
+ drop(event: CdkDragDrop<{ index: number }>) {
|
|
|
+ const previousLayoutRow = this.widgetLayout[event.previousContainer.data.index];
|
|
|
+ const newLayoutRow = this.widgetLayout[event.container.data.index];
|
|
|
+
|
|
|
+ previousLayoutRow.splice(event.previousIndex, 1);
|
|
|
+ newLayoutRow.splice(event.currentIndex, 0, event.item.data);
|
|
|
+ this.recalculateLayout();
|
|
|
+ }
|
|
|
+
|
|
|
+ private recalculateLayout() {
|
|
|
+ const flattened = this.widgetLayout
|
|
|
+ .reduce((flat, row) => [...flat, ...row], [])
|
|
|
+ .filter(item => item.id !== this.deletionMarker);
|
|
|
+ const newLayoutDef: WidgetLayoutDefinition = flattened.map(item => ({
|
|
|
+ id: item.id,
|
|
|
+ width: item.width,
|
|
|
+ }));
|
|
|
+ this.widgetLayout = this.dashboardWidgetService.getWidgetLayout(newLayoutDef);
|
|
|
+ }
|
|
|
}
|