|
|
|
@ -1,54 +1,51 @@
|
|
|
|
|
import CodeBuilder from '../../../utils/CodeBuilder.js';
|
|
|
|
|
import deindent from '../../../utils/deindent.js';
|
|
|
|
|
import getBuilders from '../utils/getBuilders.js';
|
|
|
|
|
import visit from '../visit.js';
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
enter ( generator, node ) {
|
|
|
|
|
export default function visitEachBlock ( generator, block, state, node ) {
|
|
|
|
|
const name = generator.getUniqueName( `each_block` );
|
|
|
|
|
const renderer = generator.getUniqueName( `render_each_block` );
|
|
|
|
|
const renderer = generator.getUniqueName( `create_each_block` );
|
|
|
|
|
const elseName = generator.getUniqueName( `${name}_else` );
|
|
|
|
|
const renderElse = generator.getUniqueName( `${renderer}_else` );
|
|
|
|
|
const i = generator.current.getUniqueName( `i` );
|
|
|
|
|
const params = generator.current.params.join( ', ' );
|
|
|
|
|
const i = block.getUniqueName( `i` );
|
|
|
|
|
const params = block.params.join( ', ' );
|
|
|
|
|
|
|
|
|
|
const listName = generator.current.getUniqueName( `${name}_value` );
|
|
|
|
|
const listName = block.getUniqueName( `${name}_value` );
|
|
|
|
|
|
|
|
|
|
const isToplevel = generator.current.localElementDepth === 0;
|
|
|
|
|
const isToplevel = !state.parentNode;
|
|
|
|
|
|
|
|
|
|
generator.addSourcemapLocations( node.expression );
|
|
|
|
|
const { dependencies, snippet } = generator.contextualise( block, node.expression );
|
|
|
|
|
|
|
|
|
|
const { dependencies, snippet } = generator.contextualise( node.expression );
|
|
|
|
|
|
|
|
|
|
const anchor = generator.current.getUniqueName( `${name}_anchor` );
|
|
|
|
|
generator.createAnchor( anchor );
|
|
|
|
|
const anchor = block.getUniqueName( `${name}_anchor` );
|
|
|
|
|
block.createAnchor( anchor, state.parentNode );
|
|
|
|
|
|
|
|
|
|
const localVars = {};
|
|
|
|
|
|
|
|
|
|
localVars.iteration = generator.current.getUniqueName( `${name}_iteration` );
|
|
|
|
|
localVars.iterations = generator.current.getUniqueName( `${name}_iterations` );
|
|
|
|
|
localVars._iterations = generator.current.getUniqueName( `_${name}_iterations` );
|
|
|
|
|
localVars.lookup = generator.current.getUniqueName( `${name}_lookup` );
|
|
|
|
|
localVars._lookup = generator.current.getUniqueName( `_${name}_lookup` );
|
|
|
|
|
localVars.iteration = block.getUniqueName( `${name}_iteration` );
|
|
|
|
|
localVars.iterations = block.getUniqueName( `${name}_iterations` );
|
|
|
|
|
localVars._iterations = block.getUniqueName( `_${name}_iterations` );
|
|
|
|
|
localVars.lookup = block.getUniqueName( `${name}_lookup` );
|
|
|
|
|
localVars._lookup = block.getUniqueName( `_${name}_lookup` );
|
|
|
|
|
|
|
|
|
|
generator.current.builders.init.addLine( `var ${listName} = ${snippet};` );
|
|
|
|
|
generator.current.builders.init.addLine( `var ${localVars.iterations} = [];` );
|
|
|
|
|
if ( node.key ) generator.current.builders.init.addLine( `var ${localVars.lookup} = Object.create( null );` );
|
|
|
|
|
if ( node.else ) generator.current.builders.init.addLine( `var ${elseName} = null;` );
|
|
|
|
|
block.builders.create.addLine( `var ${listName} = ${snippet};` );
|
|
|
|
|
block.builders.create.addLine( `var ${localVars.iterations} = [];` );
|
|
|
|
|
if ( node.key ) block.builders.create.addLine( `var ${localVars.lookup} = Object.create( null );` );
|
|
|
|
|
if ( node.else ) block.builders.create.addLine( `var ${elseName} = null;` );
|
|
|
|
|
|
|
|
|
|
const initialRender = new CodeBuilder();
|
|
|
|
|
|
|
|
|
|
if ( node.key ) {
|
|
|
|
|
localVars.fragment = generator.current.getUniqueName( 'fragment' );
|
|
|
|
|
localVars.value = generator.current.getUniqueName( 'value' );
|
|
|
|
|
localVars.key = generator.current.getUniqueName( 'key' );
|
|
|
|
|
localVars.fragment = block.getUniqueName( 'fragment' );
|
|
|
|
|
localVars.value = block.getUniqueName( 'value' );
|
|
|
|
|
localVars.key = block.getUniqueName( 'key' );
|
|
|
|
|
|
|
|
|
|
initialRender.addBlock( deindent`
|
|
|
|
|
var ${localVars.key} = ${listName}[${i}].${node.key};
|
|
|
|
|
${localVars.iterations}[${i}] = ${localVars.lookup}[ ${localVars.key} ] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${generator.current.component}${node.key ? `, ${localVars.key}` : `` } );
|
|
|
|
|
${localVars.iterations}[${i}] = ${localVars.lookup}[ ${localVars.key} ] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${block.component}${node.key ? `, ${localVars.key}` : `` } );
|
|
|
|
|
` );
|
|
|
|
|
} else {
|
|
|
|
|
initialRender.addLine(
|
|
|
|
|
`${localVars.iterations}[${i}] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${generator.current.component} );`
|
|
|
|
|
`${localVars.iterations}[${i}] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${block.component} );`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -58,29 +55,29 @@ export default {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generator.current.builders.init.addBlock( deindent`
|
|
|
|
|
block.builders.create.addBlock( deindent`
|
|
|
|
|
for ( var ${i} = 0; ${i} < ${listName}.length; ${i} += 1 ) {
|
|
|
|
|
${initialRender}
|
|
|
|
|
}
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
if ( node.else ) {
|
|
|
|
|
generator.current.builders.init.addBlock( deindent`
|
|
|
|
|
block.builders.create.addBlock( deindent`
|
|
|
|
|
if ( !${listName}.length ) {
|
|
|
|
|
${elseName} = ${renderElse}( ${params}, ${generator.current.component} );
|
|
|
|
|
${elseName} = ${renderElse}( ${params}, ${block.component} );
|
|
|
|
|
${!isToplevel ? `${elseName}.mount( ${anchor}.parentNode, ${anchor} );` : ''}
|
|
|
|
|
}
|
|
|
|
|
` );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isToplevel ) {
|
|
|
|
|
generator.current.builders.mount.addBlock( deindent`
|
|
|
|
|
block.builders.mount.addBlock( deindent`
|
|
|
|
|
for ( var ${i} = 0; ${i} < ${localVars.iterations}.length; ${i} += 1 ) {
|
|
|
|
|
${localVars.iterations}[${i}].mount( ${anchor}.parentNode, ${anchor} );
|
|
|
|
|
}
|
|
|
|
|
` );
|
|
|
|
|
if ( node.else ) {
|
|
|
|
|
generator.current.builders.mount.addBlock( deindent`
|
|
|
|
|
block.builders.mount.addBlock( deindent`
|
|
|
|
|
if ( ${elseName} ) {
|
|
|
|
|
${elseName}.mount( ${anchor}.parentNode, ${anchor} );
|
|
|
|
|
}
|
|
|
|
@ -89,7 +86,7 @@ export default {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( node.key ) {
|
|
|
|
|
generator.current.builders.update.addBlock( deindent`
|
|
|
|
|
block.builders.update.addBlock( deindent`
|
|
|
|
|
var ${listName} = ${snippet};
|
|
|
|
|
var ${localVars._iterations} = [];
|
|
|
|
|
var ${localVars._lookup} = Object.create( null );
|
|
|
|
@ -105,7 +102,7 @@ export default {
|
|
|
|
|
${localVars._iterations}[${i}] = ${localVars._lookup}[ ${localVars.key} ] = ${localVars.lookup}[ ${localVars.key} ];
|
|
|
|
|
${localVars._lookup}[ ${localVars.key} ].update( changed, ${params}, ${listName}, ${listName}[${i}], ${i} );
|
|
|
|
|
} else {
|
|
|
|
|
${localVars._iterations}[${i}] = ${localVars._lookup}[ ${localVars.key} ] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${generator.current.component}${node.key ? `, ${localVars.key}` : `` } );
|
|
|
|
|
${localVars._iterations}[${i}] = ${localVars._lookup}[ ${localVars.key} ] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${block.component}${node.key ? `, ${localVars.key}` : `` } );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
${localVars._iterations}[${i}].mount( ${localVars.fragment}, null );
|
|
|
|
@ -115,7 +112,7 @@ export default {
|
|
|
|
|
for ( var ${i} = 0; ${i} < ${localVars.iterations}.length; ${i} += 1 ) {
|
|
|
|
|
var ${localVars.iteration} = ${localVars.iterations}[${i}];
|
|
|
|
|
if ( !${localVars._lookup}[ ${localVars.iteration}.key ] ) {
|
|
|
|
|
${localVars.iteration}.teardown( true );
|
|
|
|
|
${localVars.iteration}.destroy( true );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -125,83 +122,70 @@ export default {
|
|
|
|
|
${localVars.lookup} = ${localVars._lookup};
|
|
|
|
|
` );
|
|
|
|
|
} else {
|
|
|
|
|
generator.current.builders.update.addBlock( deindent`
|
|
|
|
|
block.builders.update.addBlock( deindent`
|
|
|
|
|
var ${listName} = ${snippet};
|
|
|
|
|
|
|
|
|
|
for ( var ${i} = 0; ${i} < ${listName}.length; ${i} += 1 ) {
|
|
|
|
|
if ( !${localVars.iterations}[${i}] ) {
|
|
|
|
|
${localVars.iterations}[${i}] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${generator.current.component} );
|
|
|
|
|
${localVars.iterations}[${i}] = ${renderer}( ${params}, ${listName}, ${listName}[${i}], ${i}, ${block.component} );
|
|
|
|
|
${localVars.iterations}[${i}].mount( ${anchor}.parentNode, ${anchor} );
|
|
|
|
|
} else {
|
|
|
|
|
${localVars.iterations}[${i}].update( changed, ${params}, ${listName}, ${listName}[${i}], ${i} );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
teardownEach( ${localVars.iterations}, true, ${listName}.length );
|
|
|
|
|
destroyEach( ${localVars.iterations}, true, ${listName}.length );
|
|
|
|
|
|
|
|
|
|
${localVars.iterations}.length = ${listName}.length;
|
|
|
|
|
` );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( node.else ) {
|
|
|
|
|
generator.current.builders.update.addBlock( deindent`
|
|
|
|
|
block.builders.update.addBlock( deindent`
|
|
|
|
|
if ( !${listName}.length && ${elseName} ) {
|
|
|
|
|
${elseName}.update( changed, ${params} );
|
|
|
|
|
} else if ( !${listName}.length ) {
|
|
|
|
|
${elseName} = ${renderElse}( ${params}, ${generator.current.component} );
|
|
|
|
|
${elseName} = ${renderElse}( ${params}, ${block.component} );
|
|
|
|
|
${elseName}.mount( ${anchor}.parentNode, ${anchor} );
|
|
|
|
|
} else if ( ${elseName} ) {
|
|
|
|
|
${elseName}.teardown( true );
|
|
|
|
|
${elseName}.destroy( true );
|
|
|
|
|
}
|
|
|
|
|
` );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generator.current.builders.teardown.addBlock(
|
|
|
|
|
`${generator.helper( 'teardownEach' )}( ${localVars.iterations}, ${isToplevel ? 'detach' : 'false'} );` );
|
|
|
|
|
block.builders.destroy.addBlock(
|
|
|
|
|
`${generator.helper( 'destroyEach' )}( ${localVars.iterations}, ${isToplevel ? 'detach' : 'false'} );` );
|
|
|
|
|
|
|
|
|
|
if ( node.else ) {
|
|
|
|
|
generator.current.builders.teardown.addBlock( deindent`
|
|
|
|
|
block.builders.destroy.addBlock( deindent`
|
|
|
|
|
if ( ${elseName} ) {
|
|
|
|
|
${elseName}.teardown( ${isToplevel ? 'detach' : 'false'} );
|
|
|
|
|
${elseName}.destroy( ${isToplevel ? 'detach' : 'false'} );
|
|
|
|
|
}
|
|
|
|
|
` );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( node.else ) {
|
|
|
|
|
generator.generateBlock( node.else, renderElse, 'block' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const indexNames = new Map( generator.current.indexNames );
|
|
|
|
|
const indexName = node.index || generator.current.getUniqueName( `${node.context}_index` );
|
|
|
|
|
const indexNames = new Map( block.indexNames );
|
|
|
|
|
const indexName = node.index || block.getUniqueName( `${node.context}_index` );
|
|
|
|
|
indexNames.set( node.context, indexName );
|
|
|
|
|
|
|
|
|
|
const listNames = new Map( generator.current.listNames );
|
|
|
|
|
const listNames = new Map( block.listNames );
|
|
|
|
|
listNames.set( node.context, listName );
|
|
|
|
|
|
|
|
|
|
const context = generator.getUniqueName( node.context );
|
|
|
|
|
const contexts = new Map( generator.current.contexts );
|
|
|
|
|
const contexts = new Map( block.contexts );
|
|
|
|
|
contexts.set( node.context, context );
|
|
|
|
|
|
|
|
|
|
const indexes = new Map( generator.current.indexes );
|
|
|
|
|
const indexes = new Map( block.indexes );
|
|
|
|
|
if ( node.index ) indexes.set( indexName, node.context );
|
|
|
|
|
|
|
|
|
|
const contextDependencies = new Map( generator.current.contextDependencies );
|
|
|
|
|
const contextDependencies = new Map( block.contextDependencies );
|
|
|
|
|
contextDependencies.set( node.context, dependencies );
|
|
|
|
|
|
|
|
|
|
const blockParams = generator.current.params.concat( listName, context, indexName );
|
|
|
|
|
|
|
|
|
|
const getUniqueName = generator.getUniqueNameMaker( blockParams );
|
|
|
|
|
|
|
|
|
|
generator.push({
|
|
|
|
|
type: 'block',
|
|
|
|
|
const childBlock = block.child({
|
|
|
|
|
name: renderer,
|
|
|
|
|
target: 'target',
|
|
|
|
|
expression: node.expression,
|
|
|
|
|
context: node.context,
|
|
|
|
|
key: node.key,
|
|
|
|
|
localElementDepth: 0,
|
|
|
|
|
|
|
|
|
|
component: getUniqueName( 'component' ),
|
|
|
|
|
|
|
|
|
|
contextDependencies,
|
|
|
|
|
contexts,
|
|
|
|
@ -209,15 +193,28 @@ export default {
|
|
|
|
|
|
|
|
|
|
indexNames,
|
|
|
|
|
listNames,
|
|
|
|
|
params: blockParams,
|
|
|
|
|
params: block.params.concat( listName, context, indexName )
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builders: getBuilders(),
|
|
|
|
|
getUniqueName,
|
|
|
|
|
const childState = Object.assign( {}, state, {
|
|
|
|
|
parentNode: null
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
leave ( generator ) {
|
|
|
|
|
generator.addRenderer( generator.current );
|
|
|
|
|
generator.pop();
|
|
|
|
|
node.children.forEach( child => {
|
|
|
|
|
visit( generator, childBlock, childState, child );
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
generator.addBlock( childBlock );
|
|
|
|
|
|
|
|
|
|
if ( node.else ) {
|
|
|
|
|
const childBlock = block.child({
|
|
|
|
|
name: renderElse
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
node.else.children.forEach( child => {
|
|
|
|
|
visit( generator, childBlock, childState, child );
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
generator.addBlock( childBlock );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|