make addEventListener, removeEventListener, setAttribute shared

pull/227/head
Conduitry 8 years ago
parent f8701545e9
commit 7340b7fcdb

@ -72,7 +72,8 @@ export default {
} }
if ( generator.cssId && !generator.elementDepth ) { if ( generator.cssId && !generator.elementDepth ) {
render += `\n${name}.setAttribute( '${generator.cssId}', '' );`; generator.uses.setAttribute = true;
render += `\nsetAttribute( ${name}, '${generator.cssId}', '' );`;
} }
local.init.addLineAtStart( render ); local.init.addLineAtStart( render );

@ -21,8 +21,9 @@ export default function addElementAttributes ( generator, node, local ) {
`${local.name}.${propertyName} = true;` `${local.name}.${propertyName} = true;`
); );
} else { } else {
generator.uses.setAttribute = true;
local.init.addLine( local.init.addLine(
`${local.name}.setAttribute( '${attribute.name}', true );` `setAttribute( ${local.name}, '${attribute.name}', true );`
); );
} }
@ -38,8 +39,9 @@ export default function addElementAttributes ( generator, node, local ) {
`${local.name}.${propertyName} = '';` `${local.name}.${propertyName} = '';`
); );
} else { } else {
generator.uses.setAttribute = true;
local.init.addLine( local.init.addLine(
`${local.name}.setAttribute( '${attribute.name}', '' );` `setAttribute( ${local.name}, '${attribute.name}', '' );`
); );
} }
} }
@ -62,8 +64,9 @@ export default function addElementAttributes ( generator, node, local ) {
`${local.name}.${propertyName} = ${result};` `${local.name}.${propertyName} = ${result};`
); );
} else { } else {
generator.uses.setAttribute = true;
local.init.addLine( local.init.addLine(
`${local.name}.setAttribute( '${attribute.name}', ${result} );` `setAttribute( ${local.name}, '${attribute.name}', ${result} );`
); );
} }
} }
@ -74,9 +77,13 @@ export default function addElementAttributes ( generator, node, local ) {
// dynamic but potentially non-string attributes // dynamic but potentially non-string attributes
const { snippet } = generator.contextualise( value.expression ); const { snippet } = generator.contextualise( value.expression );
const updater = propertyName ? let updater;
`${local.name}.${propertyName} = ${snippet};` : if (propertyName) {
`${local.name}.setAttribute( '${attribute.name}', ${snippet} );`; // TODO use snippet both times see note below updater = `${local.name}.${propertyName} = ${snippet};`;
} else {
generator.uses.setAttribute = true;
updater = `setAttribute( ${local.name}, '${attribute.name}', ${snippet} );`; // TODO use snippet both times see note below
}
local.init.addLine( updater ); local.init.addLine( updater );
local.update.addLine( updater ); local.update.addLine( updater );
@ -99,9 +106,13 @@ export default function addElementAttributes ( generator, node, local ) {
}).join( ' + ' ) }).join( ' + ' )
); );
const updater = propertyName ? let updater;
`${local.name}.${propertyName} = ${value};` : if (propertyName) {
`${local.name}.setAttribute( '${attribute.name}', ${value} );`; updater = `${local.name}.${propertyName} = ${value};`;
} else {
generator.uses.setAttribute = true;
updater = `setAttribute( ${local.name}, '${attribute.name}', ${value} );`;
}
local.init.addLine( updater ); local.init.addLine( updater );
local.update.addLine( updater ); local.update.addLine( updater );
@ -156,16 +167,18 @@ export default function addElementAttributes ( generator, node, local ) {
${handlerName}.teardown(); ${handlerName}.teardown();
` ); ` );
} else { } else {
generator.uses.addEventListener = true;
generator.uses.removeEventListener = true;
local.init.addBlock( deindent` local.init.addBlock( deindent`
function ${handlerName} ( event ) { function ${handlerName} ( event ) {
${handlerBody} ${handlerBody}
} }
${local.name}.addEventListener( '${attribute.name}', ${handlerName}, false ); addEventListener( ${local.name}, '${attribute.name}', ${handlerName} );
` ); ` );
generator.current.builders.teardown.addLine( deindent` generator.current.builders.teardown.addLine( deindent`
${local.name}.removeEventListener( '${attribute.name}', ${handlerName}, false ); removeEventListener( ${local.name}, '${attribute.name}', ${handlerName} );
` ); ` );
} }
} }

@ -104,6 +104,8 @@ export default function createBinding ( generator, node, attribute, current, loc
} else { } else {
const updateElement = `${local.name}.${attribute.name} = ${contextual ? attribute.value : `root.${attribute.value}`}`; const updateElement = `${local.name}.${attribute.name} = ${contextual ? attribute.value : `root.${attribute.value}`}`;
generator.uses.addEventListener = true;
generator.uses.removeEventListener = true;
local.init.addBlock( deindent` local.init.addBlock( deindent`
var ${local.name}_updating = false; var ${local.name}_updating = false;
@ -113,7 +115,7 @@ export default function createBinding ( generator, node, attribute, current, loc
${local.name}_updating = false; ${local.name}_updating = false;
} }
${local.name}.addEventListener( '${eventName}', ${handler}, false ); addEventListener( ${local.name}, '${eventName}', ${handler} );
${updateElement}; ${updateElement};
` ); ` );
@ -122,7 +124,7 @@ export default function createBinding ( generator, node, attribute, current, loc
); );
generator.current.builders.teardown.addLine( deindent` generator.current.builders.teardown.addLine( deindent`
${local.name}.removeEventListener( '${eventName}', ${handler}, false ); removeEventListener( ${local.name}, '${eventName}', ${handler} );
` ); ` );
} }

@ -25,3 +25,15 @@ export function createText ( data ) {
export function createComment ( data ) { export function createComment ( data ) {
return document.createComment( data ); return document.createComment( data );
} }
export function addEventListener ( node, event, handler ) {
node.addEventListener ( event, handler, false );
}
export function removeEventListener ( node, event, handler ) {
node.removeEventListener ( event, handler, false );
}
export function setAttribute ( node, attribute, value ) {
node.setAttribute ( attribute, value );
}

Loading…
Cancel
Save