Refresh provisional results in place
This commit is contained in:
parent
bc7cb9cffa
commit
06e622d817
3 changed files with 110 additions and 3 deletions
|
|
@ -262,8 +262,8 @@
|
|||
if (currentNrClass === 'walkon') return row.walkon;
|
||||
var sectionFares = ADVANCE_FARES && ADVANCE_FARES[row.section];
|
||||
var advFares = sectionFares && row.advance_key ? sectionFares[row.advance_key] : null;
|
||||
if (!advFares) return null;
|
||||
return currentNrClass === 'advance_std' ? advFares.advance_std : advFares.advance_1st;
|
||||
if (!advFares) return row.walkon;
|
||||
return (currentNrClass === 'advance_std' ? advFares.advance_std : advFares.advance_1st) || row.walkon;
|
||||
}
|
||||
|
||||
function updateDisplay() {
|
||||
|
|
@ -342,6 +342,42 @@
|
|||
startTimetableRefresh();
|
||||
}
|
||||
|
||||
function runInsertedScripts(root) {
|
||||
root.querySelectorAll('script').forEach(function(oldScript) {
|
||||
var script = document.createElement('script');
|
||||
for (var i = 0; i < oldScript.attributes.length; i++) {
|
||||
var attr = oldScript.attributes[i];
|
||||
script.setAttribute(attr.name, attr.value);
|
||||
}
|
||||
script.text = oldScript.text;
|
||||
oldScript.parentNode.replaceChild(script, oldScript);
|
||||
});
|
||||
}
|
||||
|
||||
function fullResultsUrl() {
|
||||
var url = new URL(window.location.href);
|
||||
url.searchParams.set('render', 'full');
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function refreshFullResults() {
|
||||
fetch(fullResultsUrl(), {headers: {'X-Requested-With': 'fetch'}})
|
||||
.then(function(response) {
|
||||
if (!response.ok) throw new Error('Could not refresh results');
|
||||
return response.text();
|
||||
})
|
||||
.then(function(html) {
|
||||
var doc = new DOMParser().parseFromString(html, 'text/html');
|
||||
var nextMain = doc.querySelector('main');
|
||||
var currentMain = document.querySelector('main');
|
||||
if (!nextMain || !currentMain) throw new Error('Results page was incomplete');
|
||||
document.title = doc.title;
|
||||
currentMain.innerHTML = nextMain.innerHTML;
|
||||
runInsertedScripts(currentMain);
|
||||
})
|
||||
.catch(function() {});
|
||||
}
|
||||
|
||||
function startTimetableRefresh() {
|
||||
if (!HAS_PROVISIONAL_TIMETABLE || !TIMETABLE_REFRESH_URL || !window.EventSource) return;
|
||||
var source = new EventSource(TIMETABLE_REFRESH_URL);
|
||||
|
|
@ -349,7 +385,7 @@
|
|||
var msg = JSON.parse(event.data);
|
||||
if (msg.type === 'reload') {
|
||||
source.close();
|
||||
window.location.reload();
|
||||
refreshFullResults();
|
||||
} else if (msg.type === 'eurostar_prices') {
|
||||
mergeEurostarPrices(msg.prices);
|
||||
updateDisplay();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue