Update dashboard layout

This commit is contained in:
2019-10-07 19:50:09 -04:00
parent e9c0672006
commit 6fd416b7c6
3 changed files with 32 additions and 24 deletions

View File

@@ -10,7 +10,7 @@
</button> </button>
</header> </header>
<gridster class="content" [options]="options"> <gridster class="content" [options]="options">
<gridster-item [item]="dashboard[0]"> <gridster-item [item]="dashboardLayout.layout[0]">
<div class="dashboard-item"> <div class="dashboard-item">
<div class="dashboard-item-header"> <div class="dashboard-item-header">
Weather Weather
@@ -20,7 +20,7 @@
</div> </div>
</div> </div>
</gridster-item> </gridster-item>
<gridster-item [item]="dashboard[1]"> <gridster-item [item]="dashboardLayout.layout[1]">
<div class="dashboard-item"> <div class="dashboard-item">
<div class="dashboard-item-header"> <div class="dashboard-item-header">
Laundry Laundry
@@ -30,7 +30,7 @@
</div> </div>
</div> </div>
</gridster-item> </gridster-item>
<gridster-item [item]="dashboard[2]"> <gridster-item [item]="dashboardLayout.layout[2]">
<div class="dashboard-item"> <div class="dashboard-item">
<div class="dashboard-item-header"> <div class="dashboard-item-header">
Pressure Trend Pressure Trend

View File

@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { GridsterConfig, GridsterItem, GridsterItemComponentInterface } from 'angular-gridster2'; import { GridsterConfig, GridsterItem, GridsterItemComponentInterface } from 'angular-gridster2';
import { DashboardLayout } from 'src/app/models/dashboard/dashboard-layout';
@Component({ @Component({
selector: 'app-dashboard', selector: 'app-dashboard',
@@ -8,9 +9,18 @@ import { GridsterConfig, GridsterItem, GridsterItemComponentInterface } from 'an
}) })
export class DashboardComponent implements OnInit { export class DashboardComponent implements OnInit {
public options: GridsterConfig; public options: GridsterConfig;
public dashboard: Array<GridsterItem>; public dashboardLayout: DashboardLayout;
public locked = true; public locked = true;
private defaultLayout: DashboardLayout = {
version: 1,
layout: [
{ cols: 3, rows: 2, y: 0, x: 0 },
{ cols: 2, rows: 2, y: 0, x: 3 },
{ cols: 2, rows: 2, y: 0, x: 5 }
]
};
constructor() { } constructor() { }
ngOnInit() { ngOnInit() {
@@ -36,14 +46,6 @@ export class DashboardComponent implements OnInit {
this.options.api.optionsChanged(); this.options.api.optionsChanged();
} }
removeItem(item: GridsterItem) {
this.dashboard.splice(this.dashboard.indexOf(item), 1);
}
addItem() {
this.dashboard.push({} as GridsterItem);
}
toggleLocked() { toggleLocked() {
this.locked = !this.locked; this.locked = !this.locked;
@@ -54,27 +56,27 @@ export class DashboardComponent implements OnInit {
} }
saveOptions() { saveOptions() {
localStorage.setItem('dashboard-layout', JSON.stringify(this.dashboard)); localStorage.setItem('dashboard-layout', JSON.stringify(this.dashboardLayout));
} }
loadOptions() { loadOptions() {
const savedLayout = localStorage.getItem('dashboard-layout'); const savedLayoutString = localStorage.getItem('dashboard-layout');
const defaultLayout = [ if (savedLayoutString == null) {
{ cols: 3, rows: 2, y: 0, x: 0 }, this.dashboardLayout = this.defaultLayout;
{ cols: 2, rows: 1, y: 0, x: 3 },
{ cols: 1, rows: 1, y: 0, x: 5 }
];
if (savedLayout == null) {
this.dashboard = defaultLayout;
return; return;
} }
try { try {
this.dashboard = JSON.parse(savedLayout); const savedLayout = JSON.parse(savedLayoutString) as DashboardLayout;
if (savedLayout.version === this.defaultLayout.version) {
this.dashboardLayout = savedLayout;
} else {
this.dashboardLayout = this.defaultLayout;
}
} catch (error) { } catch (error) {
this.dashboard = defaultLayout; this.dashboardLayout = this.defaultLayout;
} }
} }
} }

View File

@@ -0,0 +1,6 @@
import { GridsterItem } from 'angular-gridster2';
export class DashboardLayout {
version: number;
layout: Array<GridsterItem>;
}