114 lines
2.5 KiB
TypeScript
Raw Normal View History

2018-06-07 18:21:56 +09:00
import {
Component,
Input,
EventEmitter,
Output,
ViewChild,
OnChanges,
SimpleChanges
} from '@angular/core';
2018-06-01 19:27:27 +09:00
2018-06-07 18:21:56 +09:00
import {
Observable,
of
} from 'rxjs';
import { Store } from '@ngrx/store';
import {
catchError,
map,
tap,
take
} from 'rxjs/operators';
2018-06-07 15:24:55 +09:00
2018-06-07 18:21:56 +09:00
import { Target } from '@overflow/commons-typescript/model/target';
2018-06-06 19:36:51 +09:00
import { TargetService } from '../../service/target.service';
2018-06-07 18:24:23 +09:00
2018-06-07 18:21:56 +09:00
import { Paginator } from 'primeng/primeng';
2018-06-07 18:24:23 +09:00
import { Page, PageParams} from '@overflow/commons-typescript';
2018-06-11 20:28:05 +09:00
import {PlatformLocation} from '@angular/common';
2018-04-16 19:45:10 +09:00
2018-04-16 17:28:39 +09:00
@Component({
2018-06-07 15:24:55 +09:00
selector: 'of-target-list',
templateUrl: './list.component.html',
2018-04-16 17:28:39 +09:00
})
2018-06-12 14:45:21 +09:00
export class ListComponent implements OnChanges {
2018-06-07 18:21:56 +09:00
@Input() probeID;
@Input() pageIdx;
@Output() pageChange = new EventEmitter<number>();
2018-06-11 16:33:34 +09:00
@Output() targetSelect = new EventEmitter<Target>();
2018-06-07 18:21:56 +09:00
@ViewChild('paginator') paginator: Paginator;
2018-04-16 17:28:39 +09:00
2018-06-07 18:21:56 +09:00
targetPage: Page<Target>;
2018-06-07 15:24:55 +09:00
pending$: Observable<boolean>;
error$: Observable<any>;
2018-06-11 22:31:42 +09:00
countPerPage: number;
2018-06-07 15:24:55 +09:00
constructor(
private store: Store<any>,
private targetService: TargetService,
2018-06-11 20:28:05 +09:00
private location: PlatformLocation
2018-06-07 15:24:55 +09:00
) {
2018-06-11 22:31:42 +09:00
this.countPerPage = 2;
2018-06-07 15:24:55 +09:00
}
2018-06-07 18:21:56 +09:00
ngOnChanges(changes: SimpleChanges): void {
2018-06-11 16:33:34 +09:00
// console.log(changes);
2018-06-11 22:02:27 +09:00
if (changes['pageIdx']) {
2018-06-11 16:33:34 +09:00
// console.log(this.pageIdx);
2018-06-11 12:27:25 +09:00
this.getTargetList();
2018-06-11 22:02:27 +09:00
// const p: number = this.pageIdx - 1;
// this.paginator.changePage(this.pageIdx);
2018-06-07 18:21:56 +09:00
}
}
2018-06-11 12:27:25 +09:00
private getTargetList() {
if (this.pageIdx <= 0 || this.pageIdx === undefined || this.pageIdx === null || isNaN(this.pageIdx)) {
2018-06-11 22:02:27 +09:00
this.pageIdx = 1;
2018-06-11 12:27:25 +09:00
}
2018-06-11 22:02:27 +09:00
const p: number = this.pageIdx - 1;
2018-06-11 12:27:25 +09:00
const pageParams: PageParams = {
2018-06-11 22:02:27 +09:00
pageNo: p,
2018-06-11 22:31:42 +09:00
countPerPage: this.countPerPage,
2018-06-11 12:27:25 +09:00
sortCol: 'id',
sortDirection: 'descending',
};
2018-06-07 18:21:56 +09:00
this.targetService.readAllByProbeID(this.probeID, pageParams)
2018-06-07 15:24:55 +09:00
.pipe(
tap(() => {
this.pending$ = of(true);
}),
map((page: Page<Target>) => {
2018-06-07 18:21:56 +09:00
this.targetPage = page;
2018-06-07 15:24:55 +09:00
}),
catchError(err => {
console.log(err);
return err;
}),
tap(() => {
this.pending$ = of(false);
}),
take(1),
).subscribe();
}
onRowSelect(event) {
2018-06-11 16:33:34 +09:00
this.targetSelect.emit(event.data);
2018-06-07 15:24:55 +09:00
}
onAddSensor(target: Target) {
// this.target = target;
// this.sensorSettingDisplay = true;
}
2018-06-07 18:21:56 +09:00
onPaginate(e) {
2018-06-11 20:28:05 +09:00
// e.changePage();
2018-06-11 22:02:27 +09:00
this.pageChange.emit(Number(e.page));
2018-06-07 15:24:55 +09:00
}
2018-04-16 17:28:39 +09:00
}