|
| 1 | +import { Component, ContentChild, Pipe, PipeTransform, OnInit, Input, Directive, TemplateRef } from '@angular/core'; |
| 2 | +import { IgxInputGroupComponent } from '../input-group/input-group.component'; |
| 3 | +import { IgxInputGroupBase } from '../input-group/input-group.common'; |
| 4 | +import { NgControl } from '@angular/forms'; |
| 5 | +import { IgxDateTimeEditorDirective } from '../directives/date-time-editor'; |
| 6 | +import { formatDate } from '@angular/common'; |
| 7 | + |
| 8 | +export interface DateRange { |
| 9 | + start: Date; |
| 10 | + end: Date; |
| 11 | +} |
| 12 | + |
| 13 | +/** @hidden @internal */ |
| 14 | +@Pipe({ name: 'dateRange' }) |
| 15 | +export class DateRangeFormatPipe implements PipeTransform { |
| 16 | + transform(values: DateRange, inputFormat?: string, locale?: string): string { |
| 17 | + if (!values) { |
| 18 | + return ''; |
| 19 | + } |
| 20 | + const { start, end } = values; |
| 21 | + // TODO: move default locale from IgxDateTimeEditorDirective to its commons file/use displayFormat |
| 22 | + const startDate = inputFormat ? formatDate(start, inputFormat, locale || 'en') : start?.toLocaleDateString(); |
| 23 | + const endDate = inputFormat ? formatDate(end, inputFormat, locale || 'en') : end?.toLocaleDateString(); |
| 24 | + let formatted; |
| 25 | + if (start) { |
| 26 | + formatted = `${startDate} - `; |
| 27 | + if (end) { |
| 28 | + formatted += endDate; |
| 29 | + } |
| 30 | + } else if (end) { |
| 31 | + formatted = `${endDate} - `; |
| 32 | + if (start) { |
| 33 | + formatted += startDate; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // TODO: no need to set format twice |
| 38 | + return formatted ? formatted : ''; |
| 39 | + } |
| 40 | + |
| 41 | + parse(value: string): DateRange { |
| 42 | + const values = value.trim().split(/ - /g); |
| 43 | + return { |
| 44 | + start: values[0] && new Date(Date.parse(values[0])), |
| 45 | + end: values[1] && new Date(Date.parse(values[1])) |
| 46 | + }; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** @hidden @internal */ |
| 51 | +@Component({ |
| 52 | + template: ``, |
| 53 | + selector: `igx-date-range-base`, |
| 54 | + providers: [{ provide: IgxInputGroupBase, useExisting: IgxDateRangeBaseComponent }] |
| 55 | +}) |
| 56 | +class IgxDateRangeBaseComponent extends IgxInputGroupComponent implements OnInit { |
| 57 | + @ContentChild(NgControl) |
| 58 | + protected ngControl: NgControl; |
| 59 | + |
| 60 | + @ContentChild(IgxDateTimeEditorDirective) |
| 61 | + public dateTimeEditor: IgxDateTimeEditorDirective; |
| 62 | + |
| 63 | + /** @hidden @internal */ |
| 64 | + public ngOnInit(): void { |
| 65 | + // this.input.nativeElement.readOnly = true; |
| 66 | + } |
| 67 | + |
| 68 | + /** @hidden @internal */ |
| 69 | + public get nativeElement() { |
| 70 | + return this.element.nativeElement; |
| 71 | + } |
| 72 | + |
| 73 | + /** @hidden @internal */ |
| 74 | + public setFocus(): void { |
| 75 | + this.input.focus(); |
| 76 | + } |
| 77 | + |
| 78 | + /** @hidden @internal */ |
| 79 | + public updateInput(value: Date) { |
| 80 | + if (this.ngControl) { |
| 81 | + this.ngControl.control.setValue(value); |
| 82 | + } |
| 83 | + |
| 84 | + this.dateTimeEditor.value = value; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +@Component({ |
| 89 | + selector: 'igx-date-start', |
| 90 | + templateUrl: '../input-group/input-group.component.html', |
| 91 | + providers: [{ provide: IgxInputGroupBase, useExisting: IgxDateStartComponent }] |
| 92 | +}) |
| 93 | +export class IgxDateStartComponent extends IgxDateRangeBaseComponent { } |
| 94 | + |
| 95 | +@Component({ |
| 96 | + selector: 'igx-date-end', |
| 97 | + templateUrl: '../input-group/input-group.component.html', |
| 98 | + providers: [{ provide: IgxInputGroupBase, useExisting: IgxDateEndComponent }] |
| 99 | +}) |
| 100 | +export class IgxDateEndComponent extends IgxDateRangeBaseComponent { } |
| 101 | + |
| 102 | +@Component({ |
| 103 | + selector: 'igx-date-single', |
| 104 | + templateUrl: 'igx-date-range-inputs.common.html', |
| 105 | + providers: [{ provide: IgxInputGroupBase, useExisting: IgxDateSingleComponent }] |
| 106 | +}) |
| 107 | +export class IgxDateSingleComponent extends IgxDateRangeBaseComponent { |
| 108 | + public updateInput(value: any, inputFormat?: string, locale?: string) { |
| 109 | + value = new DateRangeFormatPipe().transform(value, inputFormat, locale); |
| 110 | + if (this.ngControl) { |
| 111 | + this.ngControl.control.setValue(value); |
| 112 | + } else { |
| 113 | + this.input.value = value; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +@Directive({ |
| 119 | + selector: '[igxDateRangePrefix]', |
| 120 | + exportAs: 'igxDateRangePrefix', |
| 121 | +}) |
| 122 | +export class IgxDateRangePrefixDirective { |
| 123 | + constructor(public templateRef: TemplateRef<any>) { } |
| 124 | +} |
| 125 | + |
| 126 | +@Directive({ |
| 127 | + selector: '[igxDateRangeSuffix]', |
| 128 | + exportAs: 'igxDateRangeSuffix', |
| 129 | +}) |
| 130 | +export class IgxDateRangeSuffixDirective { |
| 131 | + constructor(public templateRef: TemplateRef<any>) { } |
| 132 | +} |
0 commit comments