2018-12-29 01:27:37 +01:00
|
|
|
import { isEmpty } from '@ember/utils';
|
2023-09-21 12:30:14 +02:00
|
|
|
import { DateTime } from 'luxon';
|
2020-01-18 10:13:50 +01:00
|
|
|
|
2023-10-28 19:15:06 +02:00
|
|
|
export default class Option {
|
|
|
|
// Title of the option might be either:
|
|
|
|
//
|
|
|
|
// 1) ISO 8601 date string: `YYYY-MM-DD`
|
|
|
|
// 2) ISO 8601 datetime string: `YYYY-MM-DDTHH:mm:ss.0000+01:00`
|
|
|
|
// 3) Free text if poll type is MakeAPoll
|
2023-10-29 19:16:33 +01:00
|
|
|
title: string;
|
2020-01-18 10:13:50 +01:00
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
get datetime() {
|
|
|
|
const { title } = this;
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
if (isEmpty(title)) {
|
2020-01-18 10:13:50 +01:00
|
|
|
return null;
|
2016-06-08 13:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 19:16:33 +01:00
|
|
|
const datetime = DateTime.fromISO(title);
|
|
|
|
|
|
|
|
return datetime.isValid ? datetime : null;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
get isDate() {
|
|
|
|
const { datetime } = this;
|
2023-10-29 19:16:33 +01:00
|
|
|
return datetime !== null;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
get day() {
|
2023-11-04 14:54:30 +01:00
|
|
|
if (this.datetime === null) {
|
2020-01-18 10:13:50 +01:00
|
|
|
return null;
|
2016-06-08 13:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
return this.datetime.toISODate();
|
|
|
|
}
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
get jsDate() {
|
2023-11-04 14:54:30 +01:00
|
|
|
if (this.datetime === null) {
|
2023-10-29 19:16:33 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-09-21 12:30:14 +02:00
|
|
|
return this.datetime.toJSDate();
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
get hasTime() {
|
2023-10-15 20:37:03 +02:00
|
|
|
return this.isDate && this.title.length >= 'YYYY-MM-DDTHH:mm'.length;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
2016-06-20 20:48:48 +02:00
|
|
|
|
2020-01-18 10:13:50 +01:00
|
|
|
get time() {
|
2023-10-29 19:16:33 +01:00
|
|
|
if (!this.datetime || !this.hasTime) {
|
2020-01-18 10:13:50 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-10-29 19:16:33 +01:00
|
|
|
return this.datetime.toISOTime()?.substring(0, 5);
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
|
|
|
|
2023-10-29 19:16:33 +01:00
|
|
|
constructor({ title }: { title: string }) {
|
2023-10-28 19:15:06 +02:00
|
|
|
this.title = title;
|
2020-01-18 10:13:50 +01:00
|
|
|
}
|
|
|
|
}
|