import * as ng from 'angular' ; import * as Options from '../options/_index' ; import { EventManager } from './EventManager' ; export interface IFactory { init ( key , eventMgr , factoryMgr ) ; create ( options ) ; update ( datasets , options , attributes ) ; destroy ( ) ; off ( ) ; isOff ( ) ; on ( ) ; isOn ( ) ; } export interface IFactoryEntry { key : string ; instance : IFactory ; } export class FactoryManager { private _factoryStack : IFactoryEntry [ ] = [ ] ; index ( factoryKey ) { return this . _factoryStack . map ( ( d ) => d . key ) . indexOf ( factoryKey ) ; } getBoundFunction ( factoryKey , functionName ) { let factory = this . get ( factoryKey ) ; if ( ! factory ) { return null ; } return factory [ functionName ] . bind ( factory ) ; } get ( factoryKey ) { var index = this . index ( factoryKey ) ; if ( index > - 1 ) { return this . _factoryStack [ index ] . instance ; } return null ; } all ( ) { return this . _factoryStack ; } turnFactoriesOff ( keys ) { var toUndo = [ ] ; keys . forEach ( ( key ) => { let f = this . get ( key ) ; if ( f . isOn ( ) ) { f . off ( ) ; toUndo . push ( key ) ; } } ) ; return ( ) => this . turnFactoriesOn ( toUndo ) ; } turnFactoriesOn ( keys ) { var toUndo = [ ] ; keys . forEach ( ( key ) => { let f = this . get ( key ) ; if ( f . isOff ( ) ) { f . on ( ) ; toUndo . push ( key ) ; } } ) ; return ( ) => this . turnFactoriesOff ( toUndo ) ; } registerMany ( factories ) { factories . forEach ( ( factoryArgs ) => { this . register . apply ( this , factoryArgs ) ; } ) ; return this ; } register ( key , constructor , ... args ) { var factory = constructor . bind . apply ( constructor , [ null ] . concat ( args ) ) ; var instance = new factory ( ) ; this . _factoryStack . push ( { key : key , instance : instance } ) ; return instance ; } unregister ( factoryKey ) { var index = this . index ( factoryKey ) ; if ( index > - 1 ) { delete this . _factoryStack [ index ] ; } return this ; } }