Skip to content

Commit 1e9c8cf

Browse files
committedJan 25, 2014
dom/event: finish the compatibility layer
1 parent 7aa164a commit 1e9c8cf

27 files changed

+326
-261
lines changed
 

‎opal/browser/dom/event.rb

+3-11
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ def self.class_for(name)
131131
else
132132
Custom
133133
end
134-
135-
if type == Custom
136-
Custom
137-
elsif type.supported?
138-
type
139-
else
140-
Event
141-
end
142134
end
143135

144136
def self.supported?
@@ -149,7 +141,7 @@ def self.create(name, *args, &block)
149141
name = name_for(name)
150142
klass = class_for(name)
151143

152-
event = klass.new(klass.construct(name, klass.const_get(:Definition).new(&block)))
144+
event = klass.new(klass.construct(name, klass.const_get(:Definition).new(&block)))
153145
event.arguments = args
154146

155147
event
@@ -165,14 +157,14 @@ def self.construct(name, desc)
165157
var event = document.createEvent("HTMLEvents");
166158
event.initEvent(name, desc.bubbles, desc.cancelable);
167159
168-
return event;
160+
#{return Native(`event`).merge!(desc)};
169161
}
170162
end
171163
elsif Browser.supports? 'Event.createObject'
172164
def self.construct(name, desc)
173165
Native(`document.createEventObject()`) \
174-
.merge!(`{ type: name }`) \
175166
.merge!(desc) \
167+
.merge!(`{ type: name }`) \
176168
.to_n
177169
end
178170
else

‎opal/browser/dom/event/animation.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class Animation < Event
44
def self.supported?
5-
not $$[:AnimationEvent].nil?
5+
Browser.supports? 'Event.Animation'
66
end
77

88
class Definition < Definition
@@ -29,15 +29,7 @@ def self.construct(name, desc)
2929
return event;
3030
}
3131
end
32-
elsif Browser.supports? 'Event.createObject'
33-
def self.construct(name, desc)
34-
Native(`document.createEventObject()`).merge!(desc).to_n
35-
end
36-
else
37-
def self.construct(*)
38-
raise NotImplementedError
39-
end
40-
end
32+
end if supported?
4133

4234
alias_native :name, :animationName
4335
alias_native :elapsed, :elapsedTime

‎opal/browser/dom/event/audio_processing.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class AudioProcessing < Event
44
def self.supported?
5-
not $$[:AudioProcessingEvent].nil?
5+
Browser.supports? 'Event.AudioProcessing'
66
end
77

88
class Definition < Definition
@@ -19,9 +19,11 @@ def output=(value)
1919
end
2020
end
2121

22-
def self.construct(name, desc)
23-
`new AudioProcessingEvent(#{name}, #{desc})`
24-
end
22+
if Browser.supports? 'Event.constructor'
23+
def self.construct(name, desc)
24+
`new AudioProcessingEvent(#{name}, #{desc})`
25+
end
26+
end if supported?
2527

2628
alias_native :time, :playbackTime
2729
alias_native :input, :inputBuffer

‎opal/browser/dom/event/before_unload.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ module Browser; module DOM; class Event
22

33
class BeforeUnload < Event
44
def self.supported?
5-
not $$[:BeforeUnloadEvent].nil?
5+
Browser.supports? 'Event.BeforeUnload'
66
end
77

8-
def self.construct(name, desc)
9-
`new BeforeUnloadEvent(#{name}, #{desc})`
10-
end
8+
if Browser.supports? 'Event.constructor'
9+
def self.construct(name, desc)
10+
`new BeforeUnloadEvent(#{name}, #{desc})`
11+
end
12+
end if supported?
1113
end
1214

1315
end; end; end

‎opal/browser/dom/event/clipboard.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class Clipboard < Event
44
def self.supported?
5-
not $$[:ClipboardEvent].nil?
5+
Browser.supports? 'Event.Clipboard'
66
end
77

88
class Definition < Definition
@@ -15,9 +15,11 @@ def type=(value)
1515
end
1616
end
1717

18-
def self.construct(name, desc)
19-
`new ClipboardEvent(#{name}, #{desc})`
20-
end
18+
if Browser.supports? 'Event.constructor'
19+
def self.construct(name, desc)
20+
`new ClipboardEvent(#{name}, #{desc})`
21+
end
22+
end if supported?
2123

2224
alias_native :data
2325
alias_native :type, :dataType

‎opal/browser/dom/event/close.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class Close < Event
44
def self.supported?
5-
not $$[:CloseEvent].nil?
5+
Browser.supports? 'Event.Close'
66
end
77

88
class Definition < Definition
@@ -37,15 +37,7 @@ def self.construct(name, desc)
3737
return event;
3838
}
3939
end
40-
elsif Browser.supports? 'Event.createObject'
41-
def self.construct(name, desc)
42-
Native(`document.createEventObject()`).merge!(desc).to_n
43-
end
44-
else
45-
def self.construct(*)
46-
raise NotImplementedError
47-
end
48-
end
40+
end if supported?
4941

5042
alias_native :code
5143
alias_native :reason

‎opal/browser/dom/event/composition.rb

+3-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class Composition < UI
44
def self.supported?
5-
not $$[:CompositionEvent].nil?
5+
Browser.supports? 'Event.Composition'
66
end
77

