Browse Source

Mega pre-load deception! [WIP]

Vijayakrishnan 5 years ago
parent
commit
23035ed751
1 changed files with 50 additions and 2 deletions
  1. 50 2
      resources/views/layouts/pro-logged-in.blade.php

+ 50 - 2
resources/views/layouts/pro-logged-in.blade.php

@@ -168,10 +168,45 @@
 </script>
 </script>
 
 
 <script>
 <script>
-    // Prevent variables from being global
     $(document).ready(function () {
     $(document).ready(function () {
+
+        let stagCache = {}, stagCacheDone = {};
+        function clearStagCache() {
+            stagCache = {};
+        }
+        function preloadIntoStagCache(_target, _done) {
+            if(!!stagCache[_target]) {
+                if(_done) _done(_data);
+                else if(stagCacheDone[_target]) {
+                    stagCacheDone[_target](_done);
+                    stagCacheDone[_target] = false;
+                }
+                console.log('Not pre-loading. Already in cache!');
+                return;
+            }
+            stagCache[_target] = 'loading';
+            $.get('/' + _target + '?optimised=1', function(_data) {
+                stagCache[_target] = _data;
+                if(_done) _done(_data);
+                else if(stagCacheDone[_target]) {
+                    stagCacheDone[_target](_done);
+                    stagCacheDone[_target] = false;
+                }
+            });
+        }
+        function getFromStagCache(_target, _done) {
+            if(!stagCache[_target]) { // unlikely to be hit if using mouse clicks
+                preloadIntoStagCache(_target, _done);
+            }
+            else if(stagCache[_target] === 'loading') {
+                stagCacheDone[_target] = _done;
+            }
+            else {
+                _done(stagCache[_target]);
+            }
+        }
         function gotoTarget(target, pushState) {
         function gotoTarget(target, pushState) {
-            $.get('/' + target + '?optimised=1', function(_data) {
+            getFromStagCache(target, function(_data) {
                 $('#reload-icon').removeClass('loading-rotate');
                 $('#reload-icon').removeClass('loading-rotate');
                 if(pushState) {
                 if(pushState) {
                     window.top.history.pushState(target, null, '/mc/' + target);
                     window.top.history.pushState(target, null, '/mc/' + target);
@@ -182,8 +217,21 @@
                 $('#main-sidenav a.nav-link.active').removeClass('active').blur();
                 $('#main-sidenav a.nav-link.active').removeClass('active').blur();
                 $('#main-sidenav a[href="' + activeHref + '"]').addClass('active');
                 $('#main-sidenav a[href="' + activeHref + '"]').addClass('active');
                 $(window).scrollTop(0);
                 $(window).scrollTop(0);
+                window.setTimeout(function() {
+                    clearStagCache();
+                }, 250);
             });
             });
         }
         }
+        $(document).on('mouseenter', 'a:not([href="#"]):not([up-modal]):not([up-close]):not([no-ajax]):not([logout])', function() {
+            var target = this.href;
+            if(target.indexOf('//') !== -1) {
+                target = target.split('//')[1];
+                if(target.indexOf('/') !== -1) {
+                    target = target.substr(target.indexOf('/') + 1);
+                }
+            }
+            preloadIntoStagCache(target);
+        });
         $(document).on('click', 'a:not([href="#"]):not([up-modal]):not([up-close]):not([no-ajax]):not([logout])', function() {
         $(document).on('click', 'a:not([href="#"]):not([up-modal]):not([up-close]):not([no-ajax]):not([logout])', function() {
             $('#reload-icon').addClass('loading-rotate');
             $('#reload-icon').addClass('loading-rotate');
             var target = this.href;
             var target = this.href;