2015-10-13 11:29:51 +02:00
|
|
|
import DS from 'ember-data';
|
2016-05-23 13:40:45 +02:00
|
|
|
import Ember from 'ember';
|
2016-06-08 13:55:00 +02:00
|
|
|
import moment from 'moment';
|
2016-01-19 04:56:51 +01:00
|
|
|
import {
|
|
|
|
validator, buildValidations
|
|
|
|
}
|
|
|
|
from 'ember-cp-validations';
|
2015-11-20 11:31:40 +01:00
|
|
|
/* global MF */
|
2015-10-13 11:29:51 +02:00
|
|
|
|
2016-06-13 11:14:42 +02:00
|
|
|
const { assert, computed, isEmpty } = Ember;
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2016-01-28 23:48:14 +01:00
|
|
|
const Validations = buildValidations({
|
2016-01-19 04:56:51 +01:00
|
|
|
title: [
|
2016-02-13 18:37:33 +01:00
|
|
|
validator('iso8601', {
|
2016-01-19 04:56:51 +01:00
|
|
|
active() {
|
2016-02-13 18:37:33 +01:00
|
|
|
return this.get('model.poll.isFindADate');
|
|
|
|
},
|
|
|
|
validFormats: [
|
|
|
|
'YYYY-MM-DD',
|
|
|
|
'YYYY-MM-DDTHH:mmZ',
|
|
|
|
'YYYY-MM-DDTHH:mm:ssZ',
|
|
|
|
'YYYY-MM-DDTHH:mm:ss.SSSZ'
|
2016-05-23 13:40:45 +02:00
|
|
|
],
|
|
|
|
dependentKeys: ['i18n.locale']
|
|
|
|
}),
|
|
|
|
validator('presence', {
|
|
|
|
presence: true,
|
|
|
|
dependentKeys: ['i18n.locale']
|
2016-01-19 04:56:51 +01:00
|
|
|
}),
|
2016-05-20 21:49:29 +02:00
|
|
|
validator('unique', {
|
|
|
|
parent: 'poll',
|
|
|
|
attributeInParent: 'options',
|
2016-05-23 13:40:45 +02:00
|
|
|
dependentKeys: ['poll.options.[]', 'poll.options.@each.title', 'i18n.locale']
|
2016-05-20 21:49:29 +02:00
|
|
|
})
|
2016-06-08 13:55:00 +02:00
|
|
|
],
|
|
|
|
time: [
|
|
|
|
validator('time', {
|
|
|
|
allowEmpty: true
|
|
|
|
}),
|
|
|
|
// alias title validation especially for unique validation
|
|
|
|
validator('alias', {
|
|
|
|
alias: 'title',
|
|
|
|
firstMessageOnly: true
|
|
|
|
})
|
2016-01-19 04:56:51 +01:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
export default MF.Fragment.extend(Validations, {
|
|
|
|
poll: MF.fragmentOwner(),
|
2016-05-23 13:40:45 +02:00
|
|
|
title: DS.attr('string'),
|
|
|
|
|
2016-06-08 13:55:00 +02:00
|
|
|
date: computed('title', function() {
|
|
|
|
const allowedFormats = [
|
|
|
|
'YYYY-MM-DD',
|
|
|
|
'YYYY-MM-DDTHH:mm:ss.SSSZ'
|
|
|
|
];
|
|
|
|
const value = this.get('title');
|
|
|
|
if (Ember.isEmpty(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const format = allowedFormats.find((f) => {
|
|
|
|
// if format length does not match value length
|
|
|
|
// string can't be in this format
|
|
|
|
return f.length === value.length && moment(value, f, true).isValid();
|
|
|
|
});
|
2016-06-13 11:14:42 +02:00
|
|
|
if (isEmpty(format)) {
|
2016-06-08 13:55:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return moment(value, format, true);
|
|
|
|
}),
|
|
|
|
|
|
|
|
day: computed('date', function() {
|
|
|
|
const date = this.get('date');
|
|
|
|
if (!moment.isMoment(date)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return date.format('YYYY-MM-DD');
|
|
|
|
}),
|
|
|
|
|
|
|
|
dayFormatted: computed('date', 'i18n.locale', function() {
|
|
|
|
let date = this.get('date');
|
|
|
|
if (!moment.isMoment(date)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const locale = this.get('i18n.locale');
|
|
|
|
const format = moment.localeData(locale)
|
|
|
|
.longDateFormat('LLLL')
|
|
|
|
.replace(
|
|
|
|
moment.localeData(locale).longDateFormat('LT'), '')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
// momentjs object caches the locale on creation
|
|
|
|
if (date.locale() !== locale) {
|
|
|
|
// we clone the date to allow adjusting timezone without changing the object
|
|
|
|
date = date.clone();
|
|
|
|
date.locale(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return date.format(format);
|
|
|
|
}),
|
|
|
|
|
|
|
|
time: computed('date', {
|
|
|
|
get() {
|
|
|
|
const date = this.get('date');
|
|
|
|
if (!moment.isMoment(date)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// verify that value is an ISO 8601 date string containg time
|
|
|
|
// testing length is faster than parsing with moment
|
|
|
|
const value = this.get('title');
|
|
|
|
if (value.length !== 'YYYY-MM-DDTHH:mm:ss.SSSZ'.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return date.format('HH:mm');
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
const date = this.get('date');
|
|
|
|
assert(
|
|
|
|
'can not set a time if current value is not a valid date',
|
|
|
|
moment.isMoment(date)
|
|
|
|
);
|
|
|
|
|
2016-06-10 20:59:02 +02:00
|
|
|
// set time to undefined if value is false
|
2016-06-08 13:55:00 +02:00
|
|
|
if (Ember.isEmpty(value)) {
|
2016-06-10 20:59:02 +02:00
|
|
|
this.set('title', date.format('YYYY-MM-DD'));
|
2016-06-08 13:55:00 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!moment(value, 'HH:mm', true).isValid()) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [ hour, minute ] = value.split(':');
|
|
|
|
this.set('title', date.hour(hour).minute(minute).toISOString());
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2016-05-23 13:40:45 +02:00
|
|
|
i18n: Ember.inject.service(),
|
|
|
|
init() {
|
|
|
|
this.get('i18n.locale');
|
|
|
|
}
|
2015-11-20 01:09:37 +01:00
|
|
|
});
|