Skip to content

Commit

Permalink
add built-in key code modifier utilities for events
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Feb 4, 2017
1 parent 6b20bae commit 6e1f19a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
55 changes: 36 additions & 19 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
var directives = {};
var specialDirectives = {};
var components = {};
var eventModifiersCode = {
stop: 'event.stopPropagation();',
prevent: 'event.preventDefault();',
ctrl: 'if(!event.ctrlKey) {return;};',
shift: 'if(!event.shiftKey) {return;};',
alt: 'if(!event.altKey) {return;};'
};
var id = 0;

/* ======= Global Utilities ======= */
Expand Down Expand Up @@ -81,9 +88,9 @@
if (customCode) {
compiled = customCode(compiled, match, key, modifiers);
} else if (isString) {
compiled = compiled.replace(match, "\" + instance.get(\"" + key + "\")" + modifiers + " + \"");
compiled = compiled.replace(match, '" + instance.get("' + key + '")' + modifiers + ' + "');
} else {
compiled = compiled.replace(match, "instance.get(\"" + key + "\")" + modifiers);
compiled = compiled.replace(match, 'instance.get("' + key + '")' + modifiers);
}
});
return compiled;
Expand All @@ -97,9 +104,9 @@
*/
var createCall = function (vnode, metaIsString) {
if (metaIsString) {
return "h(\"" + vnode.type + "\", " + JSON.stringify(vnode.props) + ", " + vnode.meta + ", " + (vnode.children.join(",") || null) + ")";
return 'h("' + vnode.type + '", ' + JSON.stringify(vnode.props) + ', ' + vnode.meta + ', ' + (vnode.children.join(",") || null) + ')';
}
return "h(\"" + vnode.type + "\", " + JSON.stringify(vnode.props) + ", " + JSON.stringify(vnode.meta) + ", " + (vnode.children.join(",") || null) + ")";
return 'h("' + vnode.type + '", ' + JSON.stringify(vnode.props) + ', ' + JSON.stringify(vnode.meta) + ', ' + (vnode.children.join(",") || null) + ')';
};

/**
Expand Down Expand Up @@ -611,7 +618,7 @@
var generateEl = function (el) {
var code = "";
if (typeof el === "string") {
code += "\"" + el + "\"";
code += '"' + el + '"';
} else {
// Recursively generate code for children
el.children = el.children.map(generateEl);
Expand Down Expand Up @@ -640,7 +647,7 @@
code = compileTemplate(code, true);

// Escape Newlines
code = code.replace(NEWLINE_RE, "\" + \"\\n\" + \"");
code = code.replace(NEWLINE_RE, '" + "\\n" + "');

try {
return new Function("h", code);
Expand Down Expand Up @@ -679,33 +686,37 @@
/* ======= Default Directives ======= */

specialDirectives[Moon.config.prefix + "if"] = function (value, code, vnode) {
return "(" + compileTemplate(value, false) + ") ? " + code + " : ''";
return '(' + compileTemplate(value, false) + ') ? ' + code + ' : \'\'';
};

specialDirectives[Moon.config.prefix + "for"] = function (value, code, vnode) {
var parts = value.split(" in ");
var aliases = parts[0].split(",");

var iteratable = "instance.get(\"" + parts[1] + "\")";
var iteratable = 'instance.get("' + parts[1] + '")';

var params = aliases.join(",");

var customCode = function (compiled, match, key, modifiers) {
if (aliases.indexOf(key) === -1) {
return compiled;
}
return compiled.replace(match, "\" + " + key + modifiers + " + \"");
return compiled.replace(match, '" + ' + key + modifiers + ' + "');
};

return "instance.renderLoop(" + iteratable + ", function(" + params + ") { return " + compileTemplate(code, true, customCode) + "; })";
return 'instance.renderLoop(' + iteratable + ', function(' + params + ') { return ' + compileTemplate(code, true, customCode) + '; })';
};

specialDirectives[Moon.config.prefix + "on"] = function (value, code, vnode) {
var splitVal = value.split(":");
// Extract modifiers and the event
var modifiers = splitVal[0].split(".");
var eventToCall = modifiers[0];
modifiers.shift();
var rawModifiers = splitVal[0].split(".");
var eventToCall = rawModifiers[0];
var modifiers = "";
rawModifiers.shift();
for (var i = 0; i < rawModifiers.length; i++) {
modifiers += eventModifiersCode[rawModifiers[i]];
}

// Extract method to call afterwards
var rawMethod = splitVal[1].split("(");
Expand All @@ -715,7 +726,7 @@
if (!params) {
params = "event";
}
methodToCall += "(" + params + ")";
methodToCall += '(' + params + ')';

// Code for all metadata
var metadataCode = "{";
Expand All @@ -724,19 +735,25 @@

// Add any listeners to the metadata first
if (!vnode.meta.eventListeners[eventToCall]) {
vnode.meta.eventListeners[eventToCall] = [methodToCall];
vnode.meta.eventListeners[eventToCall] = [{
method: methodToCall,
modifiers: modifiers
}];
} else {
vnode.meta.eventListeners[eventToCall].push(methodToCall);
vnode.meta.eventListeners[eventToCall].push({
method: methodToCall,
modifiers: modifiers
});
}

// Go through each type of event, and generate code with a function
for (var eventType in vnode.meta.eventListeners) {
var handlers = [];
for (var i = 0; i < vnode.meta.eventListeners[eventType].length; i++) {
var handler = vnode.meta.eventListeners[eventType][i];
handlers.push("function(event) {instance.$methods." + handler + "}");
handlers.push('function(event) {' + handler.modifiers + ' instance.$methods.' + handler.method + '}');
}
eventListenersCode += eventType + ": [" + handlers.join(",") + "],";
eventListenersCode += eventType + ': [' + handlers.join(",") + '],';
}

// Remove the ending comma, and close the object
Expand All @@ -747,7 +764,7 @@

// Generate code for the metadata
for (var key in vnode.meta) {
metadataCode += key + ": " + vnode.meta[key] + ",";
metadataCode += key + ': ' + vnode.meta[key] + ',';
}

// Remove ending comma, and close meta object
Expand Down
Loading

0 comments on commit 6e1f19a

Please sign in to comment.