hyper-d3 1.0.0.lap23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +313 -0
  5. data/README.md +118 -0
  6. data/Rakefile +31 -0
  7. data/hyper-d3.gemspec +27 -0
  8. data/lib/d3.rb +68 -0
  9. data/lib/d3/arc.rb +29 -0
  10. data/lib/d3/area.rb +53 -0
  11. data/lib/d3/axis.rb +79 -0
  12. data/lib/d3/band_scale.rb +30 -0
  13. data/lib/d3/collections.rb +9 -0
  14. data/lib/d3/color.rb +76 -0
  15. data/lib/d3/color_schemes.rb +34 -0
  16. data/lib/d3/continuous_scale.rb +60 -0
  17. data/lib/d3/creator.rb +11 -0
  18. data/lib/d3/curve.rb +74 -0
  19. data/lib/d3/dsv.rb +103 -0
  20. data/lib/d3/ease.rb +319 -0
  21. data/lib/d3/format.rb +95 -0
  22. data/lib/d3/histograms.rb +46 -0
  23. data/lib/d3/interpolate.rb +125 -0
  24. data/lib/d3/line.rb +27 -0
  25. data/lib/d3/map.rb +48 -0
  26. data/lib/d3/misc.rb +15 -0
  27. data/lib/d3/native.rb +112 -0
  28. data/lib/d3/nest.rb +100 -0
  29. data/lib/d3/ordinal_scale.rb +39 -0
  30. data/lib/d3/path.rb +24 -0
  31. data/lib/d3/pie.rb +25 -0
  32. data/lib/d3/point_scale.rb +27 -0
  33. data/lib/d3/polygon.rb +18 -0
  34. data/lib/d3/quadtree.rb +89 -0
  35. data/lib/d3/quantile_scale.rb +19 -0
  36. data/lib/d3/quantize_scale.rb +20 -0
  37. data/lib/d3/radial_area.rb +55 -0
  38. data/lib/d3/radial_line.rb +27 -0
  39. data/lib/d3/random.rb +14 -0
  40. data/lib/d3/search.rb +30 -0
  41. data/lib/d3/selection.rb +151 -0
  42. data/lib/d3/sequential_scale.rb +31 -0
  43. data/lib/d3/set.rb +30 -0
  44. data/lib/d3/source/d3-drag.js +234 -0
  45. data/lib/d3/source/d3-scale-chromatic.js +484 -0
  46. data/lib/d3/source/d3.js +17178 -0
  47. data/lib/d3/stack.rb +13 -0
  48. data/lib/d3/statistics.rb +81 -0
  49. data/lib/d3/symbol.rb +70 -0
  50. data/lib/d3/threshold_scale.rb +22 -0
  51. data/lib/d3/time_format.rb +47 -0
  52. data/lib/d3/time_interval.rb +85 -0
  53. data/lib/d3/transformations.rb +16 -0
  54. data/lib/hyper-d3.rb +17 -0
  55. data/lib/hyperloop/d3/component.rb +12 -0
  56. data/lib/hyperloop/d3/mixin.rb +59 -0
  57. data/lib/hyperloop/d3/version.rb +5 -0
  58. data/spec/arc_spec.rb +80 -0
  59. data/spec/area_spec.rb +98 -0
  60. data/spec/axis_spec.rb +174 -0
  61. data/spec/band_scale_spec.rb +73 -0
  62. data/spec/color_spec.rb +74 -0
  63. data/spec/continuous_scale_spec.rb +217 -0
  64. data/spec/coverage_spec.rb +16 -0
  65. data/spec/creator_spec.rb +15 -0
  66. data/spec/curve_spec.rb +206 -0
  67. data/spec/dsv_spec.rb +194 -0
  68. data/spec/ease_spec.rb +370 -0
  69. data/spec/format_spec.rb +87 -0
  70. data/spec/histograms_spec.rb +61 -0
  71. data/spec/html/index.html.erb +11 -0
  72. data/spec/interpolate_spec.rb +152 -0
  73. data/spec/line_spec.rb +54 -0
  74. data/spec/map_spec.rb +80 -0
  75. data/spec/misc_spec.rb +21 -0
  76. data/spec/nest_spec.rb +89 -0
  77. data/spec/objects_spec.rb +22 -0
  78. data/spec/ordinal_scale_spec.rb +59 -0
  79. data/spec/path_spec.rb +65 -0
  80. data/spec/pie_spec.rb +114 -0
  81. data/spec/point_scale_spec.rb +58 -0
  82. data/spec/polygon_spec.rb +51 -0
  83. data/spec/quadtree_spec.rb +128 -0
  84. data/spec/quantile_scale_spec.rb +24 -0
  85. data/spec/quantize_scale_spec.rb +40 -0
  86. data/spec/radial_area_spec.rb +123 -0
  87. data/spec/radial_line_spec.rb +50 -0
  88. data/spec/random_spec.rb +34 -0
  89. data/spec/search_spec.rb +69 -0
  90. data/spec/selection_data_spec.rb +64 -0
  91. data/spec/selection_manipulation_spec.rb +166 -0
  92. data/spec/selection_spec.rb +187 -0
  93. data/spec/sequential_scale_spec.rb +90 -0
  94. data/spec/set_spec.rb +57 -0
  95. data/spec/spec_helper.rb +1 -0
  96. data/spec/stack_spec.rb +5 -0
  97. data/spec/statistics_spec.rb +65 -0
  98. data/spec/symbol_spec.rb +116 -0
  99. data/spec/test_app/.gitignore +23 -0
  100. data/spec/test_app/Gemfile +7 -0
  101. data/spec/test_app/Gemfile.lock +206 -0
  102. data/spec/test_app/README.md +9 -0
  103. data/spec/test_app/Rakefile +6 -0
  104. data/spec/test_app/app/assets/config/manifest.js +3 -0
  105. data/spec/test_app/app/assets/images/.keep +0 -0
  106. data/spec/test_app/app/assets/javascripts/application.js +13 -0
  107. data/spec/test_app/app/assets/javascripts/cable.js +13 -0
  108. data/spec/test_app/app/assets/javascripts/channels/.keep +0 -0
  109. data/spec/test_app/app/assets/javascripts/data/elections_2016.rb +10 -0
  110. data/spec/test_app/app/assets/javascripts/data/harry_potter.rb +19 -0
  111. data/spec/test_app/app/assets/javascripts/data/iphones.rb +29 -0
  112. data/spec/test_app/app/assets/javascripts/data/london_population.rb +26 -0
  113. data/spec/test_app/app/assets/javascripts/data/man_vs_horse.rb +55 -0
  114. data/spec/test_app/app/assets/javascripts/data/mtg_modern_colors.rb +96 -0
  115. data/spec/test_app/app/assets/javascripts/data/mtg_modern_creatures.rb +116 -0
  116. data/spec/test_app/app/assets/javascripts/data/olympics_2016_medals.rb +100 -0
  117. data/spec/test_app/app/assets/javascripts/data/paradox.rb +60 -0
  118. data/spec/test_app/app/assets/javascripts/data/polish_pms.rb +28 -0
  119. data/spec/test_app/app/assets/javascripts/data/star_trek_voyager.rb +183 -0
  120. data/spec/test_app/app/assets/javascripts/data/weather_in_london.rb +381 -0
  121. data/spec/test_app/app/assets/javascripts/elections_2016.rb +27 -0
  122. data/spec/test_app/app/assets/javascripts/harry_potter.rb +42 -0
  123. data/spec/test_app/app/assets/javascripts/iphones.rb +54 -0
  124. data/spec/test_app/app/assets/javascripts/london_population.rb +53 -0
  125. data/spec/test_app/app/assets/javascripts/london_population_area.rb +49 -0
  126. data/spec/test_app/app/assets/javascripts/man_vs_horse.rb +60 -0
  127. data/spec/test_app/app/assets/javascripts/mtg_modern_colors.rb +56 -0
  128. data/spec/test_app/app/assets/javascripts/mtg_modern_creatures.rb +70 -0
  129. data/spec/test_app/app/assets/javascripts/olympics_2016_medals.rb +62 -0
  130. data/spec/test_app/app/assets/javascripts/paradox.rb +64 -0
  131. data/spec/test_app/app/assets/javascripts/polish_pms.rb +60 -0
  132. data/spec/test_app/app/assets/javascripts/star_trek_voyager.rb +44 -0
  133. data/spec/test_app/app/assets/javascripts/weather_in_london.rb +69 -0
  134. data/spec/test_app/app/assets/stylesheets/application.css +15 -0
  135. data/spec/test_app/app/channels/application_cable/channel.rb +4 -0
  136. data/spec/test_app/app/channels/application_cable/connection.rb +4 -0
  137. data/spec/test_app/app/controllers/application_controller.rb +27 -0
  138. data/spec/test_app/app/controllers/concerns/.keep +0 -0
  139. data/spec/test_app/app/helpers/application_helper.rb +2 -0
  140. data/spec/test_app/app/jobs/application_job.rb +2 -0
  141. data/spec/test_app/app/mailers/application_mailer.rb +4 -0
  142. data/spec/test_app/app/models/application_record.rb +3 -0
  143. data/spec/test_app/app/models/concerns/.keep +0 -0
  144. data/spec/test_app/app/views/application/index.erb +18 -0
  145. data/spec/test_app/app/views/application/visualization.erb +28 -0
  146. data/spec/test_app/app/views/layouts/application.html.erb +13 -0
  147. data/spec/test_app/bin/bundle +3 -0
  148. data/spec/test_app/bin/rails +4 -0
  149. data/spec/test_app/bin/rake +4 -0
  150. data/spec/test_app/bin/setup +38 -0
  151. data/spec/test_app/bin/update +29 -0
  152. data/spec/test_app/bin/yarn +11 -0
  153. data/spec/test_app/config.ru +5 -0
  154. data/spec/test_app/config/application.rb +18 -0
  155. data/spec/test_app/config/boot.rb +3 -0
  156. data/spec/test_app/config/cable.yml +10 -0
  157. data/spec/test_app/config/database.yml +25 -0
  158. data/spec/test_app/config/environment.rb +5 -0
  159. data/spec/test_app/config/environments/development.rb +54 -0
  160. data/spec/test_app/config/environments/production.rb +91 -0
  161. data/spec/test_app/config/environments/test.rb +42 -0
  162. data/spec/test_app/config/initializers/application_controller_renderer.rb +8 -0
  163. data/spec/test_app/config/initializers/assets.rb +14 -0
  164. data/spec/test_app/config/initializers/backtrace_silencers.rb +7 -0
  165. data/spec/test_app/config/initializers/cookies_serializer.rb +5 -0
  166. data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
  167. data/spec/test_app/config/initializers/inflections.rb +16 -0
  168. data/spec/test_app/config/initializers/mime_types.rb +4 -0
  169. data/spec/test_app/config/initializers/wrap_parameters.rb +14 -0
  170. data/spec/test_app/config/locales/en.yml +33 -0
  171. data/spec/test_app/config/puma.rb +56 -0
  172. data/spec/test_app/config/routes.rb +5 -0
  173. data/spec/test_app/config/secrets.yml +32 -0
  174. data/spec/test_app/config/spring.rb +6 -0
  175. data/spec/test_app/db/seeds.rb +7 -0
  176. data/spec/test_app/lib/assets/.keep +0 -0
  177. data/spec/test_app/lib/tasks/.keep +0 -0
  178. data/spec/test_app/log/.keep +0 -0
  179. data/spec/test_app/package.json +5 -0
  180. data/spec/test_app/public/404.html +67 -0
  181. data/spec/test_app/public/422.html +67 -0
  182. data/spec/test_app/public/500.html +66 -0
  183. data/spec/test_app/public/apple-touch-icon-precomposed.png +0 -0
  184. data/spec/test_app/public/apple-touch-icon.png +0 -0
  185. data/spec/test_app/public/favicon.ico +0 -0
  186. data/spec/test_app/public/robots.txt +1 -0
  187. data/spec/test_app/test/application_system_test_case.rb +5 -0
  188. data/spec/test_app/test/controllers/.keep +0 -0
  189. data/spec/test_app/test/fixtures/.keep +0 -0
  190. data/spec/test_app/test/fixtures/files/.keep +0 -0
  191. data/spec/test_app/test/helpers/.keep +0 -0
  192. data/spec/test_app/test/integration/.keep +0 -0
  193. data/spec/test_app/test/mailers/.keep +0 -0
  194. data/spec/test_app/test/models/.keep +0 -0
  195. data/spec/test_app/test/system/.keep +0 -0
  196. data/spec/test_app/test/test_helper.rb +10 -0
  197. data/spec/test_app/tmp/.keep +0 -0
  198. data/spec/test_app/vendor/.keep +0 -0
  199. data/spec/threshold_scale_spec.rb +28 -0
  200. data/spec/time_format_spec.rb +100 -0
  201. data/spec/time_interval_spec.rb +314 -0
  202. data/spec/transformations_spec.rb +51 -0
  203. data/startrekvoyager.png +0 -0
  204. metadata +529 -0
