hyper-vis 1.0.0.lap34 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -119,11 +119,11 @@ module Vis
119
119
  @native.JS.cluster(options_to_native(options))
120
120
  end
121
121
 
122
- def cluster_by_connection(node_id, options)
122
+ def cluster_by_connection(node_id, options = {})
123
123
  @native.JS.clusterByConnection(node_id, options_to_native(options))
124
124
  end
125
125
 
126
- def cluster_by_hubsize(hub_size, options)
126
+ def cluster_by_hubsize(hub_size, options = {})
127
127
  @native.JS.clusterByHubsize(hub_size, options_to_native(options))
128
128
  end
129
129
 
@@ -170,17 +170,17 @@ module Vis
170
170
  `Opal.Hash.$new(res)`
171
171
  end
172
172
 
173
- def set_selection(selection_hash, options)
173
+ def set_selection(selection_hash, options = {})
174
174
  @native.JS.setSelection(selection_hash.to_n, options_to_native(options))
175
175
  end
176
176
 
177
177
  # viewport
178
178
 
179
- def fit(options)
179
+ def fit(options = {})
180
180
  @native.JS.fit(options_to_native(options))
181
181
  end
182
182
 
183
- def focus(node_id, options)
183
+ def focus(node_id, options = {})
184
184
  @native.JS.focus(node_id, options_to_native(options))
185
185
  end
186
186
 
@@ -211,9 +211,188 @@ module Vis
211
211
  `Opal.Hash.$new(res)`
212
212
  end
213
213
 
