2014-11-21 12:38:32 +01:00
|
|
|
import Ember from "ember";
|
2015-07-19 19:20:31 +02:00
|
|
|
import moment from "moment";
|
2015-07-19 19:49:40 +02:00
|
|
|
/* global jstz */
|
2014-11-21 12:38:32 +01:00
|
|
|
|
2015-07-01 16:21:18 +02:00
|
|
|
export default Ember.Controller.extend({
|
2014-11-21 12:24:24 +01:00
|
|
|
optionsDates: [],
|
|
|
|
optionsDateTimes: [],
|
|
|
|
optionsTexts: [{value: ''}, {value: ''}],
|
|
|
|
|
|
|
|
updateOptions: function() {
|
2015-07-01 16:21:18 +02:00
|
|
|
var self = this,
|
|
|
|
options = [];
|
2014-11-21 12:24:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* find a date
|
|
|
|
* options are dates or datetimes
|
|
|
|
*/
|
2015-07-01 16:21:18 +02:00
|
|
|
if (this.get('model.isFindADate')) {
|
2015-06-24 02:14:14 +02:00
|
|
|
|
2015-07-01 16:21:18 +02:00
|
|
|
if (this.get('model.isDateTime')) {
|
2014-11-21 12:24:24 +01:00
|
|
|
// merge days and times
|
2015-06-24 02:14:14 +02:00
|
|
|
this.get('optionsDateTimes').forEach(function(day) {
|
|
|
|
// map dates and times
|
|
|
|
var validTimeFound = false;
|
2014-11-21 12:24:24 +01:00
|
|
|
day.get('times').forEach(function(timeObject) {
|
|
|
|
var date = new Date( day.title ),
|
|
|
|
timeString = timeObject.value;
|
|
|
|
|
|
|
|
if (self.validateTimeString(timeString)) {
|
|
|
|
var time = timeString.split(':');
|
|
|
|
|
|
|
|
date.setHours(time[0]);
|
|
|
|
date.setMinutes(time[1]);
|
|
|
|
|
|
|
|
options.pushObject({
|
2015-07-19 19:20:31 +02:00
|
|
|
title: moment(date).toISOString()
|
2014-11-21 12:24:24 +01:00
|
|
|
});
|
2015-06-24 02:14:14 +02:00
|
|
|
|
|
|
|
validTimeFound = true;
|
2014-11-21 12:24:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// set options to days
|
2015-07-19 19:33:41 +02:00
|
|
|
options = this.get('optionsDates').map(function(day) {
|
2015-07-18 21:00:28 +02:00
|
|
|
return {
|
2015-07-19 19:33:41 +02:00
|
|
|
// ISO 8601 date format
|
|
|
|
title: moment( day.title ).format('YYYY-MM-DD')
|
2015-07-18 21:00:28 +02:00
|
|
|
};
|
|
|
|
});
|
2014-11-21 12:24:24 +01:00
|
|
|
}
|
2015-06-24 02:14:14 +02:00
|
|
|
|
|
|
|
// days should be sorted to get them in correct order
|
|
|
|
options.sort(function(a, b){
|
2015-07-19 21:07:15 +02:00
|
|
|
if (a.title === b.title) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return a.title > b.title ? 1 : -1;
|
|
|
|
}
|
2015-06-24 02:14:14 +02:00
|
|
|
});
|
2014-11-21 12:24:24 +01:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* make a poll
|
|
|
|
* options are text strings
|
|
|
|
*/
|
|
|
|
else {
|
|
|
|
// remove all empty strings
|
|
|
|
|
|
|
|
this.get('optionsTexts').forEach(function(optionText){
|
|
|
|
var textString = optionText.value.trim();
|
|
|
|
|
|
|
|
if (textString !== '') {
|
2015-07-01 16:21:18 +02:00
|
|
|
options.pushObject({
|
2014-11-21 12:24:24 +01:00
|
|
|
title: textString
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-01 16:21:18 +02:00
|
|
|
|
|
|
|
this.set('model.options', options);
|
|
|
|
}.observes('optionsDates.@each.title', 'optionsDateTimes.@each.title', 'optionsDateTimes.@each.@eachTimesValue', 'optionsTexts.@each.value', 'model.isDateTime'),
|
2014-11-21 12:24:24 +01:00
|
|
|
|
|
|
|
updateDateTimesAfterDateChange: function() {
|
|
|
|
var optionsDates = this.get('optionsDates'),
|
|
|
|
newOptionsDateTimes = [],
|
|
|
|
self = this;
|
|
|
|
|
|
|
|
optionsDates.forEach(function(optionDate){
|
|
|
|
var dateTime = self.get('optionsDateTimesObject').create({
|
|
|
|
title: optionDate.title,
|
|
|
|
times: [{value: ''}, {value: ''}]
|
|
|
|
});
|
|
|
|
|
|
|
|
newOptionsDateTimes.pushObject( dateTime );
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('optionsDateTimes', newOptionsDateTimes);
|
|
|
|
|
|
|
|
}.observes('optionsDates.@each.value'),
|
|
|
|
|
|
|
|
/*
|
|
|
|
* helper Object as work-a-round to observe a nested array
|
|
|
|
*/
|
|
|
|
optionsDateTimesObject: Ember.Object.extend({
|
|
|
|
'@eachTimesValue': function(){
|
|
|
|
var times = [];
|
|
|
|
this.get('times').forEach(function(value){
|
|
|
|
times.push(value.value);
|
|
|
|
});
|
|
|
|
return times;
|
|
|
|
}.property('times.@each.value')
|
|
|
|
}),
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uncomsumed computed property does not trigger observers
|
|
|
|
* therefore we have to retrieve computed property helper to observer
|
|
|
|
* nested array
|
|
|
|
*
|
|
|
|
* More Information in Ember 1.0 RC8 Release Changelog:
|
|
|
|
* Unconsumed Computed Properties Do Not Trigger Observers
|
|
|
|
* http://emberjs.com/blog/2013/08/29/ember-1-0-rc8.html
|
|
|
|
*/
|
|
|
|
fixObserverOnOptionsDateTimesObject: function() {
|
|
|
|
this.get('optionsDateTimes').forEach(function(dateTime) {
|
|
|
|
dateTime.get('@eachTimesValue');
|
|
|
|
});
|
|
|
|
}.observes('optionsDateTimes.@each.@eachTimesValue').on('init'),
|
2015-07-19 19:49:40 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sets timezone property of model to users timezone if dates with
|
|
|
|
* times are specified
|
|
|
|
* otherwise we don't need to store timezone of user created the poll
|
|
|
|
*/
|
|
|
|
setTimezone: function() {
|
|
|
|
if(
|
|
|
|
this.get('model.isFindADate') &&
|
|
|
|
this.get('model.isDateTime')
|
|
|
|
) {
|
|
|
|
this.set('model.timezone', jstz.determine().name());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.set('model.timezone', '');
|
|
|
|
}
|
|
|
|
}.observes('model.isDateTime', 'model.isFindADate'),
|
2015-08-18 21:53:52 +02:00
|
|
|
|
2014-11-21 12:24:24 +01:00
|
|
|
/*
|
|
|
|
* validate if a given time string is in valid format
|
|
|
|
*/
|
|
|
|
validateTimeString: function(timeString) {
|
|
|
|
var time = timeString.split(':');
|
|
|
|
|
|
|
|
if (time.length === 2) {
|
|
|
|
var hours = time[0],
|
|
|
|
minutes = time[1];
|
|
|
|
|
|
|
|
if (hours >= 0 && hours <= 23 &&
|
|
|
|
minutes >= 0 && minutes <= 60) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-24 02:14:14 +02:00
|
|
|
});
|