]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
c3ff2a7ff717e5c1ddd3b38438b6cce7545b497d
[ceph.git] /
1 import { Component, Input, OnInit } from '@angular/core';
2 import { FormControl } from '@angular/forms';
3
4 import { NgbCalendar, NgbDateStruct, NgbTimeStruct } from '@ng-bootstrap/ng-bootstrap';
5 import * as moment from 'moment';
6 import { Subscription } from 'rxjs';
7
8 @Component({
9   selector: 'cd-date-time-picker',
10   templateUrl: './date-time-picker.component.html',
11   styleUrls: ['./date-time-picker.component.scss']
12 })
13 export class DateTimePickerComponent implements OnInit {
14   @Input()
15   control: FormControl;
16
17   @Input()
18   hasSeconds = true;
19
20   @Input()
21   hasTime = true;
22
23   format: string;
24   minDate: NgbDateStruct;
25   date: NgbDateStruct;
26   time: NgbTimeStruct;
27
28   sub: Subscription;
29
30   constructor(private calendar: NgbCalendar) {}
31
32   ngOnInit() {
33     this.minDate = this.calendar.getToday();
34     if (!this.hasTime) {
35       this.format = 'YYYY-MM-DD';
36     } else if (this.hasSeconds) {
37       this.format = 'YYYY-MM-DD HH:mm:ss';
38     } else {
39       this.format = 'YYYY-MM-DD HH:mm';
40     }
41
42     let mom = moment(this.control?.value, this.format);
43
44     if (!mom.isValid() || mom.isBefore(moment())) {
45       mom = moment();
46     }
47
48     this.date = { year: mom.year(), month: mom.month() + 1, day: mom.date() };
49     this.time = { hour: mom.hour(), minute: mom.minute(), second: mom.second() };
50
51     this.onModelChange();
52   }
53
54   onModelChange() {
55     if (this.date) {
56       const datetime = Object.assign({}, this.date, this.time);
57       datetime.month--;
58       setTimeout(() => {
59         this.control.setValue(moment(datetime).format(this.format));
60       });
61     } else {
62       setTimeout(() => {
63         this.control.setValue('');
64       });
65     }
66   }
67 }