stdweb/webapi/events/
focus.rs

1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3use webapi::event_target::EventTarget;
4use webapi::event::{IEvent, Event};
5
6/// The `IFocusEvent` interface represents focus-related
7/// events.
8///
9/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent)
10// https://w3c.github.io/uievents/#focusevent
11pub trait IFocusEvent: IEvent {
12    /// Returns the secondary target of this event, if any.
13    ///
14    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/relatedTarget)
15    // https://w3c.github.io/uievents/#dom-focusevent-relatedtarget
16    #[inline]
17    fn related_target( &self ) -> Option< EventTarget > {
18        js!(
19            return @{self.as_ref()}.relatedTarget;
20        ).try_into().ok()
21    }
22}
23
24/// A reference to a JavaScript object which implements the [IFocusEvent](trait.IFocusEvent.html)
25/// interface.
26///
27/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent)
28// https://w3c.github.io/uievents/#focusevent
29#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
30#[reference(instance_of = "FocusEvent")]
31#[reference(subclass_of(Event))]
32pub struct FocusRelatedEvent( Reference );
33
34impl IEvent for FocusRelatedEvent {}
35impl IFocusEvent for FocusRelatedEvent {}
36
37/// The `FocusEvent` is fired when an element has received focus. The main
38/// difference between this event and focusin is that only the latter bubbles.
39///
40/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/focus)
41// https://w3c.github.io/uievents/#event-type-focus
42#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
43#[reference(instance_of = "FocusEvent")]
44#[reference(event = "focus")]
45#[reference(subclass_of(Event, FocusRelatedEvent))]
46pub struct FocusEvent( Reference );
47
48impl IEvent for FocusEvent {}
49impl IFocusEvent for FocusEvent {}
50
51/// The `BlurEvent` is fired when an element has lost focus. The main difference
52/// between this event and focusout is that only the latter bubbles.
53///
54/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/blur)
55// https://w3c.github.io/uievents/#event-type-blur
56#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
57#[reference(instance_of = "FocusEvent")]
58#[reference(event = "blur")]
59#[reference(subclass_of(Event, FocusRelatedEvent))]
60pub struct BlurEvent( Reference );
61
62impl IEvent for BlurEvent {}
63impl IFocusEvent for BlurEvent {}
64
65#[cfg(all(test, feature = "web_test"))]
66mod tests {
67    use super::*;
68    use webapi::event::ConcreteEvent;
69
70    #[test]
71    fn test_focus_event() {
72        let event: FocusEvent = js!(
73            return new FocusEvent( "focus" );
74        ).try_into().unwrap();
75        assert_eq!( event.event_type(), "focus" );
76        assert!( event.related_target().is_none() );
77    }
78
79    #[test]
80    fn test_blur_event() {
81        let event: BlurEvent = js!(
82            return new FocusEvent( @{BlurEvent::EVENT_TYPE} );
83        ).try_into().unwrap();
84        assert_eq!( event.event_type(), BlurEvent::EVENT_TYPE );
85    }
86}