From 9622d0142ffa72a380cada03079ed99ebdbbcf94 Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Fri, 10 Feb 2017 17:01:31 -0800 Subject: [PATCH] add the ability to create custom keycode aliases --- dist/moon.js | 52 +++++++++++++++++++++------------------ dist/moon.min.js | 2 +- src/directives/default.js | 9 ------- src/global/api.js | 6 ++++- src/index.js | 8 ++++++ 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/dist/moon.js b/dist/moon.js index d175112b..ddca8981 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -17,6 +17,14 @@ 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;};', + enter: 'if(event.keyCode !== 13) {return;};' + }; var id = 0; /* ======= Global Utilities ======= */ @@ -91,9 +99,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; @@ -631,7 +639,7 @@ specialDirectives[prop].beforeGenerate(props[prop], vnode); } } - generatedObject += "\"" + prop + "\": " + compileTemplate(JSON.stringify(props[prop]), true) + ", "; + generatedObject += '"' + prop + '": ' + compileTemplate(JSON.stringify(props[prop]), true) + ', '; } generatedObject = generatedObject.slice(0, -2) + "}"; @@ -651,7 +659,7 @@ var generatedObject = "{"; for (var type in listeners) { - generatedObject += "\"" + type + "\": [" + generateArray(listeners[type]) + "], "; + generatedObject += '"' + type + '": [' + generateArray(listeners[type]) + '], '; } generatedObject = generatedObject.slice(0, -2) + "}"; @@ -669,9 +677,9 @@ for (var key in meta) { if (key === 'eventListeners') { - generatedObject += "\"" + key + "\": " + generateEventListeners(meta[key]) + ", "; + generatedObject += '"' + key + '": ' + generateEventListeners(meta[key]) + ', '; } else { - generatedObject += "\"" + key + "\": " + meta[key] + ", "; + generatedObject += '"' + key + '": ' + meta[key] + ', '; } } @@ -689,7 +697,7 @@ var generatedArray = ""; for (var i = 0; i < arr.length; i++) { - generatedArray += arr[i] + ", "; + generatedArray += arr[i] + ', '; } generatedArray = generatedArray.slice(0, -2); @@ -704,7 +712,7 @@ * @return {String} "h" call */ var createCall = function (vnode, children) { - var call = "h(\"" + vnode.type + "\", "; + var call = 'h("' + vnode.type + '", '; call += generateProps(vnode) + ", "; call += generateMeta(vnode.meta) + ", "; call += children.length ? generateArray(children) : "\"\""; @@ -719,7 +727,7 @@ if (typeof el === "string") { // Escape newlines and double quotes, and compile the string - code += "\"" + compileTemplate(escapeString(el), true) + "\""; + code += '"' + compileTemplate(escapeString(el), true) + '"'; } else { // Recursively generate code for children var childrenCode = el.children.map(generateEl); @@ -975,7 +983,11 @@ Moon.config = { silent: false, prefix: "m-", - keyCodes: {} + keyCodes: function (keyCodes) { + for (var keyCode in keyCodes) { + eventModifiersCode[keyCode] = 'if(event.keyCode !== ' + keyCodes[keyCode] + ') {return;};'; + } + } }; /** @@ -1065,7 +1077,7 @@ specialDirectives[Moon.config.prefix + "if"] = { afterGenerate: function (value, code, vnode) { - return "(" + compileTemplate(value, false) + ") ? " + code + " : ''"; + return '(' + compileTemplate(value, false) + ') ? ' + code + ' : \'\''; } }; @@ -1074,30 +1086,22 @@ 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(","); code.replace(/instance\.get\("([^"]+)"\)/g, function (match, alias) { if (aliases.indexOf(alias) !== -1) { - code = code.replace(new RegExp("instance.get\\(\"" + alias + "\"\\)", "g"), alias); + code = code.replace(new RegExp('instance.get\\("' + alias + '"\\)', "g"), alias); } }); - return "instance.renderLoop(" + iteratable + ", function(" + params + ") { return " + code + "; })"; + return 'instance.renderLoop(' + iteratable + ', function(' + params + ') { return ' + code + '; })'; } }; specialDirectives[Moon.config.prefix + "on"] = { beforeGenerate: function (value, vnode) { - var eventModifiersCode = { - stop: 'event.stopPropagation();', - prevent: 'event.preventDefault();', - ctrl: 'if(!event.ctrlKey) {return;};', - shift: 'if(!event.shiftKey) {return;};', - alt: 'if(!event.altKey) {return;};' - }; - value = compileTemplate(value, false); var splitVal = value.split(":"); @@ -1118,7 +1122,7 @@ modifiers += eventModifiersCode[rawModifiers[i]]; } - var code = "function(event) {" + modifiers + "instance.$methods." + methodToCall + params + "}"; + var code = 'function(event) {' + modifiers + 'instance.$methods.' + methodToCall + params + '}'; if (!vnode.meta.eventListeners[eventToCall]) { vnode.meta.eventListeners[eventToCall] = [code]; } else { @@ -1129,7 +1133,7 @@ specialDirectives[Moon.config.prefix + "model"] = { beforeGenerate: function (value, vnode) { - var code = "function(event) {instance.set(\"" + value + "\", event.target.value)}"; + var code = 'function(event) {instance.set("' + value + '", event.target.value)}'; if (!vnode.meta.eventListeners["input"]) { vnode.meta.eventListeners["input"] = [code]; } else { diff --git a/dist/moon.min.js b/dist/moon.min.js index adb60c56..e17f4002 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(e,t){"object"==typeof module&&module.exports?module.exports=t():e.Moon=t()}(this,function(){"use strict";function e(e){this.$opts=e||{},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||x,this.$hooks=this.$opts.hooks||{},this.$methods=this.$opts.methods||{},this.$events={},this.$dom={},this.$destroyed=!1,this.$initialRender=!0,this.$queued=!1,this.init()}var t={},n={},r={},i=0,o=function(t){e.config.silent||console.log(t)},s=function(e){console.error("[Moon] ERR: "+e)},u=function(e){var t={};if(!e.attributes)return t;for(var n=e.attributes,r=0;r",e.current);return r===-1?(e.tokens.push({type:"comment",value:t.slice(e.current)}),void(e.current=n)):(e.tokens.push({type:"comment",value:t.slice(e.current,r)}),void(e.current=r+3))},G=function(e){var t=e.input,n=(t.length,"/"===t.charAt(e.current+1));t.charAt(e.current);e.tokens.push({type:"tagStart",close:n}),e.current+=n?2:1;var r=O(e);C(e);var i="/"===t.charAt(e.current);e.tokens.push({type:"tagEnd",close:!1}),e.current+=i?2:1,i&&(e.tokens.push({type:"tagStart",close:!0}),e.tokens.push({type:"tag",value:r}),e.tokens.push({type:"attribute",value:{}}),e.tokens.push({type:"tagEnd",close:!1}))},O=function(e){for(var t=e.input,n=t.length,r=e.current;r"!==i&&" "!==i)break;r++}for(var o=r;o"===i||" "===i)break;o++}var s=t.slice(r,o);return e.tokens.push({type:"tag",value:s}),e.current=o,s},C=function(e){for(var t=e.input,n=t.length,r=e.current,i={},o="",s=/([^=\s]*)(=?)("[^"]*"|[^\s"]*)/gi;r"===u||"/"===u)break;o+=u,r++}o.replace(s,function(e,t,n,r){var o=r[0],s=r[r.length-1];("'"===o&&"'"===s||'"'===o&&'"'===s)&&(r=r.slice(1,-1)),r||(r=t),t&&r&&(i[t]=r)}),e.current=r,e.tokens.push({type:"attribute",value:i})},E=function(e){for(var t={type:"ROOT",children:[]},n={current:0,tokens:e};n.current",e.current);return r===-1?(e.tokens.push({type:"comment",value:t.slice(e.current)}),void(e.current=n)):(e.tokens.push({type:"comment",value:t.slice(e.current,r)}),void(e.current=r+3))},G=function(e){var t=e.input,n=(t.length,"/"===t.charAt(e.current+1));t.charAt(e.current);e.tokens.push({type:"tagStart",close:n}),e.current+=n?2:1;var r=O(e);E(e);var i="/"===t.charAt(e.current);e.tokens.push({type:"tagEnd",close:!1}),e.current+=i?2:1,i&&(e.tokens.push({type:"tagStart",close:!0}),e.tokens.push({type:"tag",value:r}),e.tokens.push({type:"attribute",value:{}}),e.tokens.push({type:"tagEnd",close:!1}))},O=function(e){for(var t=e.input,n=t.length,r=e.current;r"!==i&&" "!==i)break;r++}for(var o=r;o"===i||" "===i)break;o++}var s=t.slice(r,o);return e.tokens.push({type:"tag",value:s}),e.current=o,s},E=function(e){for(var t=e.input,n=t.length,r=e.current,i={},o="",s=/([^=\s]*)(=?)("[^"]*"|[^\s"]*)/gi;r"===u||"/"===u)break;o+=u,r++}o.replace(s,function(e,t,n,r){var o=r[0],s=r[r.length-1];("'"===o&&"'"===s||'"'===o&&'"'===s)&&(r=r.slice(1,-1)),r||(r=t),t&&r&&(i[t]=r)}),e.current=r,e.tokens.push({type:"attribute",value:i})},S=function(e){for(var t={type:"ROOT",children:[]},n={current:0,tokens:e};n.current