import { Container } from "pixi.js" ; import { injectable , inject , EventDispatcher } from "@robotlegsjs/core" ; import { IViewHandler } from "../api/IViewHandler" ; import { IViewManager } from "../api/IViewManager" ; import { ViewManagerEvent } from "./ViewManagerEvent" ; import { ContainerRegistry } from "../impl/ContainerRegistry" ; import { ContainerBinding } from "../impl/ContainerBinding" ; private @ injectable ( ) export class ViewManager extends EventDispatcher implements IViewManager { private _containers : Container [ ] = [ ] ; inheritDoc public get containers ( ) : Container [ ] { return this . _containers ; } private _handlers : IViewHandler [ ] = [ ] ; private _registry : ContainerRegistry ; private constructor ( @ inject ( ContainerRegistry ) containerRegistry ) { super ( ) ; this . _registry = containerRegistry ; } inheritDoc public addContainer ( container ) { if ( ! this . validContainer ( container ) ) { return ; } this . _containers . push ( container ) ; this . _handlers . forEach ( handler => { this . _registry . addContainer ( container ) . addHandler ( handler ) ; } ) ; this . dispatchEvent ( new ViewManagerEvent ( ViewManagerEvent . CONTAINER_ADD , container ) ) ; } inheritDoc public removeContainer ( container ) { let index = this . _containers . indexOf ( container ) ; if ( index === - 1 ) { return ; } this . _containers . splice ( index , 1 ) ; let binding = this . _registry . getBinding ( container ) ; this . _handlers . forEach ( handler => { binding . removeHandler ( handler ) ; } ) ; this . dispatchEvent ( new ViewManagerEvent ( ViewManagerEvent . CONTAINER_REMOVE , container ) ) ; } inheritDoc public addViewHandler ( handler ) { if ( this . _handlers . indexOf ( handler ) !== - 1 ) { return ; } this . _handlers . push ( handler ) ; this . _containers . forEach ( container => { this . _registry . addContainer ( container ) . addHandler ( handler ) ; } ) ; this . dispatchEvent ( new ViewManagerEvent ( ViewManagerEvent . HANDLER_ADD , null , handler ) ) ; } inheritDoc public removeViewHandler ( handler ) { let index = this . _handlers . indexOf ( handler ) ; if ( index === - 1 ) { return ; } this . _handlers . splice ( index , 1 ) ; this . _containers . forEach ( container => { this . _registry . getBinding ( container ) . removeHandler ( handler ) ; } ) ; this . dispatchEvent ( new ViewManagerEvent ( ViewManagerEvent . HANDLER_REMOVE , null , handler ) ) ; } inheritDoc public removeAllHandlers ( ) { let binding = null ; this . _containers . forEach ( container => { binding = this . _registry . getBinding ( container ) ; this . _handlers . forEach ( handler => { binding . removeHandler ( handler ) ; } ) ; } ) ; } private validContainer ( container ) { let isValid = this . _containers . indexOf ( container ) < 0 ; if ( isValid ) { this . _containers . forEach ( registeredContainer => { if ( registeredContainer . contains ( container ) || container . contains ( registeredContainer ) ) { throw new Error ( "Containers can not be nested" ) ; } } ) ; } return isValid ; } }