import { EventEmitter } from "./EventEmitter" ; import { DefinitionManager } from "./DefinitionManager" ; import { DirtyChecker } from "./DirtyChecker" ; import { Analyzer } from "./Analyzer" ; import { Selector } from "../extend/Selector" ; import { UkuEventType } from "./UkuEventType" ; import { IUkulele } from "./IUkulele" ; import { Event } from "./Event" ; import { ControllerModel } from "../model/ControllerModel" ; export let Ukulele = class Ukulele extends EventEmitter implements IUkulele { private defMgr : DefinitionManager ; private dirtyChecker : DirtyChecker ; private promiseArray = [ ] ; private _childrenUku = { } ; static INITIALIZED : string = 'initialized' ; static REFRESH : string = 'refresh' ; static HANDLE_ELEMENT_COMPLETED : string = "handle_element_completed" ; public init ( ) { Promise . all ( this . promiseArray ) . then ( ( ) => { this . manageApplication ( ) ; } ) ; } public setChildrenUku ( controllers , uku ) { for ( let cc of controllers ) { let alias = cc . _alias ; if ( ! this . _childrenUku [ alias ] ) { this . _childrenUku [ alias ] = [ ] ; } this . _childrenUku [ alias ] . push ( uku ) ; } } public handleElement ( element , handleElementCompletedFunc ) { this . analyizeElement ( element , ( e ) => { this . dispatchEvent ( new Event ( UkuEventType . HANDLE_ELEMENT_COMPLETED , e ) ) ; if ( handleElementCompletedFunc && typeof handleElementCompletedFunc === 'function' ) { handleElementCompletedFunc ( e . element ) ; } } ) ; } public registerController ( instanceName , controllerInst ) { window [ instanceName ] = controllerInst ; this . _internal_getDefinitionManager ( ) . addControllerDefinition ( instanceName , controllerInst ) ; } public getController ( instanceName ) { return this . _internal_getDefinitionManager ( ) . getControllerDefinition ( instanceName ) . controllerInstance ; } public registerComponent ( tag , templateUrl , preload ) { var p = this . _internal_getDefinitionManager ( ) . addComponentDefinition ( tag , templateUrl , preload ) ; this . promiseArray . push ( p ) ; } public getComponent ( tagName ) { return this . _internal_getDefinitionManager ( ) . getComponent ( tagName ) ; } public getComponentController ( componentId ) { return this . _internal_getDefinitionManager ( ) . getControllerInstByDomId ( componentId ) ; } public refresh ( alias ? , excludeElement ? ) { if ( ! this . dirtyChecker ) { this . dirtyChecker = new DirtyChecker ( this ) ; } this . dirtyChecker . runDirtyChecking ( alias , excludeElement ) ; } public _internal_getDefinitionManager ( ) { if ( ! this . defMgr ) { this . defMgr = new DefinitionManager ( this ) ; } return this . defMgr ; } public _internal_dealWithElement ( element , callback ) { this . analyizeElement ( element , callback ) ; } private manageApplication ( ) { let apps = Selector . querySelectorAll ( document , "[uku-application]" ) ; if ( apps . length === 1 ) { this . analyizeElement ( apps [ 0 ] as HTMLElement , ( ele ) => { this . dispatchEvent ( new Event ( UkuEventType . INITIALIZED , ele ) ) ; } ) ; } else { throw new Error ( "Only one 'uku-application' can be declared in a whole html." ) ; } } private analyizeElement ( element , callback ? ) { let anylyzer = new Analyzer ( this ) ; if ( callback ) { ( ( retFunc ) => { anylyzer . addListener ( Analyzer . ANALYIZE_COMPLETED , ( e ) => { retFunc ( e . element ) ; } ) ; } ) ( callback ) ; } anylyzer . analyizeElement ( element ) ; } } ;