Class: Vis::Network

Inherits:
Object
  • Object
show all
Includes:
Native, Utilities
Defined in:
lib/vis/network.rb

Constant Summary collapse

EVENTS_NO_COVERSION =
%i[afterDrawing beforeDrawing blurEdge blurNode hoverEdge hoverNode showPopup]
EVENTS_NO_PARAM =
%i[hidePopup startStabilizing stabilizationIterationsDone initRedraw]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#hash_array_to_native, included, #lower_camelize, #lower_camelize_hash, #native_to_hash_array

Constructor Details

#initialize(native_container, dataset_hash, options = {}) ⇒ Network

Returns a new instance of Network.



38
39
40
41
42
43
44
45
# File 'lib/vis/network.rb', line 38

def initialize(native_container, dataset_hash, options = {})
  native_options = options_to_native(options)
  nodes_dataset = dataset_hash[:nodes].to_n
  edges_dataset = dataset_hash[:edges].to_n
  native_data = `{ nodes: nodes_dataset, edges: edges_dataset }`
  @event_handlers = {}
  @native = `new vis.Network(native_container, native_data, native_options)`
end

Class Method Details

.convert_dot(dot_string) ⇒ Object



209
210
211
212
# File 'lib/vis/network.rb', line 209

def self.convert_dot(dot_string)
  res = `vis.network.convertDot(dot_string)`
  `Opal.Hash.$new(res)`
end

.convert_gephi(gephi_json, options) ⇒ Object

importing data



203
204
205
206
207
# File 'lib/vis/network.rb', line 203

def self.convert_gephi(gephi_json, options)
  native_options = options_to_native(options)
  res = `vis.network.convertGephi(gephi_json, native_options)`
  `Opal.Hash.$new(res)`
end

Instance Method Details

#_rubyfy_configure_options(options) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/vis/network.rb', line 261

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

#_rubyfy_edges_options(options) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/vis/network.rb', line 277

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

#_rubyfy_manipulation_options(options) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/vis/network.rb', line 328

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

#_rubyfy_nodes_options(options) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/vis/network.rb', line 362

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

#canvas_to_dom(xy_hash) ⇒ Object

canvas methods



100
101
102
103
# File 'lib/vis/network.rb', line 100

def canvas_to_dom(xy_hash)
  res = @native.JS.canvasToDOM(xy_hash.to_n)
  `Opal.Hash.$new(res)`
end

#cluster(options) ⇒ Object

clustering



118
119
120
# File 'lib/vis/network.rb', line 118

def cluster(options)
  @native.JS.cluster(options_to_native(options))
end

#cluster_by_connection(node_id, options = {}) ⇒ Object



122
123
124
# File 'lib/vis/network.rb', line 122

def cluster_by_connection(node_id, options = {})
  @native.JS.clusterByConnection(node_id, options_to_native(options))
end

#cluster_by_hubsize(hub_size, options = {}) ⇒ Object



126
127
128
# File 'lib/vis/network.rb', line 126

def cluster_by_hubsize(hub_size, options = {})
  @native.JS.clusterByHubsize(hub_size, options_to_native(options))
end

#cluster_outliers(options) ⇒ Object



130
131
132
# File 'lib/vis/network.rb', line 130

def cluster_outliers(options)
  @native.JS.clusterOutliers(options_to_native(options))
end

#dom_to_canvas(xy_hash) ⇒ Object



105
106
107
108
# File 'lib/vis/network.rb', line 105

def dom_to_canvas(xy_hash)
  res = @native.JS.DOMtoCanvas(xy_hash.to_n)
  `Opal.Hash.$new(res)`
end

#fit(options = {}) ⇒ Object

viewport



179
180
181
# File 'lib/vis/network.rb', line 179

def fit(options = {})
  @native.JS.fit(options_to_native(options))
end

#focus(node_id, options = {}) ⇒ Object



183
184
185
# File 'lib/vis/network.rb', line 183

def focus(node_id, options = {})
  @native.JS.focus(node_id, options_to_native(options))
end

#get_bounding_box(node_id) ⇒ Object

information



148
149
150
151
# File 'lib/vis/network.rb', line 148

def get_bounding_box(node_id)
  res = @native.JS.getBoundingBox(node_id)
  `Opal.Hash.$new(res)`
end

#get_edge_at(dom_xy_hash) ⇒ Object

selection



160
161
162
# File 'lib/vis/network.rb', line 160

def get_edge_at(dom_xy_hash)
  @native.JS.getEdgeAt(xy_hash.to_n)
end

#get_node_at(dom_xy_hash) ⇒ Object



