Skip to content
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

[ADD] discover the web framework #200

Open
wants to merge 1 commit 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
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},
content: {type: String, optional: true},
slots: {type: Object, optional: true},
}

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

toggle(){
this.state.open = !this.state.open;
}
}
16 changes: 16 additions & 0 deletions awesome_owl/static/src/card/card.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.card">
<div class="card d-inline-block m-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title" t-on-click="toggle"><t t-esc="props.title" /></h5>
<div t-if="this.state.open">
<p class="card-text"><t t-out="props.content"/></p>
<t t-slot="default"/>
</div>
</div>
</div>
</t>

</templates>
20 changes: 20 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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();
}
}
}
11 changes: 11 additions & 0 deletions awesome_owl/static/src/counter/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

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

</templates>
6 changes: 3 additions & 3 deletions awesome_owl/static/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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,
Expand Down
21 changes: 19 additions & 2 deletions awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import {Counter} from "./counter/counter";
import {Card} from "./card/card";
import {TodoList} from "./todo_list/todo_list";

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

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

cardHtmlContent = markup("<a href='://odoo.com'>some text 2</a>");

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

incrementSum() {
this.state.sum++;
}


}
19 changes: 17 additions & 2 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
<templates xml:space="preserve">

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


<Counter onChange.bind="incrementSum"/>
<Counter onChange.bind="incrementSum"/>
<div class="m-3">This sum is: <t t-esc="state.sum"/></div>

<hr/>

<Card title="'Card 1'" content="'content of card 1'"/>
<Card title="'Card 2'" content="cardHtmlContent"/>
<br/>
<Card title="'Card 3'">
<Counter/>
</Card>
</t>

</templates>
24 changes: 24 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,24 @@
import {Component} from "@odoo/owl";

export class TodoItem extends Component {
static template = "awesome_owl.todo_item"
static Components
static props = {
todo: {type: Object, shape: {id: Number, description: String, isCompleted: Boolean}},
toggleState: {type: Function, optional: true},
onRemoveTodo: {type: Function, optional: true},
}

onCheckToggle() {
if (this.props.toggleState) {
this.props.toggleState();
}
}

onRemoveTodo() {
if (this.props.onRemoveTodo) {
this.props.onRemoveTodo();
}
}

}
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.todo_item">
<div t-att-class="{'text-muted text-decoration-line-through': props.todo.isCompleted }">
<input type="checkbox" class="me-2" t-on-change="onCheckToggle"/>
<t t-esc="props.todo.id"/>.
<t t-esc="props.todo.description"/>
<span class="fa fa-remove ms-3 cursor-pointer" t-on-click="onRemoveTodo"/>
</div>
</t>

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

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

newtTodoId = 1;

setup() {
this.state = useState({todos: [], newTodo: ""});
useCustomAutofocus("todoInput")
this.toggleTodoState = this.toggleTodoState.bind(this);
this.removeTodo = this.removeTodo.bind(this);

}

addTodo(ev) {
if (ev.keyCode !== 13 || !this.state.newTodo) {
return;
}

const todoItem = {
id: this.newtTodoId++,
description: this.state.newTodo,
isCompleted: false
}
this.state.newTodo = ""
this.state.todos.push(todoItem)
}

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

removeTodo(todoId) {
const index = this.state.todos.findIndex((todo) => todo.id === todoId);
if (index >= 0) {
this.state.todos.splice(index, 1);
}
}

}
13 changes: 13 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,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todo_list">
<input text="text" t-ref="todoInput" placeholder="Add a todo" t-on-keyup="addTodo" t-model.trim="state.newTodo"
class="mb-3"/>
<t t-foreach="state.todos" t-as="todo" t-key="todo.id">
<TodoItem todo="todo" toggleState="() => toggleTodoState(todo.id)"
onRemoveTodo="() => removeTodo(todo.id)"/>
</t>
</t>

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

export function useCustomAutofocus(inputRefName) {
const ref = useRef(inputRefName);
/*
onMounted(() => {
ref.el.focus();
})
*/

useEffect(
(el) => el && el.focus(),
() => [ref.el]
);


}