create folder and device list components

refactor status-list to contain device and folder list
This commit is contained in:
Jesse Lucas 2020-03-11 16:00:59 -04:00
parent 031fd72dfd
commit 61b4de581d
19 changed files with 2045 additions and 153 deletions

1795
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,7 @@
"@angular/platform-browser": "~9.0.5", "@angular/platform-browser": "~9.0.5",
"@angular/platform-browser-dynamic": "~9.0.5", "@angular/platform-browser-dynamic": "~9.0.5",
"@angular/router": "~9.0.5", "@angular/router": "~9.0.5",
"component": "^1.1.0",
"rxjs": "~6.5.4", "rxjs": "~6.5.4",
"tslib": "^1.10.0", "tslib": "^1.10.0",
"zone.js": "~0.10.2" "zone.js": "~0.10.2"

View File

@ -1,14 +1,17 @@
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { MatTableModule } from '@angular/material/table';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { StatusListComponent } from './status-list/status-list.component'; import { StatusListComponent } from './status-list/status-list.component';
import { MatTableModule } from '@angular/material/table'; import { FolderListComponent } from './folder-list/folder-list.component';
import { MatPaginatorModule } from '@angular/material/paginator'; import { DeviceListComponent } from './device-list/device-list.component';
import { MatSortModule } from '@angular/material/sort';
import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { StatusToggleComponent } from './status-toggle/status-toggle.component'; import { StatusToggleComponent } from './status-toggle/status-toggle.component';
@ -16,6 +19,8 @@ import { StatusToggleComponent } from './status-toggle/status-toggle.component';
declarations: [ declarations: [
AppComponent, AppComponent,
StatusListComponent, StatusListComponent,
FolderListComponent,
DeviceListComponent,
StatusToggleComponent, StatusToggleComponent,
], ],
imports: [ imports: [

View File

@ -0,0 +1,79 @@
import { DataSource } from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { map } from 'rxjs/operators';
import { Observable, of as observableOf, merge } from 'rxjs';
import { Device } from '../device';
/**
* Data source for the DeviceList view. This class should
* encapsulate all logic for fetching and manipulating the displayed data
* (including sorting, pagination, and filtering).
*/
export class DeviceListDataSource extends DataSource<Device> {
data: Device[];
paginator: MatPaginator;
sort: MatSort;
constructor() {
super();
}
/**
* Connect this data source to the table. The table will only update when
* the returned stream emits new items.
* @returns A stream of the items to be rendered.
*/
connect(): Observable<Device[]> {
// Combine everything that affects the rendered data into one update
// st
const dataMutations = [
observableOf(this.data),
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(map(() => {
return this.getPagedData(this.getSortedData([...this.data]));
}));
}
/**
* Called when the table is being destroyed. Use this function, to clean up
* any open connections or free any held resources that were set up during connect.
*/
disconnect() { }
/**
* Paginate the data (client-side). If you're using server-side pagination,
* this would be replaced by requesting the appropriate data from the server.
*/
private getPagedData(data: Device[]) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
/**
* Sort the data (client-side). If you're using server-side sorting,
* this would be replaced by requesting the appropriate data from the server.
*/
private getSortedData(data: Device[]) {
if (!this.sort.active || this.sort.direction === '') {
return data;
}
return data.sort((a, b) => {
const isAsc = this.sort.direction === 'asc';
switch (this.sort.active) {
case 'name': return compare(a.name, b.name, isAsc);
case 'id': return compare(+a.id, +b.id, isAsc);
default: return 0;
}
});
}
}
function compare(a: string | number, b: string | number, isAsc: boolean) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

View File

@ -0,0 +1,22 @@
<div class="mat-elevation-z8">
<table mat-table class="full-width-table" matSort aria-label="Elements">
<!-- Id Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
<td mat-cell *matCellDef="let row">{{row.id}}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>

View File

@ -0,0 +1,3 @@
.full-width-table {
width: 100%;
}

View File

@ -0,0 +1,34 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { DeviceListComponent } from './device-list.component';
describe('DeviceListComponent', () => {
let component: DeviceListComponent;
let fixture: ComponentFixture<DeviceListComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [DeviceListComponent],
imports: [
NoopAnimationsModule,
MatPaginatorModule,
MatSortModule,
MatTableModule,
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DeviceListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should compile', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,41 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { DeviceListDataSource } from './device-list-datasource';
import { Device } from '../device';
import { SystemConfigService } from '../system-config.service';
@Component({
selector: 'app-device-list',
templateUrl: './device-list.component.html',
styleUrls: ['./device-list.component.scss']
})
export class DeviceListComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatTable) table: MatTable<Device>;
dataSource: DeviceListDataSource;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['id', 'name'];
constructor(private systemConfigService: SystemConfigService) { };
ngOnInit() {
this.dataSource = new DeviceListDataSource();
this.systemConfigService.getDevices().subscribe(
data => {
this.dataSource.data = data;
}
);
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.table.dataSource = this.dataSource;
}
}

View File

@ -7,12 +7,12 @@ import { Observable, of as observableOf, merge } from 'rxjs';
import { Folder } from '../folder'; import { Folder } from '../folder';
/** /**
* Data source for the StatusList view. This class should * Data source for the FolderList view. This class should
* encapsulate all logic for fetching and manipulating the displayed data * encapsulate all logic for fetching and manipulating the displayed data
* (including sorting, pagination, and filtering). * (including sorting, pagination, and filtering).
*/ */
export class StatusListFolderDataSource extends DataSource<any> { export class FolderListDataSource extends DataSource<Folder> {
data: any[]; data: Folder[];
paginator: MatPaginator; paginator: MatPaginator;
sort: MatSort; sort: MatSort;
@ -27,7 +27,7 @@ export class StatusListFolderDataSource extends DataSource<any> {
*/ */
connect(): Observable<Folder[]> { connect(): Observable<Folder[]> {
// Combine everything that affects the rendered data into one update // Combine everything that affects the rendered data into one update
// stream for the data-table to consume. // st
const dataMutations = [ const dataMutations = [
observableOf(this.data), observableOf(this.data),
this.paginator.page, this.paginator.page,

View File

@ -0,0 +1,22 @@
<div class="mat-elevation-z8">
<table mat-table class="full-width-table" matSort aria-label="Elements">
<!-- Id Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
<td mat-cell *matCellDef="let row">{{row.id}}</td>
</ng-container>
<!-- Label Column -->
<ng-container matColumnDef="label">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Label</th>
<td mat-cell *matCellDef="let row">{{row.label}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>

View File

@ -0,0 +1,3 @@
.full-width-table {
width: 100%;
}

View File

@ -0,0 +1,34 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { FolderListComponent } from './folder-list.component';
describe('FolderListComponent', () => {
let component: FolderListComponent;
let fixture: ComponentFixture<FolderListComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FolderListComponent],
imports: [
NoopAnimationsModule,
MatPaginatorModule,
MatSortModule,
MatTableModule,
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FolderListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should compile', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,41 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { FolderListDataSource } from './folder-list-datasource';
import { Folder } from '../folder';
import { SystemConfigService } from '../system-config.service';
@Component({
selector: 'app-folder-list',
templateUrl: './folder-list.component.html',
styleUrls: ['./folder-list.component.scss']
})
export class FolderListComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatTable) table: MatTable<Folder>;
dataSource: FolderListDataSource;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['id', 'label'];
constructor(private systemConfigService: SystemConfigService) { };
ngOnInit() {
this.dataSource = new FolderListDataSource();
this.systemConfigService.getFolders().subscribe(
data => {
this.dataSource.data = data;
}
);
}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.table.dataSource = this.dataSource;
}
}

View File

@ -1,23 +1,3 @@
<app-status-toggle></app-status-toggle> <app-status-toggle></app-status-toggle>
<div class="mat-elevation-z8"> <app-folder-list></app-folder-list>
<table mat-table class="full-width-table" matSort aria-label="Elements"> <app-device-list></app-device-list>
<!-- Id Column -->
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Id</th>
<td mat-cell *matCellDef="let row">{{row.id}}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Label</th>
<td mat-cell *matCellDef="let row">{{row.label}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator [length]="dataSource?.data.length" [pageIndex]="0" [pageSize]="50"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>

View File

@ -1,3 +0,0 @@
.full-width-table {
width: 100%;
}

View File

@ -1,8 +1,4 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { StatusListComponent } from './status-list.component'; import { StatusListComponent } from './status-list.component';
@ -12,14 +8,9 @@ describe('StatusListComponent', () => {
beforeEach(async(() => { beforeEach(async(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [ StatusListComponent ], declarations: [StatusListComponent]
imports: [ })
NoopAnimationsModule, .compileComponents();
MatPaginatorModule,
MatSortModule,
MatTableModule,
]
}).compileComponents();
})); }));
beforeEach(() => { beforeEach(() => {
@ -28,7 +19,7 @@ describe('StatusListComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should compile', () => { it('should create', () => {
expect(component).toBeTruthy(); expect(component).toBeTruthy();
}); });
}); });

View File

@ -1,41 +1,15 @@
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { StatusListFolderDataSource } from './status-list-folder-datasource';
import { Folder } from '../folder';
import { SystemConfigService } from '../system-config.service';
@Component({ @Component({
selector: 'app-status-list', selector: 'app-status-list',
templateUrl: './status-list.component.html', templateUrl: './status-list.component.html',
styleUrls: ['./status-list.component.scss'] styleUrls: ['./status-list.component.scss']
}) })
export class StatusListComponent implements AfterViewInit, OnInit { export class StatusListComponent implements OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatTable) table: MatTable<Folder>;
dataSource: StatusListFolderDataSource;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ constructor() { }
displayedColumns = ['id', 'name'];
constructor(private systemConfigService: SystemConfigService) { }; ngOnInit(): void {
ngOnInit() {
this.dataSource = new StatusListFolderDataSource();
this.systemConfigService.getDevices().subscribe(
data => {
this.dataSource.data = data;
}
);
} }
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.table.dataSource = this.dataSource;
}
} }

View File

@ -1,4 +1,4 @@
<mat-button-toggle-group name="fontStyle" aria-label="Font Style"> <mat-button-toggle-group name="fontStyle" aria-label="Font Style">
<mat-button-toggle value="folders">Folders</mat-button-toggle> <mat-button-toggle value="folders" (click)="onSelect(toggleAction.Folders)">Folders</mat-button-toggle>
<mat-button-toggle value="devices">Devices</mat-button-toggle> <mat-button-toggle value=" devices" (click)="onSelect(toggleAction.Devices)">Devices</mat-button-toggle>
</mat-button-toggle-group> </mat-button-toggle-group>

View File

@ -1,15 +1,33 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
export enum ToggleAction {
Folders = 1,
Devices,
}
@Component({ @Component({
selector: 'app-status-toggle', selector: 'app-status-toggle',
templateUrl: './status-toggle.component.html', templateUrl: './status-toggle.component.html',
styleUrls: ['./status-toggle.component.scss'] styleUrls: ['./status-toggle.component.scss']
}) })
export class StatusToggleComponent implements OnInit {
export class StatusToggleComponent implements OnInit {
public toggleAction = ToggleAction
constructor() { } constructor() { }
ngOnInit(): void { ngOnInit(): void {
} }
onSelect(label: ToggleAction): void {
console.log("here?");
switch (label) {
case ToggleAction.Folders:
console.log("folder action");
break;
case ToggleAction.Devices:
console.log("Device action");
break;
}
}
} }