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

Return false from callbacks to prevent default behavior #6

Merged
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
20 changes: 13 additions & 7 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,22 @@
if (ctx.ignoreActive && oldNode === document.activeElement) {
// don't morph focused element
} else if (newContent == null) {
ctx.callbacks.beforeNodeRemoved(oldNode);
if (ctx.callbacks.beforeNodeRemoved(oldNode) === false) return;

oldNode.remove();
ctx.callbacks.afterNodeRemoved(oldNode);
return null;
} else if (!isSoftMatch(oldNode, newContent)) {
ctx.callbacks.beforeNodeRemoved(oldNode);
ctx.callbacks.beforeNodeAdded(newContent);
if (ctx.callbacks.beforeNodeRemoved(oldNode) === false) return;
if (ctx.callbacks.beforeNodeAdded(newContent) === false) return;

oldNode.parentElement.replaceChild(newContent, oldNode);
ctx.callbacks.afterNodeAdded(newContent);
ctx.callbacks.afterNodeRemoved(oldNode);
return newContent;
} else {
ctx.callbacks.beforeNodeMorphed(oldNode, newContent);
if (ctx.callbacks.beforeNodeMorphed(oldNode, newContent) === false) return;

if (oldNode instanceof HTMLHeadElement && ctx.head.ignore) {
// ignore the head element
} else if (oldNode instanceof HTMLHeadElement && ctx.head.style !== "morph") {
Expand Down Expand Up @@ -167,7 +170,8 @@

// if we are at the end of the exiting parent's children, just append
if (insertionPoint == null) {
ctx.callbacks.beforeNodeAdded(newChild);
if (ctx.callbacks.beforeNodeAdded(newChild) === false) return;

oldParent.appendChild(newChild);
ctx.callbacks.afterNodeAdded(newChild);
removeIdsFromConsideration(ctx, newChild);
Expand Down Expand Up @@ -206,7 +210,8 @@

// abandon all hope of morphing, just insert the new child before the insertion point
// and move on
ctx.callbacks.beforeNodeAdded(newChild);
if (ctx.callbacks.beforeNodeAdded(newChild) === false) return;

oldParent.insertBefore(newChild, insertionPoint);
ctx.callbacks.afterNodeAdded(newChild);
removeIdsFromConsideration(ctx, newChild);
Expand Down Expand Up @@ -658,7 +663,8 @@

function removeNode(tempNode, ctx) {
removeIdsFromConsideration(ctx, tempNode)
ctx.callbacks.beforeNodeRemoved(tempNode);
if (ctx.callbacks.beforeNodeRemoved(tempNode) === false) return;

tempNode.remove();
ctx.callbacks.afterNodeRemoved(tempNode);
}
Expand Down