import d3 from 'd3' ; import $ from 'jquery' ; export interface Emitter { on : ( ... args : any [ ] ) => void ; off : ( ... args : any [ ] ) => void ; addListener : Emitter [ 'on' ] ; removeListener : Emitter [ 'off' ] ; } export class Binder { private disposal : Array < ( ) => void > = [ ] ; public on ( emitter , ... args ) { const on = emitter . on || emitter . addListener ; const off = emitter . off || emitter . removeListener ; on . apply ( emitter , args ) ; this . disposal . push ( ( ) => off . apply ( emitter , args ) ) ; } public destroy ( ) { const destroyers = this . disposal ; this . disposal = [ ] ; destroyers . forEach ( ( fn ) => fn ( ) ) ; } jqOn ( el , ... args ) { const $el = $ ( el ) ; $el . on ( ... args ) ; this . disposal . push ( ( ) => $el . off ( ... args ) ) ; } fakeD3Bind ( el , event , handler ) { this . jqOn ( el , event , ( e ) => { const o = d3 . event ; d3 . event = e ; try { handler . apply ( this , [ this . __data__ ] ) ; } finally { d3 . event = o ; } } ) ; } }