Skip to content

Commit

Permalink
add events
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 22, 2019
1 parent 4008d25 commit 844ff1a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
19 changes: 13 additions & 6 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
* List of global variables to ignore in expression scoping
*/

var globals = ["NaN", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];
var globals = ["NaN", "event", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];
/**
* Scope an expression to use variables within the `data` object.
*
Expand Down Expand Up @@ -267,7 +267,11 @@
} else {
// Store the key/value pair using the matched value or
// expression.
attributes[attributeKey] = attributeExpression === undefined ? attributeValue : scopeExpression(attributeExpression);
attributes[attributeKey] = attributeExpression === undefined ? attributeValue : scopeExpression(attributeExpression); // Add a wrapper function for events.

if (attributeKey[0] === "@") {
attributes[attributeKey] = "function(event){" + attributes[attributeKey] + "}";
}
}
} // Append an opening tag token with the type, attributes, and optional
// self-closing slash.
Expand Down Expand Up @@ -645,14 +649,17 @@
var nodeNode;

if (nodeType === types.element) {
nodeNode = document.createElement(node.name); // Set data and attributes.
nodeNode = document.createElement(node.name); // Set data, events, and attributes.

var _data = node.data;

for (var key in _data) {
var value = _data[key];

if (key !== "children") {
if (key[0] === "@") {
nodeData[key] = value;
nodeNode.addEventListener(key.slice(1), value);
} else if (key !== "children") {
nodeData[key] = value;
nodeNode.setAttribute(key, value);
}
Expand Down Expand Up @@ -874,12 +881,12 @@
var nodeOldData = _nodeOld.data;
var nodeOldNode = _nodeOld.node;
var nodeNewData = patch.nodeNew.data; // Mutate the old node with the new node's data and set attributes
// on the DOM node.
// and events on the DOM node.

for (var key in nodeNewData) {
var value = nodeNewData[key];

if (key !== "children") {
if (key[0] !== "@" && key !== "children") {
nodeOldData[key] = value;
nodeOldNode.setAttribute(key, value);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

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

7 changes: 6 additions & 1 deletion packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_
/**
* List of global variables to ignore in expression scoping
*/
const globals = ["NaN", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];
const globals = ["NaN", "event", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];

/**
* Scope an expression to use variables within the `data` object.
Expand Down Expand Up @@ -214,6 +214,11 @@ export function lex(input) {
attributeExpression === undefined ?
attributeValue :
scopeExpression(attributeExpression);

// Add a wrapper function for events.
if (attributeKey[0] === "@") {
attributes[attributeKey] = `function(event){${attributes[attributeKey]}}`;
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/moon/src/executor/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ function executeCreate(node) {
if (nodeType === types.element) {
nodeNode = document.createElement(node.name);

// Set data and attributes.
// Set data, events, and attributes.
const data = node.data;

for (let key in data) {
const value = data[key];

if (key !== "children") {
if (key[0] === "@") {
nodeData[key] = value;
nodeNode.addEventListener(key.slice(1), value);
} else if (key !== "children") {
nodeData[key] = value;
nodeNode.setAttribute(key, value);
}
Expand Down Expand Up @@ -278,11 +281,11 @@ function executePatch(patches) {
const nodeNewData = patch.nodeNew.data;

// Mutate the old node with the new node's data and set attributes
// on the DOM node.
// and events on the DOM node.
for (let key in nodeNewData) {
const value = nodeNewData[key];

if (key !== "children") {
if (key[0] !== "@" && key !== "children") {
nodeOldData[key] = value;
nodeOldNode.setAttribute(key, value);
}
Expand Down

0 comments on commit 844ff1a

Please sign in to comment.