opal-browser 0.1.0.beta1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/build.yml +95 -0
  3. data/.gitignore +3 -0
  4. data/.yardopts +1 -1
  5. data/Gemfile +22 -3
  6. data/LICENSE +20 -0
  7. data/README.md +200 -20
  8. data/Rakefile +29 -1
  9. data/config.ru +20 -2
  10. data/docs/polyfills.md +24 -0
  11. data/examples/2048/Gemfile +6 -0
  12. data/examples/2048/README.md +13 -0
  13. data/examples/2048/app/application.rb +169 -0
  14. data/examples/2048/config.ru +9 -0
  15. data/examples/canvas/Gemfile +6 -0
  16. data/examples/canvas/README.md +9 -0
  17. data/examples/canvas/app/application.rb +55 -0
  18. data/examples/canvas/config.ru +9 -0
  19. data/examples/component/Gemfile +6 -0
  20. data/examples/component/README.md +10 -0
  21. data/examples/component/app/application.rb +66 -0
  22. data/examples/component/config.ru +9 -0
  23. data/examples/integrations/README.md +24 -0
  24. data/examples/integrations/dynamic-rack-opal-sprockets-server/Gemfile +6 -0
  25. data/examples/integrations/dynamic-rack-opal-sprockets-server/README.md +16 -0
  26. data/examples/integrations/dynamic-rack-opal-sprockets-server/app/application.rb +6 -0
  27. data/examples/integrations/dynamic-rack-opal-sprockets-server/config.ru +9 -0
  28. data/examples/integrations/dynamic-roda-roda-sprockets/.gitignore +1 -0
  29. data/examples/integrations/dynamic-roda-roda-sprockets/Gemfile +7 -0
  30. data/examples/integrations/dynamic-roda-roda-sprockets/README.md +22 -0
  31. data/examples/integrations/dynamic-roda-roda-sprockets/Rakefile +4 -0
  32. data/examples/integrations/dynamic-roda-roda-sprockets/app/application.rb +6 -0
  33. data/examples/integrations/dynamic-roda-roda-sprockets/app.rb +32 -0
  34. data/examples/integrations/dynamic-roda-roda-sprockets/config.ru +3 -0
  35. data/examples/integrations/dynamic-roda-tilt/.gitignore +1 -0
  36. data/examples/integrations/dynamic-roda-tilt/Gemfile +8 -0
  37. data/examples/integrations/dynamic-roda-tilt/README.md +17 -0
  38. data/examples/integrations/dynamic-roda-tilt/Rakefile +6 -0
  39. data/examples/integrations/dynamic-roda-tilt/app/application.rb +6 -0
  40. data/examples/integrations/dynamic-roda-tilt/app.rb +50 -0
  41. data/examples/integrations/dynamic-roda-tilt/config.ru +3 -0
  42. data/examples/integrations/dynamic-sinatra-opal-sprockets-server/Gemfile +7 -0
  43. data/examples/integrations/dynamic-sinatra-opal-sprockets-server/README.md +16 -0
  44. data/examples/integrations/dynamic-sinatra-opal-sprockets-server/app/application.rb +6 -0
  45. data/examples/integrations/dynamic-sinatra-opal-sprockets-server/config.ru +29 -0
  46. data/examples/integrations/static-bash/.gitignore +2 -0
  47. data/examples/integrations/static-bash/Gemfile +3 -0
  48. data/examples/integrations/static-bash/README.md +8 -0
  49. data/examples/integrations/static-bash/app/application.rb +6 -0
  50. data/examples/integrations/static-bash/build.sh +4 -0
  51. data/examples/integrations/static-bash/index.html +10 -0
  52. data/examples/integrations/static-bash-opal-parser/.gitignore +3 -0
  53. data/examples/integrations/static-bash-opal-parser/Gemfile +3 -0
  54. data/examples/integrations/static-bash-opal-parser/README.md +10 -0
  55. data/examples/integrations/static-bash-opal-parser/build.sh +4 -0
  56. data/examples/integrations/static-bash-opal-parser/index.html +19 -0
  57. data/examples/integrations/static-rake/.gitignore +1 -0
  58. data/examples/integrations/static-rake/Gemfile +4 -0
  59. data/examples/integrations/static-rake/README.md +7 -0
  60. data/examples/integrations/static-rake/Rakefile +10 -0
  61. data/examples/integrations/static-rake/app/application.rb +6 -0
  62. data/examples/integrations/static-rake/index.html +9 -0
  63. data/examples/integrations/static-rake-guard/.gitignore +1 -0
  64. data/examples/integrations/static-rake-guard/Gemfile +6 -0
  65. data/examples/integrations/static-rake-guard/Guardfile +3 -0
  66. data/examples/integrations/static-rake-guard/README.md +10 -0
  67. data/examples/integrations/static-rake-guard/Rakefile +10 -0
  68. data/examples/integrations/static-rake-guard/app/application.rb +6 -0
  69. data/examples/integrations/static-rake-guard/index.html +9 -0
  70. data/examples/svg/.gitignore +1 -0
  71. data/examples/svg/Gemfile +4 -0
  72. data/examples/svg/README.md +7 -0
  73. data/examples/svg/Rakefile +10 -0
  74. data/examples/svg/app/application.rb +11 -0
  75. data/examples/svg/index.html +17 -0
  76. data/examples/svg/index.svg +6 -0
  77. data/index.html.erb +24 -0
  78. data/lib/opal-browser.rb +1 -0
  79. data/opal/browser/animation_frame.rb +92 -10
  80. data/opal/browser/audio/node.rb +121 -0
  81. data/opal/browser/audio/param_schedule.rb +43 -0
  82. data/opal/browser/audio.rb +66 -0
  83. data/opal/browser/blob.rb +94 -0
  84. data/opal/browser/canvas/data.rb +2 -12
  85. data/opal/browser/canvas/gradient.rb +1 -11
  86. data/opal/browser/canvas/style.rb +3 -11
  87. data/opal/browser/canvas/text.rb +1 -11
  88. data/opal/browser/canvas.rb +86 -28
  89. data/opal/browser/console.rb +6 -38
  90. data/opal/browser/cookies.rb +90 -27
  91. data/opal/browser/crypto.rb +79 -0
  92. data/opal/browser/css/declaration.rb +1 -6
  93. data/opal/browser/css/rule.rb +1 -1
  94. data/opal/browser/css/style_sheet.rb +2 -2
  95. data/opal/browser/css.rb +23 -7
  96. data/opal/browser/database/sql.rb +193 -0
  97. data/opal/browser/delay.rb +94 -0
  98. data/opal/browser/dom/attribute.rb +16 -9
  99. data/opal/browser/dom/builder.rb +35 -25
  100. data/opal/browser/dom/character_data.rb +43 -7
  101. data/opal/browser/dom/document.rb +171 -37
  102. data/opal/browser/dom/document_fragment.rb +18 -0
  103. data/opal/browser/dom/document_or_shadow_root.rb +19 -0
  104. data/opal/browser/dom/element/attributes.rb +111 -0
  105. data/opal/browser/dom/element/button.rb +31 -0
  106. data/opal/browser/dom/element/custom.rb +177 -0
  107. data/opal/browser/dom/element/data.rb +82 -0
  108. data/opal/browser/dom/element/editable.rb +47 -0
  109. data/opal/browser/dom/element/form.rb +38 -0
  110. data/opal/browser/dom/element/iframe.rb +37 -0
  111. data/opal/browser/dom/element/image.rb +25 -0
  112. data/opal/browser/dom/element/input.rb +48 -1
  113. data/opal/browser/dom/element/media.rb +17 -0
  114. data/opal/browser/dom/element/offset.rb +32 -10
  115. data/opal/browser/dom/element/position.rb +11 -2
  116. data/opal/browser/dom/element/scroll.rb +139 -20
  117. data/opal/browser/dom/element/select.rb +42 -0
  118. data/opal/browser/dom/element/size.rb +46 -0
  119. data/opal/browser/dom/element/template.rb +11 -0
  120. data/opal/browser/dom/element/textarea.rb +26 -0
  121. data/opal/browser/dom/element.rb +496 -168
  122. data/opal/browser/dom/mutation_observer.rb +69 -9
  123. data/opal/browser/dom/node.rb +270 -83
  124. data/opal/browser/dom/node_set.rb +74 -41
  125. data/opal/browser/dom/shadow_root.rb +12 -0
  126. data/opal/browser/dom/text.rb +18 -3
  127. data/opal/browser/dom.rb +40 -18
  128. data/opal/browser/effects.rb +180 -3
  129. data/opal/browser/event/all.rb +26 -0
  130. data/opal/browser/event/animation.rb +40 -0
  131. data/opal/browser/{dom/event → event}/audio_processing.rb +10 -6
  132. data/opal/browser/event/base.rb +461 -0
  133. data/opal/browser/event/before_unload.rb +17 -0
  134. data/opal/browser/event/clipboard.rb +37 -0
  135. data/opal/browser/event/close.rb +49 -0
  136. data/opal/browser/event/composition.rb +52 -0
  137. data/opal/browser/event/custom.rb +65 -0
  138. data/opal/browser/event/data_transfer.rb +95 -0
  139. data/opal/browser/event/device_light.rb +25 -0
  140. data/opal/browser/{dom/event → event}/device_motion.rb +21 -6
  141. data/opal/browser/event/device_orientation.rb +50 -0
  142. data/opal/browser/{dom/event → event}/device_proximity.rb +10 -6
  143. data/opal/browser/event/drag.rb +123 -0
  144. data/opal/browser/event/focus.rb +41 -0
  145. data/opal/browser/event/gamepad.rb +62 -0
  146. data/opal/browser/{dom/event → event}/hash_change.rb +10 -6
  147. data/opal/browser/event/keyboard.rb +128 -0
  148. data/opal/browser/event/message.rb +72 -0
  149. data/opal/browser/{dom/event → event}/mouse.rb +37 -32
  150. data/opal/browser/event/page_transition.rb +25 -0
  151. data/opal/browser/event/pop_state.rb +35 -0
  152. data/opal/browser/event/progress.rb +45 -0
  153. data/opal/browser/event/sensor.rb +17 -0
  154. data/opal/browser/{dom/event → event}/storage.rb +10 -6
  155. data/opal/browser/{dom/event → event}/touch.rb +14 -21
  156. data/opal/browser/event/ui.rb +38 -0
  157. data/opal/browser/{dom/event → event}/wheel.rb +6 -4
  158. data/opal/browser/event.rb +163 -0
  159. data/opal/browser/event_source.rb +7 -4
  160. data/opal/browser/form_data.rb +225 -0
  161. data/opal/browser/history.rb +53 -21
  162. data/opal/browser/http/binary.rb +1 -0
  163. data/opal/browser/http/headers.rb +21 -2
  164. data/opal/browser/http/request.rb +83 -55
  165. data/opal/browser/http/response.rb +5 -1
  166. data/opal/browser/http.rb +47 -9
  167. data/opal/browser/immediate.rb +128 -10
  168. data/opal/browser/interval.rb +41 -23
  169. data/opal/browser/location.rb +20 -4
  170. data/opal/browser/navigator.rb +136 -13
  171. data/opal/browser/polyfill/visual_viewport.rb +216 -0
  172. data/opal/browser/screen.rb +34 -8
  173. data/opal/browser/setup/base.rb +6 -0
  174. data/opal/browser/setup/full.rb +13 -0
  175. data/opal/browser/setup/large.rb +17 -0
  176. data/opal/browser/setup/mini.rb +8 -0
  177. data/opal/browser/setup/traditional.rb +10 -0
  178. data/opal/browser/socket.rb +16 -8
  179. data/opal/browser/storage.rb +155 -52
  180. data/opal/browser/support.rb +299 -0
  181. data/opal/browser/utils.rb +116 -18
  182. data/opal/browser/version.rb +1 -1
  183. data/opal/browser/visual_viewport.rb +39 -0
  184. data/opal/browser/window/size.rb +47 -9
  185. data/opal/browser/window/view.rb +37 -4
  186. data/opal/browser/window.rb +46 -26
  187. data/opal/browser.rb +1 -10
  188. data/opal/opal-browser.rb +1 -0
  189. data/opal-browser.gemspec +10 -12
  190. data/spec/database/sql_spec.rb +139 -0
  191. data/spec/delay_spec.rb +41 -0
  192. data/spec/dom/attribute_spec.rb +49 -0
  193. data/spec/dom/builder_spec.rb +36 -19
  194. data/spec/dom/document_spec.rb +28 -6
  195. data/spec/dom/element/attributes_spec.rb +52 -0
  196. data/spec/dom/element/custom_spec.rb +106 -0
  197. data/spec/dom/element/subclass_spec.rb +144 -0
  198. data/spec/dom/element_spec.rb +184 -7
  199. data/spec/dom/mutation_observer_spec.rb +13 -9
  200. data/spec/dom/node_set_spec.rb +44 -0
  201. data/spec/dom/node_spec.rb +87 -27
  202. data/spec/dom_spec.rb +19 -9
  203. data/spec/event_source_spec.rb +18 -15
  204. data/spec/{dom/event_spec.rb → event_spec.rb} +55 -26
  205. data/spec/history_spec.rb +32 -19
  206. data/spec/http_spec.rb +25 -36
  207. data/spec/immediate_spec.rb +10 -7
  208. data/spec/interval_spec.rb +59 -0
  209. data/spec/native_cached_wrapper_spec.rb +46 -0
  210. data/spec/runner.rb +107 -0
  211. data/spec/socket_spec.rb +18 -14
  212. data/spec/spec_helper.rb +2 -4
  213. data/spec/spec_helper_promise.rb.erb +25 -0
  214. data/spec/storage_spec.rb +7 -7
  215. data/spec/wgxpath.install.js +49 -0
  216. data/spec/window_spec.rb +2 -2
  217. metadata +181 -93
  218. data/opal/browser/compatibility/animation_frame.rb +0 -93
  219. data/opal/browser/compatibility/dom/document/window.rb +0 -15
  220. data/opal/browser/compatibility/dom/element/css.rb +0 -15
  221. data/opal/browser/compatibility/dom/element/matches.rb +0 -31
  222. data/opal/browser/compatibility/dom/element/offset.rb +0 -20
  223. data/opal/browser/compatibility/dom/element/scroll.rb +0 -25
  224. data/opal/browser/compatibility/dom/element/style.rb +0 -15
  225. data/opal/browser/compatibility/dom/mutation_observer.rb +0 -47
  226. data/opal/browser/compatibility/http/request.rb +0 -15
  227. data/opal/browser/compatibility/immediate.rb +0 -107
  228. data/opal/browser/compatibility/window/scroll.rb +0 -27
  229. data/opal/browser/compatibility/window/size.rb +0 -13
  230. data/opal/browser/compatibility/window/view.rb +0 -13
  231. data/opal/browser/compatibility.rb +0 -59
  232. data/opal/browser/dom/compatibility.rb +0 -8
  233. data/opal/browser/dom/event/animation.rb +0 -26
  234. data/opal/browser/dom/event/base.rb +0 -207
  235. data/opal/browser/dom/event/before_unload.rb +0 -13
  236. data/opal/browser/dom/event/clipboard.rb +0 -26
  237. data/opal/browser/dom/event/close.rb +0 -35
  238. data/opal/browser/dom/event/composition.rb +0 -38
  239. data/opal/browser/dom/event/custom.rb +0 -30
  240. data/opal/browser/dom/event/device_light.rb +0 -21
  241. data/opal/browser/dom/event/device_orientation.rb +0 -36
  242. data/opal/browser/dom/event/drag.rb +0 -113
  243. data/opal/browser/dom/event/focus.rb +0 -23
  244. data/opal/browser/dom/event/gamepad.rb +0 -47
  245. data/opal/browser/dom/event/keyboard.rb +0 -93
  246. data/opal/browser/dom/event/message.rb +0 -50
  247. data/opal/browser/dom/event/page_transition.rb +0 -21
  248. data/opal/browser/dom/event/pop_state.rb +0 -21
  249. data/opal/browser/dom/event/progress.rb +0 -31
  250. data/opal/browser/dom/event/sensor.rb +0 -13
  251. data/opal/browser/dom/event/ui.rb +0 -22
  252. data/opal/browser/dom/event.rb +0 -240
  253. data/opal/browser/http/compatibility.rb +0 -1
  254. data/opal/browser/http/parameters.rb +0 -8
  255. data/opal/browser/timeout.rb +0 -60
  256. data/opal/browser/window/compatibility.rb +0 -3
  257. data/opal/browser/window/scroll.rb +0 -49