214
- # test helper
215
- def self.test_container
216
- `document.body.appendChild(document.createElement('div'))`
214
+ # options
215
+
216
+ def options_to_native(options)
217
+ return unless options
218
+ # options must be duplicated, so callbacks dont get wrapped twice
219
+ new_opts = {}.merge!(options)
220
+ _rubyfy_configure_options(new_opts) if new_opts.has_key?(:configure)
221
+ _rubyfy_edges_options(new_opts) if new_opts.has_key?(:edges)
222
+ _rubyfy_manipulation_options(new_opts) if new_opts.has_key?(:manipulation)
223
+ _rubyfy_nodes_options(new_opts) if new_opts.has_key?(:nodes)
224
+
225
+ if new_opts.has_key?(:join_condition)
226
+ block = new_opts[:join_condition]
227
+ if `typeof block === "function"`
228
+ unless new_opts[:join_condition].JS[:hyper_wrapped]
229
+ new_opts[:join_condition] = %x{
230
+ function(node_options, child_options) {
231
+ if (child_options !== undefined && child_options !== null) {
232
+ return #{block.call(`Opal.Hash.$new(node_options)`, `Opal.Hash.$new(child_options)`)};
233
+ } else {
234
+ return #{block.call(`Opal.Hash.$new(node_options)`)};
235
+ }
236
+ }
237
+ }
238
+ new_opts[:join_condition].JS[:hyper_wrapped] = true
239
+ end
240
+ end
241
+ end
242
+
243
+ if new_opts.has_key?(:process_properties)
244
+ block = new_opts[:process_properties]
245
+ if `typeof block === "function"`
246
+ unless new_opts[:process_properties].JS[:hyper_wrapped]
247
+ new_opts[:process_properties] = %x{
248
+ function(item) {
249
+ var res = #{block.call(`Opal.Hash.$new(item)`)};
250
+ return res.$to_n();
251
+ }
252
+ }
253
+ new_opts[:process_properties].JS[:hyper_wrapped] = true
254
+ end
255
+ end
256
+ end
257
+
258
+ lower_camelize_hash(new_opts).to_n
259
+ end
260
+
261
+ def _rubyfy_configure_options(options)
262
+ if options[:configure].has_key?(:filter)
263
+ block = options[:configure][:filter]
264
+ if `typeof block === "function"`
265
+ unless options[:configure][:filter].JS[:hyper_wrapped]
266
+ options[:configure][:filter] = %x{
267
+ function(option, path) {
268
+ return #{block.call(`Opal.Hash.$new(options)`, `path`)};
269
+ }
270
+ }
271
+ options[:configure][:filter].JS[:hyper_wrapped] = true
272
+ end
273
+ end
274
+ end
275
+ end
276
+
277
+ def _rubyfy_edges_options(options)
278
+ if options[:edges].has_key?(:chosen)
279
+ chosen = options[:edges][:chosen]
280
+ [:edge, :label].each do |key|
281
+ if chosen.has_key?(key)
282
+ block = chosen[key]
283
+ if `typeof block === "function"`
284
+ unless options[:edges][:chosen][key].JS[:hyper_wrapped]
285
+ options[:edges][:chosen][key] = %x{
286
+ function(values, id, selected, hovering) {
287
+ return #{block.call(`Opal.Hash.$new(values)`, `id`, `selected`, `hovering`)};
288
+ }
289
+ }
290
+ options[:edges][:chosen][key].JS[:hyper_wrapped] = true
291
+ end
292
+ end
293
+ end
294
+ end
295
+ end
296
+ [:hover_width, :selection_width].each do |key|
297
+ if options[:edges].has_key?(key)
298
+ block = options[:edges][key]
299
+ if `typeof block === "function"`
300
+ unless options[:edges][key].JS[:hyper_wrapped]
301
+ options[:edges][key] = %x{
302
+ function(width) {
303
+ return #{block.call(`width`)};
304
+ }
305
+ }
306
+ options[:edges][key].JS[:hyper_wrapped] = true
307
+ end
308
+ end
309
+ end
310
+ end
311
+ if options[:edges].has_key?(:scaling)
312
+ if options[:edges][:scaling].has_key?(:custom_scaling_function)
313
+ block = options[:edges][:scaling][:custom_scaling_function]
314
+ if `typeof block === "function"`
315
+ unless options[:edges][:scaling][:custom_scaling_function].JS[:hyper_wrapped]
316
+ options[:edges][:scaling][:custom_scaling_function] = %x{
317
+ function(min, max, total, value) {
318
+ return #{block.call(`min`, `max`, `total`, `value`)};
319
+ }
320
+ }
321
+ options[:edges][:scaling][:custom_scaling_function].JS[:hyper_wrapped] = true
322
+ end
323
+ end
324
+ end
325
+ end
326
+ end
327
+
328
+ def _rubyfy_manipulation_options(options)
329
+ [:add_edge, :add_node, :edit_edge, :edit_node].each do |key|
330
+ next unless options[:manipulation].has_key?(key)
331
+ block = options[:manipulation][key]
332
+ if `typeof block === "function"`
333
+ unless options[:manipulation][key].JS[:hyper_wrapped]
334
+ options[:manipulation][key] = %x{
335
+ function(nodeData, callback) {
336
+ var wrapped_callback = #{ proc { |new_node_data| `callback(new_node_data.$to_n());` }};
337
+ block.$call(Opal.Hash.$new(nodeData), wrapped_callback);
338
+ }
339
+ }
340
+ end
341
+ options[:manipulation][key].JS[:hyper_wrapped] = true
342
+ end
343
+ end
344
+ # for delete the order of args for the callback is not clear
345
+ [:delete_edge, :delete_node].each do |key|
346
+ next unless options[:manipulation].has_key?(key)
347
+ block = options[:manipulation][key]
348
+ if `typeof block === "function"`
349
+ unless options[:manipulation][key].JS[:hyper_wrapped]
350
+ options[:manipulation][key] = %x{
351
+ function(nodeData, callback) {
352
+ var wrapped_callback = #{ proc { |new_node_data| `callback(new_node_data.$to_n());` }};
353
+ block.$call(Opal.Hash.$new(nodeData), wrapped_callback);
354
+ }
355
+ }
356
+ options[:manipulation][key].JS[:hyper_wrapped] = true
357
+ end
358
+ end
359
+ end
360
+ end
361
+
362
+ def _rubyfy_nodes_options(options)
363
+ if options[:nodes].has_key?(:chosen)
364
+ chosen = options[:nodes][:chosen]
365
+ [:node, :label].each do |key|
366
+ if chosen.has_key?(key)
367
+ block = chosen[key]
368
+ if `typeof block === "function"`
369
+ unless options[:nodes][:chosen][key].JS[:hyper_wrapped]
370
+ options[:nodes][:chosen][key] = %x{
371
+ function(values, id, selected, hovering) {
372
+ return #{block.call(`Opal.Hash.$new(values)`, `id`, `selected`, `hovering`)};
373
+ }
374
+ }
375
+ options[:nodes][:chosen][key].JS[:hyper_wrapped] = true
376
+ end
377
+ end
378
+ end
379
+ end
380
+ end
381
+ if options[:nodes].has_key?(:scaling)
382
+ if options[:nodes][:scaling].has_key?(:custom_scaling_function)
383
+ block = options[:nodes][:scaling][:custom_scaling_function]
384
+ if `typeof block === "function"`
385
+ unless options[:nodes][:scaling][:custom_scaling_function].JS[:hyper_wrapped]
386
+ options[:nodes][:scaling][:custom_scaling_function] = %x{
387
+ function(min, max, total, value) {
388
+ return #{block.call(`min`, `max`, `total`, `value`)};
389
+ }
390
+ }
391
+ options[:nodes][:scaling][:custom_scaling_function].JS[:hyper_wrapped] = true
392
+ end
393
+ end
394
+ end
395
+ end
217
396
  end
