Skip to content

Commit

Permalink
clean up conditional implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed May 23, 2018
1 parent f2e8484 commit b55f09f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
25 changes: 13 additions & 12 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,45 +355,46 @@
var insertBefore = function (element, reference, parent) { return ("m.ib(" + (getElement(element)) + "," + (getElement(reference)) + "," + (getElement(parent)) + ");"); };

var generateCreate = function (element, index, parent, root, insert) {
var createCode;
var createCode, mountCode = "", mountElement = element.index;

switch (element.type) {
case "#if":
var siblings = parent.children;
var ifReference = root.nextIndex++;
var ifCreates = "";
var ifBranches = "";

for (var i = index; i < siblings.length;) {
var sibling = siblings[i];
var siblingType = sibling.type;
var ifCreate = root.nextIndex++;
var keyword = (void 0);

if (siblingType === "#if") {
if (sibling.type === "#if") {
keyword = "if(" + (attributeValue(sibling.attributes[0])) + ")";
} else if (siblingType === "#elseif") {
} else if (sibling.type === "#elseif") {
keyword = "else if(" + (attributeValue(sibling.attributes[0])) + ")";
} else if (siblingType === "#else") {
} else if (sibling.type === "#else") {
keyword = "else";
} else {
break;
}

ifCreates += setElement(ifCreate, ("function(){" + (mapReduce(sibling.children, function (child, index) { return generateCreate(child, index, parent, root, element.index); })) + "};"));
ifBranches += keyword + "{" + (getElement(ifCreate)) + "();}";
ifCreates += setElement(sibling.index, ("function(){" + (mapReduce(sibling.children, function (child, index) { return generateCreate(child, index, parent, root, ifReference); })) + "};"));
ifBranches += keyword + "{" + (getElement(sibling.index)) + "();}";
siblings.splice(i, 1);
}

createCode = ifCreates + ifBranches + setElement(element.index, createComment());
createCode = setElement(ifReference, createComment());
mountCode = ifCreates + ifBranches;
mountElement = ifReference;
break;
case "#text":
createCode = setElement(element.index, createTextNode(attributeValue(element.attributes[0])));
createCode = setElement(mountElement, createTextNode(attributeValue(element.attributes[0])));
break;
default:
createCode = setElement(element.index, createElement(element.type)) + mapReduce(element.attributes, function (attribute) { return attribute.key[0] === "@" ? addEventListener(element.index, attribute) : setAttribute(element.index, attribute); }) + mapReduce(element.children, function (child, index) { return generateCreate(child, index, element, root); });
createCode = setElement(mountElement, createElement(element.type)) + mapReduce(element.attributes, function (attribute) { return attribute.key[0] === "@" ? addEventListener(mountElement, attribute) : setAttribute(mountElement, attribute); }) + mapReduce(element.children, function (child, index) { return generateCreate(child, index, element, root); });
}

return createCode + (insert === undefined ? appendChild(element.index, parent.index) : insertBefore(element.index, insert, parent.index));
return createCode + (insert === undefined ? appendChild(mountElement, parent.index) : insertBefore(mountElement, insert, parent.index)) + mountCode;
};

var generateUpdate = function (element, index, parent, root) {
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions src/compiler/generator/create.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
import { mapReduce, getElement, setElement, createElement, createTextNode, createComment, attributeValue, setAttribute, addEventListener, appendChild, insertBefore } from "./util";

export const generateCreate = (element, index, parent, root, insert) => {
let createCode;
let createCode, mountCode = "", mountElement = element.index;

switch (element.type) {
case "#if":
const siblings = parent.children;
const ifReference = root.nextIndex++;
let ifCreates = "";
let ifBranches = "";

for (let i = index; i < siblings.length;) {
const sibling = siblings[i];
const siblingType = sibling.type;
const ifCreate = root.nextIndex++;
let keyword;

if (siblingType === "#if") {
if (sibling.type === "#if") {
keyword = `if(${attributeValue(sibling.attributes[0])})`;
} else if (siblingType === "#elseif") {
} else if (sibling.type === "#elseif") {
keyword = `else if(${attributeValue(sibling.attributes[0])})`;
} else if (siblingType === "#else") {
} else if (sibling.type === "#else") {
keyword = "else";
} else {
break;
}

ifCreates += setElement(ifCreate, `function(){${mapReduce(sibling.children, (child, index) => generateCreate(child, index, parent, root, element.index))}};`);
ifBranches += `${keyword}{${getElement(ifCreate)}();}`;
ifCreates += setElement(sibling.index, `function(){${mapReduce(sibling.children, (child, index) => generateCreate(child, index, parent, root, ifReference))}};`);
ifBranches += `${keyword}{${getElement(sibling.index)}();}`;
siblings.splice(i, 1);
}

createCode = ifCreates + ifBranches + setElement(element.index, createComment());
createCode = setElement(ifReference, createComment());
mountCode = ifCreates + ifBranches;
mountElement = ifReference;
break;
case "#text":
createCode = setElement(element.index, createTextNode(attributeValue(element.attributes[0])));
createCode = setElement(mountElement, createTextNode(attributeValue(element.attributes[0])));
break;
default:
createCode = setElement(element.index, createElement(element.type)) + mapReduce(element.attributes, (attribute) => attribute.key[0] === "@" ? addEventListener(element.index, attribute) : setAttribute(element.index, attribute)) + mapReduce(element.children, (child, index) => generateCreate(child, index, element, root));
createCode = setElement(mountElement, createElement(element.type)) + mapReduce(element.attributes, (attribute) => attribute.key[0] === "@" ? addEventListener(mountElement, attribute) : setAttribute(mountElement, attribute)) + mapReduce(element.children, (child, index) => generateCreate(child, index, element, root));
}

return createCode + (insert === undefined ? appendChild(element.index, parent.index) : insertBefore(element.index, insert, parent.index));
return createCode + (insert === undefined ? appendChild(mountElement, parent.index) : insertBefore(mountElement, insert, parent.index)) + mountCode;
};

0 comments on commit b55f09f

Please sign in to comment.