From 416f2ce834f98a8dab1819532368a7b8dcd415f7 Mon Sep 17 00:00:00 2001 From: Fabio Gaspar Date: Tue, 29 Mar 2022 08:54:34 +0100 Subject: [PATCH] feat(EventExtractor): Support custom locations per extractor (#51, #48) --- src/js/extractors/extractor.js | 36 +++++++++++++++++++++++++++---- src/js/extractors/supervisions.js | 13 +++++------ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/js/extractors/extractor.js b/src/js/extractors/extractor.js index 5fdd7a0..2ac99c8 100644 --- a/src/js/extractors/extractor.js +++ b/src/js/extractors/extractor.js @@ -130,15 +130,22 @@ class EventExtractor extends Extractor { * A format-string that sets calendar event's title for this extractor. * The format-string is set by the user in the options page and the default * is set in {@link structure} method. + * @type {string} */ title = null; /** * A format-string that sets calendar event's description. Similar to * {@link title} + * @type {string} */ description = null; /** - * A flag indicating wether the title and description format-strings are + * A format-string for the calendar event's location + * @type {string} + */ + location = null; + /** + * A flag indicating whether the title and description format-strings are * plain text or HTML. It is necessary to handle encoding in specific * calendars. */ @@ -173,11 +180,12 @@ class EventExtractor extends Extractor { * @param {Object} childStructure * @param {string} title Default title format-string * @param {string} description Default description format-string + * @param {string} location Default location format-string * @param {boolean} isHTML If the default format-strings are HTML * @param {CalendarEventStatus} status Default availability for the events, * i.e. show as 'Busy', or show as 'Free' in calendar clients */ - structure(childStructure, title, description, isHTML, status) { + structure(childStructure, title, description, location, isHTML, status) { return { ...childStructure, storage: { @@ -186,6 +194,10 @@ class EventExtractor extends Extractor { name: "title", default: title, }, + { + name: "location", + default: location, + }, ], textarea: [ { @@ -221,7 +233,6 @@ class EventExtractor extends Extractor { * depending on user settings */ getTitle(params) { - this._validateEventParams(params); return this._evalFormatString(this.title, params); } @@ -236,10 +247,23 @@ class EventExtractor extends Extractor { * depending on user settings */ getDescription(params) { - this._validateEventParams(params); return this._evalFormatString(this.description, params); } + /** + * Build the event's location using the format set by the user in the + * options page. + * + * @param {Object} params An object that sets the value for all parameters + * available in this extractor, as declared in the {@link structure} method. + * + * @returns {String} The formatted string, which may be HTML or plaintext + * depending on user settings + */ + getLocation(params) { + return this._evalFormatString(this.location, params); + } + /** * Validates the metadata objects used to generate the calendar event title * and description. @@ -275,6 +299,10 @@ class EventExtractor extends Extractor { * @returns */ _evalFormatString(formatStr, params) { + // ensure the metadata object, params, has all the properties + // declated in this extractor's structure + this._validateEventParams(params); + // prepare the format string to be evaluated let str = "`" + formatStr + "`"; if (this.isHTML) str = str.replace("\n", "
"); diff --git a/src/js/extractors/supervisions.js b/src/js/extractors/supervisions.js index e8bdb2c..244ecb2 100644 --- a/src/js/extractors/supervisions.js +++ b/src/js/extractors/supervisions.js @@ -23,13 +23,14 @@ class ExamSupervisions extends EventExtractor { description: "The URL for the exam's information page", }, { - name: "location", + name: "rooms", description: "The list of rooms assigned to the exam, if available", }, ], }, - "Exam Supervision - ${acronym} - ${location}", + "Exam Supervision - ${acronym} - ${rooms}", 'Exam Link: ${name}', + "FEUP: ${rooms}", true, CalendarEventStatus.BUSY ); @@ -87,7 +88,7 @@ class ExamSupervisions extends EventExtractor { this.isHTML, info.startTime, info.endTime, - info.location + this.getLocation(info) ); event.status = this.status; events.push(event); @@ -106,7 +107,7 @@ class ExamSupervisions extends EventExtractor { * name: string, * acronym: string, * url: string, - * location: string?, + * rooms: string?, * }} */ parseSupervisionEvent($tr) { @@ -137,7 +138,7 @@ class ExamSupervisions extends EventExtractor { const url = $td[2].querySelector("a").href; // Parse rooms, if available - const location = $td[3].innerText || null; + const rooms = $td[3].innerText || null; return { startTime: new Date(`${date} ${startTime}`), @@ -145,7 +146,7 @@ class ExamSupervisions extends EventExtractor { name, acronym, url, - location, + rooms, }; } }