From b39602b17b81064521f038c7de07c5539312d61d Mon Sep 17 00:00:00 2001 From: dredupuika Date: Wed, 26 Oct 2016 17:45:53 +0300 Subject: [PATCH 1/3] Remove event listener after it is executed --- lib/lazy_high_charts/layout_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/lazy_high_charts/layout_helper.rb b/lib/lazy_high_charts/layout_helper.rb index 32baa91..bcaa352 100644 --- a/lib/lazy_high_charts/layout_helper.rb +++ b/lib/lazy_high_charts/layout_helper.rb @@ -109,8 +109,9 @@ def encapsulate_js(core_js) elsif defined?(Turbolinks) && request_turbolinks_5_tureferrer? js_output =<<-EOJS #{js_start} - document.addEventListener("turbolinks:load", function() { + document.addEventListener("turbolinks:load", function(e) { #{core_js} + e.target.removeEventListener(e.type, arguments.callee); }); #{js_end} EOJS From a55e7ec22ceac379a193485e2ddf3bef55da7ca5 Mon Sep 17 00:00:00 2001 From: dredupuika Date: Thu, 27 Oct 2016 01:20:23 +0300 Subject: [PATCH 2/3] fix caching problems with turbolinks 5 --- lib/lazy_high_charts/layout_helper.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/lazy_high_charts/layout_helper.rb b/lib/lazy_high_charts/layout_helper.rb index bcaa352..6846f1b 100644 --- a/lib/lazy_high_charts/layout_helper.rb +++ b/lib/lazy_high_charts/layout_helper.rb @@ -81,8 +81,8 @@ def request_is_referrer? defined?(request) && request.respond_to?(:headers) && request.headers["X-XHR-Referer"] end - def request_turbolinks_5_tureferrer? - defined?(request) && request.respond_to?(:headers) && request.headers["Turbolinks-Referrer"] + def is_turbolinks_5? + Gem::Version.new(Turbolinks::VERSION) >= Gem::Version.new('5.0.0') end def options_collection_as_string object @@ -106,7 +106,7 @@ def encapsulate_js(core_js) #{js_end} EOJS # Turbolinks >= 5 - elsif defined?(Turbolinks) && request_turbolinks_5_tureferrer? + elsif defined?(Turbolinks) && is_turbolinks_5? js_output =<<-EOJS #{js_start} document.addEventListener("turbolinks:load", function(e) { From 538525468ca96b1662a9316cd540d46790419cf7 Mon Sep 17 00:00:00 2001 From: dredupuika Date: Thu, 27 Oct 2016 10:27:03 +0300 Subject: [PATCH 3/3] check if turbolinks exists in front --- lib/lazy_high_charts/layout_helper.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/lazy_high_charts/layout_helper.rb b/lib/lazy_high_charts/layout_helper.rb index 6846f1b..a425fbc 100644 --- a/lib/lazy_high_charts/layout_helper.rb +++ b/lib/lazy_high_charts/layout_helper.rb @@ -109,10 +109,26 @@ def encapsulate_js(core_js) elsif defined?(Turbolinks) && is_turbolinks_5? js_output =<<-EOJS #{js_start} - document.addEventListener("turbolinks:load", function(e) { + var f = function() + { #{core_js} - e.target.removeEventListener(e.type, arguments.callee); - }); + } + if(typeof Turbolinks == "object") + { + document.addEventListener("turbolinks:load", function(e) { + e.target.removeEventListener(e.type, arguments.callee); + f(); + }); + } + else + { + var onload = window.onload; + window.onload = function(){ + if (typeof onload == "function") onload(); + f(); + }; + } + #{js_end} EOJS else