2020-11-26 16:45:25 +01:00
|
|
|
import { Component } from "./component/component";
|
|
|
|
|
import { ComponentNode } from "./component/component_node";
|
|
|
|
|
import { Scheduler } from "./component/scheduler";
|
|
|
|
|
import { TemplateSet } from "./qweb/template_helpers";
|
|
|
|
|
|
|
|
|
|
// reimplement dev mode stuff see last change in 0f7a8289a6fb8387c3c1af41c6664b2a8448758f
|
|
|
|
|
|
2021-10-20 16:08:31 +02:00
|
|
|
interface Config {
|
|
|
|
|
dev?: boolean;
|
|
|
|
|
env?: { [key: string]: any };
|
|
|
|
|
translatableAttributes?: string[];
|
|
|
|
|
translateFn?: (s: string) => string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const DEV_MSG = `Owl is running in 'dev' mode.
|
|
|
|
|
|
|
|
|
|
This is not suitable for production use.
|
|
|
|
|
See https://github.com/odoo/owl/blob/master/doc/reference/config.md#mode for more information.`;
|
|
|
|
|
|
2020-11-26 16:45:25 +01:00
|
|
|
export class App<T extends typeof Component = any> extends TemplateSet {
|
|
|
|
|
Root: T;
|
|
|
|
|
props: any;
|
|
|
|
|
env: any = {};
|
|
|
|
|
scheduler = new Scheduler(window.requestAnimationFrame.bind(window));
|
|
|
|
|
root: ComponentNode | null = null;
|
|
|
|
|
|
|
|
|
|
constructor(Root: T, props?: any) {
|
|
|
|
|
super();
|
|
|
|
|
this.Root = Root;
|
|
|
|
|
this.props = props;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 16:08:31 +02:00
|
|
|
configure(config: Config) {
|
|
|
|
|
if (config.dev) {
|
|
|
|
|
this.dev = config.dev;
|
|
|
|
|
console.info(DEV_MSG);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.env) {
|
|
|
|
|
this.env = config.env;
|
2020-11-26 16:45:25 +01:00
|
|
|
}
|
2021-10-20 16:08:31 +02:00
|
|
|
if (config.translateFn) {
|
|
|
|
|
this.translateFn = config.translateFn;
|
2021-10-13 16:49:34 +02:00
|
|
|
}
|
2021-10-20 16:08:31 +02:00
|
|
|
if (config.translatableAttributes) {
|
|
|
|
|
this.translatableAttributes = config.translatableAttributes;
|
2021-10-13 16:49:34 +02:00
|
|
|
}
|
2020-11-26 16:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mount(target: HTMLElement): Promise<InstanceType<T>> {
|
|
|
|
|
if (!(target instanceof HTMLElement)) {
|
|
|
|
|
throw new Error("Cannot mount component: the target is not a valid DOM element");
|
|
|
|
|
}
|
|
|
|
|
if (!document.body.contains(target)) {
|
|
|
|
|
throw new Error("Cannot mount a component on a detached dom node");
|
|
|
|
|
}
|
|
|
|
|
const node = new ComponentNode(this.Root, this.props, this);
|
|
|
|
|
this.root = node;
|
|
|
|
|
return node.mountComponent(target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
|
if (this.root) {
|
|
|
|
|
this.root.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|