-
Notifications
You must be signed in to change notification settings - Fork 1
/
todomvc.html
223 lines (202 loc) · 9.22 KB
/
todomvc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<!--
Original: https://github.com/tastejs/todomvc/tree/gh-pages/examples/vue
Adapted to be contained in one HTML document.
-->
<meta charset="utf-8">
<title>TodoMVC - Single page solution</title>
<link href="http://todomvc.com/examples/vue/node_modules/todomvc-common/base.css" rel="stylesheet">
<link href="http://todomvc.com/examples/vue/node_modules/todomvc-app-css/index.css" rel="stylesheet">
<script src="../dist/vue-blocks.js"></script>
</head>
<body>
<app>
</app>
<template url="/:visibility?">
<todoapp :visibility="$route.params.visibility"></todoapp>
</template>
<template component="app">
<router-view></router-view>
</template>
<template component="todoapp">
<section class="todoapp" v-cloak>
<header class="header">
<h1>todos</h1>
<input class="new-todo" autofocus autocomplete="off" placeholder="What needs to be done?" v-model="newTodo" @keyup.enter="addTodo">
</header>
<section class="main" v-show="todos.length">
<input id="toggle-all" class="toggle-all" type="checkbox" v-model="allDone">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<li class="todo" v-for="todo in filteredTodos" :key="todo.id" :class="{completed: todo.completed, editing: todo == editedTodo}">
<div class="view">
<input class="toggle" type="checkbox" v-model="todo.completed">
<label @dblclick="editTodo(todo)">{{todo.title}}</label>
<button class="destroy" @click="removeTodo(todo)"></button>
</div>
<input class="edit" type="text" v-model="todo.title" v-todo-focus="todo == editedTodo" @blur="doneEdit(todo)" @keyup.enter="doneEdit(todo)" @keyup.esc="cancelEdit(todo)">
</li>
</ul>
</section>
<footer class="footer" v-show="todos.length">
<span class="todo-count">
<strong v-text="remaining"></strong> {{pluralize('item', remaining)}} left
</span>
<ul class="filters">
<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>
</ul>
<button class="clear-completed" @click="removeCompleted" v-show="todos.length > remaining">
Clear completed
</button>
</footer>
</section>
<script>
var filters = {
all: function (todos) {
return todos;
},
active: function (todos) {
return todos.filter(function (todo) {
return !todo.completed;
});
},
completed: function (todos) {
return todos.filter(function (todo) {
return todo.completed;
});
}
};
var todoStorage = require('todoStorage');
export default {
// app initial state
data() {
return {
todos: todoStorage.fetch(),
newTodo: '',
editedTodo: null,
}
},
props: {
visibility: {
type: String,
default: 'all'
}
},
// watch todos change for localStorage persistence
watch: {
todos: {
deep: true,
handler: todoStorage.save
}
},
// computed properties
// http://vuejs.org/guide/computed.html
computed: {
filteredTodos: function () {
return filters[this.visibility](this.todos);
},
remaining: function () {
return filters.active(this.todos).length;
},
allDone: {
get: function () {
return this.remaining === 0;
},
set: function (value) {
this.todos.forEach(function (todo) {
todo.completed = value;
});
}
}
},
// methods that implement data logic.
// note there's no DOM manipulation here at all.
methods: {
pluralize: function (word, count) {
return word + (count === 1 ? '' : 's');
},
addTodo: function () {
var value = this.newTodo && this.newTodo.trim();
if (!value) {
return;
}
this.todos.push({ id: this.todos.length + 1, title: value, completed: false });
this.newTodo = '';
},
removeTodo: function (todo) {
var index = this.todos.indexOf(todo);
this.todos.splice(index, 1);
},
editTodo: function (todo) {
this.beforeEditCache = todo.title;
this.editedTodo = todo;
},
doneEdit: function (todo) {
if (!this.editedTodo) {
return;
}
this.editedTodo = null;
todo.title = todo.title.trim();
if (!todo.title) {
this.removeTodo(todo);
}
},
cancelEdit: function (todo) {
this.editedTodo = null;
todo.title = this.beforeEditCache;
},
removeCompleted: function () {
this.todos = filters.active(this.todos);
}
},
// a custom directive to wait for the DOM to be updated
// before focusing on the input field.
// http://vuejs.org/guide/custom-directive.html
directives: {
'todo-focus': function (el, binding) {
if (binding.value) {
el.focus();
}
}
}
}
</script>
</template>
<footer class="info">
<p>Double-click to edit a todo</p>
<p>Original by <a href="http://evanyou.me">Evan You</a></p>
<p>Vue-blocks adaptation by <a href="https://github.com/j-angnoe">Joshua Angnoe</a></p>
<p>Not (yet) a part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
<template module="todoStorage">
<script>
var STORAGE_KEY = 'todos-vuejs';
export default {
fetch: function () {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
} catch (e) {
console.error('Unable to use localStorage', e);
return [
{ id: 1, title: 'Todo 1', completed: true },
{ id: 2, title: 'Todo 2', completed: false },
{ id: 3, title: 'Todo 3', completed: false }
]
}
},
save: function (todos) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
} catch (e) {
console.error('Unable to use localStorage', e);
}
}
};
</script>
</template>
</body>
</html>