2016-01-28 11:27:00 +01:00
|
|
|
import Ember from 'ember';
|
|
|
|
import moment from 'moment';
|
2015-07-15 16:20:24 +02:00
|
|
|
/* global jstz */
|
2014-10-30 21:44:22 +01:00
|
|
|
|
2015-11-02 23:02:59 +01:00
|
|
|
export default Ember.Controller.extend({
|
2015-10-25 19:38:14 +01:00
|
|
|
encryption: Ember.inject.service(),
|
2015-07-07 11:52:46 +02:00
|
|
|
encryptionKey: '',
|
|
|
|
queryParams: ['encryptionKey'],
|
2014-10-30 20:43:22 +01:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
dateGroups: Ember.computed('dates.[]', function() {
|
2016-05-18 22:18:36 +02:00
|
|
|
// group dates only for find a date
|
|
|
|
// and if there is atleast one time
|
2016-01-28 23:48:14 +01:00
|
|
|
if (
|
2016-05-18 22:18:36 +02:00
|
|
|
this.get('model.isFindADate') !== true &&
|
|
|
|
this.get('hasTimes')
|
2016-01-28 23:48:14 +01:00
|
|
|
) {
|
2015-07-07 11:52:46 +02:00
|
|
|
return [];
|
|
|
|
}
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-01-28 11:27:00 +01:00
|
|
|
const datetimes = this.get('dates');
|
2016-05-21 20:05:13 +02:00
|
|
|
const dateGroupObject = Ember.Object.extend({
|
|
|
|
colspan: null,
|
|
|
|
formatted: Ember.computed('value', 'i18n.locale', function() {
|
|
|
|
const date = this.get('value');
|
|
|
|
const locale = this.get('i18n.locale');
|
|
|
|
const longDateFormat = moment.localeData().longDateFormat('LLLL')
|
|
|
|
.replace(
|
|
|
|
moment.localeData().longDateFormat('LT'), '')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
// moment caches locale so we have to check if it's changed
|
|
|
|
if (date.locale() !== locale) {
|
|
|
|
date.locale(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return date.format(longDateFormat);
|
|
|
|
}),
|
|
|
|
i18n: Ember.inject.service(),
|
|
|
|
value: null
|
|
|
|
});
|
|
|
|
// need to inject container into dateGroupObject to support service injection
|
|
|
|
const container = this.get('container');
|
2016-01-28 11:27:00 +01:00
|
|
|
let dateGroups = [];
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-01-28 11:27:00 +01:00
|
|
|
let count = 0;
|
|
|
|
let lastDate = null;
|
2016-01-28 23:48:14 +01:00
|
|
|
datetimes.forEach(function(el) {
|
2016-05-21 20:05:13 +02:00
|
|
|
let date = moment(el.title)
|
|
|
|
.hour(0)
|
|
|
|
.minute(0)
|
|
|
|
.seconds(0)
|
|
|
|
.milliseconds(0);
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
if (lastDate === null) {
|
|
|
|
lastDate = date;
|
2014-10-30 20:43:22 +01:00
|
|
|
}
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-05-21 20:05:13 +02:00
|
|
|
if (date.valueOf() === lastDate.valueOf()) {
|
2015-07-07 11:52:46 +02:00
|
|
|
count++;
|
2016-01-28 11:27:00 +01:00
|
|
|
} else {
|
2015-07-07 11:52:46 +02:00
|
|
|
// push last values;
|
2016-05-21 20:05:13 +02:00
|
|
|
dateGroups.pushObject(
|
|
|
|
dateGroupObject.create({
|
|
|
|
'value': lastDate,
|
|
|
|
'colspan': count,
|
|
|
|
container
|
|
|
|
})
|
|
|
|
);
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
// set lastDate to current date and reset count
|
|
|
|
lastDate = date;
|
|
|
|
count = 1;
|
2014-10-30 20:43:22 +01:00
|
|
|
}
|
2015-07-07 11:52:46 +02:00
|
|
|
});
|
2016-05-21 20:05:13 +02:00
|
|
|
dateGroups.pushObject(
|
|
|
|
dateGroupObject.create({
|
|
|
|
'value': lastDate,
|
|
|
|
'colspan': count,
|
|
|
|
container
|
|
|
|
})
|
|
|
|
);
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
return dateGroups;
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
/*
|
|
|
|
* handles options if they are dates
|
|
|
|
*/
|
2016-05-20 23:43:05 +02:00
|
|
|
dates: Ember.computed('model.options.[]', 'useLocalTimezone', function() {
|
2016-01-28 11:27:00 +01:00
|
|
|
let timezone = false;
|
|
|
|
let dates = [];
|
2016-03-17 13:32:53 +01:00
|
|
|
const dateObject = Ember.Object.extend({
|
|
|
|
i18n: Ember.inject.service(),
|
|
|
|
init() {
|
|
|
|
// retrive locale to setup observers
|
|
|
|
this.get('i18n.locale');
|
|
|
|
},
|
|
|
|
formatted: Ember.computed('title', 'i18n.locale', function() {
|
|
|
|
const date = this.get('title');
|
2016-05-21 20:05:13 +02:00
|
|
|
const locale = this.get('i18n.locale');
|
2016-03-17 13:32:53 +01:00
|
|
|
|
|
|
|
// locale is stored on date, we have to override it if it has changed since creation
|
2016-05-21 20:05:13 +02:00
|
|
|
if (date.locale() !== locale) {
|
2016-03-17 13:32:53 +01:00
|
|
|
date.locale(this.get('i18n.locale'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.get('hasTime') ? date.format('LLLL') : date.format(
|
|
|
|
moment.localeData()
|
|
|
|
.longDateFormat('LLLL')
|
|
|
|
.replace(
|
|
|
|
moment.localeData().longDateFormat('LT'), '')
|
|
|
|
.trim()
|
|
|
|
);
|
2016-05-21 20:05:13 +02:00
|
|
|
}),
|
|
|
|
formattedTime: Ember.computed('title', 'i18n.locale', function() {
|
|
|
|
const date = this.get('title');
|
|
|
|
const locale = this.get('i18n.locale');
|
|
|
|
|
|
|
|
// locale is stored on date, we have to override it if it has changed since creation
|
|
|
|
if (date.locale() !== locale) {
|
|
|
|
date.locale(this.get('i18n.locale'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return date.format('LT');
|
2016-03-17 13:32:53 +01:00
|
|
|
})
|
|
|
|
});
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
// if poll type is find a date
|
|
|
|
// we return an empty array
|
2016-01-28 23:48:14 +01:00
|
|
|
if (!this.get('model.isFindADate')) {
|
2015-07-07 11:52:46 +02:00
|
|
|
return [];
|
|
|
|
}
|
2015-07-19 20:00:07 +02:00
|
|
|
|
|
|
|
// if poll has dates with times we have to care about timezone
|
|
|
|
// but since user timezone is default we only have to set timezone
|
|
|
|
// if timezone poll got created in should be used
|
2015-07-07 11:52:46 +02:00
|
|
|
if (
|
2016-05-18 22:18:36 +02:00
|
|
|
this.get('hasTimes') &&
|
2015-07-19 20:00:07 +02:00
|
|
|
!this.get('useLocalTimezone')
|
|
|
|
) {
|
2015-07-15 16:20:24 +02:00
|
|
|
timezone = this.get('model.timezone');
|
2015-07-07 11:52:46 +02:00
|
|
|
}
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-03-17 13:32:53 +01:00
|
|
|
const container = this.get('container');
|
2016-01-28 23:48:14 +01:00
|
|
|
dates = this.get('model.options').map((option) => {
|
2016-02-13 18:37:33 +01:00
|
|
|
const date = moment(option.get('title'));
|
|
|
|
const hasTime = moment(option.get('title'), 'YYYY-MM-DD', true).isValid() === false;
|
2016-05-18 22:18:36 +02:00
|
|
|
if (timezone && hasTime) {
|
2015-07-19 20:00:07 +02:00
|
|
|
date.tz(timezone);
|
|
|
|
}
|
2016-03-17 13:32:53 +01:00
|
|
|
return dateObject.create({
|
2016-02-13 18:37:33 +01:00
|
|
|
title: date,
|
2016-03-17 13:32:53 +01:00
|
|
|
hasTime,
|
|
|
|
// inject container otherwise we could not inject i18n service
|
|
|
|
container
|
|
|
|
});
|
2015-07-15 16:20:24 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return dates;
|
2016-03-17 13:32:53 +01:00
|
|
|
}),
|
2015-07-15 14:24:57 +02:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
hasTimes: Ember.computed('model.options.[]', function() {
|
2016-05-18 22:18:36 +02:00
|
|
|
if (this.get('model.isMakeAPoll')) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return this.get('model.options').map((option) => {
|
|
|
|
return moment(option.get('title'), 'YYYY-MM-DD', true).isValid() === false;
|
|
|
|
});
|
|
|
|
}
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2016-05-18 22:18:36 +02:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
pollUrl: Ember.computed('currentPath', 'encryptionKey', function() {
|
2015-07-07 11:52:46 +02:00
|
|
|
return window.location.href;
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-08-19 22:00:01 +02:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
preventEncryptionKeyChanges: Ember.observer('encryptionKey', function() {
|
2015-08-19 22:00:01 +02:00
|
|
|
if (
|
2015-10-25 19:38:14 +01:00
|
|
|
!Ember.isEmpty(this.get('encryption.key')) &&
|
2015-08-19 22:00:01 +02:00
|
|
|
this.get('encryptionKey') !== this.get('encryption.key')
|
|
|
|
) {
|
|
|
|
// work-a-round for url not being updated
|
|
|
|
window.location.hash = window.location.hash.replace(this.get('encryptionKey'), this.get('encryption.key'));
|
|
|
|
|
|
|
|
this.set('encryptionKey', this.get('encryption.key'));
|
|
|
|
}
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
/*
|
|
|
|
* return true if current timezone differs from timezone poll got created with
|
|
|
|
*/
|
2016-05-20 23:43:05 +02:00
|
|
|
timezoneDiffers: Ember.computed('model.timezone', function() {
|
2016-02-23 00:02:16 +01:00
|
|
|
const modelTimezone = this.get('model.timezone');
|
|
|
|
return Ember.isPresent(modelTimezone) && jstz.determine().name() !== modelTimezone;
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-05-20 23:43:05 +02:00
|
|
|
useLocalTimezone: false
|
2014-10-30 20:43:22 +01:00
|
|
|
});
|