2016-01-20 02:52:21 +01:00
|
|
|
import Ember from 'ember';
|
|
|
|
import groupBy from 'ember-group-by';
|
2016-02-16 02:26:27 +01:00
|
|
|
import {
|
|
|
|
validator, buildValidations
|
|
|
|
}
|
|
|
|
from 'ember-cp-validations';
|
|
|
|
|
2016-06-09 12:27:51 +02:00
|
|
|
const { isEmpty } = Ember;
|
|
|
|
|
2016-06-06 00:37:26 +02:00
|
|
|
let modelValidations = buildValidations({
|
2016-06-08 13:55:00 +02:00
|
|
|
dates: [
|
2016-02-16 02:26:27 +01:00
|
|
|
validator('collection', true),
|
|
|
|
validator('length', {
|
2016-06-06 00:37:26 +02:00
|
|
|
dependentKeys: ['datetimes.[]'],
|
2016-04-15 11:21:26 +02:00
|
|
|
min: 1
|
2016-02-16 02:26:27 +01:00
|
|
|
}),
|
|
|
|
validator('valid-collection', {
|
2016-06-06 00:37:26 +02:00
|
|
|
dependentKeys: ['datetimes.[]', 'datetimes.@each.time']
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Ember.Component.extend(modelValidations, {
|
2016-01-22 00:19:46 +01:00
|
|
|
actions: {
|
2016-06-08 13:55:00 +02:00
|
|
|
addOption(afterOption) {
|
|
|
|
let options = this.get('dates');
|
|
|
|
let dayString = afterOption.get('day');
|
2016-01-22 00:19:46 +01:00
|
|
|
let fragment = this.get('store').createFragment('option', {
|
2016-06-08 13:55:00 +02:00
|
|
|
title: dayString
|
2016-01-22 00:19:46 +01:00
|
|
|
});
|
2016-06-08 13:55:00 +02:00
|
|
|
let position = options.indexOf(afterOption) + 1;
|
2016-01-22 00:19:46 +01:00
|
|
|
options.insertAt(
|
|
|
|
position,
|
|
|
|
fragment
|
|
|
|
);
|
|
|
|
},
|
2016-02-16 04:02:59 +01:00
|
|
|
adoptTimesOfFirstDay() {
|
2016-06-08 13:55:00 +02:00
|
|
|
const dates = this.get('dates');
|
|
|
|
const groupedDates = this.get('groupedDates');
|
|
|
|
const firstDate = groupedDates.get('firstObject');
|
|
|
|
const timesOfFirstDate = firstDate.items.map((dates) => {
|
|
|
|
return dates.get('time');
|
2016-02-16 04:02:59 +01:00
|
|
|
}).filter(Ember.isPresent);
|
2016-06-08 13:55:00 +02:00
|
|
|
groupedDates.slice(1).forEach((groupedDate) => {
|
2016-06-09 12:27:51 +02:00
|
|
|
if (isEmpty(timesOfFirstDate)) {
|
|
|
|
// there aren't any times on first day
|
|
|
|
const remainingOption = groupedDate.items.get('firstObject');
|
|
|
|
// remove all times but the first one
|
2016-06-08 13:55:00 +02:00
|
|
|
dates.removeObjects(
|
2016-06-09 12:27:51 +02:00
|
|
|
groupedDate.items.slice(1)
|
2016-02-16 04:02:59 +01:00
|
|
|
);
|
2016-06-09 12:27:51 +02:00
|
|
|
// set title as date without time
|
|
|
|
remainingOption.set('title', remainingOption.get('date').format('YYYY-MM-DD'));
|
|
|
|
} else {
|
|
|
|
// adopt times of first day
|
|
|
|
if (timesOfFirstDate.get('length') < groupedDate.items.length) {
|
|
|
|
// remove excess options
|
|
|
|
dates.removeObjects(
|
|
|
|
groupedDate.items.slice(timesOfFirstDate.get('length'))
|
2016-02-16 04:02:59 +01:00
|
|
|
);
|
|
|
|
}
|
2016-06-09 12:27:51 +02:00
|
|
|
// set times according to first day
|
|
|
|
let targetPosition;
|
|
|
|
timesOfFirstDate.forEach((timeOfFirstDate, index) => {
|
|
|
|
const target = groupedDate.items.objectAt(index);
|
|
|
|
if (target === undefined) {
|
|
|
|
const basisDate = groupedDate.items.get('firstObject.date').clone();
|
|
|
|
let [hour, minute] = timeOfFirstDate.split(':');
|
|
|
|
let dateString = basisDate.hour(hour).minute(minute).toISOString();
|
|
|
|
let fragment = this.get('store').createFragment('option', {
|
|
|
|
title: dateString
|
|
|
|
});
|
|
|
|
dates.insertAt(
|
|
|
|
targetPosition,
|
|
|
|
fragment
|
|
|
|
);
|
|
|
|
targetPosition++;
|
|
|
|
} else {
|
|
|
|
target.set('time', timeOfFirstDate);
|
|
|
|
targetPosition = dates.indexOf(target) + 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-02-16 04:02:59 +01:00
|
|
|
});
|
|
|
|
},
|
2016-06-10 20:59:02 +02:00
|
|
|
/*
|
|
|
|
* removes target option if it's not the only date for this day
|
|
|
|
* otherwise it deletes time for this date
|
|
|
|
*/
|
2016-06-08 13:55:00 +02:00
|
|
|
deleteOption(target) {
|
|
|
|
let position = this.get('dates').indexOf(target);
|
2016-06-10 20:59:02 +02:00
|
|
|
let datesForThisDay = this.get('groupedDates').find((groupedDate) => {
|
|
|
|
return groupedDate.value === target.get('day');
|
|
|
|
}).items;
|
|
|
|
if (datesForThisDay.length > 1) {
|
|
|
|
this.get('dates').removeAt(position);
|
|
|
|
} else {
|
|
|
|
target.set('time', null);
|
|
|
|
}
|
2016-02-16 02:26:27 +01:00
|
|
|
},
|
|
|
|
submit() {
|
|
|
|
if (this.get('validations.isValid')) {
|
|
|
|
this.sendAction('nextPage');
|
|
|
|
} else {
|
|
|
|
this.set('shouldShowErrors', true);
|
|
|
|
}
|
2016-01-22 00:19:46 +01:00
|
|
|
}
|
|
|
|
},
|
2016-06-08 13:55:00 +02:00
|
|
|
groupedDates: groupBy('dates', 'day'),
|
2016-01-22 00:19:46 +01:00
|
|
|
store: Ember.inject.service('store')
|
2016-01-20 02:52:21 +01:00
|
|
|
});
|