const dataList = [
{
id: 1,
text: '吃饭',
status: true,
edit: false,
},
{
id: 2,
text: '睡觉',
status: false,
edit: false,
},
{
id: 3,
text: '打豆豆',
status: true,
edit: false,
}
]
const todoList = document.querySelector('.todo-list')
const todoApp = document.querySelector('.todoapp')
const toggleAll = document.querySelector('.toggle-all')
const todoCount = document.querySelector('.todo-count strong')
const clearCompleted = document.querySelector('.clear-completed')
const newTodo = document.querySelector('.new-todo')
let flag = false
function bindHtml(dataList) {
let str = ""
for(let i = 0; i < dataList.length; i++){
str += `
<li class="${dataList[i].status?"completed":""}">
<div class="view">
<input class="toggle" data-id="${dataList[i].id}" type="checkbox" ${dataList[i].status?"checked":""}>
<label class="label">${dataList[i].text}</label>
<button class="destroy" data-id="${i}"></button>
</div>
<input class="edit" data-old="${dataList[i].text}" value="${dataList[i].text}" data-id="${dataList[i].id}">
</li>
`
}
todoList.innerHTML = str
let count = 0
for(let n = 0; n < dataList.length; n++){
dataList[n].status && count++
}
toggleAll.checked = count === dataList.length
todoCount.innerHTML = dataList.length - count
clearCompleted.style.display = count?"block":"none"
}
bindHtml(dataList)
todoApp.onclick = function(e) {
if(e.target.className === "toggle-all"){
for(let i = 0; i < dataList.length; i++){
dataList[i].status = e.target.checked
}
bindHtml(dataList)
}
if(e.target.className === "toggle"){
for(let i = 0; i < dataList.length; i++){
if(e.target.dataset.id - 0 === dataList[i].id){
dataList[i].status = e.target.checked
}
}
bindHtml(dataList)
}
if(e.target.className === 'destroy'){
if(!confirm('您确定要删除吗?')) return
dataList.splice(e.target.dataset.id,1)
bindHtml(dataList)
}
}
newTodo.onkeydown = function(e) {
if(e.key !== "Enter") return
if(e.target.value === "") return
const obj = {
id:dataList.length + 1,
text:e.target.value,
status:false,
edit:false
}
dataList.push(obj)
bindHtml(dataList)
e.target.value = ""
}
todoApp.ondblclick = function(e) {
if(e.target.className === "label"){
e.target.parentNode.parentNode.classList.add('editing')
flag = true
}
}
document.onkeyup = function(e) {
if(!flag) return
if(e.key === "Escape"){
e.target.parentNode.classList.remove('editing')
e.target.value = e.target.dataset.old
flag = false
}
if(e.key === "Enter"){
e.target.parentNode.classList.remove('editing')
for(let i = 0; i < dataList.length; i++){
if(e.target.dataset.id - 0 === dataList[i].id){
dataList[i].text = e.target.value
}
}
bindHtml(dataList)
flag = false
}
}
let filters = document.querySelector('.filters')
filters.onclick = function(e) {
if(e.target.className === "selected"){
bindHtml(dataList)
}
if(e.target.className === "active"){
let newArr = []
for(let i = 0; i < dataList.length; i++){
if(!dataList[i].status){
newArr.push(dataList[i])
}
}
bindHtml(newArr)
}
if(e.target.className === "completed"){
let newArr = []
for(let i = 0; i < dataList.length; i++){
if(dataList[i].status){
newArr.push(dataList[i])
}
}
bindHtml(newArr)
}
}