@@ -0,0 +1,95 @@
1
+ require 'browser/blob'
2
+
3
+ module Browser; class Event
4
+
5
+ # {DataTransfer} is an object which manages included data to
6
+ # an event of type {Event::Drag} or {Event::Clipboard}.
7
+ class DataTransfer
8
+ include NativeCachedWrapper
9
+
10
+ # @!attribute [rw] effect
11
+ # @return [Symbol] Effect of this drop operation.
12
+ # Must be one of: :none, :copy, :link or :move.
13
+ def effect
14
+ `#@native.dropEffect`
15
+ end
16
+
17
+ def effect= (effect)
18
+ `#@native.dropEffect = #{effect}`
19
+ end
20
+
21
+ # Extract some text data from this {DataTransfer} instance.
22
+ def [] (type)
23
+ `#@native.getData(#{type})`
24
+ end
25
+
26
+ # Embed some text data in this {DataTransfer} instance.
27
+ def []= (type, value)
28
+ `#@native.setData(#{type}, #{Native.convert(value)})`
29
+ end
30
+
31
+ # Clear some (or all, if type is not specified) text data from
32
+ # this {DataTransfer} instance.
33
+ def clear (type=nil)
34
+ `#@native.clearData(#{type.to_n})`
35
+ end
36
+
37
+ # @!attribute [w] drag_image
38
+ # Sets a drag image for this {DataTransfer}. Use some
39
+ # {DOM::Element::Image} or {Canvas} as a value.
40
+ def drag_image= (image)
41
+ `#@native.setDragImage(#{Native.convert(image)})`
42
+ end
43
+
44
+ # @!attribute [r] files
45
+ # @return [Array<File>] list of files attached to this {DataTransfer}
46
+ def files
47
+ Native::Array.new(`#@native.files`).map { |f| File.new(f.to_n) }
48
+ end
49
+
50
+ # @!attribute [r] items
51
+ # @return [Array<Item>] list of items attached to this {DataTransfer}
52
+ def items
53
+ Native::Array.new(`#@native.items`).map { |i| Item.new(i.to_n) }
54
+ end
55
+
56
+ # An instance of `DataTransferItem`
57
+ class Item
58
+ include NativeCachedWrapper
59
+
60
+ # @!attribute [r] kind
61
+ # @return [Symbol] kind of an item: :string or :file
62
+ def kind
63
+ `#@native.kind`
64
+ end
65
+
66
+ def string?; kind == 'string'; end
67
+ def file?; kind == 'file' && to_file; end # Some files can't be resolved...
68
+
69
+ # @!attribute [r] type
70
+ # @return [String] mime type of an item
71
+ def type
72
+ `#@native.type`
73
+ end
74
+
75
+ # Convert to string and call back once ready, or return a
76
+ # promise if a block isn't given.
77
+ def to_string(&block)
78
+ promise = nil
79
+ if !block
80
+ promise = Promise.new
81
+ block = proc { |i| promise.resolve(i) }
82
+ end
83
+ `#@native.getAsString(#{block.to_n})`
84
+ return promise
85
+ end
86
+
87
+ # Convert to file or return nil if impossible
88
+ def to_file
89
+ as_file = `#@native.getAsFile()`
90
+ File.new(as_file) if as_file
91
+ end
92
+ end
93
+ end
94
+
95
+ end; end
@@ -0,0 +1,25 @@
1
+ module Browser; class Event
2
+
3
+ class DeviceLight < Event
4
+ handles 'devicelight'
5
+
6
+ def self.supported?
7
+ Browser.supports? 'Event.DeviceLight'
8
+ end
9
+
10
+ class Definition < Definition
11
+ def value=(value)
12
+ `#@native.value = #{value}`
13
+ end
14
+ end
15
+
16
+ if Browser.supports? 'Event.constructor'
17
+ def self.construct(name, desc)
18
+ `new DeviceLightEvent(#{name}, #{desc})`
19
+ end
20
+ end if supported?
21
+
22
+ alias_native :value
23
+ end
24
+
25
+ end; end
@@ -1,8 +1,10 @@
1
- module Browser; module DOM; class Event
1
+ module Browser; class Event
2
2
 
