diff --git a/lib/lazy_high_charts/layout_helper.rb b/lib/lazy_high_charts/layout_helper.rb index 32baa91..a425fbc 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,12 +106,29 @@ 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() { + var f = function() + { #{core_js} - }); + } + 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