Lint and format src/compiler/compile/render_dom/wrappers/Element/EventHandler.js

pull/8569/head
Simon Holthausen 3 years ago
parent 87420085e1
commit 920f6383c8

@ -1,82 +1,75 @@
import { b, x, p } from 'code-red'; import { b, x, p } from 'code-red';
const TRUE = x `true`; const TRUE = x`true`;
const FALSE = x `false`; const FALSE = x`false`;
/** */ /** */
export default class EventHandlerWrapper { export default class EventHandlerWrapper {
/** @type {import('../../../nodes/EventHandler.js').default} */
node;
/** @type {import('../../../nodes/EventHandler.js').default} */ /** @type {import('../shared/Wrapper.js').default} */
node; parent;
/** @type {import('../shared/Wrapper.js').default} */ /**
parent; * @param {import('../../../nodes/EventHandler.js').default} node
* @param {import('../shared/Wrapper.js').default} parent
/** */
* @param {import('../../../nodes/EventHandler.js').default} node constructor(node, parent) {
* @param {import('../shared/Wrapper.js').default} parent this.node = node;
*/ this.parent = parent;
constructor(node, parent) { if (!node.expression) {
this.node = node; this.parent.renderer.add_to_context(node.handler_name.name);
this.parent = parent; this.parent.renderer.component.partly_hoisted.push(b`
if (!node.expression) {
this.parent.renderer.add_to_context(node.handler_name.name);
this.parent.renderer.component.partly_hoisted.push(b `
function ${node.handler_name.name}(event) { function ${node.handler_name.name}(event) {
@bubble.call(this, $$self, event); @bubble.call(this, $$self, event);
} }
`); `);
} }
} }
/** @param {import('../../Block.js').default} block */ /** @param {import('../../Block.js').default} block */
get_snippet(block) { get_snippet(block) {
const snippet = this.node.expression const snippet = this.node.expression
? this.node.expression.manipulate(block) ? this.node.expression.manipulate(block)
: block.renderer.reference(this.node.handler_name); : block.renderer.reference(this.node.handler_name);
if (this.node.reassigned) { if (this.node.reassigned) {
block.maintain_context = true; block.maintain_context = true;
return x `function () { if (@is_function(${snippet})) ${snippet}.apply(this, arguments); }`; return x`function () { if (@is_function(${snippet})) ${snippet}.apply(this, arguments); }`;
} }
return snippet; return snippet;
} }
/** /**
* @param {import('../../Block.js').default} block * @param {import('../../Block.js').default} block
* @param {string | import('estree').Expression} target * @param {string | import('estree').Expression} target
*/ */
render(block, target) { render(block, target) {
let snippet = this.get_snippet(block); let snippet = this.get_snippet(block);
if (this.node.modifiers.has('preventDefault')) if (this.node.modifiers.has('preventDefault')) snippet = x`@prevent_default(${snippet})`;
snippet = x `@prevent_default(${snippet})`; if (this.node.modifiers.has('stopPropagation')) snippet = x`@stop_propagation(${snippet})`;
if (this.node.modifiers.has('stopPropagation')) if (this.node.modifiers.has('stopImmediatePropagation'))
snippet = x `@stop_propagation(${snippet})`; snippet = x`@stop_immediate_propagation(${snippet})`;
if (this.node.modifiers.has('stopImmediatePropagation')) if (this.node.modifiers.has('self')) snippet = x`@self(${snippet})`;
snippet = x `@stop_immediate_propagation(${snippet})`; if (this.node.modifiers.has('trusted')) snippet = x`@trusted(${snippet})`;
if (this.node.modifiers.has('self')) const args = [];
snippet = x `@self(${snippet})`; const opts = ['nonpassive', 'passive', 'once', 'capture'].filter((mod) =>
if (this.node.modifiers.has('trusted')) this.node.modifiers.has(mod)
snippet = x `@trusted(${snippet})`; );
const args = []; if (opts.length) {
const opts = ['nonpassive', 'passive', 'once', 'capture'].filter((mod) => this.node.modifiers.has(mod)); if (opts.length === 1 && opts[0] === 'capture') {
if (opts.length) { args.push(TRUE);
if (opts.length === 1 && opts[0] === 'capture') { } else {
args.push(TRUE); args.push(
} x`{ ${opts.map((opt) => (opt === 'nonpassive' ? p`passive: false` : p`${opt}: true`))} }`
else { );
args.push(x `{ ${opts.map((opt) => (opt === 'nonpassive' ? p `passive: false` : p `${opt}: true`))} }`); }
} } else if (block.renderer.options.dev) {
} args.push(FALSE);
else if (block.renderer.options.dev) { }
args.push(FALSE); if (block.renderer.options.dev) {
} args.push(this.node.modifiers.has('preventDefault') ? TRUE : FALSE);
if (block.renderer.options.dev) { args.push(this.node.modifiers.has('stopPropagation') ? TRUE : FALSE);
args.push(this.node.modifiers.has('preventDefault') ? TRUE : FALSE); args.push(this.node.modifiers.has('stopImmediatePropagation') ? TRUE : FALSE);
args.push(this.node.modifiers.has('stopPropagation') ? TRUE : FALSE); }
args.push(this.node.modifiers.has('stopImmediatePropagation') ? TRUE : FALSE); block.event_listeners.push(x`@listen(${target}, "${this.node.name}", ${snippet}, ${args})`);
} }
block.event_listeners.push(x `@listen(${target}, "${this.node.name}", ${snippet}, ${args})`);
}
} }

Loading…
Cancel
Save