|
|
@ -1,332 +1,274 @@
|
|
|
|
import { getLocator } from 'locate-character';
|
|
|
|
import { getLocator } from 'locate-character';
|
|
|
|
import deindent from './utils/deindent.js';
|
|
|
|
import deindent from './utils/deindent.js';
|
|
|
|
import walkHtml from './utils/walkHtml.js';
|
|
|
|
import walkHtml from './utils/walkHtml.js';
|
|
|
|
|
|
|
|
import flattenReference from './utils/flattenReference.js';
|
|
|
|
|
|
|
|
|
|
|
|
const ROOT = 'options.target';
|
|
|
|
function createRenderer ( fragment ) {
|
|
|
|
|
|
|
|
return deindent`
|
|
|
|
|
|
|
|
function ${fragment.name} ( target${fragment.useAnchor ? ', anchor' : ''} ) {
|
|
|
|
|
|
|
|
${fragment.initStatements.join( '\n\n' )}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
update: function ( ${fragment.contextChain.join( ', ' )} ) {
|
|
|
|
|
|
|
|
${fragment.updateStatements.join( '\n\n' )}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
teardown: function () {
|
|
|
|
|
|
|
|
${fragment.teardownStatements.join( '\n\n' )}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default function generate ( parsed, template ) {
|
|
|
|
export default function generate ( parsed, template ) {
|
|
|
|
|
|
|
|
const locator = getLocator( template );
|
|
|
|
|
|
|
|
const renderers = [];
|
|
|
|
|
|
|
|
|
|
|
|
const counters = {
|
|
|
|
const counters = {
|
|
|
|
fragment: 0,
|
|
|
|
|
|
|
|
element: 0,
|
|
|
|
|
|
|
|
text: 0,
|
|
|
|
|
|
|
|
anchor: 0,
|
|
|
|
|
|
|
|
if: 0,
|
|
|
|
if: 0,
|
|
|
|
each: 0,
|
|
|
|
each: 0
|
|
|
|
loop: 0
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const initStatements = [];
|
|
|
|
|
|
|
|
const setStatements = [ deindent`
|
|
|
|
|
|
|
|
const oldState = state;
|
|
|
|
|
|
|
|
state = Object.assign( {}, oldState, newState );
|
|
|
|
|
|
|
|
` ];
|
|
|
|
|
|
|
|
const teardownStatements = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO add contents of <script> tag, with `export default` replaced with `var template =`
|
|
|
|
// TODO add contents of <script> tag, with `export default` replaced with `var template =`
|
|
|
|
// TODO css
|
|
|
|
// TODO css
|
|
|
|
|
|
|
|
|
|
|
|
const locator = getLocator( template );
|
|
|
|
let current = {
|
|
|
|
|
|
|
|
useAnchor: false,
|
|
|
|
|
|
|
|
name: 'renderMainFragment',
|
|
|
|
|
|
|
|
target: 'target',
|
|
|
|
|
|
|
|
|
|
|
|
parsed.html.children.forEach( child => {
|
|
|
|
initStatements: [],
|
|
|
|
const declarations = [];
|
|
|
|
updateStatements: [],
|
|
|
|
|
|
|
|
teardownStatements: [],
|
|
|
|
|
|
|
|
|
|
|
|
let current = {
|
|
|
|
contexts: {},
|
|
|
|
target: ROOT,
|
|
|
|
contextChain: [ 'context' ],
|
|
|
|
conditions: [],
|
|
|
|
|
|
|
|
children: [],
|
|
|
|
|
|
|
|
renderBlocks: [],
|
|
|
|
|
|
|
|
removeBlocks: [],
|
|
|
|
|
|
|
|
anchor: null,
|
|
|
|
|
|
|
|
renderImmediately: true
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const stack = [ current ];
|
|
|
|
counters: {
|
|
|
|
|
|
|
|
element: 0,
|
|
|
|
|
|
|
|
text: 0,
|
|
|
|
|
|
|
|
anchor: 0
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parent: null
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function flattenExpression ( node, contexts ) {
|
|
|
|
|
|
|
|
const flattened = flattenReference( node );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( flattened ) {
|
|
|
|
|
|
|
|
if ( flattened.name in contexts ) return flattened.keypath;
|
|
|
|
|
|
|
|
// TODO handle globals, e.g. {{Math.round(foo)}}
|
|
|
|
|
|
|
|
return `context.${flattened.keypath}`;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error( 'TODO expressions' );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parsed.html.children.forEach( child => {
|
|
|
|
walkHtml( child, {
|
|
|
|
walkHtml( child, {
|
|
|
|
Element: {
|
|
|
|
Element: {
|
|
|
|
enter ( node ) {
|
|
|
|
enter ( node ) {
|
|
|
|
const target = `element_${counters.element++}`;
|
|
|
|
const name = `element_${current.counters.element++}`;
|
|
|
|
|
|
|
|
|
|
|
|
stack.push( current );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
declarations.push( `var ${target};` );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( current.renderImmediately ) {
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
var ${name} = document.createElement( '${node.name}' );
|
|
|
|
${target} = document.createElement( '${node.name}' );
|
|
|
|
` );
|
|
|
|
${current.target}.appendChild( ${target} );
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
|
|
|
|
${target} = document.createElement( '${node.name}' );
|
|
|
|
|
|
|
|
${current.anchor}.parentNode.insertBefore( ${target}, ${current.anchor} );
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current.removeBlocks.push( deindent`
|
|
|
|
current.teardownStatements.push( deindent`
|
|
|
|
${target}.parentNode.removeChild( ${target} );
|
|
|
|
${name}.parentNode.removeChild( ${name} );
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
current = {
|
|
|
|
current = Object.assign( {}, current, {
|
|
|
|
target,
|
|
|
|
target: name,
|
|
|
|
conditions: current.conditions,
|
|
|
|
parent: current
|
|
|
|
children: current.children,
|
|
|
|
});
|
|
|
|
renderBlocks: current.renderBlocks,
|
|
|
|
|
|
|
|
removeBlocks: current.removeBlocks,
|
|
|
|
|
|
|
|
anchor: current.anchor,
|
|
|
|
|
|
|
|
renderImmediately: false
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
leave () {
|
|
|
|
leave () {
|
|
|
|
stack.pop();
|
|
|
|
const name = current.target;
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Text: {
|
|
|
|
current = current.parent;
|
|
|
|
enter ( node ) {
|
|
|
|
|
|
|
|
if ( current.target === ROOT ) {
|
|
|
|
|
|
|
|
const identifier = `text_${counters.text++}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
declarations.push( `var ${identifier};` );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
if ( current.useAnchor && current.target === 'target' ) {
|
|
|
|
${identifier} = document.createTextNode( ${JSON.stringify( node.data )} );
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
${current.target}.appendChild( ${identifier} );
|
|
|
|
target.insertBefore( ${name}, anchor );
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
current.removeBlocks.push( deindent`
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
${identifier}.parentNode.removeChild( ${identifier} );
|
|
|
|
${current.target}.appendChild( ${name} );
|
|
|
|
${identifier} = null;
|
|
|
|
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
Text: {
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
enter ( node ) {
|
|
|
|
${current.target}.appendChild( document.createTextNode( ${JSON.stringify( node.data )} ) );
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
` );
|
|
|
|
${current.target}.appendChild( document.createTextNode( ${JSON.stringify( node.data ) }) );
|
|
|
|
}
|
|
|
|
` );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
MustacheTag: {
|
|
|
|
MustacheTag: {
|
|
|
|
enter ( node ) {
|
|
|
|
enter ( node ) {
|
|
|
|
const identifier = `text_${counters.text++}`;
|
|
|
|
const name = `text_${current.counters.text++}`;
|
|
|
|
const expression = node.expression.type === 'Identifier' ? node.expression.name : 'TODO'; // TODO handle block-local state
|
|
|
|
const expression = flattenExpression( node.expression, current.contexts );
|
|
|
|
|
|
|
|
|
|
|
|
declarations.push( `var ${identifier};` );
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
|
|
|
|
var ${name} = document.createTextNode( '' );
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
var ${name}_value = '';
|
|
|
|
${identifier} = document.createTextNode( '' );
|
|
|
|
${current.target}.appendChild( ${name} );
|
|
|
|
${current.target}.appendChild( ${identifier} );
|
|
|
|
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
setStatements.push( deindent`
|
|
|
|
current.updateStatements.push( deindent`
|
|
|
|
if ( state.${expression} !== oldState.${expression} ) { // TODO and conditions
|
|
|
|
if ( ${expression} !== ${name}_value ) {
|
|
|
|
${identifier}.data = state.${expression};
|
|
|
|
${name}_value = ${expression};
|
|
|
|
|
|
|
|
${name}.data = ${name}_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
current.removeBlocks.push( deindent`
|
|
|
|
|
|
|
|
${identifier}.parentNode.removeChild( ${identifier} );
|
|
|
|
|
|
|
|
${identifier} = null;
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
IfBlock: {
|
|
|
|
IfBlock: {
|
|
|
|
enter ( node ) {
|
|
|
|
enter ( node ) {
|
|
|
|
const anchor = `anchor_${counters.anchor++}`;
|
|
|
|
const i = counters.if++;
|
|
|
|
const suffix = `if_${counters.if++}`;
|
|
|
|
const name = `ifBlock_${i}`;
|
|
|
|
|
|
|
|
const renderer = `renderIfBlock_${i}`;
|
|
|
|
|
|
|
|
|
|
|
|
declarations.push( `var ${anchor};` );
|
|
|
|
const expression = flattenExpression( node.expression, current.contexts );
|
|
|
|
|
|
|
|
|
|
|
|
const expression = node.expression.type === 'Identifier' ? node.expression.name : 'TODO'; // TODO handle block-local state
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
|
|
|
|
var ${name}_anchor = document.createComment( '#if ${template.slice( node.expression.start, node.expression.end )}' );
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
${current.target}.appendChild( ${name}_anchor );
|
|
|
|
${anchor} = document.createComment( '#if ${template.slice( node.expression.start, node.expression.end)}' );
|
|
|
|
var ${name} = null;
|
|
|
|
${current.target}.appendChild( ${anchor} );
|
|
|
|
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
current.removeBlocks.push( deindent`
|
|
|
|
current.updateStatements.push( deindent`
|
|
|
|
${anchor}.parentNode.removeChild( ${anchor} );
|
|
|
|
if ( ${expression} && !${name} ) {
|
|
|
|
${anchor} = null;
|
|
|
|
${name} = ${renderer}( ${current.target}, ${name}_anchor );
|
|
|
|
` );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
current = {
|
|
|
|
else if ( !${expression} && ${name} ) {
|
|
|
|
renderName: `render_${suffix}`,
|
|
|
|
${name}.teardown();
|
|
|
|
removeName: `remove_${suffix}`,
|
|
|
|
${name} = null;
|
|
|
|
target: current.target,
|
|
|
|
}
|
|
|
|
conditions: current.conditions.concat( expression ),
|
|
|
|
|
|
|
|
renderBlocks: [],
|
|
|
|
|
|
|
|
removeBlocks: [],
|
|
|
|
|
|
|
|
anchor,
|
|
|
|
|
|
|
|
renderImmediately: false
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setStatements.push( deindent`
|
|
|
|
if ( ${name} ) {
|
|
|
|
// TODO account for conditions (nested ifs)
|
|
|
|
${name}.update( context );
|
|
|
|
if ( state.${expression} && !oldState.${expression} ) ${current.renderName}();
|
|
|
|
}
|
|
|
|
else if ( !state.${expression} && oldState.${expression} ) ${current.removeName}();
|
|
|
|
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
teardownStatements.push( deindent`
|
|
|
|
current.teardownStatements.push( deindent`
|
|
|
|
// TODO account for conditions (nested ifs)
|
|
|
|
if ( ${name} ) ${name}.teardown();
|
|
|
|
if ( state.${expression} ) ${current.removeName}();
|
|
|
|
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
stack.push( current );
|
|
|
|
current = {
|
|
|
|
},
|
|
|
|
useAnchor: true,
|
|
|
|
|
|
|
|
name: renderer,
|
|
|
|
|
|
|
|
target: 'target',
|
|
|
|
|
|
|
|
|
|
|
|
leave ( node ) {
|
|
|
|
contextChain: current.contextChain,
|
|
|
|
const { line, column } = locator( node.start );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initStatements.push( deindent`
|
|
|
|
initStatements: [],
|
|
|
|
// (${line}:${column}) {{#if ${template.slice( node.expression.start, node.expression.end )}}}...{{/if}}
|
|
|
|
updateStatements: [],
|
|
|
|
function ${current.renderName} () {
|
|
|
|
teardownStatements: [],
|
|
|
|
${current.renderBlocks.join( '\n\n' )}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function ${current.removeName} () {
|
|
|
|
counters: {
|
|
|
|
${current.removeBlocks.join( '\n\n' )}
|
|
|
|
element: 0,
|
|
|
|
}
|
|
|
|
text: 0,
|
|
|
|
` );
|
|
|
|
anchor: 0
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parent: current
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
stack.pop();
|
|
|
|
leave () {
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
renderers.push( createRenderer( current ) );
|
|
|
|
|
|
|
|
current = current.parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
EachBlock: {
|
|
|
|
EachBlock: {
|
|
|
|
enter ( node ) {
|
|
|
|
enter ( node ) {
|
|
|
|
const loopIndex = counters.loop++;
|
|
|
|
const i = counters.each++;
|
|
|
|
|
|
|
|
const name = `eachBlock_${i}`;
|
|
|
|
const anchor = `anchor_${counters.anchor++}`;
|
|
|
|
const renderer = `renderEachBlock_${i}`;
|
|
|
|
|
|
|
|
|
|
|
|
declarations.push( `var fragment_${loopIndex} = document.createDocumentFragment();` );
|
|
|
|
|
|
|
|
declarations.push( `var ${anchor};` );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const expression = node.expression.type === 'Identifier' ? node.expression.name : 'TODO'; // TODO handle block-local state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current.renderBlocks.push( deindent`
|
|
|
|
|
|
|
|
${anchor} = document.createComment( '#each ${template.slice( node.expression.start, node.expression.end)}' );
|
|
|
|
|
|
|
|
${current.target}.appendChild( ${anchor} );
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current.removeBlocks.push( deindent`
|
|
|
|
const expression = flattenExpression( node.expression, current.contexts );
|
|
|
|
${anchor}.parentNode.removeChild( ${anchor} );
|
|
|
|
|
|
|
|
${anchor} = null;
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current = {
|
|
|
|
|
|
|
|
target: `fragment_${loopIndex}`,
|
|
|
|
|
|
|
|
expression,
|
|
|
|
|
|
|
|
conditions: current.conditions,
|
|
|
|
|
|
|
|
renderBlocks: [],
|
|
|
|
|
|
|
|
removeBlocks: [],
|
|
|
|
|
|
|
|
anchor,
|
|
|
|
|
|
|
|
loopIndex,
|
|
|
|
|
|
|
|
renderImmediately: true
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setStatements.push( deindent`
|
|
|
|
current.initStatements.push( deindent`
|
|
|
|
// TODO account for conditions (nested ifs)
|
|
|
|
var ${name}_anchor = document.createComment( '#each ${template.slice( node.expression.start, node.expression.end )}' );
|
|
|
|
if ( '${expression}' in state ) each_${loopIndex}.update();
|
|
|
|
${current.target}.appendChild( ${name}_anchor );
|
|
|
|
|
|
|
|
var ${name}_iterations = [];
|
|
|
|
|
|
|
|
const ${name}_fragment = document.createDocumentFragment();
|
|
|
|
` );
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
// need to add teardown logic if this is at the
|
|
|
|
current.updateStatements.push( deindent`
|
|
|
|
// top level (TODO or if there are event handlers attached?)
|
|
|
|
for ( var i = 0; i < ${expression}.length; i += 1 ) {
|
|
|
|
if ( current.target === ROOT ) {
|
|
|
|
if ( !${name}_iterations[i] ) {
|
|
|
|
teardownStatements.push( deindent`
|
|
|
|
${name}_iterations[i] = ${renderer}( ${name}_fragment );
|
|
|
|
if ( true ) { // <!-- TODO conditions
|
|
|
|
|
|
|
|
for ( let i = 0; i < state.${expression}.length; i += 1 ) {
|
|
|
|
|
|
|
|
each_${loopIndex}.removeIteration( i );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
` );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stack.push( current );
|
|
|
|
const iteration = ${name}_iterations[i];
|
|
|
|
},
|
|
|
|
${name}_iterations[i].update( ${current.contextChain.join( ', ' )}, ${expression}[i] );
|
|
|
|
|
|
|
|
}
|
|
|
|
leave ( node ) {
|
|
|
|
|
|
|
|
const { line, column } = locator( node.start );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const loopIndex = current.loopIndex;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initStatements.push( deindent`
|
|
|
|
|
|
|
|
// (${line}:${column}) {{#each ${template.slice( node.expression.start, node.expression.end )}}}...{{/each}}
|
|
|
|
|
|
|
|
${current.renderBlocks.join( '\n\n' )}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var each_${loopIndex} = {
|
|
|
|
|
|
|
|
iterations: [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
update: function () {
|
|
|
|
|
|
|
|
var target = document.createDocumentFragment();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var i;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for ( i = 0; i < state.${current.expression}.length; i += 1 ) {
|
|
|
|
|
|
|
|
if ( !this.iterations[i] ) {
|
|
|
|
|
|
|
|
this.iterations[i] = this.renderIteration( target );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const iteration = this.iterations[i];
|
|
|
|
for ( var i = ${expression}.length; i < ${name}_iterations.length; i += 1 ) {
|
|
|
|
this.updateIteration( this.iterations[i], state.${current.expression}[i] );
|
|
|
|
${name}_iterations[i].teardown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ( ; i < this.iterations.length; i += 1 ) {
|
|
|
|
${name}_anchor.parentNode.insertBefore( ${name}_fragment, ${name}_anchor );
|
|
|
|
this.removeIteration( i );
|
|
|
|
${name}_iterations.length = ${expression}.length;
|
|
|
|
}
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
${current.anchor}.parentNode.insertBefore( target, ${current.anchor} );
|
|
|
|
current.teardownStatements.push( deindent`
|
|
|
|
each_${loopIndex}.length = state.${current.expression}.length;
|
|
|
|
for ( let i = 0; i < ${name}_iterations.length; i += 1 ) {
|
|
|
|
},
|
|
|
|
${name}_iterations[i].teardown();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
renderIteration: function ( target ) {
|
|
|
|
const contexts = Object.assign( {}, current.contexts );
|
|
|
|
var fragment = fragment_0.cloneNode( true );
|
|
|
|
contexts[ node.context ] = true;
|
|
|
|
|
|
|
|
|
|
|
|
var element_0 = fragment.childNodes[0];
|
|
|
|
current = {
|
|
|
|
var text_0 = element_0.childNodes[0];
|
|
|
|
useAnchor: false,
|
|
|
|
|
|
|
|
name: renderer,
|
|
|
|
|
|
|
|
target: 'target',
|
|
|
|
|
|
|
|
|
|
|
|
var iteration = {
|
|
|
|
contexts,
|
|
|
|
element_0: element_0,
|
|
|
|
contextChain: current.contextChain.concat( node.context ),
|
|
|
|
text_0: text_0
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
target.appendChild( fragment );
|
|
|
|
initStatements: [],
|
|
|
|
return iteration;
|
|
|
|
updateStatements: [],
|
|
|
|
},
|
|
|
|
teardownStatements: [],
|
|
|
|
|
|
|
|
|
|
|
|
updateIteration: function ( iteration, context ) {
|
|
|
|
counters: {
|
|
|
|
iteration.text_0.data = context;
|
|
|
|
element: 0,
|
|
|
|
},
|
|
|
|
text: 0,
|
|
|
|
|
|
|
|
anchor: 0
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
removeIteration: function ( i ) {
|
|
|
|
parent: current
|
|
|
|
var iteration = this.iterations[i];
|
|
|
|
};
|
|
|
|
iteration.element_0.parentNode.removeChild( iteration.element_0 );
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
` );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
teardownStatements.push( ...current.removeBlocks );
|
|
|
|
leave () {
|
|
|
|
|
|
|
|
renderers.push( createRenderer( current ) );
|
|
|
|
|
|
|
|
|
|
|
|
stack.pop();
|
|
|
|
current = current.parent;
|
|
|
|
current = stack[ stack.length - 1 ];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
initStatements.push( ...current.renderBlocks );
|
|
|
|
|
|
|
|
initStatements.unshift( declarations.join( '\n' ) );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
teardownStatements.push( ...current.removeBlocks );
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
teardownStatements.push( 'state = {};' );
|
|
|
|
renderers.push( createRenderer( current ) );
|
|
|
|
|
|
|
|
|
|
|
|
const code = deindent`
|
|
|
|
const code = deindent`
|
|
|
|
|
|
|
|
${renderers.reverse().join( '\n\n' )}
|
|
|
|
|
|
|
|
|
|
|
|
export default function createComponent ( options ) {
|
|
|
|
export default function createComponent ( options ) {
|
|
|
|
var component = {};
|
|
|
|
var component = {};
|
|
|
|
var state = {};
|
|
|
|
var state = {};
|
|
|
@ -373,16 +315,18 @@ export default function generate ( parsed, template ) {
|
|
|
|
|
|
|
|
|
|
|
|
// component-specific methods
|
|
|
|
// component-specific methods
|
|
|
|
component.set = function set ( newState ) {
|
|
|
|
component.set = function set ( newState ) {
|
|
|
|
${setStatements.join( '\n\n' )}
|
|
|
|
Object.assign( state, newState );
|
|
|
|
|
|
|
|
mainFragment.update( state );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
component.teardown = function teardown () {
|
|
|
|
component.teardown = function teardown () {
|
|
|
|
${teardownStatements.join( '\n\n' )}
|
|
|
|
mainFragment.teardown();
|
|
|
|
};
|
|
|
|
mainFragment = null;
|
|
|
|
|
|
|
|
|
|
|
|
// initialisation
|
|
|
|
state = {};
|
|
|
|
${initStatements.join( '\n\n' )}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mainFragment = renderMainFragment( options.target );
|
|
|
|
component.set( options.data );
|
|
|
|
component.set( options.data );
|
|
|
|
|
|
|
|
|
|
|
|
return component;
|
|
|
|
return component;
|
|
|
|