From e0956a2cd090ed15537374fc2f7fc8fdb617ff7f Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Fri, 3 Feb 2017 18:06:02 -0800 Subject: [PATCH] support for custom keycodes --- dist/moon.js | 55 +++++++++++++++++++++++---------------- dist/moon.min.js | 2 +- src/directives/default.js | 17 +++++++++++- src/global/api.js | 3 ++- src/index.js | 7 ----- 5 files changed, 51 insertions(+), 33 deletions(-) diff --git a/dist/moon.js b/dist/moon.js index 102d9c1b..2911af75 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -17,13 +17,6 @@ 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 ======= */ @@ -88,9 +81,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; @@ -104,9 +97,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) + ")"; }; /** @@ -618,7 +611,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); @@ -647,7 +640,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); @@ -686,14 +679,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(","); @@ -701,21 +694,36 @@ 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 eventModifiersCode = { + stop: 'event.stopPropagation();', + prevent: 'event.preventDefault();', + ctrl: 'if(!event.ctrlKey) {return;};', + shift: 'if(!event.shiftKey) {return;};', + alt: 'if(!event.altKey) {return;};' + }; + var splitVal = value.split(":"); // Extract modifiers and the event 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]]; + var rawModifier = rawModifiers[i]; + if (Moon.config.keyCodes[rawModifier]) { + modifiers += "if(event.keyCode !== " + Moon.config.keyCodes[rawModifier] + ") {return;};"; + } else if (eventModifiersCode[rawModifier]) { + modifiers += eventModifiersCode[rawModifier]; + } } // Extract method to call afterwards @@ -726,7 +734,7 @@ if (!params) { params = "event"; } - methodToCall += '(' + params + ')'; + methodToCall += "(" + params + ")"; // Code for all metadata var metadataCode = "{"; @@ -751,9 +759,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) {' + handler.modifiers + ' instance.$methods.' + handler.method + '}'); + 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 @@ -764,7 +772,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 @@ -1022,7 +1030,8 @@ */ Moon.config = { silent: false, - prefix: "m-" + prefix: "m-", + keyCodes: {} }; /** diff --git a/dist/moon.min.js b/dist/moon.min.js index ef6a4580..c1576b01 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 s=this;this.$id=o++,this.$name=this.$opts.name||"root",this.$parent=this.$opts.parent||null,this.$data=this.$opts.data||{},this.$render=this.$opts.render||x,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"("+p(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 "+p(e,!0,u)+"; })"},n[t.config.prefix+"on"]=function(t,e,n){var r=t.split(":"),o=r[0].split("."),s=o[0],u="";o.shift();for(var c=0;c",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",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})},C=function(t){for(var e={type:"ROOT",children:[]},n={current:0,tokens:t};n.current