[TASK] Migrate AdminPanel modules to TypeScript
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Resources / Private / TypeScript / Modules / Preview.ts
1 namespace TYPO3 {
2 export class Preview {
3 private readonly dateField: HTMLInputElement = null;
4 private readonly timeField: HTMLInputElement = null;
5 private readonly targetField: HTMLInputElement = null;
6
7 constructor() {
8 this.dateField = <HTMLInputElement>document.getElementById('preview_simulateDate-date-hr');
9 this.timeField = <HTMLInputElement>document.getElementById('preview_simulateDate-time-hr');
10 this.targetField = <HTMLInputElement>document.getElementById(this.dateField.dataset.target);
11
12 if (this.targetField.value) {
13 const cd = new Date(this.targetField.value);
14 this.dateField.value =
15 cd.getFullYear() + '-' + ((cd.getMonth() + 1) < 10 ? '0' : '')
16 + (cd.getMonth() + 1) + '-' + (cd.getDate() < 10 ? '0' : '') + cd.getDate();
17 this.timeField.value =
18 (cd.getHours() < 10 ? '0' : '') + cd.getHours() + ':'
19 + (cd.getMinutes() < 10 ? '0' : '') + cd.getMinutes();
20 }
21
22 this.dateField.addEventListener('change', this.updateDateField);
23 this.timeField.addEventListener('change', this.updateDateField);
24 }
25
26 private updateDateField = (): void => {
27 let dateVal = this.dateField.value;
28 let timeVal = this.timeField.value;
29 if (!dateVal && timeVal) {
30 let tempDate = new Date();
31 dateVal = tempDate.getFullYear() + '-' + (tempDate.getMonth() + 1) + '-' + tempDate.getDate();
32 }
33 if (dateVal && !timeVal) {
34 timeVal = '00:00';
35 }
36
37 if (!dateVal && !timeVal) {
38 this.targetField.value = '';
39 } else {
40 const stringDate = dateVal + ' ' + timeVal;
41 const date = new Date(stringDate);
42
43 this.targetField.value = date.toISOString();
44 }
45 }
46 }
47 }
48
49 ((): void => {
50 window.addEventListener(
51 'load',
52 () => new TYPO3.Preview(),
53 false
54 );
55 })();