3
3
  class DeviceMotion < Event
4
+ handles 'devicemotion'
5
+
4
6
  def self.supported?
5
- not $$[:DeviceMotionEvent].nil?
7
+ Browser.supports? 'Event.DeviceMotion'
6
8
  end
7
9
 
8
10
  Acceleration = Struct.new(:x, :y, :z)
@@ -25,9 +27,22 @@ class DeviceMotion < Event
25
27
  end
26
28
  end
27
29
 
28
- def self.create(name, &block)
29
- new(`new DeviceMotionEvent(#{name}, #{Definition.new(&block)})`)
30
- end
30
+ if Browser.supports? 'Event.constructor'
31
+ def self.construct(name, desc)
32
+ `new DeviceMotionEvent(#{name}, #{desc})`
33
+ end
34
+ elsif Browser.supports? 'Event.create'
35
+ def self.construct(name, desc)
36
+ %x{
37
+ var event = document.createEvent("DeviceMotionEvent");
38
+ event.initDeviceMotionEvent(name, desc.bubbles, desc.cancelable,
39
+ desc.acceleration, desc.accelerationIncludingGravity,
40
+ desc.rotationRate, desc.interval);
41
+
42
+ return event;
43
+ }
44
+ end
45
+ end if supported?
31
46
 
32
47
  alias_native :acceleration
