Skip to content

Commit

Permalink
[IMP] awesome_owl: Tutorial chapter 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nape-odoo committed Nov 24, 2024
1 parent b16e643 commit 337f2ea
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 2 deletions.
18 changes: 18 additions & 0 deletions awesome_owl/static/src/misc/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @odoo-module **/

import { Component, useState } from "@odoo/owl";

export class Card extends Component {
static template = "awesome_owl.card";
static props = {
title : String,
message : String,
slots : {type: Object, optional: true}
}
setup() {
this.isMinimized = useState({ value: false });
}
cardMinMaximize() {
this.isMinimized.value = !this.isMinimized.value;
}
}
23 changes: 23 additions & 0 deletions awesome_owl/static/src/misc/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.card">
<div class="card d-inline-block m-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">
<t t-esc="props.title"/>
<span t-att-class="{'fa fa-plus' : isMinimized.value, 'fa fa-minus' : !isMinimized.value}" role="button" t-on-click="cardMinMaximize"/>
</h5>
<div t-if="!isMinimized.value">
<p class="card-text"><t t-out="props.message"/></p>
<t t-slot="default"/>
<ul>
<!-- default slot content -->
</ul>
</div>
</div>
</div>
</t>

</templates>

20 changes: 20 additions & 0 deletions awesome_owl/static/src/misc/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** @odoo-module **/

import { Component, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.counter";
static props = {
onIncrement : { type : Function, optional : true }
}
setup() {
this.state = useState({ value: 0 });
}

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

<t t-name="awesome_owl.counter">
<p>Counter: <t t-esc="state.value"/></p>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</t>

</templates>

15 changes: 14 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { Component, markup, useState } from "@odoo/owl";
import { Counter } from "./misc/counter";
import { Card } from "./misc/card";
import { TodoList } from "./todo/todo_list"

export class Playground extends Component {
static template = "awesome_owl.playground";
static components = { Counter, Card, TodoList }
static props = {}
setup(){
this.message_1 = "<div class='text-primary'>Content 1</div>"
this.message_2 = markup("<div class='text-primary'>Content 2</div>")
this.counter_sum = useState({ value: 0 })
}
onCounterIncrement(){
this.counter_sum.value++;
}
}
15 changes: 14 additions & 1 deletion awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<h3>Playground</h3>
<p>Sum of counters: <t t-esc="counter_sum.value"/></p>
</div>
<div>
<Card title="'Card 1'" message="this.message_1">
<Counter onIncrement.bind="onCounterIncrement"/>
</Card>
<Card title="'Card 2'" message="this.message_2">
<Counter onIncrement.bind="onCounterIncrement"/>
</Card>
</div>
<div>
<TodoList/>
</div>
</t>

</templates>

18 changes: 18 additions & 0 deletions awesome_owl/static/src/todo/todo_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";

export class TodoItem extends Component {
static template = "awesome_owl.todoitem";
static props = {
todo : {type: Object, shape: {id : Number, description: String, isCompleted: Boolean }},
todoListToggleItem : { type : Function, optional : false },
todoListRemoveItem : { type : Function, optional : false }
}
todoItemToggleItem() {
this.props.todoListToggleItem(this.props.todo.id);
}
todoItemRemoveItem() {
this.props.todoListRemoveItem(this.props.todo.id);
}
}
15 changes: 15 additions & 0 deletions awesome_owl/static/src/todo/todo_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todoitem">
<div t-att-class="{'text-decoration-line-through text-secondary': props.todo.isCompleted}">
<p>
<input type="checkbox" t-on-change="todoItemToggleItem" t-att-checked="props.todo.isCompleted"/>
ID: <t t-esc="props.todo.id"/>
Description: <t t-esc="props.todo.description"/>
<span class="fa fa-remove" role="button" t-on-click="todoItemRemoveItem"/>
</p>
</div>
</t>

</templates>
41 changes: 41 additions & 0 deletions awesome_owl/static/src/todo/todo_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @odoo-module **/

import { Component, useState } from "@odoo/owl";
import { TodoItem } from "./todo_item"

export class TodoList extends Component {
static template = "awesome_owl.todolist";
static components = { TodoItem }
static props = {}
setup(){
this.todos = useState([
{ id: 0, description: "Do something", isCompleted: true },
{ id: 1, description: "Do something else", isCompleted: false },
{ id: 2, description: "Do stuff", isCompleted: false },
{ id: 3, description: "Do more stuff", isCompleted: false }
]);
}
todoListAddItem(input_value) {
if (input_value.keyCode === 13 && input_value.target.value){
this.todos.push({id: (this.todos.length) ? this.todos.at(-1).id + 1 : 0, description: input_value.target.value, isCompleted: false});
input_value.target.value = "";
}
}
todoListToggleItem(todoId) {
this.todos.forEach((todo) => {
if (todo.id === todoId) {
todo.isCompleted = !todo.isCompleted;
}
})
}
todoListRemoveItem(todoId) {
this.todos.forEach((todo) => {
if (todo.id === todoId) {
this.todos.splice(todoId, 1);
for (let i = 0; i < this.todos.length; i++) {
this.todos.at(i).id = i
}
}
})
}
}
12 changes: 12 additions & 0 deletions awesome_owl/static/src/todo/todo_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todolist">
<input type="text" placeholder="Add a todo" t-on-keyup="todoListAddItem" t-ref="input"/>
<t t-foreach="todos" t-as="todo" t-key="todo.id">
<TodoItem todo="todo" todoListToggleItem.bind="todoListToggleItem" todoListRemoveItem.bind="todoListRemoveItem"/>
</t>
</t>

</templates>

0 comments on commit 337f2ea

Please sign in to comment.