mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
migration of dependency from moment to date-fns
This commit is contained in:
+21
-28
@@ -15,12 +15,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
|
import { Component, Inject, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
|
||||||
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
|
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
|
||||||
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE } from '@angular/material/core';
|
import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE, MatDateFormats } from '@angular/material/core';
|
||||||
import {
|
import {
|
||||||
MOMENT_DATE_FORMATS,
|
DateFnsUtils,
|
||||||
MomentDateAdapter,
|
|
||||||
UserPreferencesService,
|
UserPreferencesService,
|
||||||
UserPreferenceValues
|
UserPreferenceValues
|
||||||
} from '@alfresco/adf-core';
|
} from '@alfresco/adf-core';
|
||||||
@@ -29,26 +28,22 @@ import { SearchWidget } from '../../models/search-widget.interface';
|
|||||||
import { SearchWidgetSettings } from '../../models/search-widget-settings.interface';
|
import { SearchWidgetSettings } from '../../models/search-widget-settings.interface';
|
||||||
import { SearchQueryBuilderService } from '../../services/search-query-builder.service';
|
import { SearchQueryBuilderService } from '../../services/search-query-builder.service';
|
||||||
import { LiveErrorStateMatcher } from '../../forms/live-error-state-matcher';
|
import { LiveErrorStateMatcher } from '../../forms/live-error-state-matcher';
|
||||||
import { Moment } from 'moment';
|
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
import { takeUntil } from 'rxjs/operators';
|
import { takeUntil } from 'rxjs/operators';
|
||||||
|
import { endOfDay, endOfToday, format, isBefore, isValid, startOfDay } from 'date-fns';
|
||||||
|
import { DateFnsAdapter, MAT_DATE_FNS_FORMATS } from '@angular/material-date-fns-adapter';
|
||||||
|
|
||||||
export interface DateRangeValue {
|
export interface DateRangeValue {
|
||||||
from: string;
|
from: string;
|
||||||
to: string;
|
to: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare let moment: any;
|
|
||||||
|
|
||||||
const DEFAULT_FORMAT_DATE: string = 'DD/MM/YYYY';
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'adf-search-date-range',
|
selector: 'adf-search-date-range',
|
||||||
templateUrl: './search-date-range.component.html',
|
templateUrl: './search-date-range.component.html',
|
||||||
styleUrls: ['./search-date-range.component.scss'],
|
styleUrls: ['./search-date-range.component.scss'],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
|
{ provide: DateAdapter, useClass: DateFnsAdapter, deps: [MAT_DATE_LOCALE] },
|
||||||
{ provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }
|
{ provide: MAT_DATE_FORMATS, useValue: MAT_DATE_FNS_FORMATS }
|
||||||
],
|
],
|
||||||
encapsulation: ViewEncapsulation.None,
|
encapsulation: ViewEncapsulation.None,
|
||||||
host: { class: 'adf-search-date-range' }
|
host: { class: 'adf-search-date-range' }
|
||||||
@@ -74,8 +69,9 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
|
|
||||||
private onDestroy$ = new Subject<boolean>();
|
private onDestroy$ = new Subject<boolean>();
|
||||||
|
|
||||||
constructor(private dateAdapter: DateAdapter<Moment>,
|
constructor(private dateAdapter: DateAdapter<DateFnsAdapter>,
|
||||||
private userPreferencesService: UserPreferencesService) {
|
private userPreferencesService: UserPreferencesService,
|
||||||
|
@Inject(MAT_DATE_FORMATS) private dateFormatConfig: MatDateFormats) {
|
||||||
}
|
}
|
||||||
|
|
||||||
getFromValidationMessage(): string {
|
getFromValidationMessage(): string {
|
||||||
@@ -94,10 +90,8 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.datePickerFormat = this.settings?.dateFormat ? this.settings.dateFormat : DEFAULT_FORMAT_DATE;
|
this.datePickerFormat = 'dd-MMM-yy';
|
||||||
|
this.dateFormatConfig.display.dateInput = this.datePickerFormat;
|
||||||
const customDateAdapter = this.dateAdapter as MomentDateAdapter;
|
|
||||||
customDateAdapter.overrideDisplayFormat = this.datePickerFormat;
|
|
||||||
|
|
||||||
this.userPreferencesService
|
this.userPreferencesService
|
||||||
.select(UserPreferenceValues.Locale)
|
.select(UserPreferenceValues.Locale)
|
||||||
@@ -110,9 +104,9 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
|
|
||||||
if (this.settings && this.settings.maxDate) {
|
if (this.settings && this.settings.maxDate) {
|
||||||
if (this.settings.maxDate === 'today') {
|
if (this.settings.maxDate === 'today') {
|
||||||
this.maxDate = this.dateAdapter.today().endOf('day');
|
this.maxDate = endOfToday();
|
||||||
} else {
|
} else {
|
||||||
this.maxDate = moment(this.settings.maxDate).endOf('day');
|
this.maxDate = endOfDay(new Date(this.settings.maxDate));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,12 +135,12 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
this.onDestroy$.complete();
|
this.onDestroy$.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
apply(model: { from: string; to: string }, isValid: boolean) {
|
apply(model: { from: string; to: string }, isFormValid: boolean) {
|
||||||
if (isValid && this.id && this.context && this.settings && this.settings.field) {
|
if (isFormValid && this.id && this.context && this.settings && this.settings.field) {
|
||||||
this.isActive = true;
|
this.isActive = true;
|
||||||
|
|
||||||
const start = moment(model.from).startOf('day').format();
|
const start = format(startOfDay(new Date(model.from)), `yyyy-MM-dd'T'HH:mm:ssxx`);
|
||||||
const end = moment(model.to).endOf('day').format();
|
const end = format(endOfDay(new Date(model.to)), `yyyy-MM-dd'T'HH:mm:ssxx`);
|
||||||
|
|
||||||
this.context.queryFragments[this.id] = `${this.settings.field}:['${start}' TO '${end}']`;
|
this.context.queryFragments[this.id] = `${this.settings.field}:['${start}' TO '${end}']`;
|
||||||
|
|
||||||
@@ -222,7 +216,7 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
|
|
||||||
const inputValue = event.value;
|
const inputValue = event.value;
|
||||||
const formatDate = this.dateAdapter.parse(inputValue, this.datePickerFormat);
|
const formatDate = this.dateAdapter.parse(inputValue, this.datePickerFormat);
|
||||||
if (formatDate && formatDate.isValid()) {
|
if (formatDate && isValid(formatDate)) {
|
||||||
formControl.setValue(formatDate);
|
formControl.setValue(formatDate);
|
||||||
} else if (formatDate) {
|
} else if (formatDate) {
|
||||||
formControl.setErrors({
|
formControl.setErrors({
|
||||||
@@ -234,8 +228,7 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
}
|
}
|
||||||
|
|
||||||
setLocale(locale) {
|
setLocale(locale) {
|
||||||
this.dateAdapter.setLocale(locale);
|
this.dateAdapter.setLocale(DateFnsUtils.getLocaleFromString(locale));
|
||||||
moment.locale(locale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hasParseError(formControl): boolean {
|
hasParseError(formControl): boolean {
|
||||||
@@ -247,6 +240,6 @@ export class SearchDateRangeComponent implements SearchWidget, OnInit, OnDestroy
|
|||||||
}
|
}
|
||||||
|
|
||||||
setFromMaxDate() {
|
setFromMaxDate() {
|
||||||
this.fromMaxDate = (!this.to.value || this.maxDate && (moment(this.maxDate).isBefore(this.to.value))) ? this.maxDate : moment(this.to.value);
|
this.fromMaxDate = (!this.to.value || this.maxDate && (isBefore(this.maxDate, this.to.value))) ? this.maxDate : this.to.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user