-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (104 loc) · 3.46 KB
/
index.js
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
NEJ.define([
'util/chain/chainable',
'util/cache/storage',
'base/element',
'base/event'
],function($, _j, _e, _v){
var todoListTpl = $('#todolist');
var countTpl = $('#count');
var allBut = $('#allBut');
var actBut = $('#actBut');
var comBut = $('#comBut');
var currentState = 'all';
var tpl = function(item, index) {
var className = item.completed ? 'active' : '';
return `<div class="list-item ${className}" list-index="${index}">
<div class="chooseIcon"></div>
<div class="list-text">
<span>${item.value}</span>
</div>
<div class="list-remove"}>X</div>
</div>`;
};
var getTodoList = () => JSON.parse(_j._$getDataInStorage('todoList') || "[]");
var setTodoList = list => _j._$setDataInStorage('todoList', JSON.stringify(list));
var setListTpl = function(flag){
// flag = all / active / completed
var all = JSON.parse(_j._$getDataInStorage('todoList') || "[]");
var active = [];
var complete = [];
var list;
allBut[0].className = 'todo-type';
actBut[0].className = 'todo-type';
comBut[0].className = 'todo-type';
all.forEach(item => {
if(item.completed){
complete.push(item);
}else{
active.push(item);
}
});
if(flag==='all'){
allBut[0].className = 'todo-type choosed';
currentState = 'all';
list = all;
}
else if(flag === 'active'){
actBut[0].className = 'todo-type choosed';
currentState = 'active';
list = active;
}
else{
comBut[0].className = 'todo-type choosed';
currentState = 'complete';
list = complete;
}
var html = '';
list.forEach((item, index) => {
html += tpl(item, index);
});
todoListTpl[0].innerHTML = html;
countTpl[0].innerHTML = active.length;
}
//初始化
setListTpl('all');
_v._$addEvent(allBut[0],'click',function(_event){
setListTpl('all');
});
_v._$addEvent(actBut[0],'click',function(_event){
setListTpl('active');
});
_v._$addEvent(comBut[0],'click',function(_event){
setListTpl('complete');
});
_v._$addEvent('input', 'enter', function(_event){
var temp = {
completed: false,
value: _event.target.value
}
const tempList = getTodoList();
tempList.push(temp);
setTodoList(tempList);
_event.target.value = '';
setListTpl(currentState);
}, false
);
todoListTpl._$on('click','.chooseIcon', function(){
var parent = $(this)._$parent('.list-item');
var tempTodoList = getTodoList();
var index = _e._$attr(parent[0], 'list-index');
parent._$remove();
tempTodoList[index].completed = true;
setTodoList(tempTodoList);
setListTpl(currentState);
});
todoListTpl._$on('click','.list-remove', function(){
var parent = $(this)._$parent('.list-item');
var tempTodoList = getTodoList();
var index = _e._$attr(parent[0], 'list-index');
parent._$remove();
tempTodoList.splice(index, 1);
setTodoList(tempTodoList);
setListTpl(currentState);
});
});