2016-01-19 04:56:51 +01:00
|
|
|
import Ember from 'ember';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
2016-03-18 13:15:47 +01:00
|
|
|
i18n: Ember.inject.service(),
|
|
|
|
|
2016-01-19 04:56:51 +01:00
|
|
|
/*
|
|
|
|
* maps optionsDates for bootstrap datepicker as a simple array of date objects
|
|
|
|
*/
|
|
|
|
optionsBootstrapDatepicker: Ember.computed('options', {
|
|
|
|
get() {
|
2016-05-21 18:37:23 +02:00
|
|
|
const options = this.get('options');
|
|
|
|
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
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
// ember-cli-bootstrap-datepicker returns an array of Date objects
|
|
|
|
let newOptions = [];
|
2016-01-20 02:52:21 +01:00
|
|
|
if (Ember.isArray(value) && value.length > 0) {
|
2016-01-19 04:56:51 +01:00
|
|
|
value.sort(function(a, b) {
|
|
|
|
return a.getTime() - b.getTime();
|
|
|
|
});
|
|
|
|
|
|
|
|
newOptions = value.map((item) => {
|
|
|
|
return this.get('store').createFragment('option', {
|
|
|
|
title: moment(item).format('YYYY-MM-DD')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.set('options', newOptions);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
store: Ember.inject.service('store')
|
|
|
|
});
|