2020-01-18 10:13:50 +01:00
|
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
import { action, computed } from '@ember/object';
|
2018-12-29 01:27:37 +01:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-01-18 10:13:50 +01:00
|
|
|
import Component from '@ember/component';
|
2018-12-29 01:27:37 +01:00
|
|
|
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';
|
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@classic
|
|
|
|
export default class CreateOptionsDates extends Component {
|
|
|
|
@service('store')
|
|
|
|
store;
|
|
|
|
|
|
|
|
@computed('options.[]')
|
|
|
|
get selectedDays() {
|
2019-01-20 15:20:54 +01:00
|
|
|
return this.options
|
|
|
|
// should be unique
|
|
|
|
.uniqBy('day')
|
|
|
|
// raw dates
|
|
|
|
.map(({ date }) => date)
|
|
|
|
// filter out invalid
|
|
|
|
.filter(moment.isMoment)
|
|
|
|
.toArray();
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('calendarCenter')
|
|
|
|
get calendarCenterNext() {
|
2019-01-20 15:20:54 +01:00
|
|
|
return moment(this.calendarCenter).add(1, 'months');
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-07-27 23:06:54 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@action
|
|
|
|
daysSelected({ moment: newMoments }) {
|
|
|
|
let { options } = this;
|
2019-01-20 15:20:54 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
if (!isArray(newMoments)) {
|
|
|
|
// special case: all options are unselected
|
|
|
|
options.clear();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-13 11:14:42 +02:00
|
|
|
|
2020-01-18 10:13:50 +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
|
|
|
|
2020-01-18 10:13:50 +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);
|
|
|
|
});
|
2019-01-20 15:20:54 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
// remove options that represent deselected days
|
|
|
|
options.removeObjects(removedOptions);
|
2019-01-20 15:20:54 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
// add options for newly selected days
|
|
|
|
let newOptions = addedMoments.map((moment) => {
|
|
|
|
return this.store.createFragment('option', {
|
|
|
|
title: moment.format('YYYY-MM-DD'),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
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
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
return date.isAfter(newOption.date);
|
2016-06-13 11:14:42 +02:00
|
|
|
});
|
2020-01-18 10:13:50 +01:00
|
|
|
let position = isPresent(insertBefore) ? options.indexOf(insertBefore) : options.length;
|
|
|
|
options.insertAt(position, newOption);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
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() {
|
2020-01-18 10:13:50 +01:00
|
|
|
super.init(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());
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
|
|
|
}
|