mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] playground: update todoapp sample
This commit is contained in:
+86
-92
@@ -385,99 +385,94 @@ const TODO_APP_STORE = `// This example is an implementation of the TodoList app
|
|||||||
// In this implementation, we use the owl Store class to manage the state. It
|
// In this implementation, we use the owl Store class to manage the state. It
|
||||||
// is very similar to the VueX store.
|
// is very similar to the VueX store.
|
||||||
const { Component, useState } = owl;
|
const { Component, useState } = owl;
|
||||||
const { useRef } = owl.hooks;
|
const { useRef, useStore, useDispatch, onPatched, onMounted } = owl.hooks;
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// Constants, helpers
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
const ENTER_KEY = 13;
|
const ENTER_KEY = 13;
|
||||||
const ESC_KEY = 27;
|
const ESC_KEY = 27;
|
||||||
const LOCALSTORAGE_KEY = "todomvc";
|
const LOCALSTORAGE_KEY = "todomvc";
|
||||||
|
|
||||||
|
function useAutofocus(name) {
|
||||||
|
let ref = useRef(name);
|
||||||
|
let isInDom = false;
|
||||||
|
function updateFocus() {
|
||||||
|
if (!isInDom && ref.el) {
|
||||||
|
isInDom = true;
|
||||||
|
const current = ref.el.value;
|
||||||
|
ref.el.value = "";
|
||||||
|
ref.el.focus();
|
||||||
|
ref.el.value = current;
|
||||||
|
} else if (isInDom && !ref.el) {
|
||||||
|
isInDom = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onPatched(updateFocus);
|
||||||
|
onMounted(updateFocus);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Store Definition
|
// Store
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
const initialState = { todos: [], nextId: 1};
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
addTodo({ state }, title) {
|
addTodo({ state }, title) {
|
||||||
state.todos.push({
|
const todo = {
|
||||||
id: state.nextId++,
|
id: state.nextId++,
|
||||||
title,
|
title,
|
||||||
completed: false
|
completed: false
|
||||||
});
|
}
|
||||||
|
state.todos.push(todo);
|
||||||
},
|
},
|
||||||
removeTodo({ state }, id) {
|
removeTodo({ state }, id) {
|
||||||
const index = state.todos.findIndex(t => t.id === id);
|
const index = state.todos.findIndex(t => t.id === id);
|
||||||
state.todos.splice(index, 1);
|
state.todos.splice(index, 1);
|
||||||
},
|
},
|
||||||
toggleTodo({ state, dispatch }, id) {
|
updateTodo({state, dispatch}, {id, title}) {
|
||||||
|
const value = title.trim();
|
||||||
|
if (!value) {
|
||||||
|
dispatch('removeTodo', id);
|
||||||
|
} else {
|
||||||
|
const todo = state.todos.find(t => t.id === id);
|
||||||
|
todo.title = value;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
toggleTodo({ state }, id) {
|
||||||
const todo = state.todos.find(t => t.id === id);
|
const todo = state.todos.find(t => t.id === id);
|
||||||
dispatch("editTodo", { id, completed: !todo.completed });
|
todo.completed = !todo.completed;
|
||||||
},
|
},
|
||||||
clearCompleted({ state, dispatch }) {
|
clearCompleted({ state, dispatch }) {
|
||||||
state.todos
|
for (let todo of state.todos) {
|
||||||
.filter(todo => todo.completed)
|
if (todo.completed) {
|
||||||
.forEach(todo => {
|
|
||||||
dispatch("removeTodo", todo.id);
|
dispatch("removeTodo", todo.id);
|
||||||
});
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
toggleAll({ state, dispatch }, completed) {
|
toggleAll({ state, dispatch }, completed) {
|
||||||
state.todos.forEach(todo => {
|
for (let todo of state.todos) {
|
||||||
dispatch("editTodo", {
|
|
||||||
id: todo.id,
|
|
||||||
completed
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
editTodo({ state }, { id, title, completed }) {
|
|
||||||
const todo = state.todos.find(t => t.id === id);
|
|
||||||
if (title !== undefined) {
|
|
||||||
todo.title = title;
|
|
||||||
}
|
|
||||||
if (completed !== undefined) {
|
|
||||||
todo.completed = completed;
|
todo.completed = completed;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
function saveState(state) {
|
|
||||||
const str = JSON.stringify(state);
|
|
||||||
window.localStorage.setItem(LOCALSTORAGE_KEY, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadState() {
|
|
||||||
const localState = window.localStorage.getItem(LOCALSTORAGE_KEY);
|
|
||||||
return localState ? JSON.parse(localState) : { todos: [], nextId: 1};
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeStore() {
|
|
||||||
const state = loadState();
|
|
||||||
const store = new owl.store.Store({ state, actions });
|
|
||||||
store.on("update", null, () => saveState(store.state));
|
|
||||||
return store;
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// TodoItem
|
// TodoItem
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
class TodoItem extends Component {
|
class TodoItem extends Component {
|
||||||
state = useState({ isEditing: false });
|
state = useState({ isEditing: false });
|
||||||
inputRef = useRef("input");
|
dispatch = useDispatch();
|
||||||
|
|
||||||
removeTodo() {
|
constructor(...args) {
|
||||||
this.env.store.dispatch("removeTodo", this.props.id);
|
super(...args);
|
||||||
|
useAutofocus("input");
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleTodo() {
|
editTodo() {
|
||||||
this.env.store.dispatch("toggleTodo", this.props.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
async editTodo() {
|
|
||||||
this.state.isEditing = true;
|
this.state.isEditing = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
focusInput() {
|
|
||||||
this.inputRef.el.value = "";
|
|
||||||
this.inputRef.el.focus();
|
|
||||||
this.inputRef.el.value = this.props.title;
|
|
||||||
}
|
|
||||||
|
|
||||||
handleKeyup(ev) {
|
handleKeyup(ev) {
|
||||||
if (ev.keyCode === ENTER_KEY) {
|
if (ev.keyCode === ENTER_KEY) {
|
||||||
this.updateTitle(ev.target.value);
|
this.updateTitle(ev.target.value);
|
||||||
@@ -493,48 +488,34 @@ class TodoItem extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateTitle(title) {
|
updateTitle(title) {
|
||||||
const value = title.trim();
|
this.dispatch("updateTodo", {title, id: this.props.id});
|
||||||
if (!value) {
|
this.state.isEditing = false;
|
||||||
this.removeTodo(this.props.id);
|
|
||||||
} else {
|
|
||||||
this.env.store.dispatch("editTodo", {
|
|
||||||
id: this.props.id,
|
|
||||||
title: value
|
|
||||||
});
|
|
||||||
this.state.isEditing = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// TodoApp
|
// TodoApp
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
class TodoApp extends owl.store.ConnectedComponent {
|
class TodoApp extends Component {
|
||||||
static components = { TodoItem };
|
static components = { TodoItem };
|
||||||
state = useState({ filter: "all" });
|
state = useState({ filter: "all" });
|
||||||
|
todos = useStore(state => state.todos);
|
||||||
|
dispatch = useDispatch();
|
||||||
|
|
||||||
static mapStoreToProps(state) {
|
|
||||||
return {
|
|
||||||
todos: state.todos
|
|
||||||
};
|
|
||||||
}
|
|
||||||
get visibleTodos() {
|
get visibleTodos() {
|
||||||
let todos = this.storeProps.todos;
|
switch (this.state.filter) {
|
||||||
if (this.state.filter === "active") {
|
case "active": return this.todos.filter(t => !t.completed);
|
||||||
todos = todos.filter(t => !t.completed);
|
case "completed": return this.todos.filter(t => t.completed);
|
||||||
|
case "all": return this.todos;
|
||||||
}
|
}
|
||||||
if (this.state.filter === "completed") {
|
|
||||||
todos = todos.filter(t => t.completed);
|
|
||||||
}
|
|
||||||
return todos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get allChecked() {
|
get allChecked() {
|
||||||
return this.storeProps.todos.every(todo => todo.completed);
|
return this.todos.every(todo => todo.completed);
|
||||||
}
|
}
|
||||||
|
|
||||||
get remaining() {
|
get remaining() {
|
||||||
return this.storeProps.todos.filter(todo => !todo.completed).length;
|
return this.todos.filter(todo => !todo.completed).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
get remainingText() {
|
get remainingText() {
|
||||||
@@ -560,12 +541,25 @@ class TodoApp extends owl.store.ConnectedComponent {
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// App Initialization
|
// App Initialization
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
const store = makeStore();
|
function saveState(state) {
|
||||||
const qweb = new owl.QWeb(TEMPLATES);
|
const str = JSON.stringify(state);
|
||||||
const env = {
|
window.localStorage.setItem(LOCALSTORAGE_KEY, str);
|
||||||
qweb,
|
}
|
||||||
store,
|
|
||||||
};
|
function loadState() {
|
||||||
|
const localState = window.localStorage.getItem(LOCALSTORAGE_KEY);
|
||||||
|
return localState ? JSON.parse(localState) : initialState;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeEnv() {
|
||||||
|
const state = loadState();
|
||||||
|
const store = new owl.Store({ state, actions });
|
||||||
|
store.on("update", null, () => saveState(store.state));
|
||||||
|
const qweb = new owl.QWeb(TEMPLATES);
|
||||||
|
return { qweb, store };
|
||||||
|
}
|
||||||
|
|
||||||
|
const env = makeEnv();
|
||||||
const app = new TodoApp(env);
|
const app = new TodoApp(env);
|
||||||
app.mount(document.body);
|
app.mount(document.body);
|
||||||
`;
|
`;
|
||||||
@@ -576,7 +570,7 @@ const TODO_APP_STORE_XML = `<templates>
|
|||||||
<h1>todos</h1>
|
<h1>todos</h1>
|
||||||
<input class="new-todo" autofocus="true" autocomplete="off" placeholder="What needs to be done?" t-on-keyup="addTodo"/>
|
<input class="new-todo" autofocus="true" autocomplete="off" placeholder="What needs to be done?" t-on-keyup="addTodo"/>
|
||||||
</header>
|
</header>
|
||||||
<section class="main" t-if="storeProps.todos.length">
|
<section class="main" t-if="todos.length">
|
||||||
<input class="toggle-all" id="toggle-all" type="checkbox" t-att-checked="allChecked" t-on-click="dispatch('toggleAll', !allChecked)"/>
|
<input class="toggle-all" id="toggle-all" type="checkbox" t-att-checked="allChecked" t-on-click="dispatch('toggleAll', !allChecked)"/>
|
||||||
<label for="toggle-all"></label>
|
<label for="toggle-all"></label>
|
||||||
<ul class="todo-list">
|
<ul class="todo-list">
|
||||||
@@ -585,7 +579,7 @@ const TODO_APP_STORE_XML = `<templates>
|
|||||||
</t>
|
</t>
|
||||||
</ul>
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
<footer class="footer" t-if="storeProps.todos.length">
|
<footer class="footer" t-if="todos.length">
|
||||||
<span class="todo-count">
|
<span class="todo-count">
|
||||||
<strong>
|
<strong>
|
||||||
<t t-esc="remaining"/>
|
<t t-esc="remaining"/>
|
||||||
@@ -603,7 +597,7 @@ const TODO_APP_STORE_XML = `<templates>
|
|||||||
<a t-on-click="setFilter('completed')" t-att-class="{selected: state.filter === 'completed'}">Completed</a>
|
<a t-on-click="setFilter('completed')" t-att-class="{selected: state.filter === 'completed'}">Completed</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<button class="clear-completed" t-if="storeProps.todos.length gt remaining" t-on-click="dispatch('clearCompleted')">
|
<button class="clear-completed" t-if="todos.length gt remaining" t-on-click="dispatch('clearCompleted')">
|
||||||
Clear completed
|
Clear completed
|
||||||
</button>
|
</button>
|
||||||
</footer>
|
</footer>
|
||||||
@@ -611,13 +605,13 @@ const TODO_APP_STORE_XML = `<templates>
|
|||||||
|
|
||||||
<li t-name="TodoItem" class="todo" t-att-class="{completed: props.completed, editing: state.isEditing}">
|
<li t-name="TodoItem" class="todo" t-att-class="{completed: props.completed, editing: state.isEditing}">
|
||||||
<div class="view">
|
<div class="view">
|
||||||
<input class="toggle" type="checkbox" t-on-change="toggleTodo" t-att-checked="props.completed"/>
|
<input class="toggle" type="checkbox" t-on-change="dispatch('toggleTodo', props.id)" t-att-checked="props.completed"/>
|
||||||
<label t-on-dblclick="editTodo">
|
<label t-on-dblclick="editTodo">
|
||||||
<t t-esc="props.title"/>
|
<t t-esc="props.title"/>
|
||||||
</label>
|
</label>
|
||||||
<button class="destroy" t-on-click="removeTodo"></button>
|
<button class="destroy" t-on-click="dispatch('removeTodo', props.id)"></button>
|
||||||
</div>
|
</div>
|
||||||
<input class="edit" t-ref="input" t-if="state.isEditing" t-att-value="props.title" t-on-keyup="handleKeyup" t-mounted="focusInput" t-on-blur="handleBlur"/>
|
<input class="edit" t-ref="input" t-if="state.isEditing" t-att-value="props.title" t-on-keyup="handleKeyup" t-on-blur="handleBlur"/>
|
||||||
</li>
|
</li>
|
||||||
</templates>`;
|
</templates>`;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user