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

111 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 { action, computed } from '@ember/object';
import { observes } from '@ember-decorators/object';
2016-01-28 11:27:00 +01:00
import moment from 'moment';
2014-10-30 21:44:22 +01:00
export default class PollController extends Controller {
@service
encryption;
@service
flashMessages;
@service
intl;
@service
router;
queryParams = ['encryptionKey'];
encryptionKey = '';
timezoneChoosen = false;
useLocalTimezone = false;
@readOnly('intl.primaryLocale')
currentLocale;
@computed('currentLocale')
get momentLongDayFormat() {
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();
}
@readOnly('model')
poll;
@computed('router.currentURL', 'encryptionKey')
get pollUrl() {
return window.location.href;
}
@computed('poll.expirationDate')
get showExpirationWarning() {
let expirationDate = this.poll.expirationDate;
if (isEmpty(expirationDate)) {
return false;
}
return moment().add(2, 'weeks').isAfter(moment(expirationDate));
}
2015-07-07 11:52:46 +02:00
/*
* return true if current timezone differs from timezone poll got created with
*/
@computed('poll.timezone')
get timezoneDiffers() {
let modelTimezone = this.poll.timezone;
return isPresent(modelTimezone) && moment.tz.guess() !== modelTimezone;
}
@computed('timezoneDiffers', 'timezoneChoosen')
get mustChooseTimezone() {
2018-12-29 20:35:04 +01:00
return this.timezoneDiffers && !this.timezoneChoosen;
}
@computed('useLocalTimezone')
get timezone() {
return this.useLocalTimezone ? undefined : this.poll.timezone;
}
@action
linkAction(type) {
let flashMessages = this.flashMessages;
switch (type) {
case 'copied':
flashMessages.success(`poll.link.copied`);
break;
case 'selected':
flashMessages.info(`poll.link.selected`);
break;
}
}
@action
useLocalTimezone() {
this.set('useLocalTimezone', true);
this.set('timezoneChoosen', true);
}
// TODO: Remove this code. It's spooky.
@observes('encryptionKey')
preventEncryptionKeyChanges() {
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);
}
}
}