Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions lib/lazy_high_charts/layout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach. Question: what would happen if you have Turbolinks present in the project but not enabled in javascript ? I think in that case this solution will always return true

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't cross my mind. Then i'm at my wit's end with this. I guess there is no way to determine if rails has loaded turbolinks.js with certainty.
My best bet is that it is only fixable by manually passing an argument that switches the turbolinks wrapper on/off

Anyhow. I guess i can't help on this one then.
Will keep using this for my implementation for the time being though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a mix of the two solutions ?
is_turbolinks_5? && request_turbolinks_5_tureferrer?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @michelson ... The solution that only checks the gem version causes problems when I'm rendering charts in a PDF, where I have JS enabled but Turbolinks is not enabled (it is on my application though)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that has been fixed long ago with this this commit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, my charts on PDFs are still not being rendered, don't know why... (they are rendered normally when using @michelsons master).

end

def options_collection_as_string object
Expand All @@ -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
Expand Down