-
Notifications
You must be signed in to change notification settings - Fork 0
/
todos.html
176 lines (168 loc) · 6.1 KB
/
todos.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="page-top">
<div class="page-content">
<h2>任务计划列表</h2>
</div>
</div>
<div id="app" class="main">
<h3 class="big-title">添加任务:</h3>
<input
placeholder="例如:吃饭睡觉打豆豆; 提示:+回车即可添加任务"
class="task-input"
type="text"
v-model="inputvalue"
@keyup.enter="add"
/>
<ul class="task-count" v-if="list.length!==0">
<li>{{noCheckedLength}}个任务未完成</li>
<li class="action">
<a :class="{active: hashName==='all'}" href="#all">所有任务</a>
<a :class="{active: hashName==='unfinished'}" href="#unfinished">未完成的任务</a>
<a :class="{active: hashName==='finished'}" href="#finished">完成的任务</a>
</li>
</ul>
<h3 class="big-title">任务列表:</h3>
<div class="tasks">
<span class="no-task-tip" v-if="list.length===0">还没有添加任何任务</span>
<ul class="todo-list">
<!--class editing-->
<li class="todo" :class="{completed: todo.checked,editing:todo===editorTodos}" v-for="todo in afterFilterList" >
<div class="view">
<input class="toggle" v-model="todo.checked" type="checkbox"/>
<label @dblclick="editorTodo(todo)">{{todo.name}}</label>
<button class="destroy" @click="deleteTodo(todo)"></button>
</div>
<input
class="edit"
type="text"
v-model="todo.name"
v-focus="todo===editorTodos"
@blur="editedTodo"
@keyup.13="editedTodo"
@keyup.esc="cancelEdit(todo)"
/>
</li>
</ul>
</div>
</div>
<script src="vue.js"></script>
<script type="text/javascript">
var store = {
save(key,value){
localStorage.setItem(key,JSON.stringify(value));
},
fetch(key){
return JSON.parse(localStorage.getItem(key)) || [];
}
}
// var data = [
// {
// name:'很好',
// checked:false
// },{
// name:'测试一下',
// checked:true
// }
// ];
var data = store.fetch("todosdata");
var app = new Vue({
el:'#app',
data:{
list:data,
inputvalue:'',
editorTodos:null,//正在编辑的对象
beforeTodo:null,//记录编辑前的name,以便取消时回滚
hashName:'all'//通过这个值的变化对list进行筛选
},
watch:{
// list:function(){
// store.save("todosdata",this.list);
// }
list:{
handler:function(){
store.save("todosdata",this.list);
},
deep:true
}
},
computed:{//计算属性
noCheckedLength: function(){//计算未完成的任务数
return this.list.filter(function(item){
return !item.checked
}).length;
},
afterFilterList: function(){//过滤后要显示的list
var filter = {
all: function(list){
return list;
},
unfinished: function(list){
return list.filter(function(item){
return !item.checked
});
},
finished: function(list){
return list.filter(function(item){
return item.checked
});
}
};
return filter[this.hashName](this.list);
}
},
methods:{
add:function(){//添加
this.list.push({
name:this.inputvalue,
checked:false
});
this.inputvalue = '';
},
deleteTodo:function(todo){//删除
var index = this.list.indexOf(todo);
this.list.splice(index,1);
},
editorTodo:function(todo){//开始编辑
this.editorTodos = todo;
this.beforeTodo = todo.name;
},
editedTodo:function(){//完成编辑
this.editorTodos = null;
},
cancelEdit:function(todo){//取消编辑
todo.name=this.beforeTodo;
this.editorTodos = null;
this.beforeTodo = null;
}
},
directives:{//自定义命令
"focus":{
update(el,binding){
if(binding.value){
el.focus();
}
}
}
}
});
function watchHashChange(){
var hash = window.location.hash.slice(1);
//只有在这三个状态时才修改hashNamev
var status = ['all','unfinished','finished'];
if(status.indexOf(hash)!==-1){
app.hashName = hash;
}
}
watchHashChange();
//监控hash变化
window.addEventListener("hashchange",watchHashChange);
</script>
</body>
</html>