218
397
  end
219
398
  end
@@ -0,0 +1,7 @@
1
+ module Vis
2
+ class Railtie < Rails::Railtie
3
+ initializer "vis_asset_paths" do
4
+ Rails.configuration.assets.paths.prepend Pathname.new(__dir__).join('source')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,338 @@
1
+ module Vis
2
+ class Timeline
3
+ include Native
4
+ include Vis::Utilities
5
+
6
+ aliases_native %i[
7
+ addCustomTime
8
+ destroy
9
+ getCurrentTime
10
+ getCustomTime
11
+ getSelection
12
+ getVisibleItems
13
+ redraw
14
+ removeCustomTime
15
+ setCurrentTime
16
+ setCustomTime
17
+ setCustomTimeTitle
18
+ toggleRollingMode
19
+ ]
20
+
21
+ def initialize(native_container, dataset, group_dataset = nil, options = {})
22
+ if group_dataset.is_a?(Hash)
23
+ options = group_dataset
24
+ group_dataset = nil
25
+ end
26
+ native_options = options_to_native(options)
27
+ native_data = dataset.to_n
28
+ @event_handlers = {}
29
+ if group_dataset.nil?
30
+ @native = `new vis.Timeline(native_container, native_data, native_options)`
31
+ else
32
+ native_group_data = group_dataset.to_n
33
+ @native = `new vis.Timeline(native_container, native_data, native_group_data, native_options)`
34
+ end
35
+ end
36
+
37
+ def off(event, event_handler_id)
38
+ event = lower_camelize(event)
39
+ handler = @event_handlers[event][event_handler_id]
40
+ `self["native"].off(event, handler)`
41
+ @event_handlers[event].delete(event_handler_id)
42
+ end
43
+
44
+ EVENTS_NO_COVERSION = %i[groupDragged]
45
+ EVENTS_NO_PARAM = %i[currentTimeTick changed]
46
+
47
+ def on(event, &block)
48
+ event = lower_camelize(event)
49
+ @event_handlers[event] = {} unless @event_handlers[event]
50
+ event_handler_id = `Math.random().toString(36).substring(6)`
51
+ handler = if EVENTS_NO_COVERSION.include?(event)
52
+ `function(param) { #{block.call(`param`)}; }`
53
+ elsif EVENTS_NO_PARAM.include?(event)
54
+ `function() { #{block.call}; }`
55
+ else
56
+ `function(event_info) { #{block.call(`Opal.Hash.$new(event_info)`)}; }`
57
+ end
58
+ @event_handlers[event][event_handler_id] = handler
59
+ `self["native"].on(event, handler);`
60
+ event_handler_id
61
+ end
62
+
63
+ def set_data(dataset_hash)
64
+ native_data_hash = {}
65
+ native_data_hash[:groups] = dataset_hash[:groups].to_n if dataset_hash.has_key?(:groups)
66
+ native_data_hash[:items] = dataset_hash[:items].to_n if dataset_hash.has_key?(:items)
67
+ @native.JS.setData(native_data_hash.to_n)
68
+ end
69
+
70
+ def set_groups(dataset)
71
+ @native.JS.setGroups(dataset.to_n)
72
+ end
73
+
74
+ def set_items(dataset)
75
+ @native.JS.setItems(dataset.to_n)
76
+ end
77
+
78
+ def set_options(options)
79
+ @native.JS.setOptions(options_to_native(options))
80
+ end
81
+
82
+ def fit(options = {})
83
+ @native.JS.fit(options_to_native(options))
84
+ end
85
+
86
+ def focus(node_id, options = {})
87
+ @native.JS.focus(node_id, options_to_native(options))
88
+ end
89
+
90
+ def get_event_properties(event)
91
+ res = @native.JS.getEventProperties(event.to_n)
92
+ `Opal.Hash.$new(res)`
93
+ end
94
+
95
+ def get_item_range
96
+ res = @native.JS.getItemRange()
97
+ `Opal.Hash.$new(res)`
98
+ end
99
+
100
+ def get_window
101
+ res = @native.JS.getWindow()
102
+ `Opal.Hash.$new(res)`
103
+ end
104
+
105
+ def move_to(time, options = {})
106
+ @native.JS.moveTo(time, options_to_native(options))
107
+ end
108
+
109
+ def set_selection(id_or_ids, options = {})
110
+ @native.JS.setSelection(id_or_ids, options_to_native(options))
111
+ end
112
+
113
+ def set_window(start_time, end_time, options = {}, &block)
114
+ native_options = options_to_native(options)
115
+ if block_given?
116
+ callback = %x{
117
+ function() {
118
+ return block.$call();
119
+ }
120
+ }
121
+ @native.JS.setWindow(start_time, end_time, native_options, callback)
122
+ else
123
+ @native.JS.setWindow(start_time, end_time, native_options)
124
+ end
125
+ end
126
+
127
+ def zoom_in(percentage, options = {}, &block)
128
+ native_options = options_to_native(options)
129
+ if block_given?
130
+ callback = %x{
131
+ function() {
132
+ return block.$call();
133
+ }
134
+ }
135
+ @native.JS.zoomIn(percentage, native_options, callback)
136
+ else
137
+ @native.JS.zoomIn(percentage, native_options)
138
+ end
139
+ end
140
+
141
+ def zoom_out(percentage, options = {}, &block)
142
+ native_options = options_to_native(options)
143
+ if block_given?
144
+ callback = %x{
145
+ function() {
146
+ return block.$call();
147
+ }
148
+ }
149
+ @native.JS.zoomOut(percentage, native_options, callback)
150
+ else
151
+ @native.JS.zoomOut(percentage, native_options)
152
+ end
153
+ end
154
+
155
+ def options_to_native(options)
156
+ return unless options
157
+ # options must be duplicated, so callbacks dont get wrapped twice
158
+ new_opts = {}.merge!(options)
159
+
160
+ if new_opts.has_key?(:configure)
161
+ block = new_opts[:configure]
162
+ if `typeof block === "function"`
163
+ unless new_opts[:configure].JS[:hyper_wrapped]
164
+ new_opts[:configure] = %x{
165
+ function(option, path) {
166
+ return #{block.call(`option`, `path`)};
167
+ }
168
+ }
169
+ new_opts[:configure].JS[:hyper_wrapped] = true
170
+ end
171
+ end
172
+ end
173
+
174
+ if new_opts.has_key?(:format)
175
+ block = new_opts[:format]
176
+ if `typeof block === "function"`
177
+ unless new_opts[:format].JS[:hyper_wrapped]
178
+ # this is not clear in the vis docs
179
+ new_opts[:format] = %x{
180
+ function(object) {
181
+ return #{block.call(`Opal.Hash.$new(object)`)};
182
+ }
183
+ }
184
+ new_opts[:format].JS[:hyper_wrapped] = true
185
+ end
186
+ end
187
+ end
188
+
189
+ if new_opts.has_key?(:group_order)
190
+ block = new_opts[:group_order]
191
+ if `typeof block === "function"`
192
+ unless new_opts[:group_order].JS[:hyper_wrapped]
193
+ # this is not clear in the vis docs
194
+ new_opts[:group_order] = %x{
195
+ function() {
196
+ return #{block.call};
197
+ }
198
+ }
199
+ new_opts[:group_order].JS[:hyper_wrapped] = true
200
+ end
201
+ end
202
+ end
203
+
204
+ if new_opts.has_key?(:group_order_swap)
205
+ block = new_opts[:group_order_swap]
206
+ if `typeof block === "function"`
207
+ unless new_opts[:group_order_swap].JS[:hyper_wrapped]
208
+ # this is not clear in the vis docs
209
+ new_opts[:group_order_swap] = %x{
210
+ function(from_group, to_group, groups) {
211
+ return #{block.call(`Opal.Hash.$new(from_group)`, `Opal.Hash.$new(to_group)`, Vis::DataSet.wrap(groups))};
212
+ }
213
+ }
214
+ new_opts[:group_order_swap].JS[:hyper_wrapped] = true
215
+ end
216
+ end
217
+ end
218
+
219
+ if new_opts.has_key?(:group_template)
220
+ block = new_opts[:group_template]
221
+ if `typeof block === "function"`
222
+ unless new_opts[:group_template].JS[:hyper_wrapped]
223
+ # this is not clear in the vis docs
224
+ new_opts[:group_template] = %x{
225
+ function(groups, group_element) {
226
+ return #{block.call(Vis::DataSet.wrap(groups), `Opal.Hash.$new(group_element)`)};
227
+ }
228
+ }
229
+ new_opts[:group_template].JS[:hyper_wrapped] = true
230
+ end
231
+ end
232
+ end
233
+
234
+ if new_opts.has_key?(:moment)
235
+ block = new_opts[:moment]
236
+ if `typeof block === "function"`
237
+ unless new_opts[:moment].JS[:hyper_wrapped]
238
+ new_opts[:moment] = %x{
239
+ function(native_date) {
240
+ return #{block.call(`native_date`)};
241
+ }
242
+ }
243
+ new_opts[:moment].JS[:hyper_wrapped] = true
244
+ end
245
+ end
246
+ end
247
+
248
+ %i[on_add on_add_group on_move on_move_group on_moving on_remove on_remove_group on_update].each do |key|
249
+ if new_opts.has_key?(key)
250
+ block = new_opts[key]
251
+ if `typeof block === "function"`
252
+ unless new_opts[key].JS[:hyper_wrapped]
253
+ new_opts[key] = %x{
254
+ function(item, callback) {
255
+ var wrapped_callback = #{ proc { |new_node_data| `callback(new_item.$to_n());` }};
256
+ block.$call(Opal.Hash.$new(item), wrapped_callback);
257
+ }
258
+ }
259
+ new_opts[key].JS[:hyper_wrapped] = true
260
+ end
261
+ end
262
+ end
263
+ end
264
+
265
+ if new_opts.has_key?(:on_drop_object_on_item)
266
+ block = new_opts[on_drop_object_on_item]
267
+ if `typeof block === "function"`
268
+ unless new_opts[on_drop_object_on_item].JS[:hyper_wrapped]
269
+ new_opts[on_drop_object_on_item] = %x{
270
+ function(object, item) {
271
+ block.$call(Opal.Hash.$new(object), Opal.Hash.$new(item));
272
+ }
273
+ }
274
+ new_opts[on_drop_object_on_item].JS[:hyper_wrapped] = true
275
+ end
276
+ end
277
+ end
278
+
279
+ if new_opts.has_key?(:on_initial_draw_complete)
280
+ block = new_opts[:on_initial_draw_complete]
281
+ if `typeof block === "function"`
282
+ unless new_opts[:on_initial_draw_complete].JS[:hyper_wrapped]
283
+ new_opts[:on_initial_draw_complete] = %x{
284
+ function() { block.$call(); }
285
+ }
286
+ new_opts[:on_initial_draw_complete].JS[:hyper_wrapped] = true
287
+ end
288
+ end
289
+ end
290
+
291
+ if new_opts.has_key?(:snap)
292
+ block = new_opts[:snap]
293
+ if `typeof block === "function"`
294
+ unless new_opts[:snap].JS[:hyper_wrapped]
295
+ new_opts[:snap] = %x{
296
+ function(date, scale, step) {
297
+ return block.$call(date, scale, step);
298
+ }
299
+ }
300
+ new_opts[:snap].JS[:hyper_wrapped] = true
301
+ end
302
+ end
303
+ end
304
+
305
+ if new_opts.has_key?(:template)
306
+ block = new_opts[:template]
307
+ if `typeof block === "function"`
308
+ unless new_opts[:template].JS[:hyper_wrapped]
309
+ # not clear in vis docs
310
+ new_opts[:template] = %x{
311
+ function(item, element, edited_data) {
312
+ return block.$call(Opal.Hash.$new(item), element, Opal.Hash.$new(edited_data));
313
+ }
314
+ }
315
+ new_opts[:template].JS[:hyper_wrapped] = true
316
+ end
317
+ end
318
+ end
319
+
320
+ if new_opts.has_key?(:visible_frame_template)
321
+ block = new_opts[:visible_frame_template]
322
+ if `typeof block === "function"`
323
+ unless new_opts[:visible_frame_template].JS[:hyper_wrapped]
324
+ # not clear in vis docs
325
+ new_opts[:visible_frame_template] = %x{
326
+ function(item, element) {
327
+ return block.$call(Opal.Hash.$new(item), element);
328
+ }
329
+ }
330
+ new_opts[:visible_frame_template].JS[:hyper_wrapped] = true
331
+ end
332
+ end
333
+ end
334
+
335
+ lower_camelize_hash(new_opts).to_n
336
+ end
337
+ end
338
+ end