Skip to content

Commit b68fca7

Browse files
committed
add event handlers for pb-timeline
1 parent d66ef4c commit b68fca7

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

resources/js/init.js

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,8 @@ function init_fullcalendar(initialDate, lang) {
12521252
};
12531253

12541254

1255-
const backToTopBtn = document.getElementById('backToTop');
1255+
const backToTopBtn = document.getElementById('backToTop'),
1256+
pbTimelineElem = document.getElementById("pb-timeline");
12561257

12571258
if (backToTopBtn) {
12581259
window.addEventListener('scroll', function () {
@@ -1270,3 +1271,76 @@ if (backToTopBtn) {
12701271

12711272
// Initialize code highlighting
12721273
document.querySelectorAll('.prettyprint code').forEach(el => {hljs.highlightElement(el)})
1274+
1275+
function facetsToString(facets) {
1276+
const params = new URLSearchParams(
1277+
Object.entries(facets).flatMap(([key, value]) =>
1278+
Array.isArray(value) ? value.map(v => [key, v]) : [[key, value]]
1279+
)
1280+
).toString();
1281+
return params
1282+
}
1283+
1284+
function pbTimelineChangeHandler(ev) {
1285+
//console.log(ev);
1286+
let params = active_facets(),
1287+
endDateFixed;
1288+
// fix endDate which is always provided as the first day of the last period by pb-timeline
1289+
if (ev.detail.scope) {
1290+
if (ev.detail.scope.includes('Y')) {
1291+
endDateFixed = moment(ev.detail.endDateStr).endOf("year").format("YYYY-MM-DD")
1292+
} else if (ev.detail.scope.includes('M')) {
1293+
endDateFixed = moment(ev.detail.endDateStr).endOf("month").format("YYYY-MM-DD")
1294+
} else if (ev.detail.scope.includes('W')) {
1295+
endDateFixed = moment(ev.detail.endDateStr).endOf("week").format("YYYY-MM-DD")
1296+
} else {
1297+
endDateFixed = ev.detail.endDateStr
1298+
}
1299+
params.sliderDates.fromDate = ev.detail.startDateStr;
1300+
params.sliderDates.toDate = endDateFixed;
1301+
}
1302+
else {
1303+
params.facets.undated = true;
1304+
}
1305+
updatePage(params);
1306+
}
1307+
1308+
function pbTimelineResetHandler(ev) {
1309+
const undatedCheckbox = document.getElementById('undated');
1310+
if(undatedCheckbox) {
1311+
undatedCheckbox.checked = false; // uncheck hidden checkbox
1312+
}
1313+
const params = active_facets();
1314+
params.sliderDates.fromDate = ''; // set to empty string
1315+
params.sliderDates.toDate = '';
1316+
updatePage(params);
1317+
}
1318+
1319+
function pbTimelinePresendHandler(ev) {
1320+
const facets = active_facets().facets;
1321+
delete facets.limit; // limit is not needed and not supported by the `/timline` endpoint
1322+
const params = facetsToString(facets)
1323+
if (params !== "") {
1324+
ev.detail.options.url = ev.detail.options.url + "&" + params;
1325+
}
1326+
}
1327+
1328+
if(pbTimelineElem) {
1329+
/*
1330+
* Add event listener for changes to the timeline, i.e. selecting a date range.
1331+
*/
1332+
pbTimelineElem.addEventListener('pb-timeline-daterange-changed', pbTimelineChangeHandler)
1333+
1334+
pbTimelineElem.addEventListener('pb-timeline-date-changed', pbTimelineChangeHandler)
1335+
1336+
/*
1337+
* Add event listener for resetting the timeline selection, i.e. hitting the big X on the timeline.
1338+
*/
1339+
pbTimelineElem.addEventListener('pb-timeline-reset-selection', pbTimelineResetHandler)
1340+
1341+
/*
1342+
* Add event listener for intercepting AJAX requests sent by the timeline web component.
1343+
* Here, we rewrite the default URL parameters (start and end) and add additional URL parameters for facets
1344+
*/
1345+
pbTimelineElem.addEventListener('iron-ajax-presend', pbTimelinePresendHandler)
1346+
}

0 commit comments

Comments
 (0)