basic project infrastructure

This commit is contained in:
Géry Debongnie
2019-01-16 13:19:58 +01:00
parent 0f3b2d10da
commit f0d529139b
16 changed files with 671 additions and 9 deletions
+10
View File
@@ -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
}
+2 -2
View File
@@ -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);
}
}
View File
+79
View File
@@ -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;
}
}