namespace drunk { import dom = drunk . dom ; import util = drunk . util ; import Template = drunk . Template ; import Component = drunk . Component ; @ binding ( "slot" ) class SlotBinding extends Binding { static isTerminal = true ; private _nodes : Node [ ] ; private _unbinds : Function [ ] ; init ( owner , placeholder ) { if ( ! owner || ! placeholder ) { throw new Error ( `$mount(element, owner, placeholder): owner和placeholder未提供` ) ; } let nodes = [ ] ; let unbinds = [ ] ; let fragment = document . createDocumentFragment ( ) ; let viewModel = owner ; if ( this . expression && /\S/ . test ( this . expression ) ) { let element = placeholder . querySelector ( `[slot="${ this . expression }"]` ) ; if ( element ) { element . removeAttribute ( 'slot' ) ; nodes . push ( element ) ; fragment . appendChild ( element ) ; } } else { util . toArray ( placeholder . childNodes ) . forEach ( ( node ) => { if ( node . nodeType === 3 || ! ( node as HTMLElement ) . hasAttribute ( 'slot' ) ) { nodes . push ( node ) ; fragment . appendChild ( node ) ; } } ) ; } if ( this . element . tagName . toLowerCase ( ) === 'slot' && ! nodes . length && this . element . childNodes . length ) { util . toArray ( this . element . childNodes ) . forEach ( ( node ) => { nodes . push ( node ) ; fragment . appendChild ( node ) ; } ) ; viewModel = this . viewModel ; } dom . replace ( fragment , this . element ) ; nodes . forEach ( ( node ) => { let bind = Template . compile ( node ) ; unbinds . push ( bind ( viewModel , node ) ) ; } ) ; this . _nodes = nodes ; this . _unbinds = unbinds ; } /**
         * 释放绑定
         */ release ( ) { this . _unbinds . forEach ( unbind => unbind ( ) ) ; this . _nodes . forEach ( node => dom . remove ( node ) ) ; this . _unbinds = this . _nodes = null ; } } }