Skip to content

Commit

Permalink
fix reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Polyakov committed Mar 20, 2024
1 parent 781f4c4 commit ad0d1c5
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/components/shared/Widget/Grid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ const DEFAULT_COLS: LayoutConfig = {
[BreakpointKey.xss]: 4,
};
const EMPTY_LAYOUTS: LayoutConfig = {
[BreakpointKey.lg]: [],
[BreakpointKey.md]: [],
[BreakpointKey.sm]: [],
[BreakpointKey.xs]: [],
[BreakpointKey.xss]: [],
};
function findWidgetInLayout(layout: Nullable<Layout>, widgetId: string) {
return layout?.find((widget: LayoutWidget) => widget.i === widgetId);
}
Expand Down Expand Up @@ -130,7 +138,7 @@ export default class WidgetsGrid extends Vue {
private updateResponsiveLayouts = debounce(this.saveLayouts, 250);
public layouts: ResponsiveLayouts = {};
public layouts: ResponsiveLayouts = cloneDeep(EMPTY_LAYOUTS);
@Watch('layouts', { deep: true })
private onLayoutsUpdate(): void {
if (this.responsiveLayout && this.shouldUpdate) {
Expand Down Expand Up @@ -177,14 +185,23 @@ export default class WidgetsGrid extends Vue {
created(): void {
// save initial model for reset availability
this.defaultValue = cloneDeep(this.value);
this.init();
}
private init(): void {
this.layout = [];
this.layouts = cloneDeep(EMPTY_LAYOUTS);
const hasLayoutsInStorage = layoutsStorage.get(this.gridId);
const storedLayouts = layoutsStorage.get(this.gridId);
if (hasLayoutsInStorage) {
this.loadLayoutsFromStorage();
if (storedLayouts) {
this.layouts = JSON.parse(storedLayouts);
} else {
this.loadLayoutsFromConfig();
this.layouts = cloneDeep(this.defaultLayouts);
this.updateLayoutWidgetsByModel(this.defaultValue, {}, false); // don't save initial layout
}
this.updateWidgetsModelByLayout();
}
private saveLayouts(layouts: ResponsiveLayouts, save = true): void {
Expand All @@ -207,24 +224,9 @@ export default class WidgetsGrid extends Vue {
}
}
private loadLayoutsFromStorage(): void {
const storedLayouts = layoutsStorage.get(this.gridId);
if (!storedLayouts) return;
this.layouts = JSON.parse(storedLayouts);
this.updateWidgetsModelByLayout();
}
private loadLayoutsFromConfig(): void {
this.layouts = cloneDeep(this.defaultLayouts);
this.updateLayoutWidgetsByModel(this.defaultValue, {}, false); // don't save initial layout
this.updateWidgetsModelByLayout();
}
reset(): void {
this.clearLayoutsFromStorage();
this.loadLayoutsFromConfig();
this.init();
}
onBreakpointChanged(newBreakpoint: BreakpointKey): void {
Expand Down

0 comments on commit ad0d1c5

Please sign in to comment.