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

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-12-29 01:27:37 +01:00
import { inject as service } from '@ember/service';
import { readOnly } from '@ember/object/computed';
import Controller from '@ember/controller';
import { isPresent, isEmpty } from '@ember/utils';
import { observer, computed } from '@ember/object';
2016-01-28 11:27:00 +01:00
import moment from 'moment';
2014-10-30 21:44:22 +01:00
export default Controller.extend({
encryption: service(),
flashMessages: service(),
i18n: service(),
router: service(),
actions: {
linkAction(type) {
2018-12-29 20:35:04 +01:00
let flashMessages = this.flashMessages;
switch (type) {
case 'copied':
flashMessages.success(`poll.link.copied`);
break;
case 'selected':
flashMessages.info(`poll.link.selected`);
break;
}
},
useLocalTimezone() {
this.set('useLocalTimezone', true);
this.set('timezoneChoosen', true);
}
},
2018-12-29 01:27:37 +01:00
currentLocale: readOnly('i18n.locale'),
2015-07-07 11:52:46 +02:00
encryptionKey: '',
queryParams: ['encryptionKey'],
momentLongDayFormat: computed('currentLocale', function() {
2018-12-29 20:35:04 +01:00
let currentLocale = this.currentLocale;
return moment.localeData(currentLocale)
.longDateFormat('LLLL')
.replace(
moment.localeData(currentLocale).longDateFormat('LT'), '')
.trim();
}),
poll: readOnly('model'),
pollUrl: computed('router.currentURL', 'encryptionKey', function() {
2015-07-07 11:52:46 +02:00
return window.location.href;
}),
// TODO: Remove this code. It's spooky.
preventEncryptionKeyChanges: observer('encryptionKey', function() {
if (
!isEmpty(this.encryption.key) &&
this.encryptionKey !== this.encryption.key
) {
// work-a-round for url not being updated
window.location.hash = window.location.hash.replace(this.encryptionKey, this.encryption.key);
this.set('encryptionKey', this.encryption.key);
}
}),
showExpirationWarning: computed('poll.expirationDate', function() {
let expirationDate = this.poll.expirationDate;
if (isEmpty(expirationDate)) {
return false;
}
return moment().add(2, 'weeks').isAfter(moment(expirationDate));
}),
timezoneChoosen: false,
2015-07-07 11:52:46 +02:00
/*
* return true if current timezone differs from timezone poll got created with
*/
timezoneDiffers: computed('poll.timezone', function() {
let modelTimezone = this.poll.timezone;
return isPresent(modelTimezone) && moment.tz.guess() !== modelTimezone;
}),
useLocalTimezone: false,
mustChooseTimezone: computed('timezoneDiffers', 'timezoneChoosen', function() {
2018-12-29 20:35:04 +01:00
return this.timezoneDiffers && !this.timezoneChoosen;
}),
timezone: computed('useLocalTimezone', function() {
return this.useLocalTimezone ? undefined : this.poll.timezone;
})
});