import * as PIXI from 'pixi.js' ; import * as element from './element' ; import { AEDataInterceptor } from './interceptor' ; import { AssetData , Asset } from './asset' ; import pixiVersionHelper from './versionHelper' ; import { AEData } from './AfterEffects' ; class memberof prop prop export class AEDataLoader { imagePathProxy : ( path : string ) => string ; createImageLoader : Function ; constructor ( ) { this . imagePathProxy = path => path ; this . createImageLoader = pixiVersionHelper . select ( ( imageAssets ) => { const loader = new PIXI . loaders . Loader ( ) ; return ( loader as ( ( s : string , n : number ) => any ) ) ( '' , imageAssets . length ) ; } , ( imageAssets ) => { return new PIXI . Loader ( '' , imageAssets . length ) ; } ) ; } memberof param { string } return { Promise } loadJSON ( jsonPath ) { return new Promise ( ( resolve , reject ) => { fetch ( jsonPath ) . then ( ( res ) => { return res . json ( ) ; } ) . then ( ( json ) => { if ( ! json ) { return reject ( ) ; } const data = json as AEData ; return this . load ( data , jsonPath , null ) . then ( ( ) => { resolve ( data ) ; } ) . catch ( ( e ) => { reject ( e ) ; } ) ; } ) ; } ) ; } memberof param { string } param { PIXI . AEDataInterceptor } return { Promise } loadJSONWithInterceptor ( jsonPath , interceptor ) { return new Promise ( ( resolve , reject ) => { if ( ! interceptor ) { return reject ( new Error ( 'required interceptor parameter' ) ) ; } return fetch ( jsonPath ) . then ( ( res ) => { return res . json ( ) ; } ) . then ( ( json ) => { const data = json as AEData ; return this . load ( data , jsonPath , interceptor ) . then ( ( ) => { resolve ( data ) ; } ) . catch ( ( e ) => { reject ( e ) ; } ) ; } ) ; } ) ; } static loadLayers ( data , interceptor ) { return data . layers . map ( ( layer ) => { if ( interceptor ) interceptor . intercept ( layer ) ; return element . ElementFactory . create ( layer ) ; } ) . filter ( ( layer ) => layer !== null ) ; } loadAssets ( data , jsonPath , interceptor ) { const baseName = jsonPath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ; const assets = data . assets . map ( ( asset ) => { if ( interceptor ) interceptor . intercept ( asset ) ; return new Asset ( this , asset , baseName ) ; } ) ; const imageAssets = assets . filter ( ( asset ) => { return ! ! asset . imagePath ; } ) ; if ( imageAssets . length === 0 ) { return new Promise ( ( resolve ) => resolve ( assets ) ) ; } return this . loadImages ( imageAssets ) . then ( ( ) => assets ) ; } loadImages ( imageAssets ) { return new Promise ( ( resolve , reject ) => { const loader = this . createImageLoader ( imageAssets ) ; const requiredLoadAssets = imageAssets . filter ( asset => ! loader . resources [ asset . imagePath ] ) ; if ( requiredLoadAssets . length === 0 ) { imageAssets . forEach ( ( asset ) => { asset . texture = loader . resources [ asset . imagePath ] . texture ; } ) ; return resolve ( ) ; } requiredLoadAssets . forEach ( ( asset ) => { loader . add ( asset . imagePath , asset . imagePath ) ; } ) ; loader . onError . add ( ( error ) => { reject ( error ) ; } ) ; return loader . load ( ( _ , resources ) => { imageAssets . forEach ( asset => ( asset . texture = resources [ asset . imagePath ] . texture ) ) ; resolve ( ) ; } ) ; } ) ; } static resolveReference ( layers , assets ) { const assetMap = { } ; assets . forEach ( ( asset ) => { assetMap [ asset . id ] = asset ; } ) ; layers . forEach ( ( layer ) => { if ( layer . isCompType ( ) ) { layer . setupReference ( assetMap ) ; } else if ( layer . isImageType ( ) ) { layer . setupImage ( assetMap ) ; } } ) ; } load ( data , jsonPath , interceptor ) { return this . loadAssets ( data , jsonPath , interceptor ) . then ( ( assets ) => { const layers = AEDataLoader . loadLayers ( data , interceptor ) ; AEDataLoader . resolveReference ( layers , assets ) ; data . assets = assets ; data . layers = layers ; } ) ; } }