33
48
  alias_native :acceleration_with_gravity, :accelerationIncludingGravity
@@ -35,4 +50,4 @@ class DeviceMotion < Event
35
50
  alias_native :interval
36
51
  end
37
52
 
38
- end; end; end
53
+ end; end
@@ -0,0 +1,50 @@
1
+ module Browser; class Event
2
+
3
+ class DeviceOrientation < Event
4
+ handles 'deviceorientation'
5
+
6
+ def self.supported?
7
+ Browser.supports? 'Event.DeviceOrientation'
8
+ end
9
+
10
+ class Definition < Definition
11
+ def absolute=(value)
12
+ `#@native.absolute = #{value}`
13
+ end
14
+
15
+ def alpha=(value)
16
+ `#@native.alpha = #{value}`
17
+ end
18
+
19
+ def beta=(value)
20
+ `#@native.beta = #{value}`
21
+ end
22
+
23
+ def gamma=(value)
24
+ `#@native.gamma = #{value}`
25
+ end
26
+ end
27
+
28
+ if Browser.supports? 'Event.constructor'
29
+ def self.construct(name, desc)
30
+ `new DeviceOrientationEvent(#{name}, #{desc})`
31
+ end
32
+ elsif Browser.supports? 'Event.create'
33
+ def self.construct(name, desc)
34
+ %x{
35
+ var event = document.createEvent("DeviceOrientationEvent");
36
+ event.initDeviceOrientationEvent(name, desc.bubbles, desc.cancelable,
37
+ desc.alpha, desc.beta, desc.gamma, desc.absolute);
38
+
39
+ return event;
40
+ }
41
+ end
42
+ end if supported?
43
+
44
+ alias_native :absolute
45
+ alias_native :alpha
46
+ alias_native :beta
47
+ alias_native :gamma
48
+ end
49
+
50
+ end; end
@@ -1,8 +1,10 @@
1
- module Browser; module DOM; class Event
1
+ module Browser; class Event
2
2
 
