UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

353 lines (352 loc) 12 kB
import { Level2Date as EdtfDate, Level2Duration as EdtfDuration, Level2Interval as EdtfInterval, Level2Timeshift } from "@rr0/time"; /** * Time context for a RR0 page. */ export class TimeContext { /** * * @param {number} [_year] * @param {number} [_month] * @param {number} [_dayOfMonth] * @param {number} [_hour] * @param {number} [_minutes] * @param {string} [_timeZone] * @param {boolean} [approximate] * @param {boolean} [approximateTime] * @param {TimeContext | undefined} [from] * @param {TimeContext | undefined} [to] * @param {number | undefined} [duration] */ constructor(_year = undefined, _month = undefined, _dayOfMonth = undefined, _hour = undefined, _minutes = undefined, _timeZone = undefined, approximate = false, approximateTime = false, /** @deprecated */ from = undefined, /** @deprecated */ to = undefined, duration = undefined) { if (from) { this.interval = new EdtfInterval(from === null || from === void 0 ? void 0 : from.date, to === null || to === void 0 ? void 0 : to.date); } else if (duration) { this.duration = new EdtfDuration(duration); } else { this.date = new EdtfDate({ year: _year, month: _month, day: _dayOfMonth, hour: _hour, minute: _minutes, timeZone: _timeZone }); if (approximate) { this.date.year.approximate = true; } if (approximateTime) { if (this.date.hour) { this.date.hour.approximateComponent = true; } if (this.date.minutes) { this.date.minutes.approximateComponent = true; } if (this.date.seconds) { this.date.seconds.approximateComponent = true; } } } } /** * @return {EdtfDate | undefined} */ get from() { var _a; return (_a = this.interval) === null || _a === void 0 ? void 0 : _a.start; } /** * @return {EdtfDate | undefined} */ get to() { var _a; return (_a = this.interval) === null || _a === void 0 ? void 0 : _a.end; } /** * @return {boolean} */ get approximate() { var _a, _b, _c; return ((_a = this.date) === null || _a === void 0 ? void 0 : _a.approximate) || ((_b = this.interval) === null || _b === void 0 ? void 0 : _b.approximate) || ((_c = this.duration) === null || _c === void 0 ? void 0 : _c.approximate); } /** * * @param {boolean} approx */ set approximate(approx) { this.date.approximate = approx; } /** * @return {boolean} */ get approximateTime() { var _a, _b, _c, _d, _e, _f; return this.approximate || ((_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.hour) === null || _b === void 0 ? void 0 : _b.approximate) || ((_d = (_c = this.date) === null || _c === void 0 ? void 0 : _c.minute) === null || _d === void 0 ? void 0 : _d.approximate) || ((_f = (_e = this.date) === null || _e === void 0 ? void 0 : _e.second) === null || _f === void 0 ? void 0 : _f.approximate); } /** * * @param {string} timeStr * @return {TimeContext} */ static fromString(timeStr) { const timeContext = new TimeContext(); timeContext.updateFromStr(timeStr); return timeContext; } /** * @param {Date} date * @return {TimeContext} */ static fromDate(date) { return new TimeContext(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), "UTC" + (date.getTimezoneOffset() < 0 ? "-" : "+") + date.getTimezoneOffset()); } /** * @param {string} timeStr * @return If the string could be parsed */ updateFromStr(timeStr) { let valid = Boolean(timeStr); if (valid) { try { this.interval = EdtfInterval.fromString(timeStr); this.date = undefined; this.duration = undefined; } catch (e) { try { this.duration = EdtfDuration.fromString(timeStr); this.interval = undefined; this.date = undefined; } catch (e) { try { this.date = EdtfDate.fromString(timeStr); this.duration = undefined; this.interval = undefined; } catch (e) { console.warn("Could not resolve time string", timeStr, e.message); valid = false; } } } } return valid; } /** * * @return {number | undefined} */ getYear() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.year) === null || _b === void 0 ? void 0 : _b.value; } /** * @param {number | undefined} year * @param {boolean} [print] * @return {TimeContext} */ setYear(year, print = true) { var _a, _b; if (year !== ((_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.year) === null || _b === void 0 ? void 0 : _b.value) && print) { this.setMonth(undefined, print); } this.date = this.date || new EdtfDate(); this.date.year = year; return this; } /** * @return {number | undefined} */ getMonth() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.month) === null || _b === void 0 ? void 0 : _b.value; } /** * * @param {number | undefined} month * @param {boolean} [print] * @return {TimeContext} */ setMonth(month, print = true) { var _a, _b; if (month !== ((_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.month) === null || _b === void 0 ? void 0 : _b.value) && print) { this.setDayOfMonth(undefined, print); } this.date = this.date || new EdtfDate(); this.date.month = month; return this; } /** * @return {number | undefined} */ getDayOfMonth() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.day) === null || _b === void 0 ? void 0 : _b.value; } /** * @param {number | undefined} dayOfMonth * @param {boolean} [print] * @return {TimeContext} */ setDayOfMonth(dayOfMonth, print = true) { var _a, _b; if (dayOfMonth !== ((_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.day) === null || _b === void 0 ? void 0 : _b.value) && print) { this.setHour(undefined, print); } this.date = this.date || new EdtfDate(); this.date.day = dayOfMonth; return this; } /** * @return {number | undefined} */ getHour() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.hour) === null || _b === void 0 ? void 0 : _b.value; } /** * * @param {number | undefined} hour * @param {boolean} [print] * @return {TimeContext} */ setHour(hour, print = true) { var _a, _b; if (hour !== ((_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.hour) === null || _b === void 0 ? void 0 : _b.value) && print) { this.setMinutes(undefined); } this.date = this.date || new EdtfDate(); this.date.hour = hour; return this; } /** * @return {number | undefined} */ getMinutes() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.minute) === null || _b === void 0 ? void 0 : _b.value; } /** * @param {number | undefined} minutes * @return {TimeContext} */ setMinutes(minutes) { this.date = this.date || new EdtfDate(); this.date.minute = minutes; return this; } /** * @deprecated * @return {string | undefined} */ getTimeZone() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.timeshift) === null || _b === void 0 ? void 0 : _b.toString(); } /** * @return {string | undefined} */ getTimeshift() { var _a, _b; return (_b = (_a = this.date) === null || _a === void 0 ? void 0 : _a.timeshift) === null || _b === void 0 ? void 0 : _b.toString(); } /** * @param {string | undefined} timeZone * @return {TimeContext} */ setTimeZone(timeZone) { this.date = this.date || new Level2Timeshift(); try { this.date.timeshift = Level2Timeshift.fromString(timeZone); } catch (e) { console.warn("Could not resolve time zone", timeZone, e.message); } return this; } /** * @return {boolean} */ isDefined() { var _a, _b, _c, _d, _e; const date = this.date; if (!date) { return false; } return ((_a = date.year) === null || _a === void 0 ? void 0 : _a.value) !== undefined || ((_b = date.month) === null || _b === void 0 ? void 0 : _b.value) !== undefined || ((_c = date.day) === null || _c === void 0 ? void 0 : _c.value) !== undefined || ((_d = date.hour) === null || _d === void 0 ? void 0 : _d.value) !== undefined || ((_e = date.minute) === null || _e === void 0 ? void 0 : _e.value) !== undefined; } /** * @return {TimeContext} */ clone() { var _a, _b, _c, _d, _e, _f; const date = this.date; const interval = this.interval; return new TimeContext((_a = date === null || date === void 0 ? void 0 : date.year) === null || _a === void 0 ? void 0 : _a.value, (_b = date === null || date === void 0 ? void 0 : date.month) === null || _b === void 0 ? void 0 : _b.value, (_c = date === null || date === void 0 ? void 0 : date.day) === null || _c === void 0 ? void 0 : _c.value, (_d = date === null || date === void 0 ? void 0 : date.hour) === null || _d === void 0 ? void 0 : _d.value, (_e = date === null || date === void 0 ? void 0 : date.minute) === null || _e === void 0 ? void 0 : _e.value, (_f = date === null || date === void 0 ? void 0 : date.timeshift) === null || _f === void 0 ? void 0 : _f.value, this.approximate, this.approximateTime, interval === null || interval === void 0 ? void 0 : interval.from, interval === null || interval === void 0 ? void 0 : interval.to, this.duration); } /** * * @return {this} */ reset() { this.date = undefined; this.interval = undefined; this.duration = undefined; return this; } /** * @param {TimeContext} other * @return {boolean} */ isBefore(other) { return this.toString().localeCompare(other.toString()) < 0; } /** * @param {TimeContext} other * @return {boolean} */ isAfter(other) { return this.toString().localeCompare(other.toString()) > 0; } /** * @param {TimeContext} other * @return {boolean} */ equals(other) { return this.toString() === other.toString(); } /** * @return {string} */ toString() { return this.date ? this.date.toString() : ""; } /** * @return {string} */ toJSON() { return this.toString(); } /** * @protected * @param {any} value */ isSet(value) { return value !== void 0 && value != null; } } /** * @readonly */ TimeContext.MINUTE = 60; /** * @readonly */ TimeContext.HOUR = 60 * TimeContext.MINUTE; /** * @readonly */ TimeContext.DAY = 24 * TimeContext.HOUR;