@barinbritva/add-to-calendar
Version:
[](https://github.com/barinbritva/add-to-calendar/blob/master/package.json) [](https://gith
126 lines (125 loc) • 3.78 kB
JavaScript
import { DateHelper } from './Utils/DateHelper';
export class Event {
constructor(title, startDate, endDateOrDuration, description, location, attendees = [], uid) {
this.title = title;
this.description = description;
this.location = location;
this.uid = uid;
this._endDate = null;
this.duration = null;
this._startDate = startDate;
this.setEndDate(this.handleDurationInput(endDateOrDuration));
this._attendees = attendees;
if (this.uid == null) {
this.uid = 'barinbritva--add-to-calendar--' + Math.random().toString().substring(2);
}
this.assertDatesAreCorrect();
}
get startDate() {
return this._startDate;
}
get endDate() {
return this._endDate || this.getNextDayAfterStartDate();
}
get locationName() {
if (this.location == null) {
return;
}
return typeof this.location === 'string' ? this.location : this.location[0];
}
get locationCoordinates() {
if (this.location == null) {
return;
}
return typeof this.location === 'string' ? undefined : this.location[1];
}
get attendees() {
return this._attendees;
}
reschedule(startDate, endDateOrDuration) {
this._startDate = startDate;
const end = endDateOrDuration === undefined && this.duration != null
? this.duration
: this.handleDurationInput(endDateOrDuration);
this.setEndDate(end);
this.assertDatesAreCorrect();
return this;
}
isAllDayEvent() {
return this._endDate == null;
}
getStartDateAsString() {
return this.getDateAsString(this.startDate);
}
getEndDateAsString() {
return this.getDateAsString(this.endDate);
}
changeTitle(value) {
this.title = value;
return this;
}
changeDescription(value) {
this.description = value;
return this;
}
changeLocation(value) {
this.location = value;
return this;
}
addAttendees(...attendees) {
this._attendees.push(...attendees);
return this;
}
clearAttendees() {
this._attendees = [];
return this;
}
hasAttendees() {
return this._attendees.length > 0;
}
changeUid(uid) {
this.uid = uid;
return this;
}
getNextDayAfterStartDate() {
const startDate = this.startDate;
const endDate = DateHelper.cloneDate(startDate);
endDate.setDate(startDate.getDate() + 1);
return endDate;
}
setEndDate(endDateOrDuration) {
if (endDateOrDuration === undefined) {
return;
}
if (typeof endDateOrDuration === 'number') {
this.duration = endDateOrDuration;
this._endDate = new Date(this._startDate.getTime() + this.duration);
}
else {
this.duration = null;
this._endDate = endDateOrDuration;
}
}
assertDatesAreCorrect() {
if (this._endDate == null) {
return;
}
if (this._endDate <= this.startDate) {
throw new Error('End date must be greater than start date. ' +
`Passed: start date - ${this.startDate.toISOString()}, ` +
`end date - ${this._endDate.toISOString()}.`);
}
}
getDateAsString(date) {
if (this.isAllDayEvent()) {
return DateHelper.dateToDateString(date);
}
return DateHelper.dateToDateTimeString(date);
}
handleDurationInput(duration) {
if (typeof duration === 'number') {
return duration * 60 * 1000;
}
return duration;
}
}