From ed22d33d97cd5dad162fdba644b917fe5bb1af3a Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 19 Jul 2020 13:08:13 -0700 Subject: [PATCH] Reload events after pushState. --- public/umami.js | 2 +- scripts/umami/index.js | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/public/umami.js b/public/umami.js index 3d201df3..22773f3c 100644 --- a/public/umami.js +++ b/public/umami.js @@ -1 +1 @@ -!function(){"use strict";function e(e){var t=this.constructor;return this.then((function(n){return t.resolve(e()).then((function(){return n}))}),(function(n){return t.resolve(e()).then((function(){return t.reject(n)}))}))}var t=setTimeout;function n(e){return Boolean(e&&void 0!==e.length)}function r(){}function o(e){if(!(this instanceof o))throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],f(e,this)}function i(e,t){for(;3===e._state;)e=e._value;0!==e._state?(e._handled=!0,o._immediateFn((function(){var n=1===e._state?t.onFulfilled:t.onRejected;if(null!==n){var r;try{r=n(e._value)}catch(e){return void s(t.promise,e)}u(t.promise,r)}else(1===e._state?u:s)(t.promise,e._value)}))):e._deferreds.push(t)}function u(e,t){try{if(t===e)throw new TypeError("A promise cannot be resolved with itself.");if(t&&("object"==typeof t||"function"==typeof t)){var n=t.then;if(t instanceof o)return e._state=3,e._value=t,void a(e);if("function"==typeof n)return void f((r=n,i=t,function(){r.apply(i,arguments)}),e)}e._state=1,e._value=t,a(e)}catch(t){s(e,t)}var r,i}function s(e,t){e._state=2,e._value=t,a(e)}function a(e){2===e._state&&0===e._deferreds.length&&o._immediateFn((function(){e._handled||o._unhandledRejectionFn(e._value)}));for(var t=0,n=e._deferreds.length;t { + removeEvents(); currenrRef = currentUrl; currentUrl = url; execute(currentUrl, currenrRef); + setTimeout(loadEvents, 300); }; const hook = (type, cb) => { @@ -101,19 +104,32 @@ if (script) { /* Handle events */ - document.querySelectorAll("[class*='umami--']").forEach(e => { - e.className.split(' ').forEach(c => { - if (/^umami--/.test(c)) { - const [, event, value] = c.split('--'); - if (event && value) { - e.addEventListener(event, () => trackEvent(currentUrl, event, value), true); - } - } + const removeEvents = () => { + listeners.forEach(([element, type, listener]) => { + console.log('removed', element.tagName, type); + element.removeEventListener(type, listener, true); }); - }); + listeners.length = 0; + }; + + const loadEvents = () => { + document.querySelectorAll("[class*='umami--']").forEach(element => { + element.className.split(' ').forEach(className => { + if (/^umami--/.test(className)) { + const [, type, value] = className.split('--'); + if (type && value) { + const listener = () => trackEvent(currentUrl, type, value); + listeners.push([element, type, listener]); + element.addEventListener(type, listener, true); + } + } + }); + }); + }; /* Start */ execute(currentUrl, currenrRef); + loadEvents(); } }