Browse Source

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

Bachir Soussi Chiadmi 6 years ago
parent
commit
6ce6ded0f8

+ 7 - 10
sites/all/themes/custom/edlptheme/assets/dist/scripts/history.min.js

@@ -1,22 +1,19 @@
-// console.log('History');
-
 // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
 function setCookie(name, value, days) {
   var d = new Date;
   d.setTime(d.getTime() + 24*60*60*1000*days);
   document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
 }
+
 // var edlp is provided by edlp_ajax.module file
 if(edlp.redirect){
   document.body.style.visibility = 'hidden';
-
-  // record current sys_path in cookie (or storage) (without first slash to be like real system-path)
-  setCookie('edlp_origin_path', edlp.sys_path.replace(/^\//, ''), 1);
-  // create new location (transform current path to hash)
-  // TODO: understand why edlp.com/#/presentation is magicaly transformed into edlp.com/presentation
-  var newloc = window.location.origin+'#'+window.location.pathname;
-  // redirect
-  window.location.replace(newloc);
+  // record current (will be origin) sys_path in cookie
+  var cookiename = "edlp_origin_path";
+  var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname;
+  setCookie(cookiename, cookie_value, 1);
+  // redirect to home
+  window.location.replace(window.location.origin);
 }else{
   document.body.style.visibility = 'visible';
 }

+ 19 - 18
sites/all/themes/custom/edlptheme/assets/dist/scripts/main.min.js

@@ -16,7 +16,7 @@
     //  | || ' \| |  _|
     // |___|_||_|_|\__|
     function init(){
-      //console.log("EdlpTheme init()");
+      console.log("EdlpTheme init()");
 
       // redirect all no-front pages to front with right url ajax load
       initHistory();
@@ -112,7 +112,7 @@
       return ajax_path;
     };
     function ajaxLoadContent(url, sys_path, ajax_path, selector){
-      //console.log('ajaxLoadContent : ajax_path', ajax_path);
+      // console.log('ajaxLoadContent : url', url);
       _$body.addClass('ajax-loading');
 
       var path = window.location.origin + Drupal.url(ajax_path);
@@ -144,7 +144,7 @@
       _$body.removeClass('ajax-loading');
     };
     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)
       // and replace all content with newly loaded
@@ -258,21 +258,22 @@
       // console.log('theme : initFirstLoad()', window.location);
       // console.log(document.cookie);
       var url = window.location.pathname;
-      if(url != '' && url != '/'){
-        var origin_path = getCookie('edlp_origin_path');
-        // console.log('origin_path', origin_path);
-        if(origin_path){
-          var state = {
-            ajax_path: parseAjaxSysPath(origin_path),
-            sys_path: origin_path,
-          };
-          ajaxLoadContent(null, state.sys_path, state.ajax_path);
-          history.replaceState(state, null, url);
-          // reset the cookie
-          deleteCookie('edlp_origin_path');
-          deleteCookie('edlp_sys_path');
-
-        }
+      var origin_path = getCookie('edlp_origin_path');
+      // console.log('origin_path', origin_path);
+      if(origin_path){
+        var path_elements = origin_path.split('~~');
+        // console.log(path_elements);
+        // create history state
+        var state = {
+          ajax_path: parseAjaxSysPath(path_elements[0]),
+          sys_path: path_elements[0],
+        };
+        // record history state
+        history.replaceState(state, null, path_elements[1]);
+        // load content through ajax
+        ajaxLoadContent(null, state.sys_path, state.ajax_path);
+        // reset the cookie
+        deleteCookie('edlp_origin_path');
 
       }else{
         history.replaceState({home:true}, null, url);

+ 7 - 10
sites/all/themes/custom/edlptheme/assets/scripts/history.js

@@ -1,22 +1,19 @@
-// console.log('History');
-
 // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
 function setCookie(name, value, days) {
   var d = new Date;
   d.setTime(d.getTime() + 24*60*60*1000*days);
   document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
 }
+
 // var edlp is provided by edlp_ajax.module file
 if(edlp.redirect){
   document.body.style.visibility = 'hidden';
-
-  // record current sys_path in cookie (or storage) (without first slash to be like real system-path)
-  setCookie('edlp_origin_path', edlp.sys_path.replace(/^\//, ''), 1);
-  // create new location (transform current path to hash)
-  // TODO: understand why edlp.com/#/presentation is magicaly transformed into edlp.com/presentation
-  var newloc = window.location.origin+'#'+window.location.pathname;
-  // redirect
-  window.location.replace(newloc);
+  // record current (will be origin) sys_path in cookie
+  var cookiename = "edlp_origin_path";
+  var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname;
+  setCookie(cookiename, cookie_value, 1);
+  // redirect to home
+  window.location.replace(window.location.origin);
 }else{
   document.body.style.visibility = 'visible';
 }

+ 19 - 18
sites/all/themes/custom/edlptheme/assets/scripts/main.js

@@ -16,7 +16,7 @@
     //  | || ' \| |  _|
     // |___|_||_|_|\__|
     function init(){
-      //console.log("EdlpTheme init()");
+      console.log("EdlpTheme init()");
 
       // redirect all no-front pages to front with right url ajax load
       initHistory();
@@ -112,7 +112,7 @@
       return ajax_path;
     };
     function ajaxLoadContent(url, sys_path, ajax_path, selector){
-      //console.log('ajaxLoadContent : ajax_path', ajax_path);
+      // console.log('ajaxLoadContent : url', url);
       _$body.addClass('ajax-loading');
 
       var path = window.location.origin + Drupal.url(ajax_path);
@@ -144,7 +144,7 @@
       _$body.removeClass('ajax-loading');
     };
     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)
       // and replace all content with newly loaded
@@ -258,21 +258,22 @@
       // console.log('theme : initFirstLoad()', window.location);
       // console.log(document.cookie);
       var url = window.location.pathname;
-      if(url != '' && url != '/'){
-        var origin_path = getCookie('edlp_origin_path');
-        // console.log('origin_path', origin_path);
-        if(origin_path){
-          var state = {
-            ajax_path: parseAjaxSysPath(origin_path),
-            sys_path: origin_path,
-          };
-          ajaxLoadContent(null, state.sys_path, state.ajax_path);
-          history.replaceState(state, null, url);
-          // reset the cookie
-          deleteCookie('edlp_origin_path');
-          deleteCookie('edlp_sys_path');
-
-        }
+      var origin_path = getCookie('edlp_origin_path');
+      // console.log('origin_path', origin_path);
+      if(origin_path){
+        var path_elements = origin_path.split('~~');
+        // console.log(path_elements);
+        // create history state
+        var state = {
+          ajax_path: parseAjaxSysPath(path_elements[0]),
+          sys_path: path_elements[0],
+        };
+        // record history state
+        history.replaceState(state, null, path_elements[1]);
+        // load content through ajax
+        ajaxLoadContent(null, state.sys_path, state.ajax_path);
+        // reset the cookie
+        deleteCookie('edlp_origin_path');
 
       }else{
         history.replaceState({home:true}, null, url);