2019-12-02 12:54:32 +08:00
|
|
|
<script src="../../dist/vue.global.js"></script>
|
2024-08-07 10:57:18 +08:00
|
|
|
<link
|
|
|
|
rel="stylesheet"
|
|
|
|
href="../../../../node_modules/todomvc-app-css/index.css"
|
|
|
|
/>
|
2019-10-25 10:23:08 +08:00
|
|
|
|
|
|
|
<div id="app">
|
|
|
|
<section class="todoapp">
|
|
|
|
<header class="header">
|
|
|
|
<h1>todos</h1>
|
2024-08-07 10:57:18 +08:00
|
|
|
<input
|
|
|
|
class="new-todo"
|
|
|
|
autofocus
|
|
|
|
autocomplete="off"
|
|
|
|
placeholder="What needs to be done?"
|
|
|
|
v-model="newTodo"
|
|
|
|
@keyup.enter="addTodo"
|
|
|
|
/>
|
2019-10-25 10:23:08 +08:00
|
|
|
</header>
|
2019-12-03 07:18:02 +08:00
|
|
|
<section class="main" v-show="todos.length">
|
2024-08-07 10:57:18 +08:00
|
|
|
<input
|
|
|
|
id="toggle-all"
|
|
|
|
class="toggle-all"
|
|
|
|
type="checkbox"
|
|
|
|
v-model="allDone"
|
|
|
|
/>
|
2019-10-25 10:23:08 +08:00
|
|
|
<label for="toggle-all">Mark all as complete</label>
|
|
|
|
<ul class="todo-list">
|
2024-08-07 10:57:18 +08:00
|
|
|
<li
|
|
|
|
v-for="todo in filteredTodos"
|
|
|
|
class="todo"
|
|
|
|
:key="todo.id"
|
|
|
|
:class="{ completed: todo.completed, editing: todo === editedTodo }"
|
|
|
|
>
|
2019-10-25 10:23:08 +08:00
|
|
|
<div class="view">
|
2024-08-07 10:57:18 +08:00
|
|
|
<input class="toggle" type="checkbox" v-model="todo.completed" />
|
2019-10-25 10:23:08 +08:00
|
|
|
<label @dblclick="editTodo(todo)">{{ todo.title }}</label>
|
|
|
|
<button class="destroy" @click="removeTodo(todo)"></button>
|
|
|
|
</div>
|
2024-08-07 10:57:18 +08:00
|
|
|
<input
|
|
|
|
class="edit"
|
|
|
|
type="text"
|
|
|
|
v-model="todo.title"
|
|
|
|
v-todo-focus="todo === editedTodo"
|
|
|
|
@blur="doneEdit(todo)"
|
|
|
|
@keyup.enter="doneEdit(todo)"
|
|
|
|
@keyup.escape="cancelEdit(todo)"
|
|
|
|
/>
|
2019-10-25 10:23:08 +08:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</section>
|
2019-12-03 07:18:02 +08:00
|
|
|
<footer class="footer" v-show="todos.length">
|
2024-08-07 10:57:18 +08:00
|
|
|
<span class="todo-count">
|
|
|
|
<strong>{{ remaining }}</strong>
|
|
|
|
<span>{{ pluralize(remaining) }} left</span>
|
|
|
|
</span>
|
2019-10-25 10:23:08 +08:00
|
|
|
<ul class="filters">
|
2024-08-07 10:57:18 +08:00
|
|
|
<li>
|
|
|
|
<a href="#/all" :class="{ selected: visibility === 'all' }">All</a>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a href="#/active" :class="{ selected: visibility === 'active' }"
|
|
|
|
>Active</a
|
|
|
|
>
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<a
|
|
|
|
href="#/completed"
|
|
|
|
:class="{ selected: visibility === 'completed' }"
|
|
|
|
>Completed</a
|
|
|
|
>
|
|
|
|
</li>
|
2019-10-25 10:23:08 +08:00
|
|
|
</ul>
|
2024-08-07 10:57:18 +08:00
|
|
|
<button
|
|
|
|
class="clear-completed"
|
|
|
|
@click="removeCompleted"
|
|
|
|
v-show="todos.length > remaining"
|
|
|
|
>
|
2019-10-25 10:23:08 +08:00
|
|
|
Clear completed
|
|
|
|
</button>
|
|
|
|
</footer>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
2024-08-07 10:57:18 +08:00
|
|
|
const STORAGE_KEY = 'todos-vuejs-3.x'
|
|
|
|
const todoStorage = {
|
|
|
|
fetch() {
|
|
|
|
const todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
|
|
|
|
todos.forEach((todo, index) => {
|
|
|
|
todo.id = index
|
|
|
|
})
|
|
|
|
todoStorage.uid = todos.length
|
|
|
|
return todos
|
|
|
|
},
|
|
|
|
save(todos) {
|
|
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
|
|
|
|
},
|
2019-10-25 10:23:08 +08:00
|
|
|
}
|
2024-08-07 10:57:18 +08:00
|
|
|
|
|
|
|
const filters = {
|
|
|
|
all(todos) {
|
|
|
|
return todos
|
2019-10-25 10:23:08 +08:00
|
|
|
},
|
2024-08-07 10:57:18 +08:00
|
|
|
active(todos) {
|
|
|
|
return todos.filter(todo => {
|
|
|
|
return !todo.completed
|
|
|
|
})
|
2019-10-25 10:23:08 +08:00
|
|
|
},
|
2024-08-07 10:57:18 +08:00
|
|
|
completed(todos) {
|
|
|
|
return todos.filter(function (todo) {
|
|
|
|
return todo.completed
|
2019-10-25 10:23:08 +08:00
|
|
|
})
|
|
|
|
},
|
2024-08-07 10:57:18 +08:00
|
|
|
}
|
2019-10-25 10:23:08 +08:00
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
Vue.createApp({
|
|
|
|
// app initial state
|
|
|
|
data: () => ({
|
|
|
|
todos: todoStorage.fetch(),
|
|
|
|
newTodo: '',
|
|
|
|
editedTodo: null,
|
|
|
|
visibility: 'all',
|
|
|
|
}),
|
|
|
|
|
|
|
|
// watch todos change for localStorage persistence
|
|
|
|
watch: {
|
|
|
|
todos: {
|
|
|
|
handler(todos) {
|
|
|
|
todoStorage.save(todos)
|
|
|
|
},
|
|
|
|
deep: true,
|
|
|
|
},
|
2019-10-25 10:23:08 +08:00
|
|
|
},
|
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
mounted() {
|
|
|
|
window.addEventListener('hashchange', this.onHashChange)
|
|
|
|
this.onHashChange()
|
2019-10-25 10:23:08 +08:00
|
|
|
},
|
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
computed: {
|
|
|
|
filteredTodos() {
|
|
|
|
return filters[this.visibility](this.todos)
|
|
|
|
},
|
|
|
|
remaining() {
|
|
|
|
return filters.active(this.todos).length
|
|
|
|
},
|
|
|
|
allDone: {
|
|
|
|
get() {
|
|
|
|
return this.remaining === 0
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this.todos.forEach(function (todo) {
|
|
|
|
todo.completed = value
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
2019-10-25 10:23:08 +08:00
|
|
|
},
|
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
// methods that implement data logic.
|
|
|
|
// note there's no DOM manipulation here at all.
|
|
|
|
methods: {
|
|
|
|
addTodo() {
|
|
|
|
var value = this.newTodo && this.newTodo.trim()
|
|
|
|
if (!value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.todos.push({
|
|
|
|
id: todoStorage.uid++,
|
|
|
|
title: value,
|
|
|
|
completed: false,
|
|
|
|
})
|
|
|
|
this.newTodo = ''
|
|
|
|
},
|
2019-10-25 10:23:08 +08:00
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
removeTodo(todo) {
|
|
|
|
this.todos.splice(this.todos.indexOf(todo), 1)
|
|
|
|
},
|
2019-10-25 10:23:08 +08:00
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
editTodo(todo) {
|
|
|
|
this.beforeEditCache = todo.title
|
|
|
|
this.editedTodo = todo
|
|
|
|
},
|
|
|
|
|
|
|
|
doneEdit(todo) {
|
|
|
|
if (!this.editedTodo) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.editedTodo = null
|
|
|
|
todo.title = todo.title.trim()
|
|
|
|
if (!todo.title) {
|
|
|
|
this.removeTodo(todo)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelEdit(todo) {
|
|
|
|
this.editedTodo = null
|
|
|
|
todo.title = this.beforeEditCache
|
|
|
|
},
|
|
|
|
|
|
|
|
removeCompleted() {
|
|
|
|
this.todos = filters.active(this.todos)
|
|
|
|
},
|
|
|
|
|
|
|
|
onHashChange() {
|
|
|
|
var visibility = window.location.hash.replace(/#\/?/, '')
|
|
|
|
if (filters[visibility]) {
|
|
|
|
this.visibility = visibility
|
|
|
|
} else {
|
|
|
|
window.location.hash = ''
|
|
|
|
this.visibility = 'all'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
pluralize(n) {
|
|
|
|
return n === 1 ? 'item' : 'items'
|
|
|
|
},
|
2019-10-25 10:23:08 +08:00
|
|
|
},
|
|
|
|
|
2024-08-07 10:57:18 +08:00
|
|
|
directives: {
|
|
|
|
'todo-focus'(el, binding) {
|
|
|
|
if (binding.value) {
|
|
|
|
el.focus()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).mount('#app')
|
2019-10-25 10:23:08 +08:00
|
|
|
</script>
|