lib/vis/network.rb in hyper-vis-1.0.0.lap34 vs lib/vis/network.rb in hyper-vis-1.0.1

- old
+ new

@@ -117,15 +117,15 @@ def cluster(options) @native.JS.cluster(options_to_native(options)) end - def cluster_by_connection(node_id, options) + def cluster_by_connection(node_id, options = {}) @native.JS.clusterByConnection(node_id, options_to_native(options)) end - def cluster_by_hubsize(hub_size, options) + def cluster_by_hubsize(hub_size, options = {}) @native.JS.clusterByHubsize(hub_size, options_to_native(options)) end def cluster_outliers(options) @native.JS.clusterOutliers(options_to_native(options)) @@ -168,21 +168,21 @@ def get_selection res = @native.JS.getSelection `Opal.Hash.$new(res)` end - def set_selection(selection_hash, options) + def set_selection(selection_hash, options = {}) @native.JS.setSelection(selection_hash.to_n, options_to_native(options)) end # viewport - def fit(options) + def fit(options = {}) @native.JS.fit(options_to_native(options)) end - def focus(node_id, options) + def focus(node_id, options = {}) @native.JS.focus(node_id, options_to_native(options)) end def get_view_position res = @native.JS.getViewPosition @@ -209,11 +209,190 @@ def self.convert_dot(dot_string) res = `vis.network.convertDot(dot_string)` `Opal.Hash.$new(res)` end - # test helper - def self.test_container - `document.body.appendChild(document.createElement('div'))` + # options + + def options_to_native(options) + return unless options + # options must be duplicated, so callbacks dont get wrapped twice + new_opts = {}.merge!(options) + _rubyfy_configure_options(new_opts) if new_opts.has_key?(:configure) + _rubyfy_edges_options(new_opts) if new_opts.has_key?(:edges) + _rubyfy_manipulation_options(new_opts) if new_opts.has_key?(:manipulation) + _rubyfy_nodes_options(new_opts) if new_opts.has_key?(:nodes) + + if new_opts.has_key?(:join_condition) + block = new_opts[:join_condition] + if `typeof block === "function"` + unless new_opts[:join_condition].JS[:hyper_wrapped] + new_opts[:join_condition] = %x{ + function(node_options, child_options) { + if (child_options !== undefined && child_options !== null) { + return #{block.call(`Opal.Hash.$new(node_options)`, `Opal.Hash.$new(child_options)`)}; + } else { + return #{block.call(`Opal.Hash.$new(node_options)`)}; + } + } + } + new_opts[:join_condition].JS[:hyper_wrapped] = true + end + end + end + + if new_opts.has_key?(:process_properties) + block = new_opts[:process_properties] + if `typeof block === "function"` + unless new_opts[:process_properties].JS[:hyper_wrapped] + new_opts[:process_properties] = %x{ + function(item) { + var res = #{block.call(`Opal.Hash.$new(item)`)}; + return res.$to_n(); + } + } + new_opts[:process_properties].JS[:hyper_wrapped] = true + end + end + end + + lower_camelize_hash(new_opts).to_n + end + + def _rubyfy_configure_options(options) + if options[:configure].has_key?(:filter) + block = options[:configure][:filter] + if `typeof block === "function"` + unless options[:configure][:filter].JS[:hyper_wrapped] + options[:configure][:filter] = %x{ + function(option, path) { + return #{block.call(`Opal.Hash.$new(options)`, `path`)}; + } + } + options[:configure][:filter].JS[:hyper_wrapped] = true + end + end + end + end + + def _rubyfy_edges_options(options) + if options[:edges].has_key?(:chosen) + chosen = options[:edges][:chosen] + [:edge, :label].each do |key| + if chosen.has_key?(key) + block = chosen[key] + if `typeof block === "function"` + unless options[:edges][:chosen][key].JS[:hyper_wrapped] + options[:edges][:chosen][key] = %x{ + function(values, id, selected, hovering) { + return #{block.call(`Opal.Hash.$new(values)`, `id`, `selected`, `hovering`)}; + } + } + options[:edges][:chosen][key].JS[:hyper_wrapped] = true + end + end + end + end + end + [:hover_width, :selection_width].each do |key| + if options[:edges].has_key?(key) + block = options[:edges][key] + if `typeof block === "function"` + unless options[:edges][key].JS[:hyper_wrapped] + options[:edges][key] = %x{ + function(width) { + return #{block.call(`width`)}; + } + } + options[:edges][key].JS[:hyper_wrapped] = true + end + end + end + end + if options[:edges].has_key?(:scaling) + if options[:edges][:scaling].has_key?(:custom_scaling_function) + block = options[:edges][:scaling][:custom_scaling_function] + if `typeof block === "function"` + unless options[:edges][:scaling][:custom_scaling_function].JS[:hyper_wrapped] + options[:edges][:scaling][:custom_scaling_function] = %x{ + function(min, max, total, value) { + return #{block.call(`min`, `max`, `total`, `value`)}; + } + } + options[:edges][:scaling][:custom_scaling_function].JS[:hyper_wrapped] = true + end + end + end + end + end + + def _rubyfy_manipulation_options(options) + [:add_edge, :add_node, :edit_edge, :edit_node].each do |key| + next unless options[:manipulation].has_key?(key) + block = options[:manipulation][key] + if `typeof block === "function"` + unless options[:manipulation][key].JS[:hyper_wrapped] + options[:manipulation][key] = %x{ + function(nodeData, callback) { + var wrapped_callback = #{ proc { |new_node_data| `callback(new_node_data.$to_n());` }}; + block.$call(Opal.Hash.$new(nodeData), wrapped_callback); + } + } + end + options[:manipulation][key].JS[:hyper_wrapped] = true + end + end + # for delete the order of args for the callback is not clear + [:delete_edge, :delete_node].each do |key| + next unless options[:manipulation].has_key?(key) + block = options[:manipulation][key] + if `typeof block === "function"` + unless options[:manipulation][key].JS[:hyper_wrapped] + options[:manipulation][key] = %x{ + function(nodeData, callback) { + var wrapped_callback = #{ proc { |new_node_data| `callback(new_node_data.$to_n());` }}; + block.$call(Opal.Hash.$new(nodeData), wrapped_callback); + } + } + options[:manipulation][key].JS[:hyper_wrapped] = true + end + end + end + end + + def _rubyfy_nodes_options(options) + if options[:nodes].has_key?(:chosen) + chosen = options[:nodes][:chosen] + [:node, :label].each do |key| + if chosen.has_key?(key) + block = chosen[key] + if `typeof block === "function"` + unless options[:nodes][:chosen][key].JS[:hyper_wrapped] + options[:nodes][:chosen][key] = %x{ + function(values, id, selected, hovering) { + return #{block.call(`Opal.Hash.$new(values)`, `id`, `selected`, `hovering`)}; + } + } + options[:nodes][:chosen][key].JS[:hyper_wrapped] = true + end + end + end + end + end + if options[:nodes].has_key?(:scaling) + if options[:nodes][:scaling].has_key?(:custom_scaling_function) + block = options[:nodes][:scaling][:custom_scaling_function] + if `typeof block === "function"` + unless options[:nodes][:scaling][:custom_scaling_function].JS[:hyper_wrapped] + options[:nodes][:scaling][:custom_scaling_function] = %x{ + function(min, max, total, value) { + return #{block.call(`min`, `max`, `total`, `value`)}; + } + } + options[:nodes][:scaling][:custom_scaling_function].JS[:hyper_wrapped] = true + end + end + end + end end end end \ No newline at end of file