Skip to content

[ADD] awesome_owl: it adds basics for a todo list #801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 18.0
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion awesome_owl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

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

export class Card extends Component {
static template = "awesome_owl.Card";

static components = { Counter };

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

this.flip = this.flip.bind(this);
}

static props = {
title: String,
};

flip() {
this.state.open = !this.state.open;
}
}
15 changes: 15 additions & 0 deletions awesome_owl/static/src/card/card.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.Card">
<div class="card d-inline-block m-2 w-50">
<div class="card-body">
<button class="btn btn-primary" t-on-click="flip">flip</button>
<h5 class="card-title"><t t-out="props.title"/></h5>
<p t-if="state.open" class="card-text">
<t t-slot="default"/>
<Counter/>
</p>
</div>
</div>
</t>
</templates>
17 changes: 17 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.counter";

static props = {
validation_fct: { type: Function, optional: true },
};
setup() {
this.state = useState({ value: 0 });
}

increment() {
this.state.value++;
this.props.validation_fct?.call();
}
}
9 changes: 9 additions & 0 deletions awesome_owl/static/src/counter/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_owl.counter">
<div class="card">
<p>Counter: <t t-esc="state.value"/></p>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</div>
</t>
</templates>
18 changes: 16 additions & 2 deletions awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
/** @odoo-module **/

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_list/todo_list";

export class Playground extends Component {
static template = "awesome_owl.playground";
static template = "awesome_owl.playground";

static components = { Counter, Card, TodoList };

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

incrSum() {
this.state.sum++;
}
}
18 changes: 11 additions & 7 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
</div>
</t>

<t t-name="awesome_owl.playground">
<div class="card p-2 pe-3">
<div>Hello world</div>
<div><Counter validation_fct="incrSum"/></div>
<div><Counter validation_fct="incrSum"/></div>
<Card title="'Card 1'"/>
<Card title="'Card 2'"/>
<div>the sum is <t t-esc="state.sum"/></div>
<div><TodoList/></div>
</div>
</t>
</templates>
21 changes: 21 additions & 0 deletions awesome_owl/static/src/todo_list/todo_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component } from "@odoo/owl";

export class TodoItem extends Component {
static template = "awesome_owl.TodoItem";

setup() {
this.toggleState = this.toggleState.bind(this);
}

static props = {
todo: {
type: Object,
shape: { id: Number, description: String, isCompleted: Boolean },
},
remove_fct: { type: Function },
};

toggleState() {
this.props.todo.isCompleted = !this.props.todo.isCompleted;
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/todo_list/todo_item.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.TodoItem">
<div t-att-class="{'text-muted': props.todo.isCompleted}" t-att-style="props.todo.isCompleted ? 'text-decoration: line-through;':''">
<p>
<input type="checkbox" t-att-checked="props.todo.isCompleted" t-on-change="toggleState"/>
<t t-esc="props.todo.id"/>,
<t t-esc="props.todo.description"/>
<span class="fa fa-remove" t-on-click="() => props.remove_fct(props.todo.id)"/>
</p>
</div>
</t>
</templates>
44 changes: 44 additions & 0 deletions awesome_owl/static/src/todo_list/todo_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Component, useState } from "@odoo/owl";
import { TodoItem } from "./todo_item";
import { useAutofocus } from "../utils/useAutoFocus";

export class TodoList extends Component {
static template = "awesome_owl.TodoList";

static components = { TodoItem };

setup() {
this.state = useState({
todos: [],
curr_id: 0,
});

this.inputRef = useAutofocus("input");
this.delete_todo = this.delete_todo.bind(this);
}

add_todo(ev) {
if (ev.keyCode === 13) {
const value = ev.target.value.trim();
if (value !== "") {
this.state.todos.push({
id: this.state.curr_id,
description: value,
isCompleted: false,
});
}
ev.target.value = "";

this.state.curr_id++;
}
}

delete_todo(todo_to_remove_id) {
const index = this.state.todos.findIndex(
(elem) => elem.id === todo_to_remove_id
);
if (index >= 0) {
this.state.todos.splice(index, 1);
}
}
}
9 changes: 9 additions & 0 deletions awesome_owl/static/src/todo_list/todo_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_owl.TodoList">
<input t-ref="input" t-on-keyup="add_todo" type="text" id="new_todo_id" name="new_todo" placeholder="Enter a new task" />
<t t-foreach="state.todos" t-as="t" t-key="t.id">
<div><TodoItem todo="t" remove_fct="delete_todo"/></div>
</t>
</t>
</templates>
13 changes: 13 additions & 0 deletions awesome_owl/static/src/utils/useAutoFocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onMounted, useRef } from "@odoo/owl";

export function useAutofocus(refName) {
const elRef = useRef(refName);
onMounted(() => {
if (elRef.el && typeof elRef.el.focus === 'function') {
elRef.el.focus();
} else {
console.error("The component you try to focus does not have a focus method")
}
});
return elRef;
}