add attributes module in widget (+lint)

This commit is contained in:
Géry Debongnie
2019-01-21 14:38:44 +01:00
parent a3bf470930
commit fbf099f1f3
5 changed files with 27 additions and 13 deletions
+1
View File
@@ -48,3 +48,4 @@ Before even thinking about using this in a real scenario:
- style is props? difference between props and attrs - style is props? difference between props and attrs
- text node - text node
- t-extend??? - t-extend???
- improve qweb generated code: do not assign object/array if no props/attrs/children
+2 -2
View File
@@ -1,9 +1,9 @@
import Widget from "../../src/core/widget"; import Widget from "../../src/core/widget";
const template = ` const template = `
<div> <div t-debug="1">
<button t-on-click="increment(-1)">-</button> <button t-on-click="increment(-1)">-</button>
<span>Value: <t t-esc="state.counter"/></span> <span style="font-weight:bold">Value: <t t-esc="state.counter"/></span>
<button t-on-click="increment(1)">+</button> <button t-on-click="increment(1)">+</button>
</div> </div>
`; `;
+9 -6
View File
@@ -34,7 +34,7 @@ export class Context {
withParent(node: number): Context { withParent(node: number): Context {
const newContext: Context = Object.create(this); const newContext: Context = Object.create(this);
if (this === this.rootContext && this.parentNode) { if (this === this.rootContext && this.parentNode) {
throw new Error('A template should not have more than one root node'); throw new Error("A template should not have more than one root node");
} }
newContext.parentNode = node; newContext.parentNode = node;
if (!this.rootContext.rootNode) { if (!this.rootContext.rootNode) {
@@ -352,7 +352,6 @@ export default class QWeb {
let p = let p =
attrs.length + tattrs.length > 0 ? `{attrs:{${attrs.join(",")}}}` : "{}"; attrs.length + tattrs.length > 0 ? `{attrs:{${attrs.join(",")}}}` : "{}";
ctx.addLine(`let c${nodeID} = [], p${nodeID} = ${p}`); ctx.addLine(`let c${nodeID} = [], p${nodeID} = ${p}`);
for (let id of tattrs) { for (let id of tattrs) {
ctx.addLine(`if (_${id} instanceof Array) {`); ctx.addLine(`if (_${id} instanceof Array) {`);
ctx.indent(); ctx.indent();
@@ -662,14 +661,18 @@ const widgetDirective: Directive = {
let defID = ctx.generateID(); let defID = ctx.generateID();
ctx.addLine(`let _${dummyID} = {} // DUMMY`); ctx.addLine(`let _${dummyID} = {} // DUMMY`);
ctx.addLine(`c${ctx.parentNode}.push(_${dummyID})`); ctx.addLine(`c${ctx.parentNode}.push(_${dummyID})`);
let props = node.getAttribute('t-props'); let props = node.getAttribute("t-props");
let widgetID = ctx.generateID(); let widgetID = ctx.generateID();
ctx.addLine(`let _${widgetID} = new context.widgets['${value}'](context, ${props})`); ctx.addLine(
ctx.addLine(`let def${defID} = _${widgetID}.mount().then(vnode=>Object.assign(_${dummyID}, vnode))`); `let _${widgetID} = new context.widgets['${value}'](context, ${props})`
);
ctx.addLine(
`let def${defID} = _${widgetID}.mount().then(vnode=>Object.assign(_${dummyID}, vnode))`
);
ctx.addLine(`context._TEMP.push(def${defID})`); ctx.addLine(`context._TEMP.push(def${defID})`);
// split into extra directive? // split into extra directive?
let ref = node.getAttribute('t-ref'); let ref = node.getAttribute("t-ref");
if (ref) { if (ref) {
ctx.addLine(`context.refs['${ref}'] = _${widgetID}`); ctx.addLine(`context.refs['${ref}'] = _${widgetID}`);
} }
+2 -2
View File
@@ -1,11 +1,11 @@
import QWeb from "./qweb_vdom"; import QWeb from "./qweb_vdom";
import { init } from "../libs/snabbdom/src/snabbdom"; import { init } from "../libs/snabbdom/src/snabbdom";
import sdProps from "../libs/snabbdom/src/modules/props";
import sdListeners from "../libs/snabbdom/src/modules/eventlisteners"; import sdListeners from "../libs/snabbdom/src/modules/eventlisteners";
import sdAttrs from "../libs/snabbdom/src/modules/attributes";
import { VNode } from "../libs/snabbdom/src/vnode"; import { VNode } from "../libs/snabbdom/src/vnode";
const patch = init([sdProps, sdListeners]); const patch = init([sdListeners, sdAttrs]);
export interface Env { export interface Env {
qweb: QWeb; qweb: QWeb;
+10
View File
@@ -53,6 +53,16 @@ describe("basic widget properties", () => {
await click((<HTMLElement>counter.el).getElementsByTagName("button")[0]); await click((<HTMLElement>counter.el).getElementsByTagName("button")[0]);
expect(target.innerHTML).toBe("<div>1<button>Inc</button></div>"); expect(target.innerHTML).toBe("<div>1<button>Inc</button></div>");
}); });
test("widget style and classname", async () => {
class StyledWidget extends Widget {
template= `<div style="font-weight:bold;" class="some-class">world</div>`;
}
const widget = makeWidget(StyledWidget);
const target = document.createElement("div");
await widget.mount(target);
expect(target.innerHTML).toBe(`<div style="font-weight:bold;" class="some-class">world</div>`);
});
}); });
describe("lifecycle hooks", () => { describe("lifecycle hooks", () => {