use webcore::value::Reference;
use webcore::try_from::TryInto;
use webapi::event_target::EventTarget;
use webapi::event::{IEvent, Event};
pub trait IFocusEvent: IEvent {
#[inline]
fn related_target( &self ) -> Option< EventTarget > {
js!(
return @{self.as_ref()}.relatedTarget;
).try_into().ok()
}
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FocusEvent")]
#[reference(subclass_of(Event))]
pub struct FocusRelatedEvent( Reference );
impl IEvent for FocusRelatedEvent {}
impl IFocusEvent for FocusRelatedEvent {}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FocusEvent")]
#[reference(event = "focus")]
#[reference(subclass_of(Event, FocusRelatedEvent))]
pub struct FocusEvent( Reference );
impl IEvent for FocusEvent {}
impl IFocusEvent for FocusEvent {}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FocusEvent")]
#[reference(event = "blur")]
#[reference(subclass_of(Event, FocusRelatedEvent))]
pub struct BlurEvent( Reference );
impl IEvent for BlurEvent {}
impl IFocusEvent for BlurEvent {}
#[cfg(all(test, feature = "web_test"))]
mod tests {
use super::*;
use webapi::event::ConcreteEvent;
#[test]
fn test_focus_event() {
let event: FocusEvent = js!(
return new FocusEvent( "focus" );
).try_into().unwrap();
assert_eq!( event.event_type(), "focus" );
assert!( event.related_target().is_none() );
}
#[test]
fn test_blur_event() {
let event: BlurEvent = js!(
return new FocusEvent( @{BlurEvent::EVENT_TYPE} );
).try_into().unwrap();
assert_eq!( event.event_type(), BlurEvent::EVENT_TYPE );
}
}