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

117 lines
2.9 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
if ( this.get('model.isFindADate') !== true ||
this.get('model.isDateTime') !== true ) {
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;
2015-07-07 11:52:46 +02:00
datetimes.forEach(function(el){
2016-01-28 11:27:00 +01:00
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({
"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({
"value": lastDate,
"colspan": count
});
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
if( !this.get('model.isFindADate') ) {
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
}
2015-10-13 11:29:51 +02:00
dates = this.get('model.options').map(function(option){
2016-01-28 11:27:00 +01:00
let date = moment(option.get('title'));
if (timezone) {
date.tz(timezone);
}
2015-10-13 11:29:51 +02:00
return {
title: date
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() {
return jstz.determine().name() !== this.get('model.timezone');
}.property('model.timezone'),
2015-07-07 11:52:46 +02:00
useLocalTimezone: function() {
return false;
}.property()
});