2018-12-29 01:27:37 +01:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { computed } from '@ember/object';
|
|
|
|
import Component from '@ember/component';
|
|
|
|
import { isArray } from '@ember/array';
|
|
|
|
import { isEmpty } 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(),
|
2016-03-18 13:15:47 +01:00
|
|
|
|
2016-01-19 04:56:51 +01:00
|
|
|
/*
|
|
|
|
* maps optionsDates for bootstrap datepicker as a simple array of date objects
|
|
|
|
*/
|
2016-08-02 01:55:48 +02:00
|
|
|
optionsBootstrapDatepicker: computed('options', {
|
2016-01-19 04:56:51 +01:00
|
|
|
get() {
|
2018-12-29 20:35:04 +01:00
|
|
|
const options = this.options;
|
2016-05-21 18:37:23 +02:00
|
|
|
const validDates = options.filter(function(option) {
|
|
|
|
return moment(option.get('title')).isValid();
|
|
|
|
});
|
|
|
|
const normalizedDates = validDates.map(function(option) {
|
|
|
|
return moment(option.get('title'))
|
|
|
|
.hour(0)
|
|
|
|
.minute(0)
|
|
|
|
.millisecond(0);
|
|
|
|
});
|
|
|
|
// convert to primitive to allow support Ember.Array.uniq()
|
|
|
|
const uniqueDateStrings = normalizedDates
|
|
|
|
.map((moment) => {
|
|
|
|
return moment.toISOString();
|
|
|
|
})
|
|
|
|
.uniq();
|
|
|
|
const dateObjects = uniqueDateStrings.map(function(dateString) {
|
|
|
|
return moment(dateString).toDate();
|
|
|
|
});
|
|
|
|
return dateObjects;
|
2016-01-19 04:56:51 +01:00
|
|
|
},
|
2016-06-13 11:14:42 +02:00
|
|
|
/*
|
|
|
|
* value is an of Date objects set by ember-cli-bootstrap-datepicker
|
|
|
|
*/
|
|
|
|
set(key, days) {
|
2018-12-29 20:35:04 +01:00
|
|
|
const options = this.options;
|
2016-07-27 23:06:54 +02:00
|
|
|
|
2016-06-13 11:14:42 +02:00
|
|
|
// remove all days if value isn't an array of if it's empty
|
|
|
|
if (!isArray(days) || isEmpty(days)) {
|
2016-07-27 23:06:54 +02:00
|
|
|
options.clear();
|
2016-06-27 12:49:40 +02:00
|
|
|
return [];
|
2016-06-13 11:14:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// get days in correct order
|
|
|
|
days.sort(function(a, b) {
|
|
|
|
return a.getTime() - b.getTime();
|
|
|
|
});
|
|
|
|
|
|
|
|
// array of date objects
|
|
|
|
const newDays = days.filter((day) => {
|
|
|
|
return options.every((option) => {
|
|
|
|
return moment(day).format('YYYY-MM-DD') !== option.get('day');
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2016-06-13 11:14:42 +02:00
|
|
|
});
|
|
|
|
// array of options fragments
|
|
|
|
const optionsForRemovedDays = options.filter((option) => {
|
|
|
|
return days.every((day) => {
|
|
|
|
return moment(day).format('YYYY-MM-DD') !== option.get('day');
|
|
|
|
});
|
|
|
|
});
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2016-06-13 11:14:42 +02:00
|
|
|
options.removeObjects(optionsForRemovedDays);
|
|
|
|
newDays.forEach((newDay) => {
|
|
|
|
// new days must be entered at correct position
|
|
|
|
const insertBefore = options.find((option) => {
|
|
|
|
// options are sorted
|
|
|
|
// so we search for first option which value is greater than newDay
|
|
|
|
return option.get('date').valueOf() > newDay.valueOf();
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2016-06-13 11:14:42 +02:00
|
|
|
let position;
|
|
|
|
if (isEmpty(insertBefore)) {
|
|
|
|
// newDay is after all existing days
|
|
|
|
position = options.get('length');
|
|
|
|
} else {
|
|
|
|
position = options.indexOf(insertBefore);
|
|
|
|
}
|
|
|
|
options.insertAt(
|
|
|
|
position,
|
2018-12-29 20:35:04 +01:00
|
|
|
this.store.createFragment('option', {
|
2016-06-13 11:14:42 +02:00
|
|
|
title: moment(newDay).format('YYYY-MM-DD')
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2016-06-13 11:14:42 +02:00
|
|
|
return days;
|
2016-01-19 04:56:51 +01:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-12-29 01:27:37 +01:00
|
|
|
store: service('store')
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|