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

Simplified event handlers - Option 1. #235

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const doc = ENV_DOM ? document : {};

export const emptyObj = {};

export const emptyArr = [];

export function noop() {};

export function retArg0(a) { return a; }
Expand Down
4 changes: 2 additions & 2 deletions src/view/addons/devmode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const DEVMODE = {
return ["Invoking redraw() of an unmounted (sub)view may result in errors.", vm];
},

INLINE_HANDLER: function(vnode, oval, nval) {
return ["Anonymous event handlers get re-bound on each redraw, consider defining them outside of templates for better reuse.", vnode, oval, nval];
INLINE_HANDLER: function(vnode, nval) {
return ["Anonymous inline event handlers get re-created on every redraw, consider defining them outside of templates for better reuse.", vnode, nval];
},

MISMATCHED_HANDLER: function(vnode, oval, nval) {
Expand Down
121 changes: 37 additions & 84 deletions src/view/patchEvent.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,55 @@
import { isArr, isFunc, isPlainObj, doc} from '../utils';
import { isArr, isFunc, emptyArr } from '../utils';
import { getVm } from './utils';
import { onevent } from './config';
import { devNotify } from "./addons/devmode";

const registry = {};

function listen(ontype) {
if (registry[ontype]) return;
registry[ontype] = true;
bind(doc, ontype, handle, true);
}

/*
function unlisten(ontype) {
if (registry[ontype])
doc.removeEventListener(ontype.slice(2), handle, USE_CAPTURE);
}
*/

function unbind(el, type, fn, capt) {
el.removeEventListener(type.slice(2), fn, capt);
}

function bind(el, type, fn, capt) {
el.addEventListener(type.slice(2), fn, capt);
}

function exec(fn, args, e, node, vm) {
var out1 = fn.apply(vm, args.concat([e, node, vm, vm.data])), out2, out3;

if (FEAT_ONEVENT) {
out2 = vm.onevent(e, node, vm, vm.data, args),
out3 = onevent.call(null, e, node, vm, vm.data, args);
}

if (out1 === false || out2 === false || out3 === false) {
e.preventDefault();
e.stopPropagation();
return false;
}
}

function ancestEvDefs(type, node) {
var ontype = "on" + type, evDef, attrs, evDefs = [];

while (node) {
if (attrs = node.attrs) {
if ((evDef = attrs[ontype]) && isArr(evDef))
evDefs.unshift(evDef);
}
node = node.parent;
}

return evDefs;
function exec(fn, args, e, node) {
let vm = getVm(node),
out1 = fn.apply(e.currentTarget, args.concat(e, node, vm, vm.data)), // this == currentTarget, NOT vm, to match normal handler
out2,
out3;

if (FEAT_ONEVENT) {
out2 = vm.onevent(e, node, vm, vm.data, args),
out3 = onevent.call(null,e, node, vm, vm.data, args);
}

if (out1 === false || out2 === false || out3 === false) {
e.preventDefault();
return false;
}
}

function handle(e) {
var node = e.target._node;

if (node == null)
return;
let node = e.currentTarget._node;

var evDefs = ancestEvDefs(e.type, node);
if (node == null)
return;

var vm = getVm(node);
let dfn = node.attrs["on" + e.type];

for (var i = 0; i < evDefs.length; i++) {
var res = exec(evDefs[i][0], evDefs[i].slice(1), e, node, vm);

if (res === false)
break;
}
if (isArr(dfn))
exec(dfn[0], dfn.slice(1), e, node);
else
exec(dfn, emptyArr, e, node);
}

export function patchEvent(node, name, nval, oval) {
if (nval == oval)
return;

if (_DEVMODE) {
if (isFunc(nval) && isFunc(oval) && oval.name == nval.name)
devNotify("INLINE_HANDLER", [node, oval, nval]);
if (nval == oval)
return;

if (oval != null && nval != null &&
(
isArr(oval) != isArr(nval) ||
isPlainObj(oval) != isPlainObj(nval) ||
isFunc(oval) != isFunc(nval)
)
) devNotify("MISMATCHED_HANDLER", [node, oval, nval]);
}
if (_DEVMODE) {
if (nval != null && !isFunc(nval) && !isArr(nval))
devNotify("INVALID_HANDLER", [node, nval]);

var el = node.el;
if (isFunc(nval) && nval.name == '')
devNotify("INLINE_HANDLER", [node, nval]);
}

if (isFunc(nval))
bind(el, name, nval, false);
else if (nval != null)
listen(name);
let el = node.el;

if (isFunc(oval))
unbind(el, name, oval, false);
if (nval == null)
el.removeEventListener(name.slice(2),handle);
else if (oval == null)
el.addEventListener(name.slice(2),handle);
}
10 changes: 10 additions & 0 deletions test/dominstr.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

var htmlProto = HTMLElement.prototype;
var innerText = getDescr(htmlProto, "innerText");
var onclick = getDescr(htmlProto, "onclick");

var elemProto = Element.prototype;
var innerHTML = getDescr(!isIE ? elemProto : htmlProto, "innerHTML");
Expand Down Expand Up @@ -135,6 +136,14 @@
},
});

