Files
owl/src/core/widget.ts
T

98 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-01-17 18:08:39 +01:00
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"
// const patch = init([sdProps, sdListeners]);
2019-01-16 13:19:58 +01:00
export interface Env {
qweb: QWeb;
services: { [key: string]: any };
[key: string]: any;
}
export default class Widget {
name: string = "widget";
template: string = "<div></div>";
parent: Widget | null;
children: Widget[] = [];
env: Env | null = null;
el: ChildNode | null = null;
state: Object = {};
refs: { [key: string]: Widget } = {};
//--------------------------------------------------------------------------
// Lifecycle
//--------------------------------------------------------------------------
constructor(parent: Widget | null, props?: any) {
this.parent = parent;
if (parent) {
parent.children.push(this);
2019-01-16 23:03:51 +01:00
if (parent.env) {
this.setEnvironment(parent.env);
}
2019-01-16 13:19:58 +01:00
}
}
async willStart() {}
mounted() {}
willUnmount() {}
destroyed() {}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
async mount(target: HTMLElement) {
await this.willStart();
2019-01-17 12:49:13 +01:00
this.env!.qweb.addTemplate(this.name, this.template);
delete this.template;
2019-01-16 13:19:58 +01:00
await this.render();
target.appendChild(this.el!);
}
2019-01-16 23:03:51 +01:00
destroy() {
if (this.el) {
this.el.remove();
2019-01-16 13:19:58 +01:00
}
}
2019-01-16 23:03:51 +01:00
setEnvironment(env: Env) {
this.env = Object.create(env);
}
2019-01-17 18:08:39 +01:00
/**
* DOCSTRIGN
*
* @param {Object} newState
* @memberof Widget
*/
2019-01-16 13:19:58 +01:00
async updateState(newState: Object) {
Object.assign(this.state, newState);
await this.render();
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
async render() {
2019-01-17 18:08:39 +01:00
// const vnode = await this.env!.qweb.render(this.name, this);
// patch(this.el, vnode);
// this._setElement(fragment.firstChild!);
2019-01-16 13:19:58 +01:00
}
2019-01-17 18:08:39 +01:00
// private _setElement(el: ChildNode) {
// if (this.el) {
// this.el.replaceWith(el);
// }
// this.el = el;
// }
2019-01-16 13:19:58 +01:00
}