between-date.component.ts 1.63 KB
Newer Older
1
2
3
import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { FormControl } from '@angular/forms';

4
import { Criterion, BetweenCriterion } from '../../../store/model';
5
6
7
8
9
10
11
12
13
14
15
16
17
18

@Component({
    selector: 'app-between-date',
    templateUrl: 'between-date.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class BetweenDateComponent {
    @Input() id: number;
    @Input() operator: string;
    @Input() label: string;
    @Input()
    set criterion(criterion: Criterion) {
        this.getDefault(criterion);
    }
Tifenn Guillas's avatar
Tifenn Guillas committed
19
    @Output() addCriterion: EventEmitter<BetweenCriterion> = new EventEmitter();
Tifenn Guillas's avatar
Tifenn Guillas committed
20
    @Output() deleteCriterion: EventEmitter<number> = new EventEmitter();
21
22
23

    field = new FormControl('');

Tifenn Guillas's avatar
Tifenn Guillas committed
24
    emitAdd(): void {
25
26
        const dateMin = this.getDateString(this.field.value[0]);
        const dateMax = this.getDateString(this.field.value[1]);
27
        const fd = new BetweenCriterion(this.id, dateMin, dateMax);
Tifenn Guillas's avatar
Tifenn Guillas committed
28
        this.addCriterion.emit(fd);
29
30
    }

Tifenn Guillas's avatar
Tifenn Guillas committed
31
    emitDelete(): void {
Tifenn Guillas's avatar
Tifenn Guillas committed
32
        this.deleteCriterion.emit(this.id);
33
34
    }

35
36
37
38
39
    getDefault(criterion: Criterion): void {
        if (!criterion) {
            this.field.setValue('');
            this.field.enable();
        } else {
40
            const c = criterion as BetweenCriterion;
41
42
43
44
45
46
47
48
49
50
51
            this.field.setValue([new Date(c.min), new Date(c.max)]);
            this.field.disable();
        }
    }

    getDateString(date: Date): string {
        const month = ('0' + (date.getMonth() + 1)).slice(-2);
        const day = ('0' + (date.getDate())).slice(-2);
        return date.getFullYear() + '-' + month + '-' + day;
    }
}