3
3
  class DeviceProximity < Event
4
+ handles 'deviceproximity'
5
+
4
6
  def self.supported?
5
- not $$[:DeviceProximityEvent].nil?
7
+ Browser.supports? 'Event.DeviceProximity'
6
8
  end
7
9
 
8
10
  class Definition < Definition
@@ -19,13 +21,15 @@ class DeviceProximity < Event
19
21
  end
20
22
  end
21
23
 
22
- def self.create(name, &block)
23
- new(`new DeviceProximityEvent(#{name}, #{Definition.new(&block)})`)
24
- end
24
+ if Browser.supports? 'Event.constructor'
25
+ def self.construct(name, desc)
26
+ `new DeviceProximityEvent(#{name}, #{desc})`
27
+ end
28
+ end if supported?
25
29
 
26
30
  alias_native :value
27
31
  alias_native :min
28
32
  alias_native :max
29
33
  end
30
34
 
31
- end; end; end
35
+ end; end
@@ -0,0 +1,123 @@
1
+ module Browser; class Event
2
+
3
+ class Drag < Event
4
+ handles 'drag', 'dragend', 'dragenter', 'dragexit', 'dragleave', 'dragover',
5
+ 'dragstart', 'drop'
6
+
7
+ def self.supported?
8
+ Browser.supports? 'Event.Drag'
9
+ end
10
+
11
+ class Definition < Definition
12
+ class Client
13
+ include Native::Wrapper
14
+
15
+ def x=(value)
16
+ `#@native.clientX = #{value}`
17
+ end
18
+
19
+ def y=(value)
20
+ `#@native.clientY = #{value}`
21
+ end
22
+ end
23
+
24
+ class Screen
25
+ include Native::Wrapper
26
+
27
+ def x=(value)
28
+ `#@native.screenX = #{value}`
29
+ end
30
+
31
+ def y=(value)
32
+ `#@native.screenY = #{value}`
33
+ end
34
+ end
35
+
36
+ def alt!
37
+ `#@native.altKey = true`
38
+ end
39
+
40
+ def ctrl!
41
+ `#@native.ctrlKey = true`
42
+ end
43
+
44
+ def meta!
45
+ `#@native.metaKey = true`
46
+ end
47
+
48
+ def button=(value)
49
+ `#@native.button = #{value}`
50
+ end
51
+
52
+ def client
53
+ Client.new(@native)
54
+ end
55
+
56
+ def screen
57
+ Screen.new(@native)
58
+ end
59
+
60
+ def related=(elem)
61
+ `#@native.relatedTarget = #{Native.convert(elem)}`
62
+ end
63
+
64
+ def transfer=(value)
65
+ `#@native.dataTransfer = #{Native.convert(value)}`
66
+ end
67
+ end
68
+
69
+ if Browser.supports? 'Event.constructor'
70
+ def self.construct(name, desc)
71
+ `new DragEvent(#{name}, #{desc})`
72
+ end
73
+ elsif Browser.supports? 'Event.create'
74
+ def self.construct(name, desc)
75
+ %x{
76
+ var event = document.createEvent("DragEvent");
77
+ event.initDragEvent(name, desc.bubbles, desc.cancelable,
78
+ desc.view || window, 0,
79
+ desc.screenX || 0, desc.screenY || 0,
80
+ desc.clientX || 0, desc.clientY || 0,
81
+ desc.ctrlKey, desc.altKey, desc.shiftKey, desc.metaKey,
82
+ desc.button || 0, desc.relatedTarget, desc.dataTransfer);
83
+
84
+ return event;
85
+ }
86
+ end
87
+ end if supported?
88
+
89
+ alias_native :alt?, :altKey
90
+ alias_native :ctrl?, :ctrlKey
91
+ alias_native :meta?, :metaKey
92
+ alias_native :shift?, :shiftKey
93
+ alias_native :button
94
+
95
+ def client
96
+ Position.new(`#@native.clientX`, `#@native.clientY`)
97
+ end
98
+
99
+ def screen
100
+ Position.new(`#@native.screenX`, `#@native.screenY`) if defined?(`#@native.screenX`)
101
+ end
102
+
103
+ def x
104
+ screen.x
105
+ end
106
+
107
+ def y
108
+ screen.y
109
+ end
110
+
111
+ def related
112
+ DOM(`#@native.relatedTarget`)
113
+ end
114
+
115
+ # Returns a {DataTransfer} related to this event
116
+ #
117
+ # @see https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
118
+ def transfer
119
+ DataTransfer.new(`#@native.dataTransfer`)
120
+ end
121
+ end
122
+
123
+ end; end
@@ -0,0 +1,41 @@
1
+ module Browser; class Event
2
+
3
+ class Focus < UI
4
+ handles 'blur', 'focus', 'focusin', 'focusout'
5
+
6
+ def self.supported?
7
+ Browser.supports? 'Event.Focus'
8
+ end
9
+
10
+ class Definition < UI::Definition
11
+ def view=(value)
12
+ `#@native.view = #{Native.convert(value)}`
13
+ end
14
+
15
+ def related=(elem)
16
+ `#@native.relatedTarget = #{Native.convert(elem)}`
17
+ end
18
+ end
19
+
20
+ if Browser.supports? 'Event.constructor'
21
+ def self.construct(name, desc)
22
+ `new FocusEvent(#{name}, #{desc})`
23
+ end
24
+ elsif Browser.supports? 'Event.create'
25
+ def self.construct(name, desc)
26
+ %x{
27
+ var event = document.createEvent("FocusEvent");
28
+ event.initFocusEvent(name, desc.bubbles, desc.cancelable,
29
+ desc.view || window, 0, desc.relatedTarget);
30
+
31
+ return event;
32
+ }
33
+ end
34
+ end if supported?
35
+
36
+ def related
37
+ DOM(`#@native.relatedTarget`)
38
+ end
39
+ end
40
+
41
+ end; end
@@ -0,0 +1,62 @@
1
+ module Browser; class Event
2
+
3
+ class Gamepad < Event
4
+ handles 'gamepadconnected', 'gamepaddisconnected'
5
+
6
+ def self.supported?
7
+ Browser.supports? 'Event.Gamepad'
8
+ end
9
+
10
+ class Definition < Definition
11
+ def id=(value)
12
+ `#@native.id = #{value}`
13
+ end
14
+
15
+ def index=(value)
16
+ `#@native.index = #{value}`
17
+ end
18
+
19
+ def at=(value)
20
+ `#@native.timestamp = #{value}`
21
+ end
22
+
23
+ def axes=(value)
24
+ `#@native.axes = #{value}`
25
+ end
26
+
27
+ def buttons=(value)
28
+ `#@native.buttons = #{value}`
29
+ end
30
+ end
31
+
32
+ if Browser.supports? 'Event.constructor'
33
+ def self.construct(name, desc)
34
+ `return new GamepadEvent(#{name}, {
35
+ bubbles: desc.bubbles,
36
+ cancelable: desc.cancelable,
37
+ gamepad: desc })`
38
+ end
39
+ end if supported?
40
+
41
+ def id
42
+ `#@native.gamepad.id`
43
+ end
44
+
45
+ def index
46
+ `#@native.gamepad.index`
47
+ end
48
+
49
+ def at
50
+ `#@native.gamepad.timestamp`
51
+ end
52
+
53
+ def axes
54
+ `#@native.gamepad.axes`
55
+ end
56
+
57
+ def buttons
58
+ `#@native.gamepad.buttons`
59
+ end
60
+ end
61
+
62
+ end; end
@@ -1,8 +1,10 @@
1
- module Browser; module DOM; class Event
1
+ module Browser; class Event
2
2
 
