import { DomController } from '../platform/dom-controller' ; import { Platform } from '../platform/platform' ; import { ScrollView } from './scroll-view' ; name description usage demo export class Events { private _channels : any = [ ] ; param { string } topic param { function } handler subscribe ( topic , ... handlers ) { if ( ! this . _channels [ topic ] ) { this . _channels [ topic ] = [ ] ; } handlers . forEach ( ( handler ) => { this . _channels [ topic ] . push ( handler ) ; } ) ; } param { string } topic param { function } handler return unsubscribe ( topic , handler = null ) { let t = this . _channels [ topic ] ; if ( ! t ) { return false ; } if ( ! handler ) { delete this . _channels [ topic ] ; return true ; } let i = t . indexOf ( handler ) ; if ( i < 0 ) { return false ; } t . splice ( i , 1 ) ; if ( ! t . length ) { delete this . _channels [ topic ] ; } return true ; } param { string } topic param { any } eventData publish ( topic , ... args ) { var t = this . _channels [ topic ] ; if ( ! t ) { return null ; } let responses = [ ] ; t . forEach ( ( handler ) => { responses . push ( handler ( ... args ) ) ; } ) ; return responses ; } } hidden export function setupEvents ( plt , dom ) { const events = new Events ( ) ; const win = plt . win ( ) ; const doc = plt . doc ( ) ; plt . timeout ( ( ) => { win . addEventListener ( 'online' , ( ev ) => { events . publish ( 'app:online' , ev ) ; } , false ) ; win . addEventListener ( 'offline' , ( ev ) => { events . publish ( 'app:offline' , ev ) ; } , false ) ; win . addEventListener ( 'orientationchange' , ( ev ) => { events . publish ( 'app:rotated' , ev ) ; } ) ; win . addEventListener ( 'statusTap' , ( ) => { let el = < HTMLElement > doc . elementFromPoint ( plt . width ( ) / 2 , plt . height ( ) / 2 ) ; if ( ! el ) { return ; } let contentEle = < any > el . closest ( '.scroll-content' ) ; if ( contentEle ) { var style = contentEle . style ; var scroll = new ScrollView ( null , plt , dom ) ; scroll . _el = contentEle ; style [ 'WebkitBackfaceVisibility' ] = 'hidden' ; style [ 'WebkitTransform' ] = 'translate3d(0,0,0)' ; dom . write ( function ( ) { style . overflow = 'hidden' ; function finish ( ) { style . overflow = '' ; style [ 'WebkitBackfaceVisibility' ] = '' ; style [ 'WebkitTransform' ] = '' ; } let didScrollTimeout = plt . timeout ( ( ) => { finish ( ) ; } , 400 ) ; scroll . scrollTo ( 0 , 0 , 300 ) . then ( ( ) => { plt . cancelTimeout ( didScrollTimeout ) ; finish ( ) ; } ) ; } ) ; } } ) ; } , 2000 ) ; return events ; } hidden export function setupProvideEvents ( plt , dom ) { return function ( ) { return setupEvents ( plt , dom ) ; } ; }