|
|
|
@ -340,6 +340,32 @@ export default class IfBlockWrapper extends Wrapper {
|
|
|
|
|
block.add_variable(current_block_type_index);
|
|
|
|
|
block.add_variable(name);
|
|
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/indent,indent */
|
|
|
|
|
if (this.needs_update) {
|
|
|
|
|
block.builders.init.add_block(deindent`
|
|
|
|
|
function ${select_block_type}(changed, ctx) {
|
|
|
|
|
${this.branches.map(({ dependencies, condition, snippet, block }) => condition
|
|
|
|
|
? deindent`
|
|
|
|
|
${snippet && (
|
|
|
|
|
dependencies.length > 0
|
|
|
|
|
? `if ((${condition} == null) || ${dependencies.map(n => `changed.${n}`).join(' || ')}) ${condition} = !!(${snippet})`
|
|
|
|
|
: `if (${condition} == null) ${condition} = !!(${snippet})`
|
|
|
|
|
)}
|
|
|
|
|
if (${condition}) return ${block.name};`
|
|
|
|
|
: `return ${block.name};`)}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
} else {
|
|
|
|
|
block.builders.init.add_block(deindent`
|
|
|
|
|
function ${select_block_type}(changed, ctx) {
|
|
|
|
|
${this.branches.map(({ condition, snippet, block }) => condition
|
|
|
|
|
? `if (${snippet}) return ${block.name};`
|
|
|
|
|
: `return ${block.name};`)}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
/* eslint-enable @typescript-eslint/indent,indent */
|
|
|
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/indent,indent */
|
|
|
|
|
block.builders.init.add_block(deindent`
|
|
|
|
|
var ${if_block_creators} = [
|
|
|
|
@ -348,14 +374,25 @@ export default class IfBlockWrapper extends Wrapper {
|
|
|
|
|
|
|
|
|
|
var ${if_blocks} = [];
|
|
|
|
|
|
|
|
|
|
function ${select_block_type}(changed, ctx) {
|
|
|
|
|
${this.branches.map(({ dependencies, condition, snippet }, i) => condition
|
|
|
|
|
${this.needs_update
|
|
|
|
|
? deindent`
|
|
|
|
|
${snippet && `if ((${condition} == null) || ${dependencies.map(n => `changed.${n}`).join(' || ')}) ${condition} = !!(${snippet})`}
|
|
|
|
|
if (${condition}) return ${String(i)};`
|
|
|
|
|
: `return ${i};`)}
|
|
|
|
|
${!has_else && `return -1;`}
|
|
|
|
|
}
|
|
|
|
|
function ${select_block_type}(changed, ctx) {
|
|
|
|
|
${this.branches.map(({ dependencies, condition, snippet }, i) => condition
|
|
|
|
|
? deindent`
|
|
|
|
|
${snippet && `if ((${condition} == null) || ${dependencies.map(n => `changed.${n}`).join(' || ')}) ${condition} = !!(${snippet})`}
|
|
|
|
|
if (${condition}) return ${String(i)};`
|
|
|
|
|
: `return ${i};`)}
|
|
|
|
|
${!has_else && `return -1;`}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
: deindent`
|
|
|
|
|
function ${select_block_type}(changed, ctx) {
|
|
|
|
|
${this.branches.map(({ condition, snippet }, i) => condition
|
|
|
|
|
? `if (${snippet}) return ${String(i)};`
|
|
|
|
|
: `return ${i};`)}
|
|
|
|
|
${!has_else && `return -1;`}
|
|
|
|
|
}
|
|
|
|
|
`}
|
|
|
|
|
`);
|
|
|
|
|
/* eslint-enable @typescript-eslint/indent,indent */
|
|
|
|
|
|
|
|
|
@ -379,62 +416,66 @@ export default class IfBlockWrapper extends Wrapper {
|
|
|
|
|
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].m(${initial_mount_node}, ${anchor_node});`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const update_mount_node = this.get_update_mount_node(anchor);
|
|
|
|
|
|
|
|
|
|
const destroy_old_block = deindent`
|
|
|
|
|
@group_outros();
|
|
|
|
|
@transition_out(${if_blocks}[${previous_block_index}], 1, 1, () => {
|
|
|
|
|
${if_blocks}[${previous_block_index}] = null;
|
|
|
|
|
});
|
|
|
|
|
@check_outros();
|
|
|
|
|
`;
|
|
|
|
|
if (this.needs_update) {
|
|
|
|
|
const update_mount_node = this.get_update_mount_node(anchor);
|
|
|
|
|
|
|
|
|
|
const create_new_block = deindent`
|
|
|
|
|
${name} = ${if_blocks}[${current_block_type_index}];
|
|
|
|
|
if (!${name}) {
|
|
|
|
|
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](ctx);
|
|
|
|
|
${name}.c();
|
|
|
|
|
}
|
|
|
|
|
${has_transitions && `@transition_in(${name}, 1);`}
|
|
|
|
|
${name}.m(${update_mount_node}, ${anchor});
|
|
|
|
|
`;
|
|
|
|
|
const destroy_old_block = deindent`
|
|
|
|
|
@group_outros();
|
|
|
|
|
@transition_out(${if_blocks}[${previous_block_index}], 1, 1, () => {
|
|
|
|
|
${if_blocks}[${previous_block_index}] = null;
|
|
|
|
|
});
|
|
|
|
|
@check_outros();
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const change_block = has_else
|
|
|
|
|
? deindent`
|
|
|
|
|
${destroy_old_block}
|
|
|
|
|
const create_new_block = deindent`
|
|
|
|
|
${name} = ${if_blocks}[${current_block_type_index}];
|
|
|
|
|
if (!${name}) {
|
|
|
|
|
${name} = ${if_blocks}[${current_block_type_index}] = ${if_block_creators}[${current_block_type_index}](ctx);
|
|
|
|
|
${name}.c();
|
|
|
|
|
}
|
|
|
|
|
${has_transitions && `@transition_in(${name}, 1);`}
|
|
|
|
|
${name}.m(${update_mount_node}, ${anchor});
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
${create_new_block}
|
|
|
|
|
`
|
|
|
|
|
: deindent`
|
|
|
|
|
if (${name}) {
|
|
|
|
|
const change_block = has_else
|
|
|
|
|
? deindent`
|
|
|
|
|
${destroy_old_block}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (~${current_block_type_index}) {
|
|
|
|
|
${create_new_block}
|
|
|
|
|
} else {
|
|
|
|
|
${name} = null;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
`
|
|
|
|
|
: deindent`
|
|
|
|
|
if (${name}) {
|
|
|
|
|
${destroy_old_block}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dynamic) {
|
|
|
|
|
block.builders.update.add_block(deindent`
|
|
|
|
|
var ${previous_block_index} = ${current_block_type_index};
|
|
|
|
|
${current_block_type_index} = ${select_block_type}(changed, ctx);
|
|
|
|
|
if (${current_block_type_index} === ${previous_block_index}) {
|
|
|
|
|
${if_current_block_type_index}${if_blocks}[${current_block_type_index}].p(changed, ctx);
|
|
|
|
|
} else {
|
|
|
|
|
${change_block}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
} else {
|
|
|
|
|
block.builders.update.add_block(deindent`
|
|
|
|
|
var ${previous_block_index} = ${current_block_type_index};
|
|
|
|
|
${current_block_type_index} = ${select_block_type}(changed, ctx);
|
|
|
|
|
if (${current_block_type_index} !== ${previous_block_index}) {
|
|
|
|
|
${change_block}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
if (~${current_block_type_index}) {
|
|
|
|
|
${create_new_block}
|
|
|
|
|
} else {
|
|
|
|
|
${name} = null;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
if (dynamic) {
|
|
|
|
|
block.builders.update.add_block(deindent`
|
|
|
|
|
var ${previous_block_index} = ${current_block_type_index};
|
|
|
|
|
${current_block_type_index} = ${select_block_type}(changed, ctx);
|
|
|
|
|
if (${current_block_type_index} === ${previous_block_index}) {
|
|
|
|
|
${if_current_block_type_index}${if_blocks}[${current_block_type_index}].p(changed, ctx);
|
|
|
|
|
} else {
|
|
|
|
|
${change_block}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
} else {
|
|
|
|
|
block.builders.update.add_block(deindent`
|
|
|
|
|
var ${previous_block_index} = ${current_block_type_index};
|
|
|
|
|
${current_block_type_index} = ${select_block_type}(changed, ctx);
|
|
|
|
|
if (${current_block_type_index} !== ${previous_block_index}) {
|
|
|
|
|
${change_block}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
}
|
|
|
|
|
} else if (dynamic) {
|
|
|
|
|
block.builders.update.add_line(`${name}.p(changed, ctx);`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block.builders.destroy.add_line(deindent`
|
|
|
|
|