The ability to attach attrs when embedding widgets

This commit is contained in:
Robin Ward
2017-09-28 16:08:14 -04:00
parent 502f910eb5
commit df81d109e5
4 changed files with 61 additions and 8 deletions

View File

@ -1,20 +1,45 @@
function resolve(path) {
if (path.indexOf('settings') === 0) {
return `this.${path}`;
} else if (path.indexOf('parentState') === 0) {
return `attrs._${path}`;
}
return path;
}
function sexp(value) {
if (value.path.original === "hash") {
let result = [];
value.hash.pairs.forEach(p => {
result.push(`"${p.key}": ${p.value.original}`);
});
return `{ ${result.join(", ")} }`;
}
}
function argValue(arg) {
let value = arg.value;
if (value.type === "SubExpression") {
return sexp(arg.value);
} else if (value.type === "PathExpression") {
return value.original;
}
}
function mustacheValue(node, state) {
let path = node.path.original;
switch(path) {
case 'attach':
const widgetName = node.hash.pairs.find(p => p.key === "widget").value.value;
return `this.attach("${widgetName}", state ? $.extend({}, attrs, { _parentState: state }) : attrs)`;
let widgetName = node.hash.pairs.find(p => p.key === "widget").value.value;
let attrs = node.hash.pairs.find(p => p.key === "attrs");
if (attrs) {
return `this.attach("${widgetName}", ${argValue(attrs)})`;
}
return `this.attach("${widgetName}", attrs)`;
break;
case 'yield':
return `this.attrs.contents()`;