Jelajahi Sumber

Fix fastLoad, optimize caching

Vijayakrishnan Krishnan 4 tahun lalu
induk
melakukan
cf6490e9c9
2 mengubah file dengan 68 tambahan dan 45 penghapusan
  1. 66 43
      public/js/mc.js
  2. 2 2
      resources/views/layouts/template.blade.php

+ 66 - 43
public/js/mc.js

@@ -75,11 +75,11 @@ window.top.addEventListener('popstate', function (event) {
     }, 0);
 });
 $(document).ready(function () {
-    if(window === window.top) {
+    if(window.location.href === window.top.location.href) {
         window.location.href = '/mc' + window.location.pathname;
         return;
     }
-    window.top.ensureRHS();
+    // window.top.ensureRHS();
     $(document).on('click', '.stag_rhs_toggle', function () {
         var state = window.top.toggleRHS(),
             icon = $(this).find('i');
@@ -95,7 +95,9 @@ $(document).ready(function () {
         icon.removeClass().addClass('fa fa-arrow-left');
     }
     initFastLoad();
-    initializeCalendar();
+    if(typeof initializeCalendar !== 'undefined') {
+        initializeCalendar();
+    }
 });
 function enableTimeSpecificFields(_checked, _valueClass, _rangeClass) {
     if(_valueClass) $('.' + _valueClass).prop('disabled', _checked);
@@ -125,12 +127,23 @@ var fastCache = {};
 
 function initFastLoad(_parent = false) {
 
-    fastCache = {};
-
-    var allAs = $('a:not([onclick]):not([href="#"])');
+    var allAs = $('a[href]:not([onclick]):not([href="#"])');
     if (_parent) {
-        allAs = _parent.find('a:not([onclick]):not([href="#"])');
+        allAs = _parent.find('a[href]:not([onclick]):not([href="#"])');
+    }
+
+    // clear cache
+    if(!_parent) {
+        fastCache = {};
     }
+    else {
+        allAs.each(function () {
+            if(typeof fastCache[this.href] !== 'undefined') {
+                delete fastCache[this.href];
+            }
+        });
+    }
+
     // find links without event handlers
     allAs.each(function () {
         if (!$(this).closest('[moe]').length) {
@@ -146,20 +159,25 @@ function initFastLoad(_parent = false) {
     });
 
     function enableFastLoad(_a, _menuItem = false) {
-        $(_a).on('click', function () {
-            fastLoad(this.href, true, true);
-            if (_menuItem) {
-                $(this).closest('.dropdown-menu')
-                    .removeClass('show')
-                    .prev('.dropdown-toggle').attr('aria-expanded', 'false');
-            }
-            return false;
-        });
+        $(_a)
+            .off('click.fast-load')
+            .on('click.fast-load', function () {
+                console.log('ALIX: got click!');
+                showMask();
+                console.log('ALIX: showed mask');
+                fastLoad(this.href, true, true);
+                if (_menuItem) {
+                    $(this).closest('.dropdown-menu')
+                        .removeClass('show')
+                        .prev('.dropdown-toggle').attr('aria-expanded', 'false');
+                }
+                return false;
+            });
         // console.info('FastLoad enabled for ' + _a.innerText + ' [' + _a.href + ']');
     }
 
     // fast cache
-    allAs = $('a:not([onclick]):not([href="#"])');
+    allAs = $('a[href]:not([onclick]):not([href="#"])');
     allAs.each(function () {
         var a = this;
         $.get(a.href, function (_data) {
@@ -169,41 +187,46 @@ function initFastLoad(_parent = false) {
 
 }
 
-function fastLoad(_href, _history = true, _useCache = true) {
-    function onData(_data) {
-        var targetParent = $('.stag-content');
-        _data = '<div>' + _data + '</div>';
-        var content = $(_data).find('.stag-content');
-        if (content && content.length) {
-            content = content.html();
-            content += '<script src="/js/yemi.js"></script>';
-            targetParent.html(content);
+function onFastLoaded(_data, _href, _history) {
+    var targetParent = $('.stag-content');
+    _data = '<div>' + _data + '</div>';
+    var content = $(_data).find('.stag-content');
+    if (content && content.length) {
+        content = content.html();
+        content += '<script src="/js/yemi.js?_=2"></script>';
+        targetParent.html(content);
+        window.setTimeout(function() {
             initFastLoad(targetParent);
+        }, 50);
+        if(typeof initializeCalendar !== 'undefined') {
             initializeCalendar();
+        }
 
-            // push state
-            if (_history) {
-                var target = _href;
-                if (target.indexOf('//') !== -1) {
-                    target = target.split('//')[1];
-                    if (target.indexOf('/') !== -1) {
-                        target = target.substr(target.indexOf('/') + 1);
-                    }
+        // push state
+        if (_history) {
+            var target = _href;
+            if (target.indexOf('//') !== -1) {
+                target = target.split('//')[1];
+                if (target.indexOf('/') !== -1) {
+                    target = target.substr(target.indexOf('/') + 1);
                 }
-                window.top.history.pushState(target, null, '/mc/' + target);
             }
-
-        } else {
-            console.warn('Target page not found: ' + _href);
-            window.location.href = _href; // fallback
+            window.top.history.pushState(target, null, '/mc/' + target);
         }
-        hideMask();
+
+    } else {
+        console.warn('Target page not found: ' + _href);
+        window.location.href = _href; // fallback
     }
-    showMask();
+    // hideMask();
+}
+function fastLoad(_href, _history = true, _useCache = true) {
     if (_useCache && !!fastCache[_href]) {
-        onData(fastCache[_href]);
+        onFastLoaded(fastCache[_href], _href, _history);
     } else {
-        $.get(_href, onData);
+        $.get(_href, function(_data) {
+            onFastLoaded(_data, _href, _history);
+        });
     }
 }
 

+ 2 - 2
resources/views/layouts/template.blade.php

@@ -24,7 +24,7 @@
     <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
     <script src="{{ asset('fullcalendar/main.min.js') }}" type="application/javascript"></script>
     <script src="{{ asset('js/toastr.min.js') }}" type="application/javascript"></script>
-    <script src="{{ asset('js/yemi.js') }}" type="application/javascript"></script>
+    <script src="/js/yemi.js?_=2" type="application/javascript"></script>
 
     @yield('head')
 </head>
@@ -82,7 +82,7 @@
     </main><!-- /.container -->
 
     <!-- script to handle history & back/forward for mc/xxx pages -->
-    <script src="{{ asset('js/mc.js') }}" type="application/javascript"></script>
+    <script src="/js/mc.js?_=2" type="application/javascript"></script>
 
 </body>