Skip to content

Commit f9db9f6

Browse files
author
Martin Doyen
committed
[IMP] awesome_owl: discover owl components
1 parent 1324a04 commit f9db9f6

File tree

13 files changed

+197
-2
lines changed

13 files changed

+197
-2
lines changed

awesome_owl/static/src/card/card.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** @odoo-module **/
2+
3+
import { Component, useState } from "@odoo/owl";
4+
5+
export class Card extends Component {
6+
static template = "awesome_owl.card";
7+
static props = {
8+
title: String,
9+
slots: Object,
10+
};
11+
12+
setup() {
13+
this.state = useState({ isOpen: true });
14+
}
15+
16+
toggle() {
17+
this.state.isOpen = !this.state.isOpen;
18+
}
19+
}

awesome_owl/static/src/card/card.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_owl.card">
5+
<div class="card d-inline-block m-2" style="width: 18rem;">
6+
<div class="card-body">
7+
<h5 class="card-title">
8+
<t t-out="props.title" />
9+
<button class="btn btn-secondary" t-on-click="toggle">Toggle</button>
10+
</h5>
11+
<p class="card-text" t-if="this.state.isOpen">
12+
<t t-slot="default" />
13+
</p>
14+
</div>
15+
</div>
16+
</t>
17+
</templates>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @odoo-module **/
2+
3+
import { Component, useState } from "@odoo/owl";
4+
5+
export class Counter extends Component {
6+
static template = "awesome_owl.counter";
7+
static props = {
8+
onChange: { type: Function, optional: true }
9+
};
10+
11+
setup() {
12+
this.state = useState({ value:0 });
13+
}
14+
15+
increment() {
16+
this.state.value++;
17+
if (this.props.onChange) {
18+
this.props.onChange();
19+
}
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_owl.counter">
5+
<div class="m-2 p-2 d-inline-block">
6+
<span class="me-2">Counter: <t t-esc="state.value"/></span>
7+
<button class="btn btn-primary" t-on-click="increment">Increment</button>
8+
</div>
9+
</t>
10+
</templates>

awesome_owl/static/src/playground.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
/** @odoo-module **/
22

3-
import { Component } from "@odoo/owl";
3+
import { Component, markup, useState } from "@odoo/owl";
4+
import { Counter } from "./counter/counter";
5+
import { Card } from "./card/card";
6+
import { TodoList } from "./todo/todo_list";
47

58
export class Playground extends Component {
69
static template = "awesome_owl.playground";
10+
static components = { Card, Counter, TodoList };
11+
12+
setup() {
13+
this.content1 = "<div class='text-primary'>some content</div>";
14+
this.content2 = markup("<div class='text-primary'>some content</div>");
15+
this.sum = useState({ value: 2 });
16+
}
17+
18+
incrementSum() {
19+
this.sum.value++;
20+
}
721
}

awesome_owl/static/src/playground.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44
<t t-name="awesome_owl.playground">
55
<div class="p-3">
66
hello world
7+
<Counter onChange.bind="incrementSum" />
8+
<Counter onChange.bind="incrementSum" />
9+
<div>The sum is: <t t-esc="sum.value" /></div>
10+
</div>
11+
<div>
12+
<Card title.translate="card 1">
13+
text 1
14+
</Card>
15+
<Card title.translate="card 2">
16+
<Counter />
17+
</Card>
18+
</div>
19+
<div>
20+
<TodoList />
721
</div>
822
</t>
9-
1023
</templates>

awesome_owl/static/src/todo/todo.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export class Todo {
2+
static nextId = 1;
3+
4+
constructor(description) {
5+
this.id = Todo.nextId;
6+
Todo.nextId++;
7+
this.description = description;
8+
this.isCompleted = false;
9+
}
10+
11+
toggle() {
12+
this.isCompleted = !this.isCompleted;
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** @odoo-module **/
2+
3+
import { Component, useState } from "@odoo/owl";
4+
5+
export class TodoItem extends Component {
6+
static template = "awesome_owl.todoitem";
7+
static props = {
8+
todo: {
9+
type: Object, shape: {
10+
id: Number,
11+
description: String,
12+
isCompleted: Boolean,
13+
}},
14+
removeTodo: {
15+
type: Function,
16+
},
17+
};
18+
19+
remove() {
20+
this.props.removeTodo(this.props.todo.id);
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_owl.todoitem">
5+
<div>
6+
<input class="me-2" type="checkbox" t-att-id="props.todo.id" t-att-checked="props.todo.isCompleted" t-on-change="() => props.todo.toggle()" />
7+
<label t-att-for="props.todo.id" t-att-class="props.todo.isCompleted ? 'text-decoration-line-through text-muted' : ''">
8+
<t t-esc="props.todo.id" />. <t t-esc="props.todo.description" />
9+
</label>
10+
<span class="fa fa-remove" t-on-click="remove"/>
11+
</div>
12+
</t>
13+
</templates>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @odoo-module **/
2+
3+
import { Component, useState, useRef, onMounted } from "@odoo/owl";
4+
import { TodoItem } from "./todo_item";
5+
import { Todo } from "./todo";
6+
import { useAutoFocus } from "../utils";
7+
8+
export class TodoList extends Component {
9+
static template = "awesome_owl.todolist";
10+
static components = { TodoItem };
11+
12+
setup() {
13+
this.todos = useState([]);
14+
useAutoFocus("input");
15+
}
16+
17+
addTodo(ev) {
18+
if (ev.keyCode === 13 && ev.target.value !== '') {
19+
this.todos.push(new Todo(ev.target.value))
20+
ev.target.value='';
21+
}
22+
}
23+
24+
removeTodo(id) {
25+
const index = this.todos.findIndex((elem) => elem.id === id);
26+
27+
if (index >= 0) {
28+
this.todos.splice(index, 1);
29+
}
30+
}
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_owl.todolist">
5+
<div>
6+
<input class="form-control mb-3" placeholder="Add a todo" type="text" t-on-keyup="addTodo" t-ref="input" />
7+
<t t-foreach="this.todos" t-as="todo" t-key="todo.id">
8+
<TodoItem todo="todo" removeTodo.bind="removeTodo" />
9+
</t>
10+
</div>
11+
</t>
12+
</templates>

awesome_owl/static/src/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useRef, onMounted } from "@odoo/owl"
2+
3+
export function useAutoFocus(refName) {
4+
const ref = useRef(refName)
5+
onMounted(() => {
6+
ref.el.focus()
7+
})
8+
}

awesome_owl/views/templates.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<head>
66
<link type="image/x-icon" rel="shortcut icon" href="/web/static/img/favicon.ico"/>
77
<t t-call-assets="awesome_owl.assets_playground"/>
8+
<t t-call-assets="web.assets_frontend" t-js="false"/>
89
</head>
910

1011
<body>

0 commit comments

Comments
 (0)