decide.nolog.cz/app/controllers/poll.js

198 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-01-28 11:27:00 +01:00
import Ember from 'ember';
import moment from 'moment';
/* global jstz */
2014-10-30 21:44:22 +01:00
export default Ember.Controller.extend({
encryption: Ember.inject.service(),
2015-07-07 11:52:46 +02:00
encryptionKey: '',
queryParams: ['encryptionKey'],
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 [];
}
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 = [];
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-07-07 11:52:46 +02:00
if (lastDate === null) {
lastDate = date;
}
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-07-07 11:52:46 +02:00
// set lastDate to current date and reset count
lastDate = date;
count = 1;
}
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-07-07 11:52:46 +02:00
return dateGroups;
}),
2015-07-07 11:52:46 +02:00
/*
* handles options if they are dates
*/
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-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 [];
}
// 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') &&
!this.get('useLocalTimezone')
) {
timezone = this.get('model.timezone');
2015-07-07 11:52:46 +02: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) => {
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) {
date.tz(timezone);
}
2016-03-17 13:32:53 +01:00
return dateObject.create({
title: date,
2016-03-17 13:32:53 +01:00
hasTime,
// inject container otherwise we could not inject i18n service
container
});
});
return dates;
2016-03-17 13:32:53 +01:00
}),
2015-07-15 14:24:57 +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-18 22:18:36 +02:00
pollUrl: Ember.computed('currentPath', 'encryptionKey', function() {
2015-07-07 11:52:46 +02:00
return window.location.href;
}),
preventEncryptionKeyChanges: Ember.observer('encryptionKey', function() {
if (
!Ember.isEmpty(this.get('encryption.key')) &&
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'));
}
}),
2015-07-07 11:52:46 +02:00
/*
* return true if current timezone differs from timezone poll got created with
*/
timezoneDiffers: Ember.computed('model.timezone', function() {
const modelTimezone = this.get('model.timezone');
return Ember.isPresent(modelTimezone) && jstz.determine().name() !== modelTimezone;
}),
useLocalTimezone: false
});