2016-01-20 02:52:21 +01:00
|
|
|
import Ember from 'ember';
|
2016-02-16 02:26:27 +01:00
|
|
|
import {
|
|
|
|
validator, buildValidations
|
|
|
|
}
|
|
|
|
from 'ember-cp-validations';
|
2016-07-04 14:39:00 +02:00
|
|
|
import { groupBy } from 'ember-array-computed-macros';
|
2016-08-01 21:35:06 +02:00
|
|
|
import Form from 'ember-bootstrap/components/bs-form';
|
2016-02-16 02:26:27 +01:00
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
const { computed, Component, inject, isEmpty, isPresent, observer } = Ember;
|
|
|
|
const { filter, mapBy, readOnly } = computed;
|
2016-06-09 12:27:51 +02:00
|
|
|
|
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']
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
export default 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');
|
2016-07-05 11:26:03 +02:00
|
|
|
const datesForFirstDay = this.get('datesForFirstDay');
|
2016-07-04 14:39:00 +02:00
|
|
|
const timesForFirstDay = this.get('timesForFirstDay');
|
|
|
|
const datesWithoutFirstDay = this.get('groupedDates').slice(1);
|
2016-07-05 11:26:03 +02:00
|
|
|
|
|
|
|
/* validate if times on firstDay are valid */
|
|
|
|
const datesForFirstDayAreValid = datesForFirstDay.every((date) => {
|
|
|
|
// ignore dates where time is null
|
|
|
|
return isEmpty(date.get('time')) || date.get('validations.isValid');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!datesForFirstDayAreValid) {
|
|
|
|
this.set('errorMessage', 'create.options-datetime.fix-validation-errors-first-day');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-04 14:39:00 +02:00
|
|
|
datesWithoutFirstDay.forEach((groupedDate) => {
|
|
|
|
if (isEmpty(timesForFirstDay)) {
|
2016-06-09 12:27:51 +02:00
|
|
|
// there aren't any times on first day
|
2016-07-04 14:39:00 +02:00
|
|
|
const remainingOption = groupedDate.get('firstObject');
|
2016-06-09 12:27:51 +02:00
|
|
|
// remove all times but the first one
|
2016-06-08 13:55:00 +02:00
|
|
|
dates.removeObjects(
|
2016-07-04 14:39:00 +02:00
|
|
|
groupedDate.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
|
2016-07-04 14:39:00 +02:00
|
|
|
if (timesForFirstDay.get('length') < groupedDate.length) {
|
2016-06-09 12:27:51 +02:00
|
|
|
// remove excess options
|
|
|
|
dates.removeObjects(
|
2016-07-04 14:39:00 +02:00
|
|
|
groupedDate.slice(timesForFirstDay.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;
|
2016-07-04 14:39:00 +02:00
|
|
|
timesForFirstDay.forEach((timeOfFirstDate, index) => {
|
|
|
|
const target = groupedDate.objectAt(index);
|
2016-06-09 12:27:51 +02:00
|
|
|
if (target === undefined) {
|
2016-07-04 14:39:00 +02:00
|
|
|
const basisDate = groupedDate.get('firstObject.date').clone();
|
2016-06-09 12:27:51 +02:00
|
|
|
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-08-21 00:24:55 +02:00
|
|
|
back() {
|
|
|
|
this.sendAction('back');
|
|
|
|
},
|
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) => {
|
2016-07-04 14:39:00 +02:00
|
|
|
return groupedDate.get('firstObject.day') === target.get('day');
|
|
|
|
});
|
2016-06-10 20:59:02 +02:00
|
|
|
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-07-04 14:39:00 +02:00
|
|
|
// dates are sorted
|
|
|
|
datesForFirstDay: readOnly('groupedDates.firstObject'),
|
2016-07-05 11:26:03 +02:00
|
|
|
|
|
|
|
// errorMessage should be reset to null on all user interactions
|
|
|
|
errorMesage: null,
|
|
|
|
resetErrorMessage: observer('dates.@each.time', function() {
|
|
|
|
this.set('errorMessage', null);
|
|
|
|
}),
|
|
|
|
|
|
|
|
// can't use multiple computed macros at once
|
2016-07-04 14:39:00 +02:00
|
|
|
_timesForFirstDay: mapBy('datesForFirstDay', 'time'),
|
|
|
|
timesForFirstDay: filter('_timesForFirstDay', function(time) {
|
|
|
|
return isPresent(time);
|
|
|
|
}),
|
2016-07-05 11:26:03 +02:00
|
|
|
|
|
|
|
// have a look at https://github.com/martndemus/ember-array-computed-macros#groupbylistproperty-valueproperty
|
2016-06-08 13:55:00 +02:00
|
|
|
groupedDates: groupBy('dates', 'day'),
|
2016-07-05 11:26:03 +02:00
|
|
|
|
2016-08-01 21:35:06 +02:00
|
|
|
form: computed(function() {
|
|
|
|
return this.childViews.find(function(childView) {
|
|
|
|
return childView instanceof Form;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
formElements: readOnly('form.childFormElements'),
|
|
|
|
daysValidationState: computed('formElements.@each.validation', function() {
|
|
|
|
return this.get('formElements').reduce(function(daysValidationState, item) {
|
|
|
|
const day = item.get('model.day');
|
|
|
|
const validation = item.get('validation');
|
|
|
|
let currentValidationState;
|
|
|
|
|
|
|
|
// there maybe form elements without model or validation
|
|
|
|
if (isEmpty(day) || validation === undefined) {
|
|
|
|
return daysValidationState;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's not existing initialize with current value
|
|
|
|
if (!daysValidationState.hasOwnProperty(day)) {
|
|
|
|
daysValidationState[day] = validation;
|
|
|
|
return daysValidationState;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentValidationState = daysValidationState[day];
|
|
|
|
switch (currentValidationState) {
|
|
|
|
// error overrules all validation states
|
|
|
|
case 'error':
|
|
|
|
break;
|
|
|
|
|
|
|
|
// null ist overruled by 'error'
|
|
|
|
case null:
|
|
|
|
if (validation === 'error') {
|
|
|
|
daysValidationState[day] = 'error';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// success is overruled by anyother validation state
|
|
|
|
case 'success':
|
|
|
|
daysValidationState[day] = validation;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return daysValidationState;
|
|
|
|
}, {});
|
|
|
|
}),
|
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
store: inject.service()
|
2016-01-20 02:52:21 +01:00
|
|
|
});
|