Class: Vis::Graph2d

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

Constant Summary collapse

EVENTS_NO_PARAM =
%i[currentTimeTick changed]

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, item_dataset, group_dataset = nil, options = {}) ⇒ Graph2d

Returns a new instance of Graph2d.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vis/graph2d.rb', line 20

def initialize(native_container, item_dataset, group_dataset = nil, options = {})
  native_item_data = item_dataset.to_n
  if group_dataset.is_a?(Hash) && options == {}
    options = group_dataset
    group_dataset = nil
  end
  native_options = options_to_native(options)
  @event_handlers = {}
  if group_dataset.nil?
    @native = `new vis.Graph2d(native_container, native_item_data, native_options)`
  else
    native_group_data = group_dataset.to_n
    @native = `new vis.Graph2d(native_container, native_item_data, native_group_data, native_options)`
  end
end

Instance Method Details

#get_data_rangeObject



71
72
73
74
# File 'lib/vis/graph2d.rb', line 71

def get_data_range
  res = @native.JS.getDataRange()
  `Opal.Hash.$new(res)`
end

#get_event_properties(event) ⇒ Object



76
77
78
79
# File 'lib/vis/graph2d.rb', line 76

def get_event_properties(event)
  res = @native.JS.getEventProperties(event.to_n)
  `Opal.Hash.$new(res)`
end

#get_legend(group_id, icon_width, icon_height) ⇒ Object



81
82
83
84
# File 'lib/vis/graph2d.rb', line 81

def get_legend(group_id, icon_width, icon_height)
  res = @native.JS.getLegend(group_id, icon_width, icon_height)
  `Opal.Hash.$new(res)`
end

#get_windowObject



86
87
88
89
# File 'lib/vis/graph2d.rb', line 86

def get_window
  res = @native.JS.getWindow()
  `Opal.Hash.$new(res)`
end

#move_to(time, options = {}) ⇒ Object



91
92
93
# File 'lib/vis/graph2d.rb', line 91

def move_to(time, options = {})
  @native.JS.moveTo(time, options_to_native(options))
end

#off(event, event_handler_id) ⇒ Object



36
37
38
39
40
41
# File 'lib/vis/graph2d.rb', line 36

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vis/graph2d.rb', line 45

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_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

#options_to_native(options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vis/graph2d.rb', line 95

def options_to_native(options)
  return unless options
  # options must be duplicated, so callbacks dont get wrapped twice
  new_opts = {}.merge!(options)

  if new_opts.has_key?(:configure)
    block = new_opts[:configure]
    if `typeof block === "function"`
      unless new_opts[:configure].JS[:hyper_wrapped]
        new_opts[:configure] = %x{
          function(option, path) {
            return block.$call(option, path);
          }
        }
        new_opts[:configure].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:draw_points)
    block = new_opts[:draw_points]
    if `typeof block === "function"`
      unless new_opts[:draw_points].JS[:hyper_wrapped]
        new_opts[:draw_points] = %x{
          function(item, group) {
            return block.$call(Opal.Hash.$new(item), Opal.Hash.$new(group));
          }
        }
        new_opts[:draw_points].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:moment)
    block = new_opts[:moment]
    if `typeof block === "function"`
      unless new_opts[:moment].JS[:hyper_wrapped]
        new_opts[:moment] = %x{
          function(native_date) {
            return block.$call(native_date);
          }
        }
        new_opts[:moment].JS[:hyper_wrapped] = true
      end
    end
  end

  lower_camelize_hash(new_opts).to_n
end

#set_groups(dataset) ⇒ Object



59
60
61
# File 'lib/vis/graph2d.rb', line 59

def set_groups(dataset)
  @native.JS.setGroups(dataset.to_n)
end

#set_items(dataset) ⇒ Object



63
64
65
# File 'lib/vis/graph2d.rb', line 63

def set_items(dataset)
  @native.JS.setItems(dataset.to_n)
end

#set_options(options) ⇒ Object



67
68
69
# File 'lib/vis/graph2d.rb', line 67

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