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
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
export default Controller.extend({
|
2019-04-20 23:29:59 +02:00
|
|
|
encryption: service(),
|
|
|
|
flashMessages: service(),
|
|
|
|
i18n: service(),
|
|
|
|
router: service(),
|
|
|
|
|
2016-05-24 01:07:14 +02:00
|
|
|
actions: {
|
2016-08-20 22:02:53 +02:00
|
|
|
linkAction(type) {
|
2018-12-29 20:35:04 +01:00
|
|
|
let flashMessages = this.flashMessages;
|
2016-08-20 22:02:53 +02:00
|
|
|
switch (type) {
|
|
|
|
case 'copied':
|
|
|
|
flashMessages.success(`poll.link.copied`);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'selected':
|
|
|
|
flashMessages.info(`poll.link.selected`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2016-05-24 01:07:14 +02:00
|
|
|
useLocalTimezone() {
|
|
|
|
this.set('useLocalTimezone', true);
|
2016-05-26 13:17:47 +02:00
|
|
|
this.set('timezoneChoosen', true);
|
2016-05-24 01:07:14 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-12-29 01:27:37 +01:00
|
|
|
currentLocale: readOnly('i18n.locale'),
|
2017-08-11 16:39:36 +02:00
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
encryptionKey: '',
|
|
|
|
queryParams: ['encryptionKey'],
|
2014-10-30 20:43:22 +01:00
|
|
|
|
2017-08-11 16:39:36 +02:00
|
|
|
momentLongDayFormat: computed('currentLocale', function() {
|
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();
|
|
|
|
}),
|
|
|
|
|
2019-04-20 23:29:59 +02:00
|
|
|
poll: readOnly('model'),
|
|
|
|
pollUrl: computed('router.currentURL', 'encryptionKey', function() {
|
2015-07-07 11:52:46 +02:00
|
|
|
return window.location.href;
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-08-19 22:00:01 +02:00
|
|
|
|
2019-04-20 23:29:59 +02:00
|
|
|
// TODO: Remove this code. It's spooky.
|
2016-08-02 01:55:48 +02:00
|
|
|
preventEncryptionKeyChanges: observer('encryptionKey', function() {
|
2015-08-19 22:00:01 +02:00
|
|
|
if (
|
2019-04-20 23:29:59 +02:00
|
|
|
!isEmpty(this.encryption.key) &&
|
|
|
|
this.encryptionKey !== this.encryption.key
|
2015-08-19 22:00:01 +02:00
|
|
|
) {
|
|
|
|
// work-a-round for url not being updated
|
2019-04-20 23:29:59 +02:00
|
|
|
window.location.hash = window.location.hash.replace(this.encryptionKey, this.encryption.key);
|
2015-08-19 22:00:01 +02:00
|
|
|
|
2019-04-20 23:29:59 +02:00
|
|
|
this.set('encryptionKey', this.encryption.key);
|
2015-08-19 22:00:01 +02:00
|
|
|
}
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2019-04-20 23:29:59 +02:00
|
|
|
showExpirationWarning: computed('poll.expirationDate', function() {
|
|
|
|
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));
|
|
|
|
}),
|
|
|
|
|
2016-05-26 13:17:47 +02:00
|
|
|
timezoneChoosen: false,
|
|
|
|
|
2015-07-07 11:52:46 +02:00
|
|
|
/*
|
|
|
|
* return true if current timezone differs from timezone poll got created with
|
|
|
|
*/
|
2019-04-20 23:29:59 +02:00
|
|
|
timezoneDiffers: computed('poll.timezone', function() {
|
|
|
|
let modelTimezone = this.poll.timezone;
|
2017-08-26 01:54:41 +02:00
|
|
|
return isPresent(modelTimezone) && moment.tz.guess() !== modelTimezone;
|
2016-05-20 23:43:05 +02:00
|
|
|
}),
|
2015-11-02 23:02:59 +01:00
|
|
|
|
2016-05-26 13:17:47 +02:00
|
|
|
useLocalTimezone: false,
|
|
|
|
|
2016-08-02 01:55:48 +02:00
|
|
|
mustChooseTimezone: computed('timezoneDiffers', 'timezoneChoosen', function() {
|
2018-12-29 20:35:04 +01:00
|
|
|
return this.timezoneDiffers && !this.timezoneChoosen;
|
2017-08-11 16:39:36 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
timezone: computed('useLocalTimezone', function() {
|
2019-04-20 23:29:59 +02:00
|
|
|
return this.useLocalTimezone ? undefined : this.poll.timezone;
|
2016-05-26 13:17:47 +02:00
|
|
|
})
|
2014-10-30 20:43:22 +01:00
|
|
|
});
|