@ -15,28 +15,41 @@ export default function generate ( parsed, source, options ) {
const generator = {
const generator = {
addElement ( name , renderStatement , needsIdentifier = false ) {
addElement ( name , renderStatement , needsIdentifier = false ) {
const needsTeardown = generator . current . localElementDepth === 0 ;
const isToplevel = generator . current . localElementDepth === 0 ;
if ( needsIdentifier || needsTeardown ) {
if ( needsIdentifier || isToplevel ) {
generator . current . initStatements . push ( deindent `
generator . current . initStatements . push ( deindent `
var $ { name } = $ { renderStatement } ;
var $ { name } = $ { renderStatement } ;
$ { generator . appendToTarget ( name ) } ;
` );
` );
generator . createMountStatement ( name ) ;
} else {
} else {
generator . current . initStatements . push ( deindent `
generator . current . initStatements . push ( deindent `
$ { generator . current . target } . appendChild ( $ { renderStatement } ) ;
$ { generator . current . target } . appendChild ( $ { renderStatement } ) ;
` );
` );
}
}
if ( needsTeardown ) {
if ( isToplevel ) {
generator . current . teardownStatements . push ( deindent `
generator . current . teardownStatements . push ( deindent `
if ( detach ) $ { name } . parentNode . removeChild ( $ { name } ) ;
if ( detach ) $ { name } . parentNode . removeChild ( $ { name } ) ;
` );
` );
}
}
} ,
} ,
appendToTarget ( name ) {
if ( generator . current . useAnchor && generator . current . target === 'target' ) {
createMountStatement ( name ) {
return ` anchor.parentNode.insertBefore( ${ name } , anchor ) ` ;
if ( generator . current . target === 'target' ) {
generator . current . mountStatements . push ( deindent `
target . insertBefore ( $ { name } , anchor ) ;
` );
} else {
generator . current . initStatements . push ( deindent `
$ { generator . current . target } . appendChild ( $ { name } ) ;
` );
}
}
return ` ${ generator . current . target } .appendChild( ${ name } ) ` ;
} ,
createAnchor ( _name , description = '' ) {
const name = ` ${ _name } _anchor ` ;
const statement = ` document.createComment( ${ JSON . stringify ( description ) } ) ` ;
generator . addElement ( name , statement , true ) ;
return name ;
} ,
} ,
addRenderer ( fragment ) {
addRenderer ( fragment ) {
@ -45,10 +58,14 @@ export default function generate ( parsed, source, options ) {
}
}
renderers . push ( deindent `
renderers . push ( deindent `
function $ { fragment . name } ( $ { fragment . params } , component , target$ { fragment . useAnchor ? ', anchor' : '' } ) {
function $ { fragment . name } ( $ { fragment . params } , component ) {
$ { fragment . initStatements . join ( '\n\n' ) }
$ { fragment . initStatements . join ( '\n\n' ) }
return {
return {
mount : function ( target , anchor ) {
$ { fragment . mountStatements . join ( '\n\n' ) }
} ,
update : function ( changed , $ { fragment . params } ) {
update : function ( changed , $ { fragment . params } ) {
$ { fragment . updateStatements . join ( '\n\n' ) }
$ { fragment . updateStatements . join ( '\n\n' ) }
} ,
} ,
@ -101,7 +118,7 @@ export default function generate ( parsed, source, options ) {
const context = indexes [ name ] ;
const context = indexes [ name ] ;
if ( ! ~ usedContexts . indexOf ( context ) ) usedContexts . push ( context ) ;
if ( ! ~ usedContexts . indexOf ( context ) ) usedContexts . push ( context ) ;
} else {
} else {
dependencies . push ( n ode. n ame ) ;
dependencies . push ( n ame ) ;
generator . code . prependRight ( node . start , ` root. ` ) ;
generator . code . prependRight ( node . start , ` root. ` ) ;
if ( ! ~ usedContexts . indexOf ( 'root' ) ) usedContexts . push ( 'root' ) ;
if ( ! ~ usedContexts . indexOf ( 'root' ) ) usedContexts . push ( 'root' ) ;
}
}
@ -236,6 +253,7 @@ export default function generate ( parsed, source, options ) {
localElementDepth : 0 ,
localElementDepth : 0 ,
initStatements : [ ] ,
initStatements : [ ] ,
mountStatements : [ ] ,
updateStatements : [ ] ,
updateStatements : [ ] ,
teardownStatements : [ ] ,
teardownStatements : [ ] ,
@ -364,13 +382,17 @@ export default function generate ( parsed, source, options ) {
if ( generator . hasComplexBindings ) {
if ( generator . hasComplexBindings ) {
initStatements . push ( deindent `
initStatements . push ( deindent `
this . _ _bindings = [ ] ;
this . _ _bindings = [ ] ;
var mainFragment = renderMainFragment ( state , this , options . target ) ;
var mainFragment = renderMainFragment ( state , this ) ;
if ( options . target ) this . mount ( options . target ) ;
while ( this . _ _bindings . length ) this . _ _bindings . pop ( ) ( ) ;
while ( this . _ _bindings . length ) this . _ _bindings . pop ( ) ( ) ;
` );
` );
setStatements . push ( ` while ( this.__bindings.length ) this.__bindings.pop()(); ` ) ;
setStatements . push ( ` while ( this.__bindings.length ) this.__bindings.pop()(); ` ) ;
} else {
} else {
initStatements . push ( ` var mainFragment = renderMainFragment( state, this, options.target ); ` ) ;
initStatements . push ( deindent `
var mainFragment = renderMainFragment ( state , this ) ;
if ( options . target ) this . mount ( options . target ) ;
` );
}
}
if ( generator . hasComponents ) {
if ( generator . hasComponents ) {
@ -387,8 +409,8 @@ export default function generate ( parsed, source, options ) {
if ( templateProperties . onrender ) {
if ( templateProperties . onrender ) {
initStatements . push ( deindent `
initStatements . push ( deindent `
if ( options . paren t ) {
if ( options . roo t ) {
options . paren t. _ _renderHooks . push ( { fn : template . onrender , context : this } ) ;
options . roo t. _ _renderHooks . push ( { fn : template . onrender , context : this } ) ;
} else {
} else {
template . onrender . call ( this ) ;
template . onrender . call ( this ) ;
}
}
@ -449,6 +471,10 @@ export default function generate ( parsed, source, options ) {
$ { setStatements . join ( '\n\n' ) }
$ { setStatements . join ( '\n\n' ) }
} ;
} ;
this . mount = function mount ( target , anchor ) {
mainFragment . mount ( target , anchor ) ;
}
this . observe = function ( key , callback , options ) {
this . observe = function ( key , callback , options ) {
var group = ( options && options . defer ) ? observers . deferred : observers . immediate ;
var group = ( options && options . defer ) ? observers . deferred : observers . immediate ;
@ -489,6 +515,8 @@ export default function generate ( parsed, source, options ) {
state = { } ;
state = { } ;
} ;
} ;
this . root = options . root ;
$ { initStatements . join ( '\n\n' ) }
$ { initStatements . join ( '\n\n' ) }
}
}
` );
` );