fixed ajax redirecting for non-front pages with drupal performances activated

This commit is contained in:
Bachir Soussi Chiadmi
2018-04-03 17:09:09 +02:00
parent 99b14adf07
commit 6ce6ded0f8
4 changed files with 52 additions and 56 deletions
@@ -1,22 +1,19 @@
// console.log('History');
// https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/ // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
function setCookie(name, value, days) { function setCookie(name, value, days) {
var d = new Date; var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days); d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString(); document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
} }
// var edlp is provided by edlp_ajax.module file // var edlp is provided by edlp_ajax.module file
if(edlp.redirect){ if(edlp.redirect){
document.body.style.visibility = 'hidden'; document.body.style.visibility = 'hidden';
// record current (will be origin) sys_path in cookie
// record current sys_path in cookie (or storage) (without first slash to be like real system-path) var cookiename = "edlp_origin_path";
setCookie('edlp_origin_path', edlp.sys_path.replace(/^\//, ''), 1); var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname;
// create new location (transform current path to hash) setCookie(cookiename, cookie_value, 1);
// TODO: understand why edlp.com/#/presentation is magicaly transformed into edlp.com/presentation // redirect to home
var newloc = window.location.origin+'#'+window.location.pathname; window.location.replace(window.location.origin);
// redirect
window.location.replace(newloc);
}else{ }else{
document.body.style.visibility = 'visible'; document.body.style.visibility = 'visible';
} }
@@ -16,7 +16,7 @@
// | || ' \| | _| // | || ' \| | _|
// |___|_||_|_|\__| // |___|_||_|_|\__|
function init(){ function init(){
//console.log("EdlpTheme init()"); console.log("EdlpTheme init()");
// redirect all no-front pages to front with right url ajax load // redirect all no-front pages to front with right url ajax load
initHistory(); initHistory();
@@ -112,7 +112,7 @@
return ajax_path; return ajax_path;
}; };
function ajaxLoadContent(url, sys_path, ajax_path, selector){ function ajaxLoadContent(url, sys_path, ajax_path, selector){
//console.log('ajaxLoadContent : ajax_path', ajax_path); // console.log('ajaxLoadContent : url', url);
_$body.addClass('ajax-loading'); _$body.addClass('ajax-loading');
var path = window.location.origin + Drupal.url(ajax_path); var path = window.location.origin + Drupal.url(ajax_path);
@@ -144,7 +144,7 @@
_$body.removeClass('ajax-loading'); _$body.removeClass('ajax-loading');
}; };
function onAjaxLoaded(data, sys_path, selector){ function onAjaxLoaded(data, sys_path, selector){
console.log('ajax link loaded : data', data); // console.log('ajax loaded : data', data);
// reset all style may been added by other pages (like masonry for productions) // reset all style may been added by other pages (like masonry for productions)
// and replace all content with newly loaded // and replace all content with newly loaded
@@ -258,21 +258,22 @@
// console.log('theme : initFirstLoad()', window.location); // console.log('theme : initFirstLoad()', window.location);
// console.log(document.cookie); // console.log(document.cookie);
var url = window.location.pathname; var url = window.location.pathname;
if(url != '' && url != '/'){ var origin_path = getCookie('edlp_origin_path');
var origin_path = getCookie('edlp_origin_path'); // console.log('origin_path', origin_path);
// console.log('origin_path', origin_path); if(origin_path){
if(origin_path){ var path_elements = origin_path.split('~~');
var state = { // console.log(path_elements);
ajax_path: parseAjaxSysPath(origin_path), // create history state
sys_path: origin_path, var state = {
}; ajax_path: parseAjaxSysPath(path_elements[0]),
ajaxLoadContent(null, state.sys_path, state.ajax_path); sys_path: path_elements[0],
history.replaceState(state, null, url); };
// reset the cookie // record history state
deleteCookie('edlp_origin_path'); history.replaceState(state, null, path_elements[1]);
deleteCookie('edlp_sys_path'); // load content through ajax
ajaxLoadContent(null, state.sys_path, state.ajax_path);
} // reset the cookie
deleteCookie('edlp_origin_path');
}else{ }else{
history.replaceState({home:true}, null, url); history.replaceState({home:true}, null, url);
@@ -1,22 +1,19 @@
// console.log('History');
// https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/ // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
function setCookie(name, value, days) { function setCookie(name, value, days) {
var d = new Date; var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days); d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString(); document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
} }
// var edlp is provided by edlp_ajax.module file // var edlp is provided by edlp_ajax.module file
if(edlp.redirect){ if(edlp.redirect){
document.body.style.visibility = 'hidden'; document.body.style.visibility = 'hidden';
// record current (will be origin) sys_path in cookie
// record current sys_path in cookie (or storage) (without first slash to be like real system-path) var cookiename = "edlp_origin_path";
setCookie('edlp_origin_path', edlp.sys_path.replace(/^\//, ''), 1); var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname;
// create new location (transform current path to hash) setCookie(cookiename, cookie_value, 1);
// TODO: understand why edlp.com/#/presentation is magicaly transformed into edlp.com/presentation // redirect to home
var newloc = window.location.origin+'#'+window.location.pathname; window.location.replace(window.location.origin);
// redirect
window.location.replace(newloc);
}else{ }else{
document.body.style.visibility = 'visible'; document.body.style.visibility = 'visible';
} }
@@ -16,7 +16,7 @@
// | || ' \| | _| // | || ' \| | _|
// |___|_||_|_|\__| // |___|_||_|_|\__|
function init(){ function init(){
//console.log("EdlpTheme init()"); console.log("EdlpTheme init()");
// redirect all no-front pages to front with right url ajax load // redirect all no-front pages to front with right url ajax load
initHistory(); initHistory();
@@ -112,7 +112,7 @@
return ajax_path; return ajax_path;
}; };
function ajaxLoadContent(url, sys_path, ajax_path, selector){ function ajaxLoadContent(url, sys_path, ajax_path, selector){
//console.log('ajaxLoadContent : ajax_path', ajax_path); // console.log('ajaxLoadContent : url', url);
_$body.addClass('ajax-loading'); _$body.addClass('ajax-loading');
var path = window.location.origin + Drupal.url(ajax_path); var path = window.location.origin + Drupal.url(ajax_path);
@@ -144,7 +144,7 @@
_$body.removeClass('ajax-loading'); _$body.removeClass('ajax-loading');
}; };
function onAjaxLoaded(data, sys_path, selector){ function onAjaxLoaded(data, sys_path, selector){
console.log('ajax link loaded : data', data); // console.log('ajax loaded : data', data);
// reset all style may been added by other pages (like masonry for productions) // reset all style may been added by other pages (like masonry for productions)
// and replace all content with newly loaded // and replace all content with newly loaded
@@ -258,21 +258,22 @@
// console.log('theme : initFirstLoad()', window.location); // console.log('theme : initFirstLoad()', window.location);
// console.log(document.cookie); // console.log(document.cookie);
var url = window.location.pathname; var url = window.location.pathname;
if(url != '' && url != '/'){ var origin_path = getCookie('edlp_origin_path');
var origin_path = getCookie('edlp_origin_path'); // console.log('origin_path', origin_path);
// console.log('origin_path', origin_path); if(origin_path){
if(origin_path){ var path_elements = origin_path.split('~~');
var state = { // console.log(path_elements);
ajax_path: parseAjaxSysPath(origin_path), // create history state
sys_path: origin_path, var state = {
}; ajax_path: parseAjaxSysPath(path_elements[0]),
ajaxLoadContent(null, state.sys_path, state.ajax_path); sys_path: path_elements[0],
history.replaceState(state, null, url); };
// reset the cookie // record history state
deleteCookie('edlp_origin_path'); history.replaceState(state, null, path_elements[1]);
deleteCookie('edlp_sys_path'); // load content through ajax
ajaxLoadContent(null, state.sys_path, state.ajax_path);
} // reset the cookie
deleteCookie('edlp_origin_path');
}else{ }else{
history.replaceState({home:true}, null, url); history.replaceState({home:true}, null, url);