]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
4841d2ed92d0178f73ab407b5c2a43e51f48341d
[ceph.git] /
1 import { Component, Input, OnInit } from '@angular/core';
2 import { UntypedFormControl } from '@angular/forms';
3
4 import { NgbCalendar, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
5 import 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: UntypedFormControl;
16
17   @Input()
18   hasSeconds = true;
19
20   @Input()
21   hasTime = true;
22
23   @Input()
24   name = '';
25
26   @Input()
27   helperText = '';
28
29   @Input()
30   disabled = false;
31
32   format: string;
33   minDate: NgbDateStruct;
34   datetime: {
35     date: any;
36     time: string;
37     ampm: string;
38   };
39   date: { [key: number]: string }[] = [];
40   time: string;
41   ampm: string;
42
43   sub: Subscription;
44
45   constructor(private calendar: NgbCalendar) {}
46
47   ngOnInit() {
48     this.minDate = this.calendar.getToday();
49     if (!this.hasTime) {
50       this.format = 'YYYY-MM-DD';
51     } else if (this.hasSeconds) {
52       this.format = 'YYYY-MM-DD HH:mm:ss';
53     } else {
54       this.format = 'YYYY-MM-DD HH:mm';
55     }
56
57     let mom = moment(this.control?.value, this.format);
58
59     if (!mom.isValid() || mom.isBefore(moment())) {
60       mom = moment();
61     }
62
63     this.date.push(mom.format('YYYY-MM-DD'));
64     const time = mom.format('HH:mm:ss');
65     this.time = mom.format('hh:mm');
66     this.ampm = mom.hour() >= 12 ? 'PM' : 'AM';
67
68     this.datetime = {
69       date: this.date[0],
70       time: time,
71       ampm: this.ampm
72     };
73
74     this.onModelChange();
75   }
76
77   onModelChange(event?: any) {
78     if (event) {
79       if (Array.isArray(event)) {
80         this.datetime.date = moment(event[0]).format('YYYY-MM-DD');
81       } else if (event && ['AM', 'PM'].includes(event)) {
82         const initialMoment = moment(this.datetime.time, 'hh:mm:ss A');
83         const updatedMoment = initialMoment.set(
84           'hour',
85           (initialMoment.hour() % 12) + (event === 'PM' ? 12 : 0)
86         );
87         this.datetime.time = moment(updatedMoment).format('HH:mm:ss');
88         this.datetime.ampm = event;
89       } else {
90         const time = event;
91         this.datetime.time = moment(`${this.datetime.date} ${time} ${this.datetime.ampm}`).format(
92           'HH:mm:ss'
93         );
94       }
95     }
96     if (this.datetime) {
97       const datetime = moment(`${this.datetime.date} ${this.datetime.time}`).format(this.format);
98
99       setTimeout(() => {
100         this.control.setValue(datetime);
101       });
102     } else {
103       setTimeout(() => {
104         this.control.setValue('');
105       });
106     }
107   }
108 }