counts.onclick = 0;
defProp(htmlProto, "onclick", {
set: function(s) {
counts.onclick++;
onclick.set.call(this, s);
},
});

counts.innerHTML = 0;
defProp(!isIE ? elemProto : htmlProto, "innerHTML", {
set: function(s) {
Expand Down Expand Up @@ -236,6 +245,7 @@
defProp(nodeProto, "textContent", textContent);
defProp(nodeProto, "nodeValue", nodeValue);
defProp(htmlProto, "innerText", innerText);
defProp(htmlProto, "onclick", onclick);
defProp(!isIE ? elemProto : htmlProto, "innerHTML", innerHTML);
defProp(!isIE ? elemProto : htmlProto, "className", className);
defProp(!isIE ? elemProto : htmlProto, "id", id);
Expand Down
8 changes: 4 additions & 4 deletions test/src/attrs-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ QUnit.module("Attrs/props", function() {
var callCounts = instr.end();

var expcHtml = '<input type="text" disabled custom="abc" custom3="" custom4="foo" id="foo" class="bar baz" min="-1" style="font-family: Arial; font-size: 12px;">';
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { id: 1, className: 1, createElement: 1, insertBefore: 1, setAttribute: 5, addEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { id: 1, className: 1, createElement: 1, insertBefore: 1, setAttribute: 5, onclick: 1 });
});

// TODO: can 'id' or 'name' be allowed to change for recycling since they implicitly double as keys?
Expand All @@ -28,7 +28,7 @@ QUnit.module("Attrs/props", function() {
var callCounts = instr.end();

var expcHtml = '<input type="text" custom="xyz" custom2="..." id="foo" style="padding: 10px;">';
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeAttribute: 4, setAttribute: 2, removeEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeAttribute: 4, setAttribute: 2, onclick: 1 });
});


Expand Down Expand Up @@ -223,7 +223,7 @@ QUnit.module("Attrs/props", function() {
var callCounts = instr.end();

var expcHtml = '<div style="color: red;">moo</div>';
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 1, cssText: 1, textContent: 1, insertBefore: 1, addEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 1, cssText: 1, textContent: 1, insertBefore: 1, onclick: 1 });

attrs = null; // or simply {}?

Expand All @@ -232,7 +232,7 @@ QUnit.module("Attrs/props", function() {
var callCounts = instr.end();

var expcHtml = '<div>moo</div>';
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeAttribute: 1, removeEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeAttribute: 1, onclick: 1 });
});

QUnit.test("Reused static attrs object", function(assert) {
Expand Down
16 changes: 8 additions & 8 deletions test/src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ QUnit.module("Events", function() {
vm = domvm.createView(View).mount(testyDiv);
var callCounts = instr.end();
var expcHtml = '<div><input></div>';
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, addEventListener: 1, insertBefore: 2 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, onclick: 1, insertBefore: 2 });

// clicked args
doClick(vm.node.body[0].el);
Expand Down Expand Up @@ -106,7 +106,7 @@ QUnit.module("Events", function() {
var callCounts = instr.end();
var expcHtml = '<div><input></div>';

evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { addEventListener: 1, removeEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { });

/* // return false -> preventDefault + stopPropagation
// todo: spy on Event.prototype.stopPropagation
Expand All @@ -125,7 +125,7 @@ QUnit.module("Events", function() {
var callCounts = instr.end();
var expcHtml = '<div><input></div>';

evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { onclick: 1 });

reset();
});
Expand All @@ -139,7 +139,7 @@ QUnit.module("Events", function() {
var expcHtml = '<div><input></div>';

// TODO: test if "handle" is the thing that's bound
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, addEventListener: 1, insertBefore: 2 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, onclick: 1, insertBefore: 2 });

// clicked args
doClick(vm.node.body[0].el);
Expand Down Expand Up @@ -203,7 +203,7 @@ QUnit.module("Events", function() {

// TODO: test if "handle" is the thing that's bound
// TOFIX: when last listener of this type is dropped, remove top-level capturing listener
// evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeEventListener: 1 });
// evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { onclick: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { });

reset();
Expand Down Expand Up @@ -246,7 +246,7 @@ QUnit.module("Events", function() {
var expcHtml = '<div><input></div>';

// TODO: test if "handle" is the thing that's bound
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, addEventListener: 1, insertBefore: 2 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, onclick: 1, insertBefore: 2 });

reset();

Expand Down Expand Up @@ -281,7 +281,7 @@ QUnit.module("Events", function() {
var expcHtml = '<div><input></div>';

// TODO: test if "handle" is the thing that's bound
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { removeEventListener: 1 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { onclick: 1 });

reset();
});
Expand All @@ -295,7 +295,7 @@ QUnit.module("Events", function() {
var expcHtml = '<div><input></div>';

// TODO: test if "handle" is the thing that's bound
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, addEventListener: 1, insertBefore: 2 });
evalOut(assert, vm.node.el, vm.html(), expcHtml, callCounts, { createElement: 2, onclick: 1, insertBefore: 2 });

reset();

Expand Down