From 07f93177da594f078275a5918af529b53f8cc2d7 Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Mon, 13 Feb 2017 18:21:35 -0800 Subject: [PATCH] slots support for functional components --- dist/moon.js | 32 ++++++++++++++++++++++++++++---- dist/moon.min.js | 2 +- src/util/util.js | 32 ++++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/dist/moon.js b/dist/moon.js index d4693361..71b94a45 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -105,6 +105,29 @@ return compiled; }; + /** + * Extracts the Slots From Component Children + * @param {Array} children + * @return {Object} extracted slots + */ + var getSlots = function (children) { + var slots = {}; + + // No Children Means No Slots + if (!children) { + return slots; + } + + slots.default = []; + + for (var i = 0; i < children.length; i++) { + var child = children[i]; + slots.default.push(child); + } + + return slots; + }; + /** * Creates a Virtual DOM Node * @param {String} type @@ -133,9 +156,9 @@ * @param {Object} component * @return {Object} Virtual DOM Node */ - var createComponent = function (type, props, children, meta, component) { + var createComponent = function (type, props, meta, children, component) { if (component.opts.functional) { - return createFunctionalComponent(type, props, children, meta, component); + return createFunctionalComponent(type, props, meta, children, component); } }; @@ -148,7 +171,7 @@ * @param {Object} functionalComponent * @return {Object} Virtual DOM Node */ - var createFunctionalComponent = function (type, props, children, meta, functionalComponent) { + var createFunctionalComponent = function (type, props, meta, children, functionalComponent) { var data = functionalComponent.opts.data || {}; // Merge data with provided props if (functionalComponent.opts.props) { @@ -158,7 +181,8 @@ } } return functionalComponent.opts.render(h, { - data: data + data: data, + slots: getSlots(children) }); }; diff --git a/dist/moon.min.js b/dist/moon.min.js index fe160a5a..9ea17305 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(t){this.$opts=t||{},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||C,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 e={},n={},r={},i={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;};"},o=0,s=function(e){t.config.silent||console.log(e)},u=function(t){console.error("[Moon] ERR: "+t)},a=function(t){for(var e={},n=t.attributes,r=n.length;r--;)e[n[r].name]=n[r].value;return t.__moon__attrs__=e,e},c=function(){return{shouldRender:!0,component:!1,eventListeners:{}}},p=function(t){var e=/\n/g,n=/"/g;return t.replace(e,"\\n").replace(n,'\\"')},f=function(t,e,n){var r=/{{([A-Za-z0-9_]+)([A-Za-z0-9_.()\[\]]+)?}}/gi,i=t;return t.replace(r,function(t,r,o){o||(o=""),i=n?n(i,t,r,o):e?i.replace(t,'" + instance.get("'+r+'")'+o+' + "'):i.replace(t,'instance.get("'+r+'")'+o)}),i},h=function(t,e,n,r,i){return{type:t,val:e,props:n,children:r,meta:i||c()}},l=function(t,e,n,r,i){if(i.opts.functional)return v(t,e,n,r,i)},v=function(t,e,n,r,i){var o=i.opts.data||{};if(i.opts.props)for(var s=0;s",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=E(t);S(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},S=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})},T=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))},E=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=S(t);T(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}))},S=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},T=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})},j=function(t){for(var e={type:"ROOT",children:[]},n={current:0,tokens:t};n.current