mirror of
https://gitlab.com/octtspacc/sitoctt
synced 2025-06-05 22:09:20 +02:00
Nuova pagina sitoctt internals, Fix misti di pagine, Ristrutturazione file
This commit is contained in:
88
static/res/ajax-navigation.js
Normal file
88
static/res/ajax-navigation.js
Normal file
@ -0,0 +1,88 @@
|
||||
(function(){
|
||||
// TODO investigate/fix strange screen flash when navigating back from an hash URL
|
||||
// TODO error handling, caching, loading indication, totally handle hash change
|
||||
|
||||
window.SiteInitOnLoad = [];
|
||||
var oldUrl = null;
|
||||
|
||||
// TODO before deploying this: handle page-specific scripts (comments, etc...)
|
||||
//if (location.protocol === 'file:' /* && !isSingleFileBuild */) {
|
||||
window.PatchAjaxNavigationAnchor = Void;
|
||||
return; // HTTP requests don't work on local files with default browser configuration
|
||||
//}
|
||||
|
||||
function toPathUrl (url) {
|
||||
return url.split('#')[0];
|
||||
}
|
||||
|
||||
function initPage () {
|
||||
if (oldUrl !== null) {
|
||||
document.body.scrollIntoView();
|
||||
SiteInitOnLoad.forEach(function(routine){ routine(); });
|
||||
}
|
||||
oldUrl = location.href;
|
||||
var langToken = ('/' + document.documentElement.lang + '/');
|
||||
var baseHref = (location.href.split(langToken)[0] + langToken); // we reload page on language change due to search index differences
|
||||
Array.from(document.querySelectorAll('a[href]')).filter(function(anchorEl){
|
||||
//var tokens = href.split('/' + document.documentElement.lang + '/');
|
||||
//if (location.href.slice(0, href[0]))
|
||||
//return (anchorEl.getAttribute('href').replace(location.host, '').replace(/^(\.\.\/)+/, '').replace(/^(\/)+/, '').split('/')[1] === document.documentElement.lang);
|
||||
return (anchorEl.href.slice(0, baseHref.length) === baseHref);
|
||||
}).forEach(PatchAjaxNavigationAnchor);
|
||||
}
|
||||
|
||||
function setNewPage (html, pushUrl, fallbackTimeout) {
|
||||
var domNew = (new DOMParser).parseFromString(html, 'text/html');
|
||||
if (pushUrl) {
|
||||
history.pushState(null, null, pushUrl);
|
||||
}
|
||||
document.head.innerHTML = domNew.head.innerHTML;
|
||||
document.body.innerHTML = domNew.body.innerHTML;
|
||||
initPage();
|
||||
clearTimeout(fallbackTimeout);
|
||||
}
|
||||
|
||||
function loadContent (url, push) {
|
||||
//if (isSingleFileBuild) { // TODO
|
||||
// setNewPage(html, (push && url), fallbackTimeout);
|
||||
//} else {
|
||||
function useFallback () {
|
||||
location.href = url;
|
||||
}
|
||||
var fallbackTimeout = setTimeout(useFallback, 3000);
|
||||
fetch(url)
|
||||
.then(function(request){ return request.text(); })
|
||||
.then(function(html){
|
||||
setNewPage(html, (push && url), fallbackTimeout);
|
||||
}).catch(function(err){
|
||||
console.error(err);
|
||||
useFallback();
|
||||
});
|
||||
//}
|
||||
}
|
||||
|
||||
window.PatchAjaxNavigationAnchor = (function PatchAjaxNavigationAnchor (anchorEl) {
|
||||
var isSitewideLink = anchorEl.href.startsWith(location.protocol + '//' + location.host);
|
||||
var isPagewideLink = (toPathUrl(anchorEl.href) === toPathUrl(location.href));
|
||||
if (isSitewideLink && !isPagewideLink) {
|
||||
anchorEl.addEventListener('click', (function(clickEvent){
|
||||
clickEvent.preventDefault();
|
||||
loadContent(anchorEl.href, true);
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initPage);
|
||||
} else {
|
||||
initPage();
|
||||
}
|
||||
|
||||
window.addEventListener('popstate', (function(stateEvent){
|
||||
if (toPathUrl(location.href) !== toPathUrl(oldUrl)) {
|
||||
loadContent(location.href, false);
|
||||
}
|
||||
oldUrl = location.href;
|
||||
}));
|
||||
|
||||
})();
|
Reference in New Issue
Block a user