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';
|
2020-01-18 10:13:50 +01:00
|
|
|
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
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
export default class PollController extends Controller {
|
|
|
|
@service
|
|
|
|
encryption;
|
|
|
|
|
|
|
|
@service
|
|
|
|
flashMessages;
|
|
|
|
|
|
|
|
@service
|
|
|
|
i18n;
|
|
|
|
|
|
|
|
@service
|
|
|
|
router;
|
|
|
|
|
|
|
|
queryParams = ['encryptionKey'];
|
2016-05-24 01:07:14 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
encryptionKey = '';
|
|
|
|
timezoneChoosen = false;
|
|
|
|
useLocalTimezone = false;
|
2017-08-11 16:39:36 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@readOnly('i18n.locale')
|
|
|
|
currentLocale;
|
2014-10-30 20:43:22 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('currentLocale')
|
|
|
|
get momentLongDayFormat() {
|
2018-12-29 20:35:04 +01:00
|
|
|
let currentLocale = this.currentLocale;
|
2017-08-11 16:39:36 +02:00
|
|
|
return moment.localeData(currentLocale)
|
|
|
|
.longDateFormat('LLLL')
|
|
|
|
.replace(
|
|
|
|
moment.localeData(currentLocale).longDateFormat('LT'), '')
|
|
|
|
.trim();
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2017-08-11 16:39:36 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@readOnly('model')
|
|
|
|
poll;
|
2015-08-19 22:00:01 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('router.currentURL', 'encryptionKey')
|
|
|
|
get pollUrl() {
|
|
|
|
return window.location.href;
|
|
|
|
}
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('poll.expirationDate')
|
|
|
|
get showExpirationWarning() {
|
2019-04-20 23:29:59 +02:00
|
|
|
let expirationDate = this.poll.expirationDate;
|
2017-08-26 02:14:09 +02:00
|
|
|
if (isEmpty(expirationDate)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return moment().add(2, 'weeks').isAfter(moment(expirationDate));
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-05-26 13:17:47 +02:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
/*
|
|
|
|
* return true if current timezone differs from timezone poll got created with
|
|
|
|
*/
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('poll.timezone')
|
|
|
|
get timezoneDiffers() {
|
2019-04-20 23:29:59 +02:00
|
|
|
let modelTimezone = this.poll.timezone;
|
2017-08-26 01:54:41 +02:00
|
|
|
return isPresent(modelTimezone) && moment.tz.guess() !== modelTimezone;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-05-26 13:17:47 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('timezoneDiffers', 'timezoneChoosen')
|
|
|
|
get mustChooseTimezone() {
|
2018-12-29 20:35:04 +01:00
|
|
|
return this.timezoneDiffers && !this.timezoneChoosen;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2017-08-11 16:39:36 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
@computed('useLocalTimezone')
|
|
|
|
get timezone() {
|
2019-04-20 23:29:59 +02:00
|
|
|
return this.useLocalTimezone ? undefined : this.poll.timezone;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|