From 2759196cde6132257ba8e70c7f3d8b4e168ae195 Mon Sep 17 00:00:00 2001 From: Vito Date: Wed, 27 Jun 2018 09:45:14 +0100 Subject: [PATCH] [ADF-3268] added datetime widget for dynamic table (#3529) --- .../editors/datetime/datetime.editor.html | 20 ++++ .../editors/datetime/datetime.editor.scss | 10 ++ .../editors/datetime/datetime.editor.spec.ts | 89 ++++++++++++++++++ .../editors/datetime/datetime.editor.ts | 92 +++++++++++++++++++ .../dynamic-table/editors/row.editor.html | 8 +- lib/core/form/components/widgets/index.ts | 5 +- 6 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.html create mode 100644 lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.scss create mode 100644 lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.spec.ts create mode 100644 lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.ts diff --git a/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.html b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.html new file mode 100644 index 0000000000..55be3f2f1c --- /dev/null +++ b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.html @@ -0,0 +1,20 @@ +
+ + + + + + +
diff --git a/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.scss b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.scss new file mode 100644 index 0000000000..3be29ca848 --- /dev/null +++ b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.scss @@ -0,0 +1,10 @@ +.adf { + &-date-editor { + width: 100%; + } + + &-date-editor-button { + position: relative; + top: 25px; + } +} diff --git a/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.spec.ts b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.spec.ts new file mode 100644 index 0000000000..d0c635e82e --- /dev/null +++ b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.spec.ts @@ -0,0 +1,89 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import moment from 'moment-es6'; +import { FormFieldModel, FormModel } from '../../../index'; +import { DynamicTableColumn } from './../../dynamic-table-column.model'; +import { DynamicTableRow } from './../../dynamic-table-row.model'; +import { DynamicTableModel } from './../../dynamic-table.widget.model'; +import { DateTimeEditorComponent } from './datetime.editor'; +import { setupTestBed } from '../../../../../../testing/setupTestBed'; +import { CoreModule } from '../../../../../../core.module'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; + +describe('DateTimeEditorComponent', () => { + let component: DateTimeEditorComponent; + let fixture: ComponentFixture; + let row: DynamicTableRow; + let column: DynamicTableColumn; + let table: DynamicTableModel; + + setupTestBed({ + imports: [ + NoopAnimationsModule, + CoreModule.forRoot() + ] + }); + + beforeEach(() => { + fixture = TestBed.createComponent(DateTimeEditorComponent); + component = fixture.componentInstance; + + row = { value: { date: '1879-03-14T00:00:00.000Z' } }; + column = { id: 'datetime', type: 'Datetime' }; + const field = new FormFieldModel(new FormModel()); + table = new DynamicTableModel(field, null); + table.rows.push(row); + table.columns.push(column); + component.table = table; + component.row = row; + component.column = column; + }); + + it('should create instance of DateTimeEditorComponent', () => { + expect(fixture.componentInstance instanceof DateTimeEditorComponent).toBe(true, 'should create DateTimeEditorComponent'); + }); + + it('should update fow value on change', () => { + component.ngOnInit(); + let newDate = moment('22-6-2018 04:20 AM', 'D-M-YYYY hh:mm A'); + component.onDateChanged(newDate); + expect(moment(row.value[column.id]).isSame(newDate)).toBeTruthy(); + }); + + it('should update row value upon user input', () => { + const input = '22-6-2018 04:20 AM'; + + component.ngOnInit(); + component.onDateChanged(input); + + let actual = row.value[column.id]; + expect(actual).toBe('22-6-2018 04:20 AM'); + }); + + it('should flush value on user input', () => { + spyOn(table, 'flushValue').and.callThrough(); + const input = '22-6-2018 04:20 AM'; + + component.ngOnInit(); + component.onDateChanged(input); + + expect(table.flushValue).toHaveBeenCalled(); + }); + +}); diff --git a/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.ts b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.ts new file mode 100644 index 0000000000..32f1f32b26 --- /dev/null +++ b/lib/core/form/components/widgets/dynamic-table/editors/datetime/datetime.editor.ts @@ -0,0 +1,92 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* tslint:disable:component-selector */ + +import { UserPreferencesService } from '../../../../../../services/user-preferences.service'; +import { MomentDateAdapter } from '../../../../../../utils/momentDateAdapter'; +import { MOMENT_DATE_FORMATS } from '../../../../../../utils/moment-date-formats.model'; +import { Component, Input, OnInit } from '@angular/core'; +import { DateAdapter, MAT_DATE_FORMATS } from '@angular/material'; +import moment from 'moment-es6'; +import { Moment } from 'moment'; +import { DynamicTableColumn } from './../../dynamic-table-column.model'; +import { DynamicTableRow } from './../../dynamic-table-row.model'; +import { DynamicTableModel } from './../../dynamic-table.widget.model'; +import { DatetimeAdapter, MAT_DATETIME_FORMATS } from '@mat-datetimepicker/core'; +import { MomentDatetimeAdapter, MAT_MOMENT_DATETIME_FORMATS } from '@mat-datetimepicker/moment'; + +@Component({ + selector: 'adf-datetime-editor', + templateUrl: './datetime.editor.html', + providers: [ + { provide: DateAdapter, useClass: MomentDateAdapter }, + { provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS }, + { provide: DatetimeAdapter, useClass: MomentDatetimeAdapter }, + { provide: MAT_DATETIME_FORMATS, useValue: MAT_MOMENT_DATETIME_FORMATS } + ], + styleUrls: ['./datetime.editor.scss'] +}) +export class DateTimeEditorComponent implements OnInit { + + DATE_FORMAT: string = 'D-M-YYYY hh:mm A'; + + value: any; + + @Input() + table: DynamicTableModel; + + @Input() + row: DynamicTableRow; + + @Input() + column: DynamicTableColumn; + + minDate: Moment; + maxDate: Moment; + + constructor(private dateAdapter: DateAdapter, + private preferences: UserPreferencesService) { + } + + ngOnInit() { + this.preferences.locale$.subscribe((locale) => { + this.dateAdapter.setLocale(locale); + }); + let momentDateAdapter = this.dateAdapter; + momentDateAdapter.overrideDisplyaFormat = this.DATE_FORMAT; + + this.value = moment(this.table.getCellValue(this.row, this.column), this.DATE_FORMAT); + } + + onDateChanged(newDateValue) { + if (newDateValue && newDateValue.value) { + const newValue = moment(newDateValue.value, this.DATE_FORMAT); + this.row.value[this.column.id] = newDateValue.value.format(this.DATE_FORMAT); + this.value = newValue; + this.table.flushValue(); + } else if (newDateValue) { + const newValue = moment(newDateValue, this.DATE_FORMAT); + this.value = newValue; + this.row.value[this.column.id] = newDateValue; + this.table.flushValue(); + } else { + this.row.value[this.column.id] = ''; + } + } + +} diff --git a/lib/core/form/components/widgets/dynamic-table/editors/row.editor.html b/lib/core/form/components/widgets/dynamic-table/editors/row.editor.html index cc4b07d634..7f283a27e5 100644 --- a/lib/core/form/components/widgets/dynamic-table/editors/row.editor.html +++ b/lib/core/form/components/widgets/dynamic-table/editors/row.editor.html @@ -16,7 +16,13 @@ [column]="column"> - +
+ + +