'use strict' ; import * as vg from 'vega' ; import { Runtime , Spec , View } from 'vega' ; import { CELL , MOUSE } from '../store/factory/Signal' ; import { exporter } from './export' ; import { BUBBLE_CURSOR , BUBBLE_CURSOR_TIP } from './manipulators' ; import { hydrate , persist } from './persist' ; import { api } from './signals' ; const dl = require ( 'datalib' ) , manips = require ( './manipulators' ) , ns = require ( '../util/ns' ) , CancellablePromise = require ( '../util/simple-cancellable-promise' ) ; interface LyraCtrl { view : View ; export : ( internal : boolean , preview ? : boolean ) => Spec ; persist : ( ) => { } ; hydrate : ( { } ) => void ; manipulators : ( ) => Spec ; parse : ( el : string ) => Promise < Runtime > ; update : ( ) => void ; } ; namespace const ctrl = ( module . exports = { view : null } as any ) , listeners = require ( './listeners' ) ; let parsePromise = null ; ctrl . export = exporter ; ctrl . persist = persist ; ctrl . hydrate = hydrate ; returns { Object } ctrl . manipulators = function ( ) : Spec { const spec = ctrl . export ( true ) , data = spec . data || ( spec . data = [ ] ) , signals = spec . signals || ( spec . signals = [ ] ) , marks = spec . marks || ( spec . marks = [ ] ) , idx = dl . comparator ( '_idx' ) ; signals . push . apply ( signals , Object . values ( api ( ) ) . sort ( idx ) ) ; data . push ( { name : 'bubble_cursor' , transform : [ { type : ns ( 'bubble_cursor' ) , lyra_cell : { signal : CELL } , lyra_mouse : { signal : MOUSE } } as any ] } ) ; marks . push ( BUBBLE_CURSOR ) ; marks . push . apply ( marks , BUBBLE_CURSOR_TIP ) ; data . push ( { name : 'dummy_data' , values : [ { x : 0 , y : 0 } , { x : 100 , y : 100 } ] } ) ; return spec ; } ; param { string } el returns { Object } ctrl . parse = function ( el ) { el = el || '#vis' ; if ( parsePromise ) { parsePromise . cancel ( ) ; } parsePromise = new CancellablePromise ( function ( resolve , reject ) { const that = this ; setTimeout ( function ( ) { if ( that . cancelled ) { return ; } try { resolve ( vg . parse ( ctrl . manipulators ( ) ) ) ; } catch ( e ) { console . error ( e ) ; } } , 10 ) ; } ) ; return parsePromise . then ( function ( runtime ) { ctrl . view = new vg . View ( runtime , { renderer : 'svg' } ) . initialize ( el ) . logLevel ( vg . Warn ) . hover ( ) ; listeners . register ( ) ; ctrl . update ( ) ; parsePromise = null ; } ) ; } ; returns { void } ctrl . update = function ( ) { if ( ctrl . view && ctrl . view . runAsync && typeof ctrl . view . runAsync === 'function' ) { ctrl . view . runAsync ( ) ; } } ;