Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defined show and show_in_iruby methods #236

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lazy_high_charts.gemspec
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3"

s.add_development_dependency "bundler", ">= 1.0"
s.add_development_dependency "iruby"
s.add_dependency "hash-deep-merge"

s.description = <<-DESC
Expand Down
53 changes: 53 additions & 0 deletions lib/lazy_high_charts.rb
Expand Up @@ -15,4 +15,57 @@ module LazyHighCharts
def self.root
File.expand_path '../..', __FILE__
end

@@dep_libraries = {
# jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
highcharts: 'http://code.highcharts.com/highcharts.js'
}
@@additional_libraries = {}

# Load extension library to IRuby notebook after Nyaplotjs is loaded
def self.add_dependency(name, url)
@@dep_libraries[name]=url;
end

# Load extension library to IRuby notebook before Nyaplotjs is loaded
def self.add_additional_library(name, url)
@@additional_libraries[name]=url
end

# generate initializing code
def self.generate_init_code(assets=:inline)
Copy link
Author

Choose a reason for hiding this comment

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

assets= :cdn is not working . I don't know why.

dep_libraries = @@dep_libraries
additional_libraries = @@additional_libraries
js_dir = File.expand_path("../../vendor/assets/javascripts/highcharts", __FILE__)
case assets
when :cdn
path = File.expand_path("../templates/init.cdn.js.erb", __FILE__)
when :inline
path = File.expand_path("../templates/init.inline.js.erb", __FILE__)
end
template = File.read(path)
ERB.new(template).result(binding)
end

# Enable to show plots on IRuby notebook
def self.init_iruby
js = self.generate_init_code
IRuby.display(IRuby.javascript(js))
end

def self.load_notebook(assets=:inline)
init_code = generate_init_code
case assets
when :cdn
IRuby.display(IRuby.javascript(init_code))
when :inline
IRuby.display(IRuby.html(<<END_HTML))
<script type="application/javascript">
#{init_code}
</script>
END_HTML
end
true
end
self.init_iruby if defined? IRuby
end
4 changes: 4 additions & 0 deletions lib/lazy_high_charts/layout_helper.rb
Expand Up @@ -10,6 +10,10 @@ def high_chart(placeholder, object, &block)
high_graph(placeholder, object, &block).concat(content_tag("div", "", object.html_options))
end

def high_chart_iruby(placeholder, object, &block)
Copy link
Author

Choose a reason for hiding this comment

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

Display nothing. @michelson can you check why it doesn't work ?

IRuby.html(high_chart(placeholder, object, &block))
end

def high_stock(placeholder, object, &block)
object.html_options.merge!({:id => placeholder})
object.options[:chart][:renderTo] = placeholder
Expand Down
47 changes: 47 additions & 0 deletions lib/templates/init.cdn.js.erb
@@ -0,0 +1,47 @@
if(window['jquery'] === undefined ||
window['highcharts'] === undefined){
var path = <%= dep_libraries.merge(additional_libraries).to_json %>;

<%
shim = dep_libraries.merge(additional_libraries).inject({}){|hash, (k, v)| hash[k]={exports: k};next hash}
%>

var shim = <%= shim.to_json %>;

require.config({paths: path, shim:shim});

<%
str=""
dep_libraries.each do |key, val|
str.concat("require(['%s'], function(%s){window['%s']=%s;console.log('finished loading %s');"% Array.new(5, key))
end
%>
<%= str %>

var script = jquen.select("head")
.append("script")
.attr("src", "http://code.highcharts.com/highcharts.js")
.attr("async", true);

script[0][0].onload = script[0][0].onreadystatechange = function(){
<%
str=""
additional_libraries.each do |key, val|
str.concat("require(['%s'], function(%s){window['%s']=%s;console.log('finished loading %s');"% Array.new(5, key))
end
%>
<%= str %>
var event = document.createEvent("HTMLEvents");
event.initEvent("load_highcharts",false,false);
window.dispatchEvent(event);
console.log('Finished loading highchartsjs');
<%=
str = Array.new(additional_libraries.length, "});").join("")
%>
};

<%
str = Array.new(dep_libraries.length, "});").join("")
%>
<%= str %>
}
17 changes: 17 additions & 0 deletions lib/templates/init.inline.js.erb
@@ -0,0 +1,17 @@
if (window['jquery'] === undefined || window['highcharts'] === undefined) {
<%
all_libraries = dep_libraries.merge(additional_libraries)
all_libraries.each do |name, path|
basename = File.basename(path)
contents = IO.read(File.join(js_dir, basename), mode: 'r:UTF-8')
%>
/* BEGIN <%= name %>.js */
<%= contents %>
/* END <%= name %>.js */
<% end %>

var event = document.createEvent("HTMLEvents");
event.initEvent("load_highcharts", false, false);
window.dispatchEvent(event);
console.log("Finish loading highchartsjs");
}