2019-01-20 15:20:54 +01:00
|
|
|
import Component from '@ember/component';
|
2018-12-29 01:27:37 +01:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import { isArray } from '@ember/array';
|
2019-01-20 15:20:54 +01:00
|
|
|
import { isPresent } from '@ember/utils';
|
2016-01-19 04:56:51 +01:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
export default Component.extend({
|
2018-12-29 01:27:37 +01:00
|
|
|
i18n: service(),
|
2019-01-20 15:20:54 +01:00
|
|
|
store: service('store'),
|
2016-03-18 13:15:47 +01:00
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
selectedDays: computed('options.[]', function() {
|
|
|
|
return this.options
|
|
|
|
// should be unique
|
|
|
|
.uniqBy('day')
|
|
|
|
// raw dates
|
|
|
|
.map(({ date }) => date)
|
|
|
|
// filter out invalid
|
|
|
|
.filter(moment.isMoment)
|
|
|
|
.toArray();
|
|
|
|
}),
|
|
|
|
calendarCenterNext: computed('calendarCenter', function() {
|
|
|
|
return moment(this.calendarCenter).add(1, 'months');
|
|
|
|
}),
|
2016-07-27 23:06:54 +02:00
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
actions: {
|
|
|
|
daysSelected({ moment: newMoments }) {
|
|
|
|
let { options } = this;
|
|
|
|
|
|
|
|
if (!isArray(newMoments)) {
|
|
|
|
// special case: all options are unselected
|
2016-07-27 23:06:54 +02:00
|
|
|
options.clear();
|
2019-01-20 15:20:54 +01:00
|
|
|
return;
|
2016-06-13 11:14:42 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
// array of options that represent days missing in updated selection
|
|
|
|
let removedOptions = options.filter((option) => {
|
|
|
|
return !newMoments.find((newMoment) => newMoment.format('YYYY-MM-DD') === option.day);
|
2016-06-13 11:14:42 +02:00
|
|
|
});
|
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
// array of moments that aren't represented yet by an option
|
|
|
|
let addedMoments = newMoments.filter((moment) => {
|
|
|
|
return !options.find((option) => moment.format('YYYY-MM-DD') === option.day);
|
2016-06-13 11:14:42 +02:00
|
|
|
});
|
2019-01-20 15:20:54 +01:00
|
|
|
|
|
|
|
// remove options that represent deselected days
|
|
|
|
options.removeObjects(removedOptions);
|
|
|
|
|
|
|
|
// add options for newly selected days
|
|
|
|
let newOptions = addedMoments.map((moment) => {
|
|
|
|
return this.store.createFragment('option', {
|
|
|
|
title: moment.format('YYYY-MM-DD'),
|
|
|
|
})
|
2016-06-13 11:14:42 +02:00
|
|
|
});
|
2019-01-20 15:20:54 +01:00
|
|
|
newOptions.forEach((newOption) => {
|
|
|
|
// options must be insert into options array at correct position
|
|
|
|
let insertBefore = options.find(({ date }) => {
|
|
|
|
if (!moment.isMoment(date)) {
|
|
|
|
// ignore options that do not represent a valid date
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
return date.isAfter(newOption.date);
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2019-01-20 15:20:54 +01:00
|
|
|
let position = isPresent(insertBefore) ? options.indexOf(insertBefore) : options.length;
|
|
|
|
options.insertAt(position, newOption);
|
2016-06-13 11:14:42 +02:00
|
|
|
});
|
2019-01-20 15:20:54 +01:00
|
|
|
},
|
|
|
|
updateCalenderCenter(diff) {
|
|
|
|
this.calendarCenter.add(diff, 'months');
|
|
|
|
this.notifyPropertyChange('calenderCenter');
|
|
|
|
},
|
|
|
|
},
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
init() {
|
|
|
|
this._super(arguments);
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2019-01-20 15:20:54 +01:00
|
|
|
let { selectedDays } = this;
|
|
|
|
this.set('calendarCenter', selectedDays.length >= 1 ? selectedDays[0] : moment());
|
|
|
|
},
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|