Fix: adopt times of first day if there aren't any times on first day

This commit is contained in:
jelhan 2016-06-09 12:27:51 +02:00
parent 327d6cd737
commit bcccffacec
2 changed files with 69 additions and 23 deletions

View file

@ -5,6 +5,8 @@ import {
}
from 'ember-cp-validations';
const { isEmpty } = Ember;
let modelValidations = buildValidations({
dates: [
validator('collection', true),
@ -40,33 +42,45 @@ export default Ember.Component.extend(modelValidations, {
return dates.get('time');
}).filter(Ember.isPresent);
groupedDates.slice(1).forEach((groupedDate) => {
// remove excess options
if (timesOfFirstDate.get('length') < groupedDate.items.length) {
if (isEmpty(timesOfFirstDate)) {
// there aren't any times on first day
const remainingOption = groupedDate.items.get('firstObject');
// remove all times but the first one
dates.removeObjects(
groupedDate.items.slice(timesOfFirstDate.get('length'))
groupedDate.items.slice(1)
);
}
// 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
// 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'))
);
targetPosition++;
} else {
target.set('time', timeOfFirstDate);
targetPosition = dates.indexOf(target) + 1;
}
});
// 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;
}
});
}
});
},
deleteOption(target) {

View file

@ -180,3 +180,35 @@ test('adopt times of first day - having times on the other days', function(asser
'times adopted correctly'
);
});
test('adopt times of first day - no times on first day', function(assert) {
let component;
Ember.run(() => {
component = this.subject({
dates: [
this.store.createFragment('option', {
title: '2015-01-01'
}),
this.store.createFragment('option', {
title: '2015-01-02'
}),
this.store.createFragment('option', {
title: moment('2015-01-03T11:00:00.000Z').toISOString()
}),
this.store.createFragment('option', {
title: moment('2015-01-03T15:00:00.000Z').toISOString()
})
]
});
component.send('adoptTimesOfFirstDay');
});
assert.deepEqual(
component.get('dates').map((option) => option.get('title')),
[
'2015-01-01',
'2015-01-02',
'2015-01-03'
],
'times are removed from all days'
);
});