mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
basic project infrastructure
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import QWeb from "../core/qweb";
|
||||
import * as utils from "../core/utils";
|
||||
import Widget from "../core/widget";
|
||||
|
||||
|
||||
(<any>window).core = {
|
||||
QWeb,
|
||||
utils,
|
||||
Widget
|
||||
}
|
||||
@@ -75,9 +75,9 @@ export class Context {
|
||||
}
|
||||
}
|
||||
addLine(line: string) {
|
||||
const prefix = new Array(this.indentLevel).join('\t');
|
||||
const prefix = new Array(this.indentLevel).join("\t");
|
||||
const lastChar = line[line.length - 1];
|
||||
const suffix = lastChar !=='}' && lastChar !== '{' ? ';' : '';
|
||||
const suffix = lastChar !== "}" && lastChar !== "{" ? ";" : "";
|
||||
this.code.push(prefix + line + suffix);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import QWeb from "./qweb";
|
||||
|
||||
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);
|
||||
this.setEnvironment(parent.env);
|
||||
}
|
||||
}
|
||||
|
||||
async willStart() {}
|
||||
|
||||
mounted() {}
|
||||
|
||||
willUnmount() {}
|
||||
|
||||
destroyed() {}
|
||||
//--------------------------------------------------------------------------
|
||||
// Public
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
async mount(target: HTMLElement) {
|
||||
await this.willStart();
|
||||
await this.render();
|
||||
target.appendChild(this.el!);
|
||||
}
|
||||
|
||||
destroy() {}
|
||||
|
||||
setEnvironment(env: Env | null) {
|
||||
this.env = env ? Object.create(env) : null;
|
||||
if (this.env) {
|
||||
this.env.qweb.addTemplate(this.name, this.template);
|
||||
delete this.template;
|
||||
}
|
||||
}
|
||||
|
||||
async updateState(newState: Object) {
|
||||
Object.assign(this.state, newState);
|
||||
await this.render();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Private
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
async render() {
|
||||
const fragment = await this.env!.qweb.render(this.name, this);
|
||||
this._setElement(fragment.firstChild!);
|
||||
}
|
||||
|
||||
private _setElement(el: ChildNode) {
|
||||
if (this.el) {
|
||||
this.el.replaceWith(el);
|
||||
}
|
||||
this.el = el;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user