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

122 lines
3 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'],
2015-07-07 11:52:46 +02:00
dateGroups: function() {
// group dates only for find a date with times
2016-01-28 23:48:14 +01:00
if (
this.get('model.isFindADate') !== true ||
this.get('model.isDateTime') !== true
) {
2015-07-07 11:52:46 +02:00
return [];
}
2016-01-28 11:27:00 +01:00
const datetimes = this.get('dates');
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) {
let date = new Date(el.title);
2015-07-07 11:52:46 +02:00
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
2015-07-07 11:52:46 +02:00
if (lastDate === null) {
lastDate = date;
}
2015-07-07 11:52:46 +02:00
if (date.getTime() === lastDate.getTime()) {
count++;
2016-01-28 11:27:00 +01:00
} else {
2015-07-07 11:52:46 +02:00
// push last values;
dateGroups.pushObject({
2016-01-28 23:48:14 +01:00
'value': lastDate,
'colspan': count
});
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
});
dateGroups.pushObject({
2016-01-28 23:48:14 +01:00
'value': lastDate,
'colspan': count
2015-07-07 11:52:46 +02:00
});
2015-07-07 11:52:46 +02:00
return dateGroups;
}.property('dates.@each'),
2015-07-07 11:52:46 +02:00
/*
* handles options if they are dates
*/
dates: function() {
2016-01-28 11:27:00 +01:00
let timezone = false;
let dates = [];
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 (
this.get('model.isDateTime') &&
!this.get('useLocalTimezone')
) {
timezone = this.get('model.timezone');
2015-07-07 11:52:46 +02:00
}
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;
if (timezone) {
date.tz(timezone);
}
2015-10-13 11:29:51 +02:00
return {
title: date,
hasTime
2015-10-13 11:29:51 +02:00
};
});
return dates;
2015-07-07 11:52:46 +02:00
}.property('model.options.@each', 'useLocalTimezone'),
2015-07-15 14:24:57 +02:00
2015-07-07 11:52:46 +02:00
pollUrl: function() {
return window.location.href;
}.property('currentPath', 'encryptionKey'),
preventEncryptionKeyChanges: 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'));
}
}.observes('encryptionKey'),
2015-07-07 11:52:46 +02:00
/*
* return true if current timezone differs from timezone poll got created with
*/
timezoneDiffers: function() {
const modelTimezone = this.get('model.timezone');
return Ember.isPresent(modelTimezone) && jstz.determine().name() !== modelTimezone;
}.property('model.timezone'),
2015-07-07 11:52:46 +02:00
useLocalTimezone: function() {
return false;
}.property()
});