From 8223bdd3c4206b02ca5d260249f0355ae1f088d5 Mon Sep 17 00:00:00 2001 From: Prakriti-nith Date: Wed, 27 Jun 2018 03:09:11 +0530 Subject: [PATCH] Modified listener implementation accordingly --- .../view/adapters/googlecharts/base_chart.rb | 17 +++++++++++++---- .../adapters/googlecharts/data_table_iruby.rb | 17 +++++++++++++---- lib/daru/view/adapters/googlecharts/display.rb | 14 +++----------- spec/adapters/googlecharts/base_chart_spec.rb | 6 ++++-- .../googlecharts/data_table_iruby_spec.rb | 6 ++++-- spec/adapters/googlecharts/display_spec.rb | 6 ------ 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/daru/view/adapters/googlecharts/base_chart.rb b/lib/daru/view/adapters/googlecharts/base_chart.rb index 7061d5b..f5dc4e5 100644 --- a/lib/daru/view/adapters/googlecharts/base_chart.rb +++ b/lib/daru/view/adapters/googlecharts/base_chart.rb @@ -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 @@ -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 diff --git a/lib/daru/view/adapters/googlecharts/data_table_iruby.rb b/lib/daru/view/adapters/googlecharts/data_table_iruby.rb index 1687417..eb154b6 100644 --- a/lib/daru/view/adapters/googlecharts/data_table_iruby.rb +++ b/lib/daru/view/adapters/googlecharts/data_table_iruby.rb @@ -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. # @@ -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 diff --git a/lib/daru/view/adapters/googlecharts/display.rb b/lib/daru/view/adapters/googlecharts/display.rb index 740758e..dae57b3 100644 --- a/lib/daru/view/adapters/googlecharts/display.rb +++ b/lib/daru/view/adapters/googlecharts/display.rb @@ -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 @@ -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 " js end diff --git a/spec/adapters/googlecharts/base_chart_spec.rb b/spec/adapters/googlecharts/base_chart_spec.rb index 18b4c19..0d65eae 100644 --- a/spec/adapters/googlecharts/base_chart_spec.rb +++ b/spec/adapters/googlecharts/base_chart_spec.rb @@ -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/) diff --git a/spec/adapters/googlecharts/data_table_iruby_spec.rb b/spec/adapters/googlecharts/data_table_iruby_spec.rb index c4b0156..a201365 100644 --- a/spec/adapters/googlecharts/data_table_iruby_spec.rb +++ b/spec/adapters/googlecharts/data_table_iruby_spec.rb @@ -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 diff --git a/spec/adapters/googlecharts/display_spec.rb b/spec/adapters/googlecharts/display_spec.rb index c0f96ff..6934193 100644 --- a/spec/adapters/googlecharts/display_spec.rb +++ b/spec/adapters/googlecharts/display_spec.rb @@ -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')