164
165
166
# File 'lib/vis/network.rb', line 164

def get_node_at(dom_xy_hash)
  @native.JS.getNodeAt(xy_hash.to_n)
end

#get_options_from_configuratorObject

configurator module



197
198
199
200
# File 'lib/vis/network.rb', line 197

def get_options_from_configurator
  res = @native.JS.getOptionsFromConfigurator
  `Opal.Hash.$new(res)`
end

#get_positions(array_of_node_ids) ⇒ Object



153
154
155
156
# File 'lib/vis/network.rb', line 153

def get_positions(array_of_node_ids)
  res = @native.JS.getPositions(array_of_node_ids)
  `Opal.Hash.$new(res)`
end

#get_selectionObject



168
169
170
171
# File 'lib/vis/network.rb', line 168

def get_selection
  res = @native.JS.getSelection
  `Opal.Hash.$new(res)`
end

#get_view_positionObject



187
188
189
190
# File 'lib/vis/network.rb', line 187

def get_view_position
  res = @native.JS.getViewPosition
  `Opal.Hash.$new(res)`
end

#move_to(options) ⇒ Object



192
193
194
# File 'lib/vis/network.rb', line 192

def move_to(options)
  @native.JS.moveTo(options_to_native(options))
end

#off(event, event_handler_id) ⇒ Object

global methods



49
50
51
52
53
54
# File 'lib/vis/network.rb', line 49

def off(event, event_handler_id)
  event = lower_camelize(event)
  handler = @event_handlers[event][event_handler_id]
  `self["native"].off(event, handler)`
  @event_handlers[event].delete(event_handler_id)
end

#on(event, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vis/network.rb', line 59

def on(event, &block)
  event = lower_camelize(event)
  @event_handlers[event] = {} unless @event_handlers[event]
  event_handler_id = `Math.random().toString(36).substring(6)`
  handler = if EVENTS_NO_COVERSION.include?(event)
    `function(param) { #{block.call(`param`)}; }`
  elsif EVENTS_NO_PARAM.include?(event)
    `function() { #{block.call}; }`
  else
    `function(event_info) { #{block.call(`Opal.Hash.$new(event_info)`)}; }`
  end
  @event_handlers[event][event_handler_id] = handler
  `self["native"].on(event, handler);`
  event_handler_id
end

#once(event, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/vis/network.rb', line 75

def once(event, &block)
  handler = if EVENTS_NO_COVERSION.include?(event)
    `function(param) { #{block.call(`param`)}; }`
  elsif EVENTS_NO_PARAM.include?(event)
    `function() { #{block.call}; }`
  else
    `function(event_info) { #{block.call(`Opal.Hash.$new(event_info)`)}; }`
  end
  `self["native"].once(event, handler);`
end

#open_cluster(node_id, options) ⇒ Object



134
135
136
# File 'lib/vis/network.rb', line 134

def open_cluster(node_id, options)
  @native.JS.openCluster(node_id, options_to_native(options))
end

#options_to_native(options) ⇒ Object

options



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/vis/network.rb', line 216

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

#set_data(dataset_hash) ⇒ Object



86
87
88
89
90
91
# File 'lib/vis/network.rb', line 86

def set_data(dataset_hash)
  nodes_dataset = dataset_hash[:nodes].to_n
  edges_dataset = dataset_hash[:edges].to_n
  native_data = `{ nodes: nodes_dataset, edges: edges_dataset }`
  @native.JS.setData(native_data)
end

#set_options(options) ⇒ Object



93
94
95
96
# File 'lib/vis/network.rb', line 93

def set_options(options)
  native_options = options_to_native(options)
  @native.JS.setOptions(native_options)
end

#set_selection(selection_hash, options = {}) ⇒ Object



173
174
175
# File 'lib/vis/network.rb', line 173

def set_selection(selection_hash, options = {})
  @native.JS.setSelection(selection_hash.to_n, options_to_native(options))
end

#set_size(width, height) ⇒ Object



110
111
112
113
114
# File 'lib/vis/network.rb', line 110

def set_size(width, height)
  width = width.to_s
  height = height.to_s
  @native.JS.setSize(width, height)
end

#update_clustered_node(clustered_node_id, options) ⇒ Object



142
143
144
# File 'lib/vis/network.rb', line 142

def update_clustered_node(clustered_node_id, options)
  @native.JS.updateClusteredNode(start_edge_id, options_to_native(options))
end

#update_edge(start_edge_id, options) ⇒ Object



138
139
140
# File 'lib/vis/network.rb', line 138

def update_edge(start_edge_id, options)
  @native.JS.updateEdge(start_edge_id, options_to_native(options))
end