-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlayout_helper_iruby.rb
97 lines (85 loc) · 2.4 KB
/
layout_helper_iruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module LazyHighCharts
module LayoutHelper
def high_chart_iruby(chart_class, placeholder, object, &block)
object.html_options[:id] = placeholder
object.options[:chart][:renderTo] = placeholder
build_html_output_iruby(
chart_class, placeholder, object, &block
).concat(content_tag('div', '', object.html_options))
end
def high_map(placeholder, object, &block)
object.html_options[:id] = placeholder
object.options[:chart][:renderTo] = placeholder
build_html_output(
'Map', placeholder, object, &block
).concat(content_tag('div', '', object.html_options))
end
private
def build_html_output_iruby(type, placeholder, object, &block)
core_js =<<-EOJS
var options = #{options_collection_as_string(object)};
#{capture(&block) if block_given?}
window.chart_#{placeholder.underscore} = new Highcharts.#{type}(options);
EOJS
encapsulate_js_iruby core_js
end
def encapsulate_js_iruby(core_js)
js_output =
if request_is_xhr?
"#{js_start_iruby} #{core_js} #{js_end_iruby}"
# Turbolinks.version < 5
elsif defined?(Turbolinks)
encapsulate_js_for_turbolinks(core_js)
else
call_core_js(core_js)
end
defined?(raw) ? raw(js_output) : js_output
end
def encapsulate_js_for_turbolinks(core_js)
if request_is_referrer?
eventlistener_page_load(core_js)
elsif request_turbolinks_5_tureferrer?
eventlistener_turbolinks_load(core_js)
end
end
def eventlistener_page_load(core_js)
<<-EOJS
#{js_start_iruby}
var f = function(){
document.removeEventListener('page:load', f, true);
#{core_js}
};
document.addEventListener('page:load', f, true);
#{js_end_iruby}
EOJS
end
def eventlistener_turbolinks_load(core_js)
<<-EOJS
#{js_start_iruby}
document.addEventListener("turbolinks:load", function() {
#{core_js}
});
#{js_end_iruby}
EOJS
end
def call_core_js(core_js)
<<-EOJS
#{js_start_iruby}
#{core_js}
#{js_end_iruby}
EOJS
end
def js_start_iruby
<<-EOJS
<script type="text/javascript">
$(function() {
EOJS
end
def js_end_iruby
<<-EOJS
});
</script>
EOJS
end
end
end