Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component, useState } from "@odoo/owl";

export class Card extends Component {
static template = "awesome_owl.card";
static props = {
title: { type: String },
slots: { type: Object, optional: true },
};

setup() {
this.state = useState({
isOpen: true,
});
}

onToggle() {
this.state.isOpen = !this.state.isOpen;
}
}
21 changes: 21 additions & 0 deletions awesome_owl/static/src/card/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.card">
<div class="card m-2">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="card-title m-0">
<t t-esc="props.title"/>
</h5>
<button class="btn btn-light ml-2" t-on-click="onToggle">Toggle</button>
</div>

<div class="card-body">
<div t-att-class="state.isOpen ? 'd-inline-block' : 'd-none'">
<t t-slot="default" />
</div>
</div>
</div>
</t>

</templates>
18 changes: 18 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Component, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.counter";
static props = {
onChange: { type: Function, optional: true },
};

setup() {
this.state = useState({
count: 0,
});
}

increment() {
this.props.onChange?.(++this.state.count);
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/counter/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.counter">
<div>
<p>
<t t-esc="state.count"/>
</p>
<button class="btn" t-on-click="increment">Increment</button>
</div>
</t>

</templates>
17 changes: 16 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { Component } from "@odoo/owl";
import { Component, useState } from "@odoo/owl";
import { Counter } from "./counter/counter";
import { Card } from "./card/card";
import { TodoList } from "./todo/todoList";

export class Playground extends Component {
static template = "awesome_owl.playground";
static components = { Counter, Card, TodoList };

setup() {
this.state = useState({
sum: 0,
});
}

incrementSum(count) {
this.state.sum += 1;
}

}
21 changes: 18 additions & 3 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<div class="d-flex align-items-start gap-3 p-3">
<Card title="'Counters'">
<Card title="'Counter 1'">
<Counter onChange.bind="this.incrementSum"/>
</Card>
<Card title="'Counter 2'">
<Counter onChange.bind="this.incrementSum"/>
</Card>
<div>
<p>Sum: <t t-esc="state.sum"/>
</p>
</div>
</Card>

<Card title="'Todo List'">
<TodoList />
</Card>
</div>
</t>

Expand Down
15 changes: 15 additions & 0 deletions awesome_owl/static/src/todo/todoItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Component, markup } from "@odoo/owl";
import { Counter } from "../counter/counter";

export class TodoItem extends Component {
static template = "awesome_owl.todoItem";
static components = { Counter };
static props = {
id: { type: Number },
description: { type: String },
isCompleted: { type: Boolean},
toggleState: { type: Function, optional: true },
removeTodo: { type: Function, optional: true },
};

}
16 changes: 16 additions & 0 deletions awesome_owl/static/src/todo/todoItem.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todoItem">
<div class="d-flex flex-row gap-2 justify-center align-items-center">
<input type="checkbox" t-att-checked="props.isCompleted" t-on-click="props.toggleState" />
<label t-att-class="{'text-muted text-decoration-line-through': props.isCompleted}">
<t t-esc="props.id" />
<span>.</span>
<t t-esc="props.description"/>
</label>
<span class="fa fa-remove" t-on-click=" props.removeTodo"/>
</div>
</t>

</templates>
36 changes: 36 additions & 0 deletions awesome_owl/static/src/todo/todoList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Component, useState } from "@odoo/owl";
import { TodoItem } from "./todoItem";
import { useAutoFocus } from "../utils";

export class TodoList extends Component {
static template = "awesome_owl.todoList";
static components = { TodoItem };

setup() {
this.state = useState({todos: []});
this.idCounter = 1;
this.addTodoRef = useAutoFocus();
}

onAddTodo(ev) {
if (ev.key == "Enter" && ev.target.value.trim() !== "") {
const newTodo = {
id: this.idCounter++,
description: ev.target.value,
isCompleted: false,
};

this.state.todos.push(newTodo);
ev.target.value = "";
}
}

toggleState(todoId) {
const todo = this.state.todos?.find((t) => t.id === todoId);
if (todo) todo.isCompleted = !todo.isCompleted;
}

removeTodo(todoId) {
this.state.todos = this.state.todos?.filter((t) => t.id !== todoId);
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/todo/todoList.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todoList">
<input t-ref="add-todo-input" type="text" t-on-keyup="onAddTodo" placeholder="Add a new todo" class="form-control mb-2"/>
<div class="card d-inline-block m-2 p-2" style="width: 14rem;">
<div t-foreach="state.todos" t-as="i" t-key="i.id">
<TodoItem id="i.id" description="i.description" isCompleted="i.isCompleted" toggleState="() => this.toggleState(i.id)" removeTodo="() => this.removeTodo(i.id)"/>
</div>
</div>
</t>

</templates>
13 changes: 13 additions & 0 deletions awesome_owl/static/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onMounted, useRef } from "@odoo/owl";

export function useAutoFocus() {
const ref = useRef("add-todo-input");

onMounted(() => {
if (ref.el) {
ref.el.focus();
}
});

return ref;
}