Skip to content

Commit

Permalink
Modified listener implementation accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakriti-nith committed Jun 26, 2018
1 parent 86f8cf7 commit 8223bdd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
17 changes: 13 additions & 4 deletions lib/daru/view/adapters/googlecharts/base_chart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ def query_response_function_name(element_id)
"handleQueryResponse_#{element_id.tr('-', '_')}"
end

# @return [String] js function to add the listener to the chart
def add_listeners_js
js = ''
@listeners.each do |listener|
js << "\n google.visualization.events.addListener("
js << "chart, '#{listener[:event]}', function (e) {"
js << "\n #{listener[:callback]}"
js << "\n });"
end
js
end

# Generates JavaScript when data is imported from spreadsheet and renders
# the Google Chart in the final HTML output when data is URL of the
# google spreadsheet
Expand Down Expand Up @@ -70,10 +82,7 @@ def draw_chart_js(element_id)
js << "\n #{@data_table.to_js}"
js << "\n chart = new google.#{chart_class}.#{chart_name}"
js << "(document.getElementById('#{element_id}'));"
@listeners.each do |listener|
js << "\n google.visualization.events.addListener("
js << "chart, '#{listener[:event]}', #{listener[:callback]});"
end
js << add_listeners_js
js << "\n chart.draw(data_table, #{js_parameters(@options)});"
js << "\n };"
js
Expand Down
17 changes: 13 additions & 4 deletions lib/daru/view/adapters/googlecharts/data_table_iruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ def package_name
'table'
end

# @return [String] js function to add the listener to the chart
def add_listeners_js
js = ''
@listeners.each do |listener|
js << "\n google.visualization.events.addListener("
js << "table, '#{listener[:event]}', function (e) {"
js << "\n #{listener[:callback]}"
js << "\n });"
end
js
end

# Generates JavaScript for loading the appropriate Google Visualization
# package, with callback to render chart.
#
Expand All @@ -123,10 +135,7 @@ def draw_js(element_id)
js << "\n #{to_js}"
js << "\n var table = new google.visualization.Table("
js << "\n document.getElementById('#{element_id}'));"
@listeners.each do |listener|
js << "\n google.visualization.events.addListener("
js << "table, '#{listener[:event]}', #{listener[:callback]});"
end
js << add_listeners_js
js << "\n table.draw(data_table, #{js_parameters(@options)}); "
js << "\n };"
js
Expand Down
14 changes: 3 additions & 11 deletions lib/daru/view/adapters/googlecharts/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ def show_in_iruby(dom=SecureRandom.uuid)

# @see #Daru::View::Plot.export
def export(export_type='png', file_name='chart')
id = SecureRandom.uuid
js = ''
add_listener('ready', "exportChart_#{id.tr('-', '_')}")
js << to_html(id)
js << extract_export_code(export_type, file_name)
add_listener('ready', extract_export_code(export_type, file_name))
js = to_html
js
end

Expand All @@ -119,15 +116,10 @@ def export_iruby(export_type='png', file_name='chart')
# @param file_name [String] The name of the file after exporting the chart
# @return [String] the script to export the chart
def extract_export_code(export_type='png', file_name='chart')
js = ''
js << "\n <script>"
js << "\n function exportChart_#{@html_id.tr('-', '_')}() {"
case export_type
when 'png'
js << extract_export_png_code(file_name)
js = extract_export_png_code(file_name)
end
js << "\n }"
js << "\n </script>"
js
end

Expand Down
6 changes: 4 additions & 2 deletions spec/adapters/googlecharts/base_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@
expect(js).to match(/data_table.addRow\(\[{v: "2013"}\]\)/)
end
it "adds correct listener" do
column_chart.chart.add_listener('ready', "callback")
column_chart.chart.add_listener('ready', "alert('hi');")
expect(js).to match(
/google.visualization.events.addListener\(chart, 'ready', callback\)/)
/google.visualization.events.addListener\(chart, 'ready', function \(e\) {/
)
expect(js).to match(/alert\('hi'\);/)
end
it "generates the valid chart script" do
expect(js).to match(/new google.visualization.DataTable/)
Expand Down
6 changes: 4 additions & 2 deletions spec/adapters/googlecharts/data_table_iruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@
expect(js).to match(/table.draw\(data_table, \{\}/i)
end
it "adds correct listener" do
data_table.table.add_listener('ready', "callback")
data_table.table.add_listener('ready', "alert('hi');")
js = data_table.table.draw_js('id')
expect(js).to match(
/google.visualization.events.addListener\(table, 'ready', callback\)/)
/google.visualization.events.addListener\(table, 'ready', function \(e\) {/
)
expect(js).to match(/alert\('hi'\);/)
end
end

Expand Down
6 changes: 0 additions & 6 deletions spec/adapters/googlecharts/display_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,6 @@
end

describe "#extract_export_code" do
it "generates a script and export fuction" do
area_chart_chart.chart.html_id = 'id'
js = area_chart_chart.chart.extract_export_code
expect(js).to match(/script/)
expect(js).to match(/function exportChart_id/)
end
it "extracts correct png code" do
area_chart_chart.chart.html_id = 'id'
js = area_chart_chart.chart.extract_export_code('png', 'daru')
Expand Down

0 comments on commit 8223bdd

Please sign in to comment.