Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move addEventListener, removeEventListener, setAttribute into shared #227

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/generators/dom/visitors/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export default {
}

if ( generator.cssId && !generator.elementDepth ) {
render += `\n${name}.setAttribute( '${generator.cssId}', '' );`;
generator.uses.setAttribute = true;
render += `\nsetAttribute( ${name}, '${generator.cssId}', '' );`;
}

local.init.addLineAtStart( render );
Expand Down
35 changes: 24 additions & 11 deletions src/generators/dom/visitors/attributes/addElementAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export default function addElementAttributes ( generator, node, local ) {
`${local.name}.${propertyName} = true;`
);
} else {
generator.uses.setAttribute = true;
local.init.addLine(
`${local.name}.setAttribute( '${attribute.name}', true );`
`setAttribute( ${local.name}, '${attribute.name}', true );`
);
}

Expand All @@ -38,8 +39,9 @@ export default function addElementAttributes ( generator, node, local ) {
`${local.name}.${propertyName} = '';`
);
} else {
generator.uses.setAttribute = true;
local.init.addLine(
`${local.name}.setAttribute( '${attribute.name}', '' );`
`setAttribute( ${local.name}, '${attribute.name}', '' );`
);
}
}
Expand All @@ -62,8 +64,9 @@ export default function addElementAttributes ( generator, node, local ) {
`${local.name}.${propertyName} = ${result};`
);
} else {
generator.uses.setAttribute = true;
local.init.addLine(
`${local.name}.setAttribute( '${attribute.name}', ${result} );`
`setAttribute( ${local.name}, '${attribute.name}', ${result} );`
);
}
}
Expand All @@ -74,9 +77,13 @@ export default function addElementAttributes ( generator, node, local ) {
// dynamic – but potentially non-string – attributes
const { snippet } = generator.contextualise( value.expression );

const updater = propertyName ?
`${local.name}.${propertyName} = ${snippet};` :
`${local.name}.setAttribute( '${attribute.name}', ${snippet} );`; // TODO use snippet both times – see note below
let updater;
if (propertyName) {
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.update.addLine( updater );
Expand All @@ -99,9 +106,13 @@ export default function addElementAttributes ( generator, node, local ) {
}).join( ' + ' )
);

const updater = propertyName ?
`${local.name}.${propertyName} = ${value};` :
`${local.name}.setAttribute( '${attribute.name}', ${value} );`;
let updater;
if (propertyName) {
updater = `${local.name}.${propertyName} = ${value};`;
} else {
generator.uses.setAttribute = true;
updater = `setAttribute( ${local.name}, '${attribute.name}', ${value} );`;
}

local.init.addLine( updater );
local.update.addLine( updater );
Expand Down Expand Up @@ -156,16 +167,18 @@ export default function addElementAttributes ( generator, node, local ) {
${handlerName}.teardown();
` );
} else {
generator.uses.addEventListener = true;
generator.uses.removeEventListener = true;
local.init.addBlock( deindent`
function ${handlerName} ( event ) {
${handlerBody}
}

${local.name}.addEventListener( '${attribute.name}', ${handlerName}, false );
addEventListener( ${local.name}, '${attribute.name}', ${handlerName} );
` );

generator.current.builders.teardown.addLine( deindent`
${local.name}.removeEventListener( '${attribute.name}', ${handlerName}, false );
removeEventListener( ${local.name}, '${attribute.name}', ${handlerName} );
` );
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/generators/dom/visitors/attributes/binding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export default function createBinding ( generator, node, attribute, current, loc
} else {
const updateElement = `${local.name}.${attribute.name} = ${contextual ? attribute.value : `root.${attribute.value}`}`;

generator.uses.addEventListener = true;
generator.uses.removeEventListener = true;
local.init.addBlock( deindent`
var ${local.name}_updating = false;

Expand All @@ -113,7 +115,7 @@ export default function createBinding ( generator, node, attribute, current, loc
${local.name}_updating = false;
}

${local.name}.addEventListener( '${eventName}', ${handler}, false );
addEventListener( ${local.name}, '${eventName}', ${handler} );
${updateElement};
` );

Expand All @@ -122,7 +124,7 @@ export default function createBinding ( generator, node, attribute, current, loc
);

generator.current.builders.teardown.addLine( deindent`
${local.name}.removeEventListener( '${eventName}', ${handler}, false );
removeEventListener( ${local.name}, '${eventName}', ${handler} );
` );
}

Expand Down
12 changes: 12 additions & 0 deletions src/shared/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ export function createText ( data ) {
export function 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 );
}