Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var todo = (function(){
var tasks = [];
var STATE_P = '진행';
var STATE_C = '완료';
var mode = 'html';
var addTask = (function(){
var id = 0;
return function(title){
var result = id;
tasks.push({id: id++, title: title, state: STATE_P});
render();
return result;
}
})();
var removeTask = function(id){
var isRemoved = false;
for(var i =0; i< tasks.length; i++){
if(tasks[i].id === id){
tasks.splice(i,1);
isRemoved = true;
break;
}
}
if(!isRemoved) {
warning('삭제된거 없음(removeTask : invalid id)');
}
render();
};
var changeState = function(id, state){
var ID = false, STATE;
for(var i = 0; i < tasks.length; i++){
if(tasks[i].id === id){
ID = id;
break;
}
}
if(ID === false) {
warning('changeState : invalid id - ' + id);
return;
}
STATE = state;
for(var i =0; i< tasks.length; i++){
if(tasks[i].id === ID){
tasks[i].state = STATE;
break;
}
}
render();
};
var warning = console.log;
var init, render;
(function(){
var completeLi, progressLi;
init = (function(){
var initHtml = function(){
progressLi = document.querySelector('#todo .progress li');
completeLi = document.querySelector('#todo .complete li');
progressLi.parentNode.removeChild(progressLi);
completeLi.parentNode.removeChild(completeLi);
};
return function(){
if(mode === 'html') initHtml();
};
})();
render = (function(){
var renderConsole = function(){
var task;
console.log('진행');
for(var i = 0; i < tasks.length; i++){
task = tasks[i];
if(task.state === '진행'){
console.log(task.id+'.', task.title+'('+task.state+')');
}
}
console.log('완료');
for(var i = 0; i < tasks.length; i++){
task = tasks[i];
if(task.state === '완료'){
console.log(task.id+'.', task.title+'('+task.state+')');
}
}
};
var renderHtml = function(){
console.log('// 각 목록(진행, 완료)을 비운다.');
document.querySelector('#todo .progress').innerHtml = '';
document.querySelector('#todo .complete').innerHtml = '';
console.log('// 진행 목록(ul)에 li를 채운다.');
console.log('// 완료 목록(ul)에 li를 채운다.');
console.log('// 할일 입력창을 비운다.');
};
return function() {
if (mode === 'console') {
renderConsole();
} else if (mode === 'html') {
renderHtml();
}
}
})();
})();
render();
return{
init: init,
add: addTask,
remove: removeTask,
toggle: function(id){
for(var i = 0; i<tasks.length;i++){
if(tasks[i].id === id){
if(tasks[i].state === STATE_P) changeState(id, STATE_C);
else changeState(id, STATE_P);
break;
}
}
},
modeHtml: function(){
mode = 'html';
},
modeConsole: function(){
mode = 'console';
}
}
})();
todo.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment