-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
177 lines (170 loc) · 4.84 KB
/
index.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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/vue.min.js"></script>
<script src="js/underscore-min.js"></script>
<script src="js/store.js" charset="utf-8"></script>
</head>
<body>
<section class="todoapp">
<header class="header">
<h1>云备忘</h1>
<input class="new-todo" placeholder="你将要做的是什么?" autofocus="" v-model="newTodo" @keyup.enter.trim="addTodo">
</header>
<section class="main" style="display: block;" v-show="showList">
<input class="toggle-all" id="toggle-all" type="checkbox" v-model="allDone" />
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<li
:class="{completed: todo.checked, editing: index === editingIndex}"
v-for="(todo,index) in filteredTodoList" :key="'todo-'+index">
<div class="view">
<input class="toggle" type="checkbox" v-model="todo.checked">
<label @dblclick="editTodo(index)">{{ todo.text }}</label>
<button class="destroy" @click="deleteTodo(todo)"></button>
</div>
<input class="edit" type="text" v-model="todo.text" v-focus="index === editingIndex" @blur="saveTodo(todo)" @keyup.enter="saveTodo(todo)"/>
</li>
</ul>
</section>
<footer class="footer" style="display: block;" v-show="showList">
<span class="todo-count"><strong>{{ activeCount }}</strong> 项 未完成</span>
<ul class="filters">
<li>
<a
:class="{selected: visibility === 'all'}"
href="#/"
@click="visibility='all'"
>所有</a>
</li>
<li>
<a
:class="{selected: visibility === 'active'}"
href="#/active"
@click="visibility = 'active'"
>未完成</a>
</li>
<li>
<a
:class="{selected: visibility === 'completed'}"
href="#/completed"
@click="visibility = 'completed'">已完成</a>
</ul>
<button class="clear-completed" @click="clearCompleted" v-show="completedCount > 0">清空已完成</button>
</footer>
</section>
<script>
var filters = {
all: function (todos) {
return todos;
},
active: function (todos) {
return todos.filter(function (todo) {
return !todo.checked;
});
},
completed: function (todos) {
return todos.filter(function (todo) {
return todo.checked;
});
}
};
Vue.directive('focus', {
// 当绑定元素插入到 DOM 中。
inserted: function (el) {
// 聚焦元素
el.focus();
},
update(el) {
el.focus();
}
})
var visibility = location.hash.substr(location.hash.indexOf('/')+1);
visibility = visibility === '' ? 'all' : visibility
// 新建一个Vue的实例对象
var todoapp = new Vue({
watch: {
todoList: {
deep: true,
handler: todoStorage.save
}
},
// 挂载
el: '.todoapp',
// 数据
data: {
visibility: visibility,
editingIndex: -1,
newTodo: '',
// 备忘录数组
todoList: todoStorage.fetch()
},
// 方法
methods: {
addTodo() {
// 去除前后的空格
this.newTodo = this.newTodo.trim();
//内容为空
if (this.newTodo.length < 1) {
return;
}
// 新建的任务添加到数组,默认状态为未完成
this.todoList.unshift({
text: this.newTodo,
checked: false
});
// 回车后清空输入框的内容
this.newTodo = ''
},
deleteTodo(todo) {
this.todoList = _.without(this.todoList, todo)
},
editTodo(index) {
// 设置一下当前正在编辑的索引
this.editingIndex = index;
},
saveTodo(todo) {
this.editingIndex = -1
if (todo.text.trim().length < 1) {
this.deleteTodo(todo)
}
},
clearCompleted() {
this.todoList = filters.active(this.todoList)
}
},
// 计算属性
computed: {
showList() {
return this.todoList.length > 0;
},
activeCount() {
return filters.active(this.todoList).length;
},
allDone: {
get() {
// 未完成的数量为0表示全部完成,全部完成返回true
return this.activeCount === 0;
},
set(value) {
this.todoList.forEach(todo => {
todo.checked = value
});
}
},
filteredTodoList: function () {
return filters[this.visibility](this.todoList);
},
completedCount() {
return filters.completed(this.todoList).length;
},
}
})
</script>
</body>
</html>