From 0872d9d55ffc1ccb9fb3aa0f5e1f6fb90f68a0b2 Mon Sep 17 00:00:00 2001 From: Oswaldo Acauan Date: Sun, 25 May 2014 23:49:11 -0300 Subject: [PATCH] feat(js): Only load GA/Disqus when they are setted. Close #45 --- src/assets/js/scripts.js | 40 ++++++++++++++++++++++++++++------------ src/default.hbs | 39 +++++++++++++++++++++++++-------------- src/post.hbs | 24 +++++++++++++----------- 3 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/assets/js/scripts.js b/src/assets/js/scripts.js index 52d90c25..3cebfef5 100644 --- a/src/assets/js/scripts.js +++ b/src/assets/js/scripts.js @@ -39,9 +39,11 @@ _prismHandler(); - // Reset DISQUS + // DISQUS Handlers // ================= - var _resetDisqus = function() { + var _disqusHandler = function() { + if(!GHOSTIUM.haveDisqus) return; + if(typeof DISQUS === 'object' && $('#disqus_thread').length) { DISQUS.reset({ reload: true, @@ -52,7 +54,12 @@ } }; - var _loadDisqusCounter = function() { + var _disqusCounterHandler = function() { + if(!GHOSTIUM.haveDisqus) { + $('[data-disqus-identifier]').parent('li').remove(); + return; + } + if(typeof DISQUSWIDGETS === 'object') { var newScript = document.createElement("script"), countScript = $html.find('head script[src*="disqus.com/count-data.js"]').remove(), @@ -76,6 +83,19 @@ } }; + _disqusCounterHandler() + + // GA Handler + // ================= + var _gaHandler = function() { + if(!GHOSTIUM.haveGA) return; + + if(typeof ga === 'function') { + ga('set', 'location', window.location.href); + ga('send', 'pageview'); + } + } + // PJax bindings // ================= if ($.support.pjax) { @@ -85,19 +105,15 @@ }); $document.on('pjax:end', function() { - if(typeof ga === 'function') { - ga('set', 'location', window.location.href); - ga('send', 'pageview'); - } - - _resetDisqus(); - _loadDisqusCounter(); + _disqusHandler(); + _gaHandler(); + _disqusCounterHandler(); + _prismHandler(); $('[data-load-image]', $content).each(function() { ImageLoader.load($(this)); }); - _prismHandler(); NProgress.done(); }); @@ -214,7 +230,7 @@ // Fix DISQUS iframe does not resize on mobile orientation change // ================= $window.on('orientationchange', function(e) { - _resetDisqus(); + _disqusHandler(); }); }); diff --git a/src/default.hbs b/src/default.hbs index 54bb1347..c600f074 100644 --- a/src/default.hbs +++ b/src/default.hbs @@ -43,6 +43,14 @@ + + {{ghost_head}} @@ -97,21 +105,24 @@ diff --git a/src/post.hbs b/src/post.hbs index 6fe6d64f..35850cf9 100644 --- a/src/post.hbs +++ b/src/post.hbs @@ -91,18 +91,20 @@