Skip to content
Draft
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
25 changes: 25 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Component, useState} from "@odoo/owl";

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

static props = {
title: {
type: String,
},
slots: {
type: Object,
shape:{
default: Object,
}
}
}

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

showContent() {
this.state.visible = !this.state.visible;
}
}
18 changes: 18 additions & 0 deletions awesome_owl/static/src/card/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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"/>
<button t-on-click="showContent">toggle</button>
</h5>
<p t-if="state.visible" class="card-text">
<t t-slot="default"/>
</p>
</div>
</div>
</t>

</templates>
23 changes: 23 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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({value: 0});
}

increment() {
this.state.value++;
if (this.props.onChange) {
this.props.onChange();
}
}
}
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">
<p>Counter: <t t-esc="state.value"/></p>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</t>

</templates>
9 changes: 4 additions & 5 deletions awesome_owl/static/src/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { whenReady } from "@odoo/owl";
import { mountComponent } from "@web/env";
import { Playground } from "./playground";
import {whenReady} from "@odoo/owl";
import {mountComponent} from "@web/env";
import {Playground} from "./playground";

const config = {
dev: true,
name: "Owl Tutorial"
name: "Owl Tutorial"
};

// Mount the Playground component when the document.body is ready
whenReady(() => mountComponent(Playground, document.body, config));

15 changes: 14 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
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 components = {Counter, Card, TodoList};

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

incrementSum() {
this.state.value++;
}
}
13 changes: 10 additions & 3 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
</div>
<h1>My Counter</h1>
<Card title="'Card 1'">
<Counter onChange.bind="incrementSum"/>
</Card>
<Card title="'Card 2'">
<Counter onChange.bind="incrementSum"/>
</Card>

<p>Sum: <t t-esc="state.value"/></p>
<TodoList items="[]"/>
</t>

</templates>
33 changes: 33 additions & 0 deletions awesome_owl/static/src/todo/item/todo_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Component} from "@odoo/owl";
import {Card} from "../../card/card";

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

static Components = [Card]

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

changeCheckbox(ev) {
this.props.toggleState(this.props.todo.id, ev.target.checked);
}

removeTodo() {
this.props.removeTodo(this.props.todo.id)
}
}
14 changes: 14 additions & 0 deletions awesome_owl/static/src/todo/item/todo_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todo_item">
<div style="display: flex;align-items: center;gap: 15px;" t-att-class="{'text-muted text-decoration-line-through': props.todo.isCompleted}">
<input t-att-checked="props.todo.isCompleted" t-on-change="changeCheckbox" type="checkbox"
t-att-id="props.todo.id"/>
<t t-esc="props.todo.id"/>
<t t-esc="props.todo.description"/>
<span t-on-click="removeTodo" class="fa fa-remove"/>
</div>
</t>

</templates>
60 changes: 60 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,60 @@
import {Component, useState, useRef, onMounted} from "@odoo/owl";
import {TodoItem} from "../item/todo_item";
import {useAutoFocus} from "../../utils/utils";

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

static components = {TodoItem};

static props = {
items: {
type: Array,
optional: true,
}
}

setup() {
this.props.items = useState([])
this.descriptionRef = useRef('descriptionInput');
this.nextId = 1;
onMounted(() => {
useAutoFocus(this.descriptionRef.el)
});
}

#checkDescription() {
return this.descriptionRef?.el?.value?.trim();
}

addTodoItem() {
if (this.#checkDescription()) {
this.props.items.push({
id: this.nextId++,
description: this.descriptionRef.el.value,
isCompleted: false
})
this.descriptionRef.el.value = '';
}
}

checkAndAddTask(ev) {
if (ev.keyCode === 13 && this.#checkDescription()) {
this.addTodoItem();
}
}

toggleTodoState = (id, isChecked) => {
const element = this.props.items.find(item => item.id === id);
if (element) {
element.isCompleted = isChecked;
}
}

removeTodoItem = (id) => {
const index = this.props.items.findIndex(item => item.id === id);
if(index >= 0) {
this.props.items.splice(index, 1);
}
}
}
15 changes: 15 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,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todo_list">
<div class="card d-inline-block m-2" style="width: 20rem; padding: 20px">
<input id="description" t-on-keyup="checkAndAddTask" t-ref="descriptionInput" placeholder="Enter a new task"/>
<button class="btn btn-primary" t-on-click="addTodoItem">Add</button>
<t t-foreach="props.items" t-as="todo" t-key="todo.id">
<TodoItem removeTodo="removeTodoItem" toggleState="toggleTodoState" todo="todo"/>
<br/>
</t>
</div>
</t>

</templates>
3 changes: 3 additions & 0 deletions awesome_owl/static/src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const useAutoFocus = (el) => {
el?.focus();
}