Skip to content

Commit 5044748

Browse files
[IMP] awesome_owl: Web Framework Finished Chapter 1 Part 9-14
1 parent dceda6c commit 5044748

File tree

9 files changed

+98
-19
lines changed

9 files changed

+98
-19
lines changed

awesome_owl/static/src/TodoItem/TodoItem.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ export class TodoItem extends Component
99
shape:{
1010
id: {type : Number},
1111
description: {type : String},
12-
isCompleted: {type: Boolean}}}
13-
12+
isCompleted: {type: Boolean}}
13+
},
14+
15+
updateState : {
16+
type: Function,
17+
},
18+
19+
deleteTodo : {
20+
type : Function,
21+
optional: true
22+
}
1423
};
24+
25+
updateState(event)
26+
{
27+
this.props.updateState(this.props.todo.id);
28+
}
29+
deleteTodo(event)
30+
{
31+
this.props.deleteTodo(this.props.todo.id);
32+
}
1533
}

awesome_owl/static/src/TodoItem/TodoItem.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<p>ID : <t t-esc="props.todo.id"/></p>
88
<p>Desc : <t t-esc="props.todo.description"/></p>
9+
<input type="checkbox" t-on-click="updateState"/>
10+
<span class="fa fa-remove" t-on-click="deleteTodo"/>
911
</div>
1012
</t>
1113
</templates>
Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
11
import { Component, useState } from "@odoo/owl";
22
import { TodoItem } from "../TodoItem/TodoItem";
3+
import { useAutoFocus } from "../utils";
34

45
export class TodoList extends Component
56
{
67
static template = 'awesome_owl.TodoList';
78
static components = { TodoItem };
8-
setup()
9-
{
10-
this.todos = useState([
11-
{id: 3, description: "buy milk", isCompleted: false },
12-
{id: 4, description: "buy milk 2", isCompleted: false },
13-
{id: 5, description: "buy milk -1", isCompleted: true }
14-
]);
15-
}
9+
static props = {};
10+
setup()
11+
{
12+
this.todos = useState([]);
13+
this.ids = 0;
14+
useAutoFocus('todo_input');
15+
}
16+
addTodo(event)
17+
{
18+
if (event.type !== 'keyup' || event.keyCode !== 13 || event.target.value == "" )
19+
{
20+
return;
21+
}
22+
this.todos.push({id:this.ids++,description:event.target.value,isCompleted:false});
23+
event.target.value = "";
24+
25+
}
26+
updateTodo(id)
27+
{
28+
let selectedTodo = this.todos.find(todo => todo.id == id);
29+
if(selectedTodo)
30+
{
31+
selectedTodo.isCompleted = !selectedTodo.isCompleted;
32+
}
33+
34+
}
35+
deleteTodo(id)
36+
{
37+
const selectedTodo = this.todos.findIndex(todo => todo.id == id);
38+
if(selectedTodo >= 0)
39+
{
40+
this.todos.splice(selectedTodo, 1);
41+
}
42+
43+
44+
}
45+
1646
}

awesome_owl/static/src/TodoList/TodoList.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
<templates xml:space="preserve">
33

44
<t t-name="awesome_owl.TodoList">
5+
<input t-on-keyup="addTodo" t-ref="todo_input"/>
56
<t t-foreach="todos" t-as="todo" t-key="todo.id">
6-
<TodoItem todo="todo"/>
7+
<TodoItem todo="todo" updateState.bind="updateTodo" deleteTodo.bind="deleteTodo"/>
78
</t>
89
</t>
910
</templates>

awesome_owl/static/src/card/card.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@ export class Card extends Component {
44
static template = "awesome_owl.Card";
55
static props = {
66
title: String,
7-
content: String,
8-
html: String
7+
slots: {
8+
type: Object,
9+
shape: {
10+
default:true
11+
}
12+
},
13+
html: {type:String}
914
};
1015

16+
setup()
17+
{
18+
this.hide = useState({value:false});
19+
}
20+
21+
hideToggle()
22+
{
23+
this.hide.value = ! this.hide.value;
24+
}
25+
1126
}

awesome_owl/static/src/card/card.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
<templates xml:space="preserve">
33
<t t-name="awesome_owl.Card">
44
<div class="card d-inline-block m-2" style="width: 18rem;">
5+
<span class="fa fa-window-minimize" t-on-click="hideToggle"/>
56
<div class="card-body">
67
<h5 class="card-title"><t t-esc="props.title"/></h5>
7-
<p class="card-text">
8-
<t t-esc="props.content"/>
9-
<t t-out="props.html"/>
8+
<p class="card-text" t-if="! hide.value">
9+
<t t-slot="default"/>
10+
<t t-out="props.html"/>
1011
</p>
1112
</div>
1213
</div>

awesome_owl/static/src/playground.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { TodoList } from "./TodoList/TodoList"
66
export class Playground extends Component {
77
static template = "awesome_owl.playground";
88
static components = { Counter, Card, TodoList };
9-
todo= {id: 3, description: "buy milk", isCompleted: false };
9+
static props = {};
10+
todo = {id: 3, description: "buy milk", isCompleted: false };
1011
html = markup("<h1> Hi </h1>");
1112

1213
setup()
@@ -15,7 +16,6 @@ export class Playground extends Component {
1516
}
1617
incrementSum()
1718
{
18-
console.log("test this");
1919
this.sum.value ++;
2020
}
2121

awesome_owl/static/src/playground.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<t t-name="awesome_owl.playground">
44
<Counter onChange.bind="incrementSum"/>
55
<Counter onChange.bind="incrementSum"/>
6-
<Card content="'test content'" title="'Test title'" html="html"/>
6+
<Card title="'Test title'" html="html">
7+
<Counter onChange.bind="incrementSum"/>
8+
</Card>
79
<t t-esc="sum.value"/>
810
<div>
911
<TodoList/>

awesome_owl/static/src/utils.js

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

0 commit comments

Comments
 (0)