-
+
+
🦉 Odoo Web Lab 🦉
v
Note: these examples require a recent browser to work without a transpilation step.
+
+
+
+
`;
@@ -46,7 +53,9 @@ class Playground extends Component {
currentTab: "js",
js: SAMPLES[0].code,
css: SAMPLES[0].css || "",
- xml: ""
+ xml: SAMPLES[0].xml || DEFAULT_XML,
+ error: false,
+ displayWelcome: true
};
}
@@ -59,9 +68,24 @@ class Playground extends Component {
this.editor.session.setMode("ace/mode/javascript");
}
- runCode() {
+ async runCode() {
this.updateStateFromEditor();
+ // check templates
+ var qweb = new owl.core.QWeb();
+ var error = false;
+ try {
+ qweb.loadTemplates(this.state.xml);
+ } catch (e) {
+ error = e;
+ }
+
+ await this.updateState({ error, displayWelcome: false });
+ if (error) {
+ this.refs.content.innerHTML = "";
+ return;
+ }
+
// create iframe
const iframe = document.createElement("iframe");
@@ -74,7 +98,10 @@ class Playground extends Component {
owlScript.addEventListener("load", () => {
const script = doc.createElement("script");
script.type = "text/javascript";
- script.innerHTML = this.state.js;
+ const content = `window.TEMPLATES = \`${this.state.xml}\`\n${
+ this.state.js
+ }`;
+ script.innerHTML = content;
doc.body.appendChild(script);
});
doc.head.appendChild(owlScript);
@@ -93,7 +120,7 @@ class Playground extends Component {
this.updateState({
js: sample.code,
css: sample.css || "",
- xml: ""
+ xml: sample.xml || DEFAULT_XML
});
this.editor.setValue(this.state[this.state.currentTab], -1);
}
diff --git a/src/samples.js b/src/samples.js
index 97c7180e..5fe07339 100644
--- a/src/samples.js
+++ b/src/samples.js
@@ -1,7 +1,7 @@
const HELLO_WORLD = `class HelloWorld extends owl.core.Component {
constructor(...args) {
super(...args);
- this.inlineTemplate = \`
Hello
\`;
+ this.inlineTemplate = \`
Hello
\`;
}
}
@@ -13,9 +13,14 @@ const hello = new HelloWorld(env, { name: "World" });
hello.mount(document.body);
`;
+const HELLO_WORLD_CSS = `.hello {
+ color: darkred;
+ font-size: 30px;
+}`;
+
const HELLO_WORLD_ESNEXT = `// This example will not work if your browser does not support ESNext Class Fields
class HelloWorld extends owl.core.Component {
- inlineTemplate = \`
Hello
\`;
+ inlineTemplate = \`
Hello
\`;
}
const env = {
@@ -99,12 +104,7 @@ for (let i = 1; i < 16000; i++) {
class Counter extends owl.core.Component {
constructor(parent, props) {
super(parent, props);
- this.inlineTemplate = \`
-
- -
- Value:
- +
-
\`;
+ this.template = "counter";
this.state = {
counter: props.initialState || 0
};
@@ -122,13 +122,7 @@ class Message extends owl.core.Component {
constructor(parent, props) {
super(parent, props);
- this.inlineTemplate = \`
-
-
-
- Remove
-
-
\`;
+ this.template = "message";
this.widgets = { Counter };
}
@@ -142,34 +136,10 @@ class Message extends owl.core.Component {
//------------------------------------------------------------------------------
// Root Widget
//------------------------------------------------------------------------------
-const template = \`
-
-
-
- -
- Value:
- +
-
-
10 messages
-
20 messages
-
500 messages
-
1000 messages
-
5000 messages
-
15000 messages
-
-
-
\`;
-
class App extends owl.core.Component {
constructor(parent, props) {
super(parent, props);
- this.inlineTemplate = template;
+ this.template = "root";
this.widgets = { Message };
this.state = {
messages: messages.slice(0, 10)
@@ -198,9 +168,9 @@ class App extends owl.core.Component {
//------------------------------------------------------------------------------
// Application initialization
//------------------------------------------------------------------------------
-const env = {
- qweb: new owl.core.QWeb()
-};
+const qweb = new owl.core.QWeb();
+qweb.loadTemplates(TEMPLATES);
+const env = {qweb};
const app = new App(env);
app.mount(document.body);
@@ -252,6 +222,45 @@ const BENCHMARK_APP_CSS = `.main {
padding: 5px;
}`;
+const BENCHMARK_APP_XML = `
+
+
+
+ -
+ Value:
+ +
+
+
10 messages
+
20 messages
+
500 messages
+
1000 messages
+
5000 messages
+
15000 messages
+
+
+
+
+
+
+
+ Remove
+
+
+
+
+ -
+ Value:
+ +
+
+
+ `;
+
const STATE_MANAGEMENT = `// todo`;
const EMPTY = ``;
@@ -259,11 +268,13 @@ const EMPTY = ``;
export const SAMPLES = [
{
description: "Hello World",
- code: HELLO_WORLD
+ code: HELLO_WORLD,
+ css: HELLO_WORLD_CSS
},
{
description: "Hello World (ESNext)",
- code: HELLO_WORLD_ESNEXT
+ code: HELLO_WORLD_ESNEXT,
+ css: HELLO_WORLD_CSS
},
{
description: "Widget Composition",
@@ -272,7 +283,8 @@ export const SAMPLES = [
{
description: "Benchmark application",
code: BENCHMARK_APP,
- css: BENCHMARK_APP_CSS
+ css: BENCHMARK_APP_CSS,
+ xml: BENCHMARK_APP_XML
},
{
description: "State management app",