Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/datetime #688

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions app/pods/components/input/md-date-range/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import { notEmpty, alias } from '@ember/object/computed';

import Component from '@ember/component';
import { set, get, computed } from '@ember/object';
import { set, computed } from '@ember/object';
import { observer } from '@ember/object';
import { once } from '@ember/runloop';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';

dayjs.extend(utc);

import { validator, buildValidations } from 'ember-cp-validations';

Expand Down Expand Up @@ -54,37 +57,30 @@ export default Component.extend(Validations, {
selectedPrecisionChanged: observer('selectedPrecision', function () {
const startDate = this.start;
const endDate = this.end;
if (!startDate || !endDate) return;

const parsedStartDate = dayjs(startDate);
const parsedEndDate = dayjs(endDate);
let newStartDate, newEndDate;

switch (this.selectedPrecision) {
case 'Time':
newStartDate = parsedStartDate.format('YYYY-MM-DD HH:mm:ss');
newEndDate = parsedEndDate.format('YYYY-MM-DD HH:mm:ss');
this.set('selectedFormat', 'YYYY-MM-DDTHH:mm:ssZ');
break;
case 'Day':
newStartDate = parsedStartDate.format('YYYY-MM-DD');
newEndDate = parsedEndDate.format('YYYY-MM-DD');
this.set('selectedFormat', 'YYYY-MM-DD');
case 'Year':
this.set('selectedFormat', 'YYYY');
if (startDate) newStartDate = dayjs(startDate).format('YYYY');
if (endDate) newEndDate = dayjs(endDate).format('YYYY');
break;
case 'Month':
newStartDate = parsedStartDate.format('YYYY-MM');
newEndDate = parsedEndDate.format('YYYY-MM');
this.set('selectedFormat', 'YYYY-MM');
if (startDate) newStartDate = dayjs(startDate).format('YYYY-MM');
if (endDate) newEndDate = dayjs(endDate).format('YYYY-MM');
break;
case 'Year':
newStartDate = parsedStartDate.format('YYYY');
newEndDate = parsedEndDate.format('YYYY');
this.set('selectedFormat', 'YYYY');
case 'Day':
this.set('selectedFormat', 'YYYY-MM-DD');
if (startDate) newStartDate = dayjs(startDate).format('YYYY-MM-DD');
if (endDate) newEndDate = dayjs(endDate).format('YYYY-MM-DD');
break;
case 'Time':
default:
newStartDate = parsedStartDate.format('YYYY-MM-DD HH:mm:ss');
newEndDate = parsedEndDate.format('YYYY-MM-DD HH:mm:ss');
this.set('selectedFormat', 'YYYY-MM-DDTHH:mm:ssZ');
if (startDate)
newStartDate = dayjs(startDate).format('YYYY-MM-DDTHH:mm:ssZ');
if (endDate) newEndDate = dayjs(endDate).format('YYYY-MM-DDTHH:mm:ssZ');
break;
}

Expand All @@ -103,18 +99,15 @@ export default Component.extend(Validations, {
this.set('selectedFormat', 'YYYY');
return;
}
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(date)) {
this.set('selectedPrecision', 'Time');
this.set('selectedFormat', 'YYYY-MM-DDTHH:mm:ssZ');
} else if (/\d{4}-\d{2}-\d{2}/.test(date)) {
this.set('selectedPrecision', 'Day');
this.set('selectedFormat', 'YYYY-MM-DD');
} else if (/\d{4}-\d{2}/.test(date)) {
this.set('selectedPrecision', 'Month');
this.set('selectedFormat', 'YYYY-MM');
} else if (/\d{4}/.test(date)) {
if (/^\d{4}$/.test(date)) {
this.set('selectedPrecision', 'Year');
this.set('selectedFormat', 'YYYY');
} else if (/^\d{4}-\d{2}$/.test(date)) {
this.set('selectedPrecision', 'Month');
this.set('selectedFormat', 'YYYY-MM');
} else if (/^\d{4}-\d{2}-\d{2}$/.test(date)) {
this.set('selectedPrecision', 'Day');
this.set('selectedFormat', 'YYYY-MM-DD');
} else {
this.set('selectedPrecision', 'Time');
this.set('selectedFormat', 'YYYY-MM-DDTHH:mm:ssZ');
Expand Down
46 changes: 24 additions & 22 deletions app/pods/components/object/md-date/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ export default Component.extend(Validations, {
},

selectedPrecision: null,
selectedFormat: 'YYYY-MM-DDTHH:mm:ssZ',

tagName: '',
date: alias('model.date'),
dateType: alias('model.dateType'),

selectedPrecisionChanged: observer('selectedPrecision', function () {
const date = this.get('model.date');
if (!date) return;

const parsedDate = dayjs(date);
const dateObj = this.get('model.date');
let newDate;

switch (this.get('selectedPrecision')) {
case 'Time':
newDate = parsedDate.format('YYYY-MM-DD HH:mm:ss');
break;
case 'Day':
newDate = parsedDate.format('YYYY-MM-DD');
switch (this.selectedPrecision) {
case 'Year':
this.set('selectedFormat', 'YYYY');
if (dateObj) newDate = dayjs(dateObj).format('YYYY');
break;
case 'Month':
newDate = parsedDate.format('YYYY-MM');
this.set('selectedFormat', 'YYYY-MM');
if (dateObj) newDate = dayjs(dateObj).format('YYYY-MM');
break;
case 'Year':
newDate = parsedDate.format('YYYY');
case 'Day':
this.set('selectedFormat', 'YYYY-MM-DD');
if (dateObj) newDate = dayjs(dateObj).format('YYYY-MM-DD');
break;
case 'Time':
default:
newDate = parsedDate.format('YYYY-MM-DD HH:mm:ss');
this.set('selectedFormat', 'YYYY-MM-DDTHH:mm:ssZ');
if (dateObj) newDate = dayjs(dateObj).format('YYYY-MM-DDTHH:mm:ssZ');
break;
}

if (newDate !== date) {
if (newDate !== dateObj) {
this.set('model.date', newDate);
}
}),
Expand All @@ -71,16 +71,18 @@ export default Component.extend(Validations, {
this.set('selectedPrecision', 'Year');
return;
}
if (/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(date)) {
this.set('selectedPrecision', 'Time');
} else if (/\d{4}-\d{2}-\d{2}/.test(date)) {
this.set('selectedPrecision', 'Day');
} else if (/\d{4}-\d{2}/.test(date)) {
this.set('selectedPrecision', 'Month');
} else if (/\d{4}/.test(date)) {
if (/^\d{4}$/.test(date)) {
this.set('selectedPrecision', 'Year');
this.set('selectedFormat', 'YYYY');
} else if (/^\d{4}-\d{2}$/.test(date)) {
this.set('selectedPrecision', 'Month');
this.set('selectedFormat', 'YYYY-MM');
} else if (/^\d{4}-\d{2}-\d{2}$/.test(date)) {
this.set('selectedPrecision', 'Day');
this.set('selectedFormat', 'YYYY-MM-DD');
} else {
this.set('selectedPrecision', 'Time');
this.set('selectedFormat', 'YYYY-MM-DDTHH:mm:ssZ');
}
},
});
74 changes: 21 additions & 53 deletions app/pods/components/object/md-date/template.hbs
Original file line number Diff line number Diff line change
@@ -1,59 +1,27 @@
<td>
{{input/md-select
showValidations=true
valuePath="value"
namePath="name"
valuePath='value'
namePath='name'
model=this
path="selectedPrecision"
path='selectedPrecision'
objectArray=precisionOptions
searchEnabled=false
placeholder="Date Precision"
placeholder='Date Precision'
}}
</td>
<td>
{{#if (eq selectedPrecision "Time")}}
{{input/md-datetime
valuePath="date"
model=this
showValidations=true
required=true
forceDateOutput=true
profilePath=(concat profilePath ".date")
}}
{{else if (eq selectedPrecision "Day")}}
{{input/md-datetime
valuePath="date"
model=this
showValidations=true
required=true
forceDateOutput=true
format="YYYY-MM-DD"
placeholder="Enter date"
profilePath=(concat profilePath ".date")
}}
{{else if (eq selectedPrecision "Month")}}
{{input/md-datetime
valuePath="date"
model=this
showValidations=true
required=true
forceDateOutput=true
format="YYYY-MM"
placeholder="Enter Year/Month"
profilePath=(concat profilePath ".date")
}}
{{else if (eq selectedPrecision "Year")}}
{{input/md-datetime
valuePath="date"
model=this
showValidations=true
required=true
forceDateOutput=true
format="YYYY"
placeholder="Enter Year"
profilePath=(concat profilePath ".date")
}}
{{/if}}
{{input/md-datetime
valuePath='date'
model=this
showValidations=true
forceDateOutput=true
showClear=true
format=selectedFormat
placeholder='Enter date'
profilePath=(concat profilePath '.date')
required=true
}}
</td>
<td>
{{input/md-codelist
Expand All @@ -64,18 +32,18 @@
disabled=disabled
allowClear=true
showValidations=true
mdCodeName="dateType"
profilePath=(concat profilePath ".dateType")
path="dateType"
mdCodeName='dateType'
profilePath=(concat profilePath '.dateType')
path='dateType'
model=this
placeholder="Choose date type"
placeholder='Choose date type'
}}
</td>
<td>
{{input/md-input
value=model.description
profilePath=(concat profilePath ".description")
placeholder="Describe the date."
profilePath=(concat profilePath '.description')
placeholder='Describe the date.'
}}
</td>
{{yield}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mdeditor",
"version": "1.5.0-beta.27",
"version": "1.5.0-beta.28",
"description": "A web application for authoring and editing metadata.",
"repository": {
"type": "git",
Expand Down