data/lib/d3/format.rb ADDED
@@ -0,0 +1,95 @@
1
+ module D3
2
+ class FormatSpecifier
3
+ include D3::Native
4
+
5
+ FIELDS = %W[fill align sign symbol zero width comma precision type]
6
+ FIELDS.each do |key|
7
+ define_method(key) {
8
+ `v = #@native[key]`
9
+ `v === undefined ? nil : v`
10
+ }
11
+ define_method("#{key}=") {|v|
12
+ `#@native[key] = (v === nil ? undefined : v)`
13
+ }
14
+ end
15
+
16
+ def to_h
17
+ result = {}
18
+ FIELDS.each do |key|
19
+ result[key] = send(key)
20
+ end
21
+ result
22
+ end
23
+
24
+ def to_n
25
+ @native
26
+ end
27
+ end
28
+
29
+ class FormatLocale
30
+ include D3::Native
31
+
32
+ def format(specifier)
33
+ if specifier.is_a?(FormatSpecifier)
34
+ @native.JS.format(specifier.to_n)
35
+ else
36
+ @native.JS.format(specifier)
37
+ end
38
+ end
39
+
40
+ def format_prefix(specifier, value)
41
+ if specifier.is_a?(FormatSpecifier)
42
+ @native.JS.formatPrefix(specifier.to_n, value)
43
+ else
44
+ @native.JS.formatPrefix(specifier, value)
45
+ end
46
+ end
47
+ end
48
+
49
+
50
+ class << self
51
+ aliases_d3 %i[precisionFixed precisionPrefix precisionRound]
52
+
53
+ def format(specifier)
54
+ if specifier.is_a?(FormatSpecifier)
55
+ @d3.JS.format(specifier.to_n)
56
+ else
57
+ @d3.JS.format(specifier)
58
+ end
59
+ end
60
+
61
+ def format_prefix(specifier, value)
62
+ if specifier.is_a?(FormatSpecifier)
63
+ @d3.JS.formatPrefix(specifier.to_n, value)
64
+ else
65
+ @d3.JS.formatPrefix(specifier, value)
66
+ end
67
+ end
68
+
69
+ def format_specifier(specifier)
70
+ D3::FormatSpecifier.new @d3.JS.formatSpecifier(specifier)
71
+ end
72
+
73
+ def format_locale(spec={})
74
+ D3::FormatLocale.new @d3.JS.formatLocale(
75
+ {
76
+ decimal: spec.fetch("decimal", "."),
77
+ thousands: spec.fetch("thousands", ","),
78
+ grouping: spec.fetch("grouping", [3]),
79
+ currency: spec.fetch("currency", ["$", ""]),
80
+ }.to_n
81
+ )
82
+ end
83
+
84
+ def format_default_locale(spec={})
85
+ D3::FormatLocale.new @d3.JS.formatDefaultLocale(
86
+ {
87
+ decimal: spec.fetch("decimal", "."),
88
+ thousands: spec.fetch("thousands", ","),
89
+ grouping: spec.fetch("grouping", [3]),
90
+ currency: spec.fetch("currency", ["$", ""]),
91
+ }.to_n
92
+ )
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,46 @@
1
+ module D3
2
+ class Histogram
3
+ include D3::Native
4
+
5
+ def call(data)
6
+ @native.call(data)
7
+ end
8
+
9
+ attributes_d3 %i[value domain]
10
+
11
+ def thresholds(count=nil, &block)
12
+ if block_given?
13
+ @native.JS.thresholds(block)
14
+ elsif count.is_a?(Numeric)
15
+ @native.JS.thresholds(count)
16
+ elsif count == :scott
17
+ @native.JS.thresholds{|*args| D3.threshold_scott(*args)}
18
+ elsif count == :sturges
19
+ @native.JS.thresholds{|*args| D3.threshold_sturges(*args)}
20
+ elsif count == :freedman_diaconis
21
+ @native.JS.thresholds{|*args| D3.threshold_freedman_diaconis(*args)}
22
+ else
23
+ raise ArgumentError, "Wrong use of D3::Histogram.thresholds API - pass block, number, or symbol"
24
+ end
25
+ self
26
+ end
27
+ end
28
+
29
+ class << self
30
+ def histogram
31
+ D3::Histogram.new @d3.JS.histogram
32
+ end
33
+
34
+ def threshold_freedman_diaconis(*args)
35
+ @d3.JS.thresholdFreedmanDiaconis(*args)
36
+ end
37
+
38
+ def threshold_scott(*args)
39
+ @d3.JS.thresholdScott(*args)
40
+ end
41
+
42
+ def threshold_sturges(*args)
43
+ @d3.JS.thresholdSturges(*args)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,125 @@
1
+ module D3
2
+ class << self
3
+ def interpolate_number(a=nil,b=nil)
4
+ if a == nil and b == nil
5
+ `#@d3.interpolateNumber`
6
+ else
7
+ @d3.JS.interpolateNumber(a,b)
8
+ end
9
+ end
10
+
11
+ def interpolate_round(a=nil,b=nil)
12
+ if a == nil and b == nil
13
+ `#@d3.interpolateRound`
14
+ else
15
+ @d3.JS.interpolateRound(a,b)
16
+ end
17
+ end
18
+
19
+ def interpolate_string(a=nil,b=nil)
20
+ if a == nil and b == nil
21
+ `#@d3.interpolateString`
22
+ else
23
+ @d3.JS.interpolateString(a,b)
24
+ end
25
+ end
26
+
27
+ def interpolate_array(a=nil,b=nil)
28
+ if a == nil and b == nil
29
+ `#@d3.interpolateArray`
30
+ else
31
+ @d3.JS.interpolateArray(a,b)
32
+ end
33
+ end
34
+
35
+ def interpolate_date(a=nil,b=nil)
36
+ if a == nil and b == nil
37
+ `#@d3.interpolateDate`
38
+ else
39
+ @d3.JS.interpolateDate(a,b)
40
+ end
41
+ end
42
+
43
+ # These should support gamma
44
+ def interpolate_rgb(a=nil,b=nil)
45
+ if a == nil and b == nil
46
+ `#@d3.interpolateRgb`
47
+ else
48
+ @d3.JS.interpolateRgb(a,b)
49
+ end
50
+ end
51
+
52
+ def interpolate_rgb_basis(a=nil,b=nil)
53
+ if a == nil and b == nil
54
+ `#@d3.interpolateRgbBasis`
55
+ else
56
+ @d3.JS.interpolateRgbBasis(a,b)
57
+ end
58
+ end
59
+
60
+ def interpolate_rgb_basis_closed(a=nil,b=nil)
61
+ if a == nil and b == nil
62
+ `#@d3.interpolateRgbBasisClosed`
63
+ else
64
+ @d3.JS.interpolateRgbBasisClosed(a,b)
65
+ end
66
+ end
67
+
68
+ def interpolate_hsl(a=nil,b=nil)
69
+ if a == nil and b == nil
70
+ `#@d3.interpolateHsl`
71
+ else
72
+ @d3.JS.interpolateHsl(a,b)
73
+ end
74
+ end
75
+
76
+ def interpolate_hsl_long(a=nil,b=nil)
77
+ if a == nil and b == nil
78
+ `#@d3.interpolateHslLong`
79
+ else
80
+ @d3.JS.interpolateHslLong(a,b)
81
+ end
82
+ end
83
+
84
+ def interpolate_lab(a=nil,b=nil)
85
+ if a == nil and b == nil
86
+ `#@d3.interpolateLab`
87
+ else
88
+ @d3.JS.interpolateLab(a,b)
89
+ end
90
+ end
91
+
92
+ def interpolate_hcl(a=nil,b=nil)
93
+ if a == nil and b == nil
94
+ `#@d3.interpolateHcl`
95
+ else
96
+ @d3.JS.interpolateHcl(a,b)
97
+ end
98
+ end
99
+
100
+ def interpolate_hcl_long(a=nil,b=nil)
101
+ if a == nil and b == nil
102
+ `#@d3.interpolateHclLong`
103
+ else
104
+ @d3.JS.interpolateHclLong(a,b)
105
+ end
106
+ end
107
+
108
+ def interpolate_cubehelix(a=nil,b=nil)
109
+ if a == nil and b == nil
110
+ `#@d3.interpolateCubehelix`
111
+ else
112
+ @d3.JS.interpolateCubehelix(a,b)
113
+ end
114
+ end
115
+
116
+ def interpolate_cubehelix_long(a=nil,b=nil)
117
+ if a == nil and b == nil
118
+ `#@d3.interpolateCubehelixLong`
119
+ else
120
+ @d3.JS.interpolateCubehelixLong(a,b)
121
+ end
122
+ end
123
+
124
+ end
125
+ end
data/lib/d3/line.rb ADDED
@@ -0,0 +1,27 @@
1
+ module D3
2
+ class LineGenerator
3
+ include D3::Native
4
+
5
+ def call(*args)
6
+ result = @native.call(*args)
7
+ `result === null ? nil : result`
8
+ end
9
+
10
+ attributes_d3_block %i[x y defined]
11
+
12
+ def curve(new_value=`undefined`)
13
+ if `new_value === undefined`
14
+ D3::Curve.new @native.JS.curve
15
+ else
16
+ @native.JS.curve(new_value.to_n)
17
+ self
18
+ end
19
+ end
20
+ end
21
+
22
+ class << self
23
+ def line
24
+ D3::LineGenerator.new @d3.JS.line
25
+ end
26
+ end
27
+ end
data/lib/d3/map.rb ADDED
@@ -0,0 +1,48 @@
1
+ module D3
2
+ class Map
3
+ include D3::Native
4
+
5
+ def get(key)
6
+ result = @native.JS.get(key)
7
+ `result === undefined ? nil : result`
8
+ end
9
+ include Native
10
+ aliases_native_chainable %i[set remove clear]
11
+ aliases_native %i[get keys values size]
12
+ alias_native :empty?, :empty
13
+ alias_native :has?, :has
14
+ alias_method :[], :get
15
+ alias_method :[]=, :set
16
+
17
+ def entries
18
+ @native.JS.entries().map{|o| [`o.key`, `o.value`]}
19
+ end
20
+
21
+ def to_h
22
+ `Opal.hash(#{entries})`
23
+ end
24
+
25
+ # Should that be reversed to better match ruby and to work with Enumerable?
26
+ # Or just use #to_h if you want ruby interface?
27
+ def each(&block)
28
+ @native.JS.each(block)
29
+ self
30
+ end
31
+
32
+ def inspect
33
+ "#<D3::Map: #{ to_h.inspect }>"
34
+ end
35
+ end
36
+
37
+ class << self
38
+ def map(object=nil, &block)
39
+ if block_given?
40
+ D3::Map.new(@d3.JS.map(object, proc{|x| yield(x)}))
41
+ elsif object
42
+ D3::Map.new(@d3.JS.map(object))
43
+ else
44
+ D3::Map.new(@d3.JS.map())
45
+ end
46
+ end
47
+ end
48
+ end
data/lib/d3/misc.rb ADDED
@@ -0,0 +1,15 @@
1
+ module D3
2
+ class << self
3
+ def version
4
+ `window.d3.version`
5
+ end
6
+
7
+ def namespaces
8
+ `Opal.hash(window.d3.namespaces)`
9
+ end
10
+
11
+ def namespace(name)
12
+ `Opal.hash(window.d3.namespace(name))`
13
+ end
14
+ end
15
+ end
data/lib/d3/native.rb ADDED
@@ -0,0 +1,112 @@
1
+ require "native"
2
+
3
+ module Native
4
+ module Helpers
5
+ def aliases_native(native_names)
6
+ native_names.each do |native_name|
7
+ alias_native(native_name.underscore, native_name)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ module D3
14
+ module Native
15
+ include ::Native
16
+ def initialize(native)
17
+ raise unless native
18
+ @native = native
19
+ end
20
+
21
+ def self.included(klass)
22
+ klass.extend ::Native::Helpers
23
+ klass.extend Helpers
24
+ end
25
+
26
+ module Helpers
27
+ # This provides ruby style interfaces:
28
+ # obj.foo
29
+ # obj.foo = 1; obj.bar = 2
30
+ # obj.foo(1).bar(2)
31
+
32
+ def attributes_d3(native_names)
33
+ native_names.each do |native_name|
34
+ attribute_d3(native_name.underscore, native_name)
35
+ end
36
+ end
37
+
38
+ def attribute_d3(ruby_name, js_name=ruby_name)
39
+ define_method(ruby_name) do |new_value=`undefined`|
40
+ if `new_value !== undefined`
41
+ new_value = `null` if new_value == nil
42
+ `self["native"][#{js_name}](#{new_value})`
43
+ self
44
+ else
45
+ value = `self["native"][#{js_name}]()`
46
+ `value === null ? nil : value`
47
+ end
48
+ end
49
+ define_method(ruby_name + '=') do |new_value|
50
+ new_value = `null` if new_value == nil
51
+ @native.JS.call(js_name, new_value)
52
+ end
53
+ end
54
+
55
+ def attributes_d3_block(native_names)
56
+ native_names.each do |native_name|
57
+ attribute_d3_block(native_name.underscore, native_name)
58
+ end
59
+ end
60
+
61
+ # This provides ruby style and jquery style interfaces,
62
+ # and also block interface:
63
+ # obj.foo
64
+ # obj.foo = 1; obj.bar = 2
65
+ # obj.foo(1).bar(2).buzz{...}
66
+ def attribute_d3_block(ruby_name, js_name=ruby_name)
67
+ define_method(ruby_name) do |new_value=`undefined`, &block|
68
+ if block_given?
69
+ @native.JS[js_name].JS.apply(@native, `Opal.to_a(block)`)
70
+ self
71
+ elsif `new_value !== undefined`
72
+ new_value = `null` if new_value == nil
73
+ `self["native"][#{js_name}](#{new_value})`
74
+ self
75
+ else
76
+ value = `self["native"][#{js_name}]()`
77
+ `value === null ? nil : value`
78
+ end
79
+ end
80
+ define_method(ruby_name + '=') do |new_value|
81
+ new_value = `null` if new_value == nil
82
+ @native.JS.call(js_name, new_value)
83
+ end
84
+ end
85
+
86
+ def aliases_native_chainable(native_names)
87
+ native_names.each do |native_name|
88
+ alias_native_chainable(native_name.underscore, native_name)
89
+ end
90
+ end
91
+
92
+ def alias_native_chainable(ruby_name, js_name=ruby_name)
93
+ define_method(ruby_name) do |*args|
94
+ @native.JS[js_name].JS.apply(@native, `Opal.to_a(args)`)
95
+ self
96
+ end
97
+ end
98
+
99
+ def aliases_native_new(native_names)
100
+ native_names.each do |native_name|
101
+ alias_native_new(native_name.downcase, native_name)
102
+ end
103
+ end
104
+
105
+ def alias_native_new(ruby_name, js_name=ruby_name)
106
+ define_method(ruby_name) do |*args|
107
+ self.class.new( @native.JS[js_name].JS.apply(@native, `Opal.to_a(args)`) )
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end