3
3
  class HashChange < Event
4
+ handles 'hashchange'
5
+
4
6
  def self.supported?
5
- not $$[:HashChangeEvent].nil?
7
+ Browser.supports? 'Event.HashChange'
6
8
  end
7
9
 
8
10
  class Definition < Definition
@@ -15,12 +17,14 @@ class HashChange < Event
15
17
  end
16
18
  end
17
19
 
18
- def self.create(name, &block)
19
- new(`new HashChangeEvent(#{name}, #{Definition.new(&block)})`)
20
- end
20
+ if Browser.supports? 'Event.constructor'
21
+ def self.construct(name, desc)
22
+ `new HashChangeEvent(#{name}, #{desc})`
23
+ end
24
+ end if supported?
21
25
 
22
26
  alias_native :old, :oldURL
23
27
  alias_native :new, :newURL
24
28
  end
25
29
 
26
- end; end; end
30
+ end; end
@@ -0,0 +1,128 @@
1
+ module Browser; class Event
2
+
3
+ class Keyboard < UI
4
+ handles 'keydown', 'keypress', 'keyup'
5
+
6
+ def self.supported?
7
+ Browser.supports? 'Event.Keyboard'
8
+ end
9
+
10
+ class Definition < UI::Definition
11
+ def alt!
12
+ `#@native.altKey = true`
13
+ end
14
+
15
+ def ctrl!
16
+ `#@native.ctrlKey = true`
17
+ end
18
+
19
+ def meta!
20
+ `#@native.metaKey = true`
21
+ end
22
+
23
+ def shift!
24
+ `#@native.shiftKey = true`
25
+ end
26
+
27
+ def code=(code)
28
+ `#@native.keyCode = #@native.which = #{code}`
29
+ end
30
+
31
+ def key=(key)
32
+ `#@native.key = #{key}`
33
+ end
34
+
35
+ def char=(char)
36
+ `#@native.char = #@native.charCode = #{char}`
37
+ end
38
+
39
+ def repeat!
40
+ `#@native.repeat = true`
41
+ end
42
+
43
+ def locale=(value)
44
+ `#@native.locale = value`
45
+ end
46
+ end
47
+
48
+ if Browser.supports? 'Event.constructor'
49
+ def self.construct(name, desc)
50
+ `new KeyboardEvent(#{name}, #{desc})`
51
+ end
52
+ elsif Browser.supports? 'Event.create'
53
+ def self.construct(name, desc)
54
+ %x{
55
+ var modifiers = "";
56
+
57
+ if (desc.altKey) {
58
+ modifiers += "Alt ";
59
+ }
60
+
61
+ if (desc.ctrlKey) {
62
+ modifiers += "Ctrl ";
63
+ }
64
+
65
+ if (desc.shiftKey) {
66
+ modifiers += "Shift" ;
67
+ }
68
+
69
+ if (desc.metaKey) {
70
+ modifiers += "Meta ";
71
+ }
72
+
73
+ var event = document.createEvent("KeyboardEvent");
74
+ event.initKeyboardEvent(name, desc.bubbles, desc.cancelable,
75
+ desc.view || window, desc.which, 0,
76
+ modifiers, desc.repeat, desc.locale);
77
+
78
+ return event;
79
+ }
80
+ end
81
+ end if supported?
82
+
83
+ alias_native :alt?, :altKey
84
+ alias_native :ctrl?, :ctrlKey
85
+ alias_native :meta?, :metaKey
86
+ alias_native :shift?, :shiftKey
87
+ alias_native :locale
88
+ alias_native :repeat?, :repeat
89
+
90
+ # IE 11 at least has different names for those keys.
91
+ IE_MAP = {
92
+ 'Down' => 'ArrowDown',
93
+ 'Up' => 'ArrowUp',
94
+ 'Left' => 'ArrowLeft',
95
+ 'Right' => 'ArrowRight',
96
+ 'Esc' => 'Escape',
97
+ 'Del' => 'Delete'
98
+ }
99
+
100
+ def key
101
+ key = `#@native.key || #@native.keyIdentifier || nil`
102
+ IE_MAP[key] || key
103
+ end
104
+
105
+ def code
106
+ `#@native.keyCode || #@native.which || nil`
107
+ end
108
+
109
+ def char
110
+ `#@native.char || #@native.charCode || #{code ? code.chr : nil}`
111
+ end
112
+
113
+ alias to_i key
114
+
115
+ def down?
116
+ name.downcase == 'keydown'
117
+ end
118
+
119
+ def press?
120
+ name.downcase == 'keypress'
121
+ end
122
+
123
+ def up?
124
+ name.downcase == 'keyup'
125
+ end
126
+ end
127
+
128
+ end; end
@@ -0,0 +1,72 @@
1
+ require 'buffer'
2
+
3
+ module Browser; class Event
4
+
5
+ class Message < Event
6
+ handles 'message'
7
+
8
+ def self.supported?
9
+ Browser.supports? 'Event.Message'
10
+ end
11
+
12
+ class Definition < Definition
13
+ def data=(value)
14
+ `#@native.data = value`
15
+ end
16
+
17
+ def origin=(value)
18
+ `#@native.origin = value`
19
+ end
20
+
21
+ def source=(value)
22
+ `#@native.source = #{Native.convert(value)}`
23
+ end
24
+ end
25
+
26
+ if Browser.supports? 'Event.constructor'
27
+ def self.construct(name, desc)
28
+ `new MessageEvent(#{name}, #{desc})`
29
+ end
30
+ elsif Browser.supports? 'Event.create'
31
+ def self.construct(name, desc)
32
+ %x{
33
+ var event = document.createEvent("MessageEvent");
34
+ event.initMessageEvent(name, desc.bubbles, desc.cancelable,
35
+ desc.data, desc.origin, "", desc.source || window);
36
+
37
+ return event;
38
+ }
39
+ end
40
+ end if supported?
41
+
42
+ def data
43
+ %x{
44
+ if (window.ArrayBuffer && #@native.data instanceof ArrayBuffer) {
45
+ return #{Buffer.new(`#@native.data`)};
46
+ }
47
+ else if (window.Blob && #@native.data instanceof Blob) {
48
+ return #{Blob.new(`#@native.data`)};
49
+ }
50
+ else {
51
+ return #@native.data;
52
+ }
53
+ }
54
+ end
55
+
56
+ alias_native :origin
57
+
58
+ def source
59
+ %x{
60
+ var source = #@native.source;
61
+
62
+ if (window.Window && source instanceof window.Window) {
63
+ return #{Window.new(`source`)};
64
+ }
65
+ else {
66
+ return nil;
67
+ }
68
+ }
69
+ end
70
+ end
71
+
72
+ end; end