use webcore::value::Reference;
use webcore::try_from::TryInto;
use webapi::event::{IEvent, Event, ConcreteEvent};
pub trait IProgressEvent: IEvent {
#[inline]
fn length_computable( &self ) -> bool {
js!(
return @{self.as_ref()}.lengthComputable;
).try_into().unwrap()
}
#[inline]
fn loaded( &self ) -> u64 {
js!(
return @{self.as_ref()}.loaded;
).try_into().unwrap()
}
#[inline]
fn total( &self ) -> u64 {
js!(
return @{self.as_ref()}.total;
).try_into().unwrap()
}
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")]
#[reference(subclass_of(Event))]
pub struct ProgressRelatedEvent( Reference );
impl IEvent for ProgressRelatedEvent {}
impl IProgressEvent for ProgressRelatedEvent {}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")] #[reference(subclass_of(Event, ProgressRelatedEvent))]
pub struct ProgressEvent( Reference );
impl IEvent for ProgressEvent {}
impl IProgressEvent for ProgressEvent {}
impl ConcreteEvent for ProgressEvent {
const EVENT_TYPE: &'static str = "progress";
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")] #[reference(subclass_of(Event, ProgressRelatedEvent))]
pub struct ProgressLoadEvent( Reference );
impl IEvent for ProgressLoadEvent {}
impl IProgressEvent for ProgressLoadEvent {}
impl ConcreteEvent for ProgressLoadEvent {
const EVENT_TYPE: &'static str = "load";
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")] #[reference(subclass_of(Event, ProgressRelatedEvent))]
pub struct LoadStartEvent( Reference );
impl IEvent for LoadStartEvent {}
impl IProgressEvent for LoadStartEvent {}
impl ConcreteEvent for LoadStartEvent {
const EVENT_TYPE: &'static str = "loadstart";
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")] #[reference(subclass_of(Event, ProgressRelatedEvent))]
pub struct LoadEndEvent( Reference );
impl IEvent for LoadEndEvent {}
impl IProgressEvent for LoadEndEvent {}
impl ConcreteEvent for LoadEndEvent {
const EVENT_TYPE: &'static str = "loadend";
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")] #[reference(subclass_of(Event, ProgressRelatedEvent))]
pub struct ProgressAbortEvent( Reference );
impl IEvent for ProgressAbortEvent {}
impl IProgressEvent for ProgressAbortEvent {}
impl ConcreteEvent for ProgressAbortEvent {
const EVENT_TYPE: &'static str = "abort";
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "ProgressEvent")] #[reference(subclass_of(Event, ProgressRelatedEvent))]
pub struct ProgressErrorEvent( Reference );
impl IEvent for ProgressErrorEvent {}
impl IProgressEvent for ProgressErrorEvent {}
impl ConcreteEvent for ProgressErrorEvent {
const EVENT_TYPE: &'static str = "error";
}
#[cfg(all(test, feature = "web_test"))]
mod tests {
use super::*;
#[test]
fn test_progress_event() {
let event: ProgressEvent = js!(
return new ProgressEvent(
@{ProgressEvent::EVENT_TYPE},
{
lengthComputable: true,
loaded: 10,
total: 100,
}
);
).try_into().unwrap();
assert_eq!( event.event_type(), ProgressEvent::EVENT_TYPE );
assert!( event.length_computable() );
assert_eq!( event.loaded(), 10 );
assert_eq!( event.total(), 100 );
}
#[test]
fn test_load_start_event() {
let event: LoadStartEvent = js!(
return new ProgressEvent( @{LoadStartEvent::EVENT_TYPE} );
).try_into().unwrap();
assert_eq!( event.event_type(), LoadStartEvent::EVENT_TYPE );
}
#[test]
fn test_load_end_event() {
let event: LoadEndEvent = js!(
return new ProgressEvent( @{LoadEndEvent::EVENT_TYPE} );
).try_into().unwrap();
assert_eq!( event.event_type(), LoadEndEvent::EVENT_TYPE );
}
}