From 5ce4f60fc743ca5d0fdd25a112bd37d3ecffd150 Mon Sep 17 00:00:00 2001 From: fabacab Date: Fri, 6 Oct 2023 12:35:44 -0400 Subject: [PATCH] Actually include one-off event sources, make a `utils.js` kitchen sink. --- static/js/event-source-data.js | 4 +-- static/js/event-source-data/main.js | 6 ++-- .../wordpress-modern-events-calendar.js | 20 ++----------- static/js/utils.js | 29 +++++++++++++++++++ 4 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 static/js/utils.js diff --git a/static/js/event-source-data.js b/static/js/event-source-data.js index 5978be5..4902c41 100644 --- a/static/js/event-source-data.js +++ b/static/js/event-source-data.js @@ -1,10 +1,10 @@ /** * This file simply shims various event source descriptions together. */ -import { default as EventSourceData } from './event-source-data/main.js'; +import { default as MainEventSourceData } from './event-source-data/main.js'; import { default as OneOffEventSourceData } from './event-source-data/one-off.js'; -EventSourceData.concat( +var EventSourceData = MainEventSourceData.concat( OneOffEventSourceData ); diff --git a/static/js/event-source-data/main.js b/static/js/event-source-data/main.js index 5bbcf0b..b145bd4 100644 --- a/static/js/event-source-data/main.js +++ b/static/js/event-source-data/main.js @@ -4,7 +4,7 @@ */ /** - * The `EventSourceData` constant is the main data + * The `MainEventSourceData` constant is the main data * structure for sourcing event information. It's * a nested structure that looks like this: * @@ -31,7 +31,7 @@ * ] * ``` */ -const EventSourceData = [ +const MainEventSourceData = [ { sourceType: 'Dice', options: { @@ -994,4 +994,4 @@ const EventSourceData = [ } ]; -export default EventSourceData; +export default MainEventSourceData; diff --git a/static/js/event-sources/wordpress-modern-events-calendar.js b/static/js/event-sources/wordpress-modern-events-calendar.js index 439644a..85ba233 100644 --- a/static/js/event-sources/wordpress-modern-events-calendar.js +++ b/static/js/event-sources/wordpress-modern-events-calendar.js @@ -6,6 +6,7 @@ */ import { corsbase, domparser } from '../calendar.js'; import FullCalendarEvent from '../event.js'; +import { convert12To24HourTime } from '../utils.js'; export default function ModernEventsCalendarEvents (optionsObj) { this.events = []; @@ -40,24 +41,7 @@ ModernEventsCalendarEvents.prototype.parse = function () { var doc = domparser.parseFromString(this.html, 'text/html'); doc.querySelectorAll('script[type="application/ld+json"]').forEach(function (el) { var times = el.nextElementSibling.querySelector('.mec-event-time').textContent - .trim().split(' - ').map(function (str) { - var h; - var m; - if ( str.match(/ am$/) ) { - [h, m] = str.match(/^(\d?\d):(\d\d)/).slice(1); - if ( '12' === h ) { - h = '00'; - } - } else { - h = parseInt(str.match(/^\d?\d/)[0]) + 12; // Convert to 24-hour. - m = str.match(/:(\d\d) /)[1]; - if ( '24' === h ) { - h = '12'; - } - } - h.toString().padStart(2, '0'); - return [h, m]; - }); + .trim().split(' - ').map(convert12To24HourTime); var data = FullCalendarEvent.fromSchemaDotOrg(JSON.parse(el.textContent)); data.start = new Date(`${data.start} ${times[0].join(':')}`); data.end = new Date(`${data.end} ${times[1].join(':')}`); diff --git a/static/js/utils.js b/static/js/utils.js new file mode 100644 index 0000000..d9dce5a --- /dev/null +++ b/static/js/utils.js @@ -0,0 +1,29 @@ +/** + * Simplistic and "good enough" helper functions, mostly for dealing + * with common one-off event source type issues. + */ + +/** + * Simplistically converts a 12-hour time format string to a 24-hour time. + * + * @param str {String} The 12-hour formatted time string, e.g., `"8:00 am"` + * @return + */ +export function convert12To24HourTime (str) { + var h; + var m; + if ( str.match(/ am$/) ) { + [h, m] = str.match(/^(\d?\d):(\d\d)/).slice(1); + if ( '12' === h ) { + h = '00'; + } + } else { + h = parseInt(str.match(/^\d?\d/)[0]) + 12; + m = str.match(/:(\d\d) /)[1]; + if ( '24' === h ) { + h = '12'; + } + } + h.toString().padStart(2, '0'); + return [h, m]; +}