|
|
|
@ -29,60 +29,61 @@ export default class EachBlock extends Node {
|
|
|
|
|
|
|
|
|
|
this.var = block.getUniqueName(`each`);
|
|
|
|
|
this.iterations = block.getUniqueName(`${this.var}_blocks`);
|
|
|
|
|
this.each_context = block.getUniqueName(`${this.var}_context`);
|
|
|
|
|
|
|
|
|
|
const { dependencies } = this.metadata;
|
|
|
|
|
block.addDependencies(dependencies);
|
|
|
|
|
|
|
|
|
|
const indexNames = new Map(block.indexNames);
|
|
|
|
|
const indexName =
|
|
|
|
|
this.index || block.getUniqueName(`${this.context}_index`);
|
|
|
|
|
indexNames.set(this.context, indexName);
|
|
|
|
|
this.block = block.child({
|
|
|
|
|
comment: createDebuggingComment(this, this.generator),
|
|
|
|
|
name: this.generator.getUniqueName('create_each_block'),
|
|
|
|
|
context: this.context,
|
|
|
|
|
key: this.key,
|
|
|
|
|
|
|
|
|
|
const listNames = new Map(block.listNames);
|
|
|
|
|
const listName = block.getUniqueName(
|
|
|
|
|
(this.expression.type === 'MemberExpression' && !this.expression.computed) ? this.expression.property.name :
|
|
|
|
|
this.expression.type === 'Identifier' ? this.expression.name :
|
|
|
|
|
`each_value`
|
|
|
|
|
);
|
|
|
|
|
listNames.set(this.context, listName);
|
|
|
|
|
contexts: new Map(block.contexts),
|
|
|
|
|
contextTypes: new Map(block.contextTypes),
|
|
|
|
|
indexes: new Map(block.indexes),
|
|
|
|
|
changeableIndexes: new Map(block.changeableIndexes),
|
|
|
|
|
|
|
|
|
|
const contextTypes = new Map(block.contextTypes);
|
|
|
|
|
contextTypes.set(this.context, 'each');
|
|
|
|
|
listName: (
|
|
|
|
|
(this.expression.type === 'MemberExpression' && !this.expression.computed) ? this.expression.property.name :
|
|
|
|
|
this.expression.type === 'Identifier' ? this.expression.name :
|
|
|
|
|
`each_value`
|
|
|
|
|
),
|
|
|
|
|
indexName: this.index || `${this.context}_index`,
|
|
|
|
|
|
|
|
|
|
const context = block.getUniqueName(this.context);
|
|
|
|
|
const contexts = new Map(block.contexts);
|
|
|
|
|
contexts.set(this.context, context);
|
|
|
|
|
indexNames: new Map(block.indexNames),
|
|
|
|
|
listNames: new Map(block.listNames)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const indexes = new Map(block.indexes);
|
|
|
|
|
if (this.index) indexes.set(this.index, this.context);
|
|
|
|
|
this.block.contextTypes.set(this.context, 'each');
|
|
|
|
|
this.block.indexNames.set(this.context, this.block.indexName);
|
|
|
|
|
this.block.listNames.set(this.context, this.block.listName);
|
|
|
|
|
if (this.index) {
|
|
|
|
|
this.block.indexes.set(this.index, this.context);
|
|
|
|
|
this.block.changeableIndexes.set(this.index, this.key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const changeableIndexes = new Map(block.changeableIndexes);
|
|
|
|
|
if (this.index) changeableIndexes.set(this.index, this.key);
|
|
|
|
|
const context = this.block.getUniqueName(this.context);
|
|
|
|
|
this.block.contexts.set(this.context, context); // TODO this is now redundant?
|
|
|
|
|
|
|
|
|
|
if (this.destructuredContexts) {
|
|
|
|
|
for (let i = 0; i < this.destructuredContexts.length; i += 1) {
|
|
|
|
|
contexts.set(this.destructuredContexts[i], `${context}[${i}]`);
|
|
|
|
|
const context = this.block.getUniqueName(this.destructuredContexts[i]);
|
|
|
|
|
this.block.contexts.set(this.destructuredContexts[i], context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.block = block.child({
|
|
|
|
|
comment: createDebuggingComment(this, this.generator),
|
|
|
|
|
name: this.generator.getUniqueName('create_each_block'),
|
|
|
|
|
context: this.context,
|
|
|
|
|
key: this.key,
|
|
|
|
|
|
|
|
|
|
contexts,
|
|
|
|
|
contextTypes,
|
|
|
|
|
indexes,
|
|
|
|
|
changeableIndexes,
|
|
|
|
|
|
|
|
|
|
listName,
|
|
|
|
|
indexName,
|
|
|
|
|
this.contextProps = [
|
|
|
|
|
`${this.context}: ${this.block.listName}[#i]`,
|
|
|
|
|
`${this.block.indexName}: #i`
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
indexNames,
|
|
|
|
|
listNames,
|
|
|
|
|
params: block.params.concat(listName, context, indexName),
|
|
|
|
|
});
|
|
|
|
|
if (this.destructuredContexts) {
|
|
|
|
|
for (let i = 0; i < this.destructuredContexts.length; i += 1) {
|
|
|
|
|
this.contextProps.push(`${this.destructuredContexts[i]}: ${this.block.listName}[#i][${i}]`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.generator.blocks.push(this.block);
|
|
|
|
|
this.initChildren(this.block, stripWhitespace, nextSibling);
|
|
|
|
@ -117,7 +118,6 @@ export default class EachBlock extends Node {
|
|
|
|
|
const create_each_block = this.block.name;
|
|
|
|
|
const each_block_value = this.block.listName;
|
|
|
|
|
const iterations = this.iterations;
|
|
|
|
|
const params = block.params.join(', ');
|
|
|
|
|
|
|
|
|
|
const needsAnchor = this.next ? !this.next.isDomNode() : !parentNode || !this.parent.isDomNode();
|
|
|
|
|
const anchor = needsAnchor
|
|
|
|
@ -138,7 +138,6 @@ export default class EachBlock extends Node {
|
|
|
|
|
each_block_value,
|
|
|
|
|
length,
|
|
|
|
|
iterations,
|
|
|
|
|
params,
|
|
|
|
|
anchor,
|
|
|
|
|
mountOrIntro,
|
|
|
|
|
};
|
|
|
|
@ -171,7 +170,7 @@ export default class EachBlock extends Node {
|
|
|
|
|
// TODO neaten this up... will end up with an empty line in the block
|
|
|
|
|
block.builders.init.addBlock(deindent`
|
|
|
|
|
if (!${each_block_value}.${length}) {
|
|
|
|
|
${each_block_else} = ${this.else.block.name}(${params}, #component);
|
|
|
|
|
${each_block_else} = ${this.else.block.name}(#component, state);
|
|
|
|
|
${each_block_else}.c();
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
@ -187,9 +186,9 @@ export default class EachBlock extends Node {
|
|
|
|
|
if (this.else.block.hasUpdateMethod) {
|
|
|
|
|
block.builders.update.addBlock(deindent`
|
|
|
|
|
if (!${each_block_value}.${length} && ${each_block_else}) {
|
|
|
|
|
${each_block_else}.p( changed, ${params} );
|
|
|
|
|
${each_block_else}.p(changed, state);
|
|
|
|
|
} else if (!${each_block_value}.${length}) {
|
|
|
|
|
${each_block_else} = ${this.else.block.name}(${params}, #component);
|
|
|
|
|
${each_block_else} = ${this.else.block.name}(#component, state);
|
|
|
|
|
${each_block_else}.c();
|
|
|
|
|
${each_block_else}.${mountOrIntro}(${initialMountNode}, ${anchor});
|
|
|
|
|
} else if (${each_block_else}) {
|
|
|
|
@ -207,7 +206,7 @@ export default class EachBlock extends Node {
|
|
|
|
|
${each_block_else} = null;
|
|
|
|
|
}
|
|
|
|
|
} else if (!${each_block_else}) {
|
|
|
|
|
${each_block_else} = ${this.else.block.name}(${params}, #component);
|
|
|
|
|
${each_block_else} = ${this.else.block.name}(#component, state);
|
|
|
|
|
${each_block_else}.c();
|
|
|
|
|
${each_block_else}.${mountOrIntro}(${initialMountNode}, ${anchor});
|
|
|
|
|
}
|
|
|
|
@ -244,7 +243,6 @@ export default class EachBlock extends Node {
|
|
|
|
|
create_each_block,
|
|
|
|
|
each_block_value,
|
|
|
|
|
length,
|
|
|
|
|
params,
|
|
|
|
|
anchor,
|
|
|
|
|
mountOrIntro,
|
|
|
|
|
}
|
|
|
|
@ -275,7 +273,9 @@ export default class EachBlock extends Node {
|
|
|
|
|
block.builders.init.addBlock(deindent`
|
|
|
|
|
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
|
|
|
|
|
var ${key} = ${each_block_value}[#i].${this.key};
|
|
|
|
|
var ${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key});
|
|
|
|
|
var ${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, @assign({}, state, {
|
|
|
|
|
${this.contextProps.join(',\n')}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (${last}) ${last}.next = ${iteration};
|
|
|
|
|
${iteration}.last = ${last};
|
|
|
|
@ -379,8 +379,12 @@ export default class EachBlock extends Node {
|
|
|
|
|
var ${key} = ${each_block_value}[#i].${this.key};
|
|
|
|
|
var ${iteration} = ${lookup}[${key}];
|
|
|
|
|
|
|
|
|
|
var ${this.each_context} = @assign({}, state, {
|
|
|
|
|
${this.contextProps.join(',\n')}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
${dynamic &&
|
|
|
|
|
`if (${iteration}) ${iteration}.p(changed, ${params}, ${each_block_value}, ${each_block_value}[#i], #i);`}
|
|
|
|
|
`if (${iteration}) ${iteration}.p(changed, ${this.each_context});`}
|
|
|
|
|
|
|
|
|
|
if (${expected}) {
|
|
|
|
|
if (${key} === ${expected}.key) {
|
|
|
|
@ -401,7 +405,7 @@ export default class EachBlock extends Node {
|
|
|
|
|
if (!${expected}) ${iteration}.m(${updateMountNode}, ${anchor});
|
|
|
|
|
} else {
|
|
|
|
|
// key is being inserted
|
|
|
|
|
${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key});
|
|
|
|
|
${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context});
|
|
|
|
|
${iteration}.c();
|
|
|
|
|
${iteration}.${mountOrIntro}(${updateMountNode}, ${expected}.first);
|
|
|
|
|
|
|
|
|
@ -416,7 +420,7 @@ export default class EachBlock extends Node {
|
|
|
|
|
${iteration}.next = null;
|
|
|
|
|
${iteration}.m(${updateMountNode}, ${anchor});
|
|
|
|
|
} else {
|
|
|
|
|
${iteration} = ${lookup}[${key}] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component, ${key});
|
|
|
|
|
${iteration} = ${lookup}[${key}] = ${create_each_block}(#component, ${key}, ${this.each_context});
|
|
|
|
|
${iteration}.c();
|
|
|
|
|
${iteration}.${mountOrIntro}(${updateMountNode}, ${anchor});
|
|
|
|
|
}
|
|
|
|
@ -464,7 +468,6 @@ export default class EachBlock extends Node {
|
|
|
|
|
each_block_value,
|
|
|
|
|
length,
|
|
|
|
|
iterations,
|
|
|
|
|
params,
|
|
|
|
|
anchor,
|
|
|
|
|
mountOrIntro,
|
|
|
|
|
}
|
|
|
|
@ -473,7 +476,9 @@ export default class EachBlock extends Node {
|
|
|
|
|
var ${iterations} = [];
|
|
|
|
|
|
|
|
|
|
for (var #i = 0; #i < ${each_block_value}.${length}; #i += 1) {
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(#component, @assign({}, state, {
|
|
|
|
|
${this.contextProps.join(',\n')}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
@ -517,24 +522,24 @@ export default class EachBlock extends Node {
|
|
|
|
|
? this.block.hasIntroMethod
|
|
|
|
|
? deindent`
|
|
|
|
|
if (${iterations}[#i]) {
|
|
|
|
|
${iterations}[#i].p(changed, ${params}, ${each_block_value}, ${each_block_value}[#i], #i);
|
|
|
|
|
${iterations}[#i].p(changed, ${this.each_context});
|
|
|
|
|
} else {
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(#component, ${this.each_context});
|
|
|
|
|
${iterations}[#i].c();
|
|
|
|
|
}
|
|
|
|
|
${iterations}[#i].i(${updateMountNode}, ${anchor});
|
|
|
|
|
`
|
|
|
|
|
: deindent`
|
|
|
|
|
if (${iterations}[#i]) {
|
|
|
|
|
${iterations}[#i].p(changed, ${params}, ${each_block_value}, ${each_block_value}[#i], #i);
|
|
|
|
|
${iterations}[#i].p(changed, ${this.each_context});
|
|
|
|
|
} else {
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(#component, ${this.each_context});
|
|
|
|
|
${iterations}[#i].c();
|
|
|
|
|
${iterations}[#i].m(${updateMountNode}, ${anchor});
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
: deindent`
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(${params}, ${each_block_value}, ${each_block_value}[#i], #i, #component);
|
|
|
|
|
${iterations}[#i] = ${create_each_block}(#component, ${this.each_context});
|
|
|
|
|
${iterations}[#i].c();
|
|
|
|
|
${iterations}[#i].${mountOrIntro}(${updateMountNode}, ${anchor});
|
|
|
|
|
`;
|
|
|
|
@ -569,6 +574,10 @@ export default class EachBlock extends Node {
|
|
|
|
|
|
|
|
|
|
if (${condition}) {
|
|
|
|
|
for (var #i = ${start}; #i < ${each_block_value}.${length}; #i += 1) {
|
|
|
|
|
var ${this.each_context} = @assign({}, state, {
|
|
|
|
|
${this.contextProps.join(',\n')}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
${forLoopBody}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|