diff --git a/dist/moon.js b/dist/moon.js index 7d3aaf6b..102d9c1b 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -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 ======= */ @@ -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; @@ -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) + ')'; }; /** @@ -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); @@ -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); @@ -679,14 +686,14 @@ /* ======= 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(","); @@ -694,18 +701,22 @@ 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("("); @@ -715,7 +726,7 @@ if (!params) { params = "event"; } - methodToCall += "(" + params + ")"; + methodToCall += '(' + params + ')'; // Code for all metadata var metadataCode = "{"; @@ -724,9 +735,15 @@ // 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 @@ -734,9 +751,9 @@ 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 @@ -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 diff --git a/dist/moon.min.js b/dist/moon.min.js index 17dadd47..ef6a4580 100644 --- a/dist/moon.min.js +++ b/dist/moon.min.js @@ -5,4 +5,4 @@ * Free to use under the MIT license. * https://kingpixil.github.io/license */ -!function(t,e){"object"==typeof module&&module.exports?module.exports=e():t.Moon=e()}(this,function(){"use strict";function t(r){this.$opts=r||{};var o=this;this.$id=i++,this.$name=this.$opts.name||"root",this.$parent=this.$opts.parent||null,this.$data=this.$opts.data||{},this.$render=this.$opts.render||k,this.$hooks=this.$opts.hooks||{},this.$methods=this.$opts.methods||{},this.$events={},this.$dom={},this.$destroyed=!1,this.$initialRender=!0,this.$queued=!1,n[t.config.prefix+"if"]=function(t,e,n){return"("+a(t,!1)+") ? "+e+" : ''"},n[t.config.prefix+"for"]=function(t,e,n){var r=t.split(" in "),i=r[0].split(","),o='instance.get("'+r[1]+'")',s=i.join(","),u=function(t,e,n,r){return i.indexOf(n)===-1?t:t.replace(e,'" + '+n+r+' + "')};return"instance.renderLoop("+o+", function("+s+") { return "+a(e,!0,u)+"; })"},n[t.config.prefix+"on"]=function(t,e,n){var r=t.split(":"),i=r[0].split("."),o=i[0];i.shift();var s=r[1].split("("),u=s[0];s.shift();var c=s.join(",").slice(0,-1);c||(c="event"),u+="("+c+")";var a="{",h="{";n.meta.eventListeners[o]?n.meta.eventListeners[o].push(u):n.meta.eventListeners[o]=[u];for(var f in n.meta.eventListeners){for(var l=[],v=0;v",t.current);return r===-1?(t.tokens.push({type:"comment",value:e.slice(t.current)}),void(t.current=n)):(t.tokens.push({type:"comment",value:e.slice(t.current,r)}),void(t.current=r+3))},O=function(t){var e=t.input,n=(e.length,"/"===e.charAt(t.current+1));e.charAt(t.current);t.tokens.push({type:"tagStart",close:n}),t.current+=n?2:1;var r=A(t);E(t);var i="/"===e.charAt(t.current);t.tokens.push({type:"tagEnd",close:!1}),t.current+=i?2:1,i&&(t.tokens.push({type:"tagStart",close:!0}),t.tokens.push({type:"tag",value:r}),t.tokens.push({type:"attribute",value:{}}),t.tokens.push({type:"tagEnd",close:!1}))},A=function(t){for(var e=t.input,n=e.length,r=t.current;r"!==i&&" "!==i)break;r++}for(var o=r;o"===i||" "===i)break;o++}var s=e.slice(r,o);return t.tokens.push({type:"tag",value:s}),t.current=o,s},E=function(t){for(var e=t.input,n=e.length,r=t.current,i={},o="",s=/([^=\s]*)(=?)("[^"]*"|[^\s"]*)/gi;r"===u||"/"===u)break;o+=u,r++}o.replace(s,function(t,e,n,r){var o=r[0],s=r[r.length-1];("'"===o&&"'"===s||'"'===o&&'"'===s)&&(r=r.slice(1,-1)),r||(r=e),e&&r&&(i[e]=r)}),t.current=r,t.tokens.push({type:"attribute",value:i})},M=function(t){for(var e={type:"ROOT",children:[]},n={current:0,tokens:t};n.current",t.current);return r===-1?(t.tokens.push({type:"comment",value:e.slice(t.current)}),void(t.current=n)):(t.tokens.push({type:"comment",value:e.slice(t.current,r)}),void(t.current=r+3))},A=function(t){var e=t.input,n=(e.length,"/"===e.charAt(t.current+1));e.charAt(t.current);t.tokens.push({type:"tagStart",close:n}),t.current+=n?2:1;var r=E(t);M(t);var i="/"===e.charAt(t.current);t.tokens.push({type:"tagEnd",close:!1}),t.current+=i?2:1,i&&(t.tokens.push({type:"tagStart",close:!0}),t.tokens.push({type:"tag",value:r}),t.tokens.push({type:"attribute",value:{}}),t.tokens.push({type:"tagEnd",close:!1}))},E=function(t){for(var e=t.input,n=e.length,r=t.current;r"!==i&&" "!==i)break;r++}for(var o=r;o"===i||" "===i)break;o++}var s=e.slice(r,o);return t.tokens.push({type:"tag",value:s}),t.current=o,s},M=function(t){for(var e=t.input,n=e.length,r=t.current,i={},o="",s=/([^=\s]*)(=?)("[^"]*"|[^\s"]*)/gi;r"===u||"/"===u)break;o+=u,r++}o.replace(s,function(t,e,n,r){var o=r[0],s=r[r.length-1];("'"===o&&"'"===s||'"'===o&&'"'===s)&&(r=r.slice(1,-1)),r||(r=e),e&&r&&(i[e]=r)}),t.current=r,t.tokens.push({type:"attribute",value:i})},N=function(t){for(var e={type:"ROOT",children:[]},n={current:0,tokens:t};n.current