From a2146ffa83cb8031cbe58734801f20d521f89745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 20 Jan 2019 09:25:41 +0100 Subject: [PATCH] fix widget class --- src/core/widget.ts | 25 ++++++++++++++++--------- tests/widget.test.ts | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/core/widget.ts b/src/core/widget.ts index 879ff3c9..20d3cd75 100644 --- a/src/core/widget.ts +++ b/src/core/widget.ts @@ -1,10 +1,11 @@ import QWeb from "./qweb_vdom"; -// import {init} from "../libs/snabbdom/src/snabbdom" -// import sdProps from "../libs/snabbdom/src/modules/props" -// import sdListeners from "../libs/snabbdom/src/modules/eventlisteners" +import {init} from "../libs/snabbdom/src/snabbdom" +import sdProps from "../libs/snabbdom/src/modules/props" +import sdListeners from "../libs/snabbdom/src/modules/eventlisteners" +import { VNode } from "../libs/snabbdom/src/vnode"; -// const patch = init([sdProps, sdListeners]); +const patch = init([sdProps, sdListeners]); export interface Env { qweb: QWeb; @@ -15,11 +16,12 @@ export interface Env { export default class Widget { name: string = "widget"; template: string = "
"; + vnode: VNode | null = null; parent: Widget | null; children: Widget[] = []; env: Env | null = null; - el: ChildNode | null = null; + el: HTMLElement | null = null; state: Object = {}; refs: { [key: string]: Widget } = {}; @@ -53,6 +55,8 @@ export default class Widget { this.env!.qweb.addTemplate(this.name, this.template); delete this.template; await this.render(); + + target.appendChild(this.el!); } @@ -82,10 +86,13 @@ export default class Widget { //-------------------------------------------------------------------------- async render() { - // const vnode = await this.env!.qweb.render(this.name, this); - // patch(this.el, vnode); - - // this._setElement(fragment.firstChild!); + let vnode = await this.env!.qweb.render(this.name, this); + if (!this.el) { + this.el = document.createElement(vnode.sel!); + } + patch(this.vnode || this.el, vnode); + this.vnode = vnode; + } // private _setElement(el: ChildNode) { diff --git a/tests/widget.test.ts b/tests/widget.test.ts index 8b7b1707..f96cee7f 100644 --- a/tests/widget.test.ts +++ b/tests/widget.test.ts @@ -1,5 +1,5 @@ import Widget from "../src/core/widget"; -import QWeb from "../src/core/qweb"; +import QWeb from "../src/core/qweb_vdom"; function makeWidget(W: typeof Widget): Widget { const env = {