Skip to content

Commit

Permalink
more elegant solution to m-show and custom directives
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 5, 2017
1 parent 9ca9fb4 commit 5380d31
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
30 changes: 22 additions & 8 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@
var generatedObject = "{attrs: {";

// Array of all directives (to be generated later)
var allDirectives = [];
vnode.props.directives = [];

if (attrs) {
// Invoke any special directives that need to change values before code generation
Expand All @@ -1222,10 +1222,7 @@
var attrName = attrs[attr].name;

// If it is a directive, mark it as dynamic
if (directives[attrName]) {
allDirectives.push(attrs[attr]);
vnode.meta.shouldRender = true;
} else if (specialDirectives[attrName]) {
if (specialDirectives[attrName]) {
// Generate Special Directives
// Special directive found that generates code after initial generation, push it to its known special directives to run afterGenerate later
if (specialDirectives[attrName].afterGenerate) {
Expand All @@ -1245,6 +1242,9 @@

// Remove special directive
delete attrs[attr];
} else if (directives[attrName]) {
vnode.props.directives.push(attrs[attr]);
vnode.meta.shouldRender = true;
} else {
var normalizedProp = JSON.stringify(attrs[attr].value);
var compiledProp = compileTemplate(normalizedProp, true);
Expand Down Expand Up @@ -1274,12 +1274,16 @@
}

// Check for Directives
var allDirectives = vnode.props.directives;
if (allDirectives.length !== 0) {
generatedObject += ", directives: {";

for (var i = 0; i < allDirectives.length; i++) {
var directiveInfo = allDirectives[i];
generatedObject += '"' + directiveInfo.name + '": ' + JSON.stringify(directiveInfo.value) + ', ';
var normalizedValue = directiveInfo.literal ? directiveInfo.value : JSON.stringify(directiveInfo.value);
generatedObject += '"' + directiveInfo.name + '": ' + normalizedValue + ', ';
}

generatedObject = generatedObject.slice(0, -2) + "}";
}

Expand Down Expand Up @@ -1861,8 +1865,18 @@
};

specialDirectives[Moon.config.prefix + "show"] = {
duringPropGenerate: function (value, meta, vnode) {
return '"m-show": ' + compileTemplate(value, false) + ', ';
beforeGenerate: function (value, meta, vnode, parentVNode) {
var runTimeShowDirective = {
name: Moon.config.prefix + "show",
value: compileTemplate(value, false),
literal: true
};

if (!vnode.props.directives) {
vnode.props.directives = [runTimeShowDirective];
} else {
vnode.props.directives.push(runTimeShowDirective);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/compiler/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const generateProps = function(vnode, parentVNode) {
let generatedObject = "{attrs: {";

// Array of all directives (to be generated later)
let allDirectives = [];
vnode.props.directives = [];

if(attrs) {
// Invoke any special directives that need to change values before code generation
Expand All @@ -26,10 +26,7 @@ const generateProps = function(vnode, parentVNode) {
const attrName = attrs[attr].name;

// If it is a directive, mark it as dynamic
if(directives[attrName]) {
allDirectives.push(attrs[attr]);
vnode.meta.shouldRender = true;
} else if(specialDirectives[attrName]) {
if(specialDirectives[attrName]) {
// Generate Special Directives
// Special directive found that generates code after initial generation, push it to its known special directives to run afterGenerate later
if(specialDirectives[attrName].afterGenerate) {
Expand All @@ -49,6 +46,9 @@ const generateProps = function(vnode, parentVNode) {

// Remove special directive
delete attrs[attr];
} else if(directives[attrName]) {
vnode.props.directives.push(attrs[attr]);
vnode.meta.shouldRender = true;
} else {
const normalizedProp = JSON.stringify(attrs[attr].value);
const compiledProp = compileTemplate(normalizedProp, true);
Expand Down Expand Up @@ -78,12 +78,16 @@ const generateProps = function(vnode, parentVNode) {
}

// Check for Directives
let allDirectives = vnode.props.directives;
if(allDirectives.length !== 0) {
generatedObject += ", directives: {";

for(var i = 0; i < allDirectives.length; i++) {
let directiveInfo = allDirectives[i];
generatedObject += `"${directiveInfo.name}": ${JSON.stringify(directiveInfo.value)}, `;
const normalizedValue = directiveInfo.literal ? directiveInfo.value : JSON.stringify(directiveInfo.value);
generatedObject += `"${directiveInfo.name}": ${normalizedValue}, `;
}

generatedObject = generatedObject.slice(0, -2) + "}";
}

Expand Down
14 changes: 12 additions & 2 deletions src/directives/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ specialDirectives[Moon.config.prefix + "if"] = {
}

specialDirectives[Moon.config.prefix + "show"] = {
duringPropGenerate: function(value, meta, vnode) {
return `"m-show": ${compileTemplate(value, false)}, `;
beforeGenerate: function(value, meta, vnode, parentVNode) {
const runTimeShowDirective = {
name: Moon.config.prefix + "show",
value: compileTemplate(value, false),
literal: true
}

if(!vnode.props.directives) {
vnode.props.directives = [runTimeShowDirective];
} else {
vnode.props.directives.push(runTimeShowDirective);
}
}
}

Expand Down

0 comments on commit 5380d31

Please sign in to comment.