diff --git a/lib/nyaplot/core.rb b/lib/nyaplot/core.rb index 7d5b4ce..a5b1530 100644 --- a/lib/nyaplot/core.rb +++ b/lib/nyaplot/core.rb @@ -11,16 +11,6 @@ def self.extension_lists @@extension_lists end - def self.init_iruby - raise "IRuby notebook is not loaded." unless defined? IRuby - - path = File.expand_path("../templates/init.html.erb", __FILE__) - template = File.read(path) - dep_libraries = @@dep_libraries - html = ERB.new(template).result(binding) - return IRuby.html(html) - end - def self.add_extension(name) @@extension_lists.push(name) end @@ -28,4 +18,14 @@ def self.add_extension(name) def self.add_dependency(name, url) @@dep_libraries[name]=url; end + + def self.init_iruby + path = File.expand_path("../templates/init.js.erb", __FILE__) + template = File.read(path) + dep_libraries = @@dep_libraries + js = ERB.new(template).result(binding) + IRuby.display(IRuby.javascript(js)) + end + + init_iruby if defined? IRuby end diff --git a/lib/nyaplot/frame.rb b/lib/nyaplot/frame.rb index 508ccbe..6fdb3cb 100644 --- a/lib/nyaplot/frame.rb +++ b/lib/nyaplot/frame.rb @@ -27,16 +27,19 @@ def export_html template = File.read(path) model = self.to_json html = ERB.new(template).result(binding) - return html + html end - def show + def to_iruby path = File.expand_path("../templates/iruby.erb", __FILE__) template = File.read(path) id = SecureRandom.uuid() model = self.to_json - html = ERB.new(template).result(binding) - return IRuby.html(html) + ['text/html', ERB.new(template).result(binding)] + end + + def show + IRuby.display(self) end def configure(&block) diff --git a/lib/nyaplot/plot.rb b/lib/nyaplot/plot.rb index fe2a5ab..0d7048d 100644 --- a/lib/nyaplot/plot.rb +++ b/lib/nyaplot/plot.rb @@ -27,10 +27,12 @@ def add_with_df(df, type, *labels) return diagram end + def to_iruby + Frame.new.tap {|f| f.add(self) }.to_iruby + end + def show - frame = Frame.new - frame.add(self) - frame.show + Frame.new.tap {|f| f.add(self) }.show end def df_list diff --git a/lib/nyaplot/templates/init.html.erb b/lib/nyaplot/templates/init.js.erb similarity index 98% rename from lib/nyaplot/templates/init.html.erb rename to lib/nyaplot/templates/init.js.erb index f7142cd..9ec8343 100644 --- a/lib/nyaplot/templates/init.html.erb +++ b/lib/nyaplot/templates/init.js.erb @@ -1,4 +1,3 @@ - diff --git a/lib/nyaplot/templates/iruby.erb b/lib/nyaplot/templates/iruby.erb index fa30412..582bec7 100644 --- a/lib/nyaplot/templates/iruby.erb +++ b/lib/nyaplot/templates/iruby.erb @@ -8,7 +8,8 @@ if(window['Nyaplot']==undefined){ window.addEventListener('load_nyaplot', render, false); return; + } else { + render(); } - render(); })();