Class: Vis::Timeline

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

Constant Summary collapse

EVENTS_NO_COVERSION =
%i[groupDragged]
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, dataset, group_dataset = nil, options = {}) ⇒ Timeline

Returns a new instance of Timeline.



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

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

Instance Method Details

#fit(options = {}) ⇒ Object



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

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

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



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

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

#get_event_properties(event) ⇒ Object



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

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

#get_item_rangeObject



95
96
97
98
# File 'lib/vis/timeline.rb', line 95

def get_item_range
  res = @native.JS.getItemRange()
  `Opal.Hash.$new(res)`
end

#get_windowObject



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

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

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



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

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

#off(event, event_handler_id) ⇒ Object



37
38
39
40
41
42
# File 'lib/vis/timeline.rb', line 37

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vis/timeline.rb', line 47

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

#options_to_native(options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
327
328
329
330
331
332
333
334
335
336
# File 'lib/vis/timeline.rb', line 155

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?(:format)
    block = new_opts[:format]
    if `typeof block === "function"`
      unless new_opts[:format].JS[:hyper_wrapped]
        # this is not clear in the vis docs
        new_opts[:format] = %x{
          function(object) {
            return #{block.call(`Opal.Hash.$new(object)`)};
          }
        }
        new_opts[:format].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:group_order)
    block = new_opts[:group_order]
    if `typeof block === "function"`
      unless new_opts[:group_order].JS[:hyper_wrapped]
        # this is not clear in the vis docs
        new_opts[:group_order] = %x{
          function() {
            return #{block.call};
          }
        }
        new_opts[:group_order].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:group_order_swap)
    block = new_opts[:group_order_swap]
    if `typeof block === "function"`
      unless new_opts[:group_order_swap].JS[:hyper_wrapped]
        # this is not clear in the vis docs
        new_opts[:group_order_swap] = %x{
          function(from_group, to_group, groups) {
            return #{block.call(`Opal.Hash.$new(from_group)`, `Opal.Hash.$new(to_group)`, Vis::DataSet.wrap(groups))};
          }
        }
        new_opts[:group_order_swap].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:group_template)
    block = new_opts[:group_template]
    if `typeof block === "function"`
      unless new_opts[:group_template].JS[:hyper_wrapped]
        # this is not clear in the vis docs
        new_opts[:group_template] = %x{
          function(groups, group_element) {
            return #{block.call(Vis::DataSet.wrap(groups), `Opal.Hash.$new(group_element)`)};
          }
        }
        new_opts[:group_template].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

  %i[on_add on_add_group on_move on_move_group on_moving on_remove on_remove_group on_update].each do |key|
    if new_opts.has_key?(key)
      block = new_opts[key]
      if `typeof block === "function"`
        unless new_opts[key].JS[:hyper_wrapped]
          new_opts[key] = %x{
            function(item, callback) {
              var wrapped_callback = #{ proc { |new_node_data| `callback(new_item.$to_n());` }};
              block.$call(Opal.Hash.$new(item), wrapped_callback);
            }
          }
          new_opts[key].JS[:hyper_wrapped] = true
        end
      end
    end
  end

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

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

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

  if new_opts.has_key?(:template)
    block = new_opts[:template]
    if `typeof block === "function"`
      unless new_opts[:template].JS[:hyper_wrapped]
        # not clear in vis docs
        new_opts[:template] = %x{
          function(item, element, edited_data) {
            return block.$call(Opal.Hash.$new(item), element, Opal.Hash.$new(edited_data));
          }
        }
        new_opts[:template].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:visible_frame_template)
    block = new_opts[:visible_frame_template]
    if `typeof block === "function"`
      unless new_opts[:visible_frame_template].JS[:hyper_wrapped]
        # not clear in vis docs
        new_opts[:visible_frame_template] = %x{
          function(item, element) {
            return block.$call(Opal.Hash.$new(item), element);
          }
        }
        new_opts[:visible_frame_template].JS[:hyper_wrapped] = true
      end
    end
  end

  lower_camelize_hash(new_opts).to_n
end

#set_data(dataset_hash) ⇒ Object



63
64
65
66
67
68
# File 'lib/vis/timeline.rb', line 63

def set_data(dataset_hash)
  native_data_hash = {}
  native_data_hash[:groups] = dataset_hash[:groups].to_n if dataset_hash.has_key?(:groups)
  native_data_hash[:items] = dataset_hash[:items].to_n if dataset_hash.has_key?(:items)
  @native.JS.setData(native_data_hash.to_n)
end

#set_groups(dataset) ⇒ Object



70
71
72
# File 'lib/vis/timeline.rb', line 70

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

#set_items(dataset) ⇒ Object



74
75
76
# File 'lib/vis/timeline.rb', line 74

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

#set_options(options) ⇒ Object



78
79
80
# File 'lib/vis/timeline.rb', line 78

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

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



109
110
111
# File 'lib/vis/timeline.rb', line 109

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

#set_window(start_time, end_time, options = {}, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vis/timeline.rb', line 113

def set_window(start_time, end_time, options = {}, &block)
  native_options = options_to_native(options)
  if block_given?
    callback = %x{
      function() {
        return block.$call();
      }
    }
    @native.JS.setWindow(start_time, end_time, native_options, callback)
  else
    @native.JS.setWindow(start_time, end_time, native_options)
  end
end

#zoom_in(percentage, options = {}, &block) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/vis/timeline.rb', line 127

def zoom_in(percentage, options = {}, &block)
  native_options = options_to_native(options)
  if block_given?
    callback = %x{
      function() {
        return block.$call();
      }
    }
    @native.JS.zoomIn(percentage, native_options, callback)
  else
    @native.JS.zoomIn(percentage, native_options)
  end
end

#zoom_out(percentage, options = {}, &block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/vis/timeline.rb', line 141

def zoom_out(percentage, options = {}, &block)
  native_options = options_to_native(options)
  if block_given?
    callback = %x{
      function() {
        return block.$call();
      }
    }
    @native.JS.zoomOut(percentage, native_options, callback)
  else
    @native.JS.zoomOut(percentage, native_options)
  end
end