reimplement adopt times of first day (was: copy first line)

This commit is contained in:
jelhan 2016-02-16 04:02:59 +01:00
parent e906fbd09d
commit b921632465
4 changed files with 125 additions and 1 deletions

View file

@ -76,6 +76,44 @@ export default Ember.Component.extend(Validations, {
fragment
);
},
adoptTimesOfFirstDay() {
const options = this.get('options');
const groupedDateTimes = this.get('groupedDatetimes');
const firstDate = groupedDateTimes.get('firstObject');
const timesOfFirstDate = firstDate.items.map((datetime) => {
return datetime.get('time');
}).filter(Ember.isPresent);
groupedDateTimes.slice(1).forEach((groupedDateTime) => {
// remove excess options
if (timesOfFirstDate.get('length') < groupedDateTime.items.length) {
options.removeObjects(
groupedDateTime.items.slice(timesOfFirstDate.get('length')).map((datetime) => {
return datetime.get('option');
})
);
}
// set times according to first day
let targetPosition;
timesOfFirstDate.forEach((timeOfFirstDate, index) => {
const target = groupedDateTime.items.objectAt(index);
if (target === undefined) {
let [hour, minute] = timeOfFirstDate.split(':');
let dateString = moment(groupedDateTime.value, 'YYYY-MM-DD', true).hour(hour).minute(minute).toISOString();
let fragment = this.get('store').createFragment('option', {
title: dateString
});
options.insertAt(
targetPosition,
fragment
);
targetPosition++;
} else {
target.set('time', timeOfFirstDate);
targetPosition = options.indexOf(target.get('option'));
}
});
});
},
delOption(element) {
let position = this.get('options').indexOf(element.get('option'));
this.get('options').removeAt(position);

View file

@ -25,7 +25,7 @@
<div class='form-group'>
<div class='col-sm-10 col-sm-offset-2'>
<button {{action "copyFirstLine"}} class="btn btn-default button-copy-first-line">{{t "create.options-datetime.copy-first-line"}}</button>
<button {{action "adoptTimesOfFirstDay"}} class="btn btn-default adopt-times-of-first-day">{{t "create.options-datetime.copy-first-line"}}</button>
</div>
</div>
{{/if}}

View file

@ -0,0 +1,12 @@
import Ember from 'ember';
export default Ember.View.extend({
actions: {
moreOptions() {
// create new Option
this.get('controller.optionsTexts').pushObject({
'value': ''
});
}
}
});

View file

@ -169,3 +169,77 @@ test('allows to delete an option', function(assert) {
);
});
});
test('adopt times of first day - simple', function(assert) {
this.set('options', [
Ember.Object.create({ title: moment().hour(10).minute(0).toISOString() }),
Ember.Object.create({ title: '2015-02-02' }),
Ember.Object.create({ title: '2015-03-03' })
]);
this.set('isDateTime', true);
this.render(hbs`{{create-options-datetime options=options isDateTime=isDateTime}}`);
Ember.run(() => {
this.$('button.adopt-times-of-first-day').click();
});
assert.equal(
this.$('.grouped-input:eq(0) input:not(.ws-inputreplace)').val(),
'10:00',
'time was not changed for first day'
);
assert.equal(
this.$('.grouped-input:eq(1) input:not(.ws-inputreplace)').val(),
'10:00',
'time was adopted for second day'
);
assert.equal(
this.$('.grouped-input:eq(1) input:not(.ws-inputreplace)').val(),
'10:00',
'time was adopted for third day'
);
});
test('adopt times of first day - more times on first day than on others', function(assert) {
this.set('options', [
Ember.Object.create({ title: moment().hour(10).minute(0).toISOString() }),
Ember.Object.create({ title: moment().hour(22).minute(0).toISOString() }),
Ember.Object.create({ title: '2015-02-02' })
]);
this.set('isDateTime', true);
this.render(hbs`{{create-options-datetime options=options isDateTime=isDateTime}}`);
Ember.run(() => {
this.$('button.adopt-times-of-first-day').click();
});
assert.deepEqual(
this.$('.grouped-input:eq(0) input:not(.ws-inputreplace)').map((i, el) => $(el).val()).toArray(),
['10:00', '22:00'],
'time was not changed for first day after additionally time was added to first day'
);
assert.deepEqual(
this.$('.grouped-input:eq(0) input:not(.ws-inputreplace)').map((i, el) => $(el).val()).toArray(),
['10:00', '22:00'],
'time was adopted for second day after additionally time was added to first day'
);
assert.deepEqual(
this.$('.grouped-input:eq(0) input:not(.ws-inputreplace)').map((i, el) => $(el).val()).toArray(),
['10:00', '22:00'],
'time was adopted for third day after additionally time was added to first day'
);
});
test('adopt times of first day - excess times on other days got deleted', function(assert) {
this.set('options', [
Ember.Object.create({ title: moment().hour(10).minute(0).toISOString() }),
Ember.Object.create({ title: moment().add(1, 'day').hour(10).minute(0).toISOString() }),
Ember.Object.create({ title: moment().add(1, 'day').hour(22).minute(0).toISOString() })
]);
this.set('isDateTime', true);
this.render(hbs`{{create-options-datetime options=options isDateTime=isDateTime}}`);
Ember.run(() => {
this.$('button.adopt-times-of-first-day').click();
});
assert.deepEqual(
this.$('.grouped-input:eq(1) input:not(.ws-inputreplace)').map((i, el) => $(el).val()).toArray(),
['10:00'],
'additional time on secondary day got deleted'
);
});