move some files, tidy up a bit

pull/456/head
Rich-Harris 8 years ago
parent 57f1b64ddb
commit e960e8b5c5

@ -1,6 +1,6 @@
import deindent from '../../../utils/deindent.js';
import CodeBuilder from '../../../utils/CodeBuilder.js';
import visit from '../visit.js';
import deindent from '../../../../utils/deindent.js';
import CodeBuilder from '../../../../utils/CodeBuilder.js';
import visit from '../../visit.js';
import addComponentAttributes from './attributes/addComponentAttributes.js';
function capDown ( name ) {

@ -1,5 +1,5 @@
import addComponentBinding from './addComponentBinding.js';
import deindent from '../../../../utils/deindent.js';
import deindent from '../../../../../utils/deindent.js';
export default function addComponentAttributes ( generator, block, node, local ) {
local.staticAttributes = [];

@ -1,6 +1,6 @@
import deindent from '../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js';
import getSetter from './binding/getSetter.js';
import deindent from '../../../../../utils/deindent.js';
import flattenReference from '../../../../../utils/flattenReference.js';
import getSetter from '../../shared/binding/getSetter.js';
export default function addComponentBinding ( generator, node, attribute, block, local ) {
const { name, keypath } = flattenReference( attribute.value );

@ -1,8 +1,7 @@
import CodeBuilder from '../../../utils/CodeBuilder.js';
import deindent from '../../../utils/deindent.js';
import visit from '../visit.js';
import deindent from '../../../../utils/deindent.js';
import visit from '../../visit.js';
import addElementAttributes from './attributes/addElementAttributes.js';
import visitComponent from './Component.js';
import visitComponent from '../Component/Component.js';
import visitWindow from './meta/Window.js';
const meta = {
@ -26,15 +25,20 @@ export default function visitElement ( generator, block, state, node ) {
namespace: node.name === 'svg' ? 'http://www.w3.org/2000/svg' : state.namespace
});
const local = {
allUsedContexts: [],
block.builders.create.addLine( `var ${name} = ${getRenderStatement( generator, childState.namespace, node.name )};` );
create: new CodeBuilder(),
update: new CodeBuilder(),
destroy: new CodeBuilder()
};
if ( !state.parentNode ) {
block.builders.detach.addLine( `${generator.helper( 'detachNode' )}( ${name} );` );
}
const isToplevel = !state.parentNode;
// add CSS encapsulation attribute
if ( generator.cssId && state.isTopLevel ) {
block.builders.create.addLine( `${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );` );
}
const local = {
allUsedContexts: []
};
addElementAttributes( generator, block, childState, node, local );
@ -57,47 +61,22 @@ export default function visitElement ( generator, block, state, node ) {
return `${name}.__svelte.${listName} = ${listName};\n${name}.__svelte.${indexName} = ${indexName};`;
}).join( '\n' );
local.create.addBlock( deindent`
block.builders.create.addBlock( deindent`
${name}.__svelte = {
${initialProps}
};
` );
local.update.addBlock( updates );
}
let render;
if ( childState.namespace ) {
if ( childState.namespace === 'http://www.w3.org/2000/svg' ) {
render = `var ${name} = ${generator.helper( 'createSvgElement' )}( '${node.name}' )`;
} else {
render = `var ${name} = document.createElementNS( '${childState.namespace}', '${node.name}' );`;
}
} else {
render = `var ${name} = ${generator.helper( 'createElement' )}( '${node.name}' );`;
}
if ( generator.cssId && state.isTopLevel ) {
render += `\n${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );`;
}
local.create.addLineAtStart( render );
if ( isToplevel ) {
block.builders.detach.addLine( `${generator.helper( 'detachNode' )}( ${name} );` );
block.builders.update.addBlock( updates );
}
// special case bound <option> without a value attribute
if ( node.name === 'option' && !node.attributes.find( attribute => attribute.type === 'Attribute' && attribute.name === 'value' ) ) { // TODO check it's bound
const statement = `${name}.__value = ${name}.textContent;`;
local.update.addLine( statement );
block.builders.update.addLine( statement );
node.initialUpdate = statement;
}
block.builders.create.addBlock( local.create );
if ( !local.update.isEmpty() ) block.builders.update.addBlock( local.update );
if ( !local.destroy.isEmpty() ) block.builders.destroy.addBlock( local.destroy );
block.createMountStatement( name, state.parentNode );
node.children.forEach( child => {
@ -107,4 +86,16 @@ export default function visitElement ( generator, block, state, node ) {
if ( node.initialUpdate ) {
block.builders.create.addBlock( node.initialUpdate );
}
}
function getRenderStatement ( generator, namespace, name ) {
if ( namespace ) {
if ( namespace === 'http://www.w3.org/2000/svg' ) {
return `${generator.helper( 'createSvgElement' )}( '${name}' )`;
}
return `document.createElementNS( '${namespace}', '${name}' )`;
}
return `${generator.helper( 'createElement' )}( '${name}' )`;
}

@ -1,8 +1,8 @@
import attributeLookup from './lookup.js';
import addElementBinding from './addElementBinding';
import deindent from '../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js';
import getStaticAttributeValue from './binding/getStaticAttributeValue.js';
import deindent from '../../../../../utils/deindent.js';
import flattenReference from '../../../../../utils/flattenReference.js';
import getStaticAttributeValue from './getStaticAttributeValue.js';
export default function addElementAttributes ( generator, block, state, node, local ) {
node.attributes.forEach( attribute => {
@ -31,11 +31,11 @@ export default function addElementAttributes ( generator, block, state, node, lo
if ( attribute.value === true ) {
// attributes without values, e.g. <textarea readonly>
if ( propertyName ) {
local.create.addLine(
block.builders.create.addLine(
`${state.parentNode}.${propertyName} = true;`
);
} else {
local.create.addLine(
block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', true );`
);
}
@ -48,11 +48,11 @@ export default function addElementAttributes ( generator, block, state, node, lo
else if ( attribute.value.length === 0 ) {
if ( propertyName ) {
local.create.addLine(
block.builders.create.addLine(
`${state.parentNode}.${propertyName} = '';`
);
} else {
local.create.addLine(
block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', '' );`
);
}
@ -74,7 +74,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
state.namespace = value.data;
addAttribute = true;
} else if ( propertyName ) {
local.create.addLine(
block.builders.create.addLine(
`${state.parentNode}.${propertyName} = ${result};`
);
} else {
@ -82,7 +82,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
}
if ( addAttribute ) {
local.create.addLine(
block.builders.create.addLine(
`${generator.helper( method )}( ${state.parentNode}, '${name}', ${result} );`
);
}
@ -95,7 +95,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
const { snippet } = generator.contextualise( block, value.expression );
const last = `last_${state.parentNode}_${name.replace( /-/g, '_')}`;
local.create.addLine( `var ${last} = ${snippet};` );
block.builders.create.addLine( `var ${last} = ${snippet};` );
let updater;
if ( propertyName ) {
@ -104,9 +104,9 @@ export default function addElementAttributes ( generator, block, state, node, lo
updater = `${generator.helper( method )}( ${state.parentNode}, '${name}', ${last} );`;
}
local.create.addLine( updater );
block.builders.create.addLine( updater );
local.update.addBlock( deindent`
block.builders.update.addBlock( deindent`
if ( ( ${block.tmp()} = ${snippet} ) !== ${last} ) {
${last} = ${block.tmp()};
${updater}
@ -136,15 +136,15 @@ export default function addElementAttributes ( generator, block, state, node, lo
updater = `${generator.helper( method )}( ${state.parentNode}, '${name}', ${value} );`;
}
local.create.addLine( updater );
local.update.addLine( updater );
block.builders.create.addLine( updater );
block.builders.update.addLine( updater );
}
if ( isIndirectlyBoundValue ) {
const updateValue = `${state.parentNode}.value = ${state.parentNode}.__value;`;
local.create.addLine( updateValue );
if ( dynamic ) local.update.addLine( updateValue );
block.builders.create.addLine( updateValue );
if ( dynamic ) block.builders.update.addLine( updateValue );
}
}
@ -182,7 +182,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
const handlerBody = ( declarations.length ? declarations.join( '\n' ) + '\n\n' : '' ) + `[✂${attribute.expression.start}-${attribute.expression.end}✂];`;
if ( generator.events.has( name ) ) {
local.create.addBlock( deindent`
block.builders.create.addBlock( deindent`
var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${block.component}, ${state.parentNode}, function ( event ) {
${handlerBody}
}.bind( ${state.parentNode} ) );
@ -192,7 +192,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
${handlerName}.teardown();
` );
} else {
local.create.addBlock( deindent`
block.builders.create.addBlock( deindent`
function ${handlerName} ( event ) {
${handlerBody}
}
@ -213,7 +213,7 @@ export default function addElementAttributes ( generator, block, state, node, lo
else if ( attribute.type === 'Ref' ) {
generator.usesRefs = true;
local.create.addLine(
block.builders.create.addLine(
`${block.component}.refs.${name} = ${state.parentNode};`
);

@ -1,7 +1,7 @@
import deindent from '../../../../utils/deindent.js';
import flattenReference from '../../../../utils/flattenReference.js';
import getSetter from './binding/getSetter.js';
import getStaticAttributeValue from './binding/getStaticAttributeValue.js';
import deindent from '../../../../../utils/deindent.js';
import flattenReference from '../../../../../utils/flattenReference.js';
import getSetter from '../../shared/binding/getSetter.js';
import getStaticAttributeValue from './getStaticAttributeValue.js';
export default function addElementBinding ( generator, node, block, state, attribute, local ) {
const { name, keypath } = flattenReference( attribute.value );
@ -66,11 +66,11 @@ export default function addElementBinding ( generator, node, block, state, attri
`~${snippet}.indexOf( ${state.parentNode}.__value )` :
`${state.parentNode}.__value === ${snippet}`;
local.create.addLine(
block.builders.create.addLine(
`${block.component}._bindingGroups[${bindingGroup}].push( ${state.parentNode} );`
);
local.destroy.addBlock(
block.builders.destroy.addBlock(
`${block.component}._bindingGroups[${bindingGroup}].splice( ${block.component}._bindingGroups[${bindingGroup}].indexOf( ${state.parentNode} ), 1 );`
);
@ -84,7 +84,7 @@ export default function addElementBinding ( generator, node, block, state, attri
const updating = block.getUniqueName( `${state.parentNode}_updating` );
local.create.addBlock( deindent`
block.builders.create.addBlock( deindent`
var ${updating} = false;
function ${handler} () {
@ -98,7 +98,7 @@ export default function addElementBinding ( generator, node, block, state, attri
node.initialUpdate = updateElement;
local.update.addLine( deindent`
block.builders.update.addLine( deindent`
if ( !${updating} ) {
${updateElement}
}

@ -1,5 +1,5 @@
import flattenReference from '../../../../utils/flattenReference.js';
import deindent from '../../../../utils/deindent.js';
import flattenReference from '../../../../../utils/flattenReference.js';
import deindent from '../../../../../utils/deindent.js';
const associatedEvents = {
innerWidth: 'resize',

@ -1,6 +1,6 @@
import Comment from './Comment.js';
import EachBlock from './EachBlock.js';
import Element from './Element.js';
import Element from './Element/Element.js';
import IfBlock from './IfBlock.js';
import MustacheTag from './MustacheTag.js';
import RawMustacheTag from './RawMustacheTag.js';

Loading…
Cancel
Save