Skip to content

Commit

Permalink
Actually include one-off event sources, make a utils.js kitchen sink.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabacab committed Oct 6, 2023
1 parent 16cfba8 commit 5ce4f60
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
4 changes: 2 additions & 2 deletions static/js/event-source-data.js
Original file line number Diff line number Diff line change
@@ -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
);

Expand Down
6 changes: 3 additions & 3 deletions static/js/event-source-data/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand All @@ -31,7 +31,7 @@
* ]
* ```
*/
const EventSourceData = [
const MainEventSourceData = [
{
sourceType: 'Dice',
options: {
Expand Down Expand Up @@ -994,4 +994,4 @@ const EventSourceData = [
}
];

export default EventSourceData;
export default MainEventSourceData;
20 changes: 2 additions & 18 deletions static/js/event-sources/wordpress-modern-events-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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(':')}`);
Expand Down
29 changes: 29 additions & 0 deletions static/js/utils.js
Original file line number Diff line number Diff line change
@@ -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];
}

0 comments on commit 5ce4f60

Please sign in to comment.