88
class Definition < UI::Definition
@@ -24,21 +24,12 @@ def self.construct(name, desc)
2424
%x{
2525
var event = document.createEvent("CompositionEvent");
2626
event.initCompositionEvent(name, desc.bubbles, desc.cancelable,
27-
window, desc.data, desc.locale);
27+
desc.view || window, desc.data, desc.locale);
2828
2929
return event;
3030
}
3131
end
32-
elsif Browser.supports? 'Event.createObject'
33-
def self.construct(name, desc)
34-
Native(`document.createEventObject()`).merge!(desc).to_n
35-
end
36-
else
37-
def self.construct(*)
38-
raise NotImplementedError
39-
end
40-
end
41-
32+
end if supported?
4233

4334
alias_native :data
4435
alias_native :locale

‎opal/browser/dom/event/custom.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Browser; module DOM; class Event
44

55
class Custom < Event
66
def self.supported?
7-
not $$[:CustomEvent].nil?
7+
Browser.supports? 'Event.Custom'
88
end
99

1010
class Definition < Definition
@@ -17,7 +17,10 @@ def method_missing(name, value)
1717

1818
if Browser.supports? 'Event.constructor'
1919
def self.construct(name, desc)
20-
`new CustomEvent(name, desc)`
20+
`new CustomEvent(name, {
21+
bubbles: desc.bubbles,
22+
cancelable: desc.cancelable,
23+
detail: desc })`
2124
end
2225
elsif Browser.supports? 'Event.create'
2326
def self.construct(name, desc)
@@ -37,8 +40,12 @@ def self.construct(name, desc)
3740
detail: desc }`).to_n
3841
end
3942
else
40-
def self.construct(*)
41-
raise NotImplementedError
43+
def self.construct(name, desc)
44+
Native(desc).merge!(`{
45+
type: name,
46+
bubbles: desc.bubbles,
47+
cancelable: desc.cancelable,
48+
detail: desc }`).to_n
4249
end
4350
end
4451

‎opal/browser/dom/event/device_light.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class DeviceLight < Event
44
def self.supported?
5-
not $$[:DeviceLightEvent].nil?
5+
Browser.supports? 'Event.DeviceLight'
66
end
77

88
class Definition < Definition
@@ -11,9 +11,11 @@ def value=(value)
1111
end
1212
end
1313

14-
def self.construct(name, desc)
15-
`new DeviceLightEvent(#{name}, #{desc})`
16-
end
14+
if Browser.supports? 'Event.constructor'
15+
def self.construct(name, desc)
16+
`new DeviceLightEvent(#{name}, #{desc})`
17+
end
18+
end if supported?
1719

1820
alias_native :value
1921
end

‎opal/browser/dom/event/device_motion.rb

+2-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class DeviceMotion < Event
44
def self.supported?
5-
not $$[:DeviceMotionEvent].nil?
5+
Browser.supports? 'Event.DeviceMotion'
66
end
77

88
Acceleration = Struct.new(:x, :y, :z)
@@ -40,16 +40,7 @@ def self.construct(name, desc)
4040
return event;
4141
}
4242
end
43-
elsif Browser.supports? 'Event.createObject'
44-
def self.construct(name, desc)
45-
Native(`document.createEventObject()`).merge!(desc).to_n
46-
end
47-
else
48-
def self.construct(*)
49-
raise NotImplementedError
50-
end
51-
end
52-
43+
end if supported?
5344

5445
alias_native :acceleration
5546
alias_native :acceleration_with_gravity, :accelerationIncludingGravity

‎opal/browser/dom/event/device_orientation.rb

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class DeviceOrientation < Event
44
def self.supported?
5-
not $$[:DeviceOrientationEvent].nil?
5+
Browser.supports? 'Event.DeviceOrientation'
66
end
77

88
class Definition < Definition
@@ -23,9 +23,21 @@ def gamma=(value)
2323
end
2424
end
2525

26-
def self.construct(name, desc)
27-
`new DeviceOrientationEvent(#{name}, #{desc})`
28-
end
26+
if Browser.supports? 'Event.constructor'
27+
def self.construct(name, desc)
28+
`new DeviceOrientationEvent(#{name}, #{desc})`
29+
end
30+
elsif Browser.supports? 'Event.create'
31+
def self.construct(name, desc)
32+
%x{
33+
var event = document.createEvent("DeviceOrientationEvent");
34+
event.initDeviceOrientationEvent(name, desc.bubbles, desc.cancelable,
35+
desc.alpha, desc.beta, desc.gamma, desc.absolute);
36+
37+
return event;
38+
}
39+
end
40+
end if supported?
2941

3042
alias_native :absolute
3143
alias_native :alpha

‎opal/browser/dom/event/device_proximity.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Browser; module DOM; class Event
22

33
class DeviceProximity < Event
44
def self.supported?
5-
not $$[:DeviceProximityEvent].nil?
5+
Browser.supports? 'Event.DeviceProximity'
66
end
77

88
class Definition < Definition
@@ -19,9 +19,11 @@ def max=(value)
1919
end
2020
end
2121

22-
def self.construct(name, desc)
23-
`new DeviceProximityEvent(#{name}, #{desc})`
24-
end
22+
if Browser.supports? 'Event.constructor'
23+
def self.construct(name, desc)
24+
`new DeviceProximityEvent(#{name}, #{desc})`
25+
end
26+
end if supported?
2527

2628
alias_native :value
2729
alias_native :min

0 commit comments

Comments
 (0)
Please sign in to comment.