Skip to content
Prev Previous commit
Next Next commit
undo기능 구현
  • Loading branch information
kkw10 committed Apr 29, 2019
commit f7b8aebf2a482556fd70e9f15a06a7c8139be5a9
5 changes: 3 additions & 2 deletions oop/ErrorCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ ErrorCheck.prototype.syntaxError = function(input) {
let firstWord = input.match(/\w+/);
let seperator = input.match(/\$/g);
let zeroSeperator = (seperator===null);
if(zeroSeperator === true){
if(zeroSeperator === true && !(firstWord[0] ==="undo" || firstWord[0] === "redo")) {
return false;
}else{
let zeroSeperator = (firstWord[0] ==="undo" || firstWord[0] === "redo");
let oneSeperator = ((firstWord[0] === "delete" || firstWord[0] === "show") && (seperator.length===1));
let twoSeperator = ((firstWord[0] === "add" || firstWord[0] === "update") && (seperator.length===2));
return oneSeperator || twoSeperator ? true : false;
return zeroSeperator || oneSeperator || twoSeperator ? true : false;
}

}
Expand Down
202 changes: 136 additions & 66 deletions oop/my-todo-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,139 +3,207 @@ const todoList = require('./data.js');
const errorCheck = require('./ErrorCheck.js');
const errorMsg = require('./errorMsg.js');
const todoPrint = require('./todoPrint.js');
//const util = require('./todo-util-method.js');
const todoCommonMethod = function() {}

const Print = new todoPrint.ShowPrint();
const Error = new errorCheck.ErrorCheck();
const Print = new todoPrint.ShowPrint();

let r = readline.createInterface({
input:process.stdin,
output:process.stdout
});

const todoForm = function (name, tag, status, id) {
this.name = name;
this.tag = [tag];
this.status = status;
this.id = id;
}



const todoModel = Object.create(todoCommonMethod.prototype)

// show
todoModel.show = function (input) {
let status = input[0];
status === 'all' ? this.todoCount() : this.showElse(status);
const showAllTimer = (input) => {
setTimeout(todoCommonMethod.prototype.todoCount, input);
}

// add
todoModel.add = function (input) {
let [name, tag] = input;
let id = this.makingID();
let newTodoList = new this.todoForm(name, tag, 'todo', id);
todoList.push(newTodoList);
const todoCommonMethod = class {
todoCount() {
let todo = todoList.filter(v => v.status === 'todo').length;
let doing = todoList.filter(v => v.status === 'doing').length;
let done = todoList.filter(v => v.status === 'done').length;

Print.printShowAll(todo,doing,done);
}

showElse(input) {
let temp = todoList.filter(v => v.status === input).map((obj)=>{ return ` '${obj.name}, ${obj.id}번'`})
Print.printShowElse(temp.length, temp);
}

makingID(){
return Math.floor(Math.random() * 10000);
}

Print.printAdd(name,id);
showAllTimer(1000);
findDataIdObj(input){
let target = todoList.filter(v => v["id"] === input);
let targetName = target[0].name;
let targetIdx = todoList.indexOf(target[0]);
return [targetIdx, targetName];
}
}

// delete
todoModel.delete = function (id) {
let [targetIdx, targetName] = this.findDataIdObj(id)
todoList.splice(targetIdx,1);

Print.printDelete(targetName);
showAllTimer(1000);
}

// update
todoModel.update = function (id, status) {
let [targetIdx, targetName] = this.findDataIdObj(id)
todoList[targetIdx].status = status;
const undoHistory = [];

setTimeout(()=>{Print.printUpdate(targetName, status)}, 1000);
showAllTimer(4000);
}
const HistoryForm = function (model, name, id, status, tag, prevCommend){
this.model = model;
this.name = name;
this.id = id;
this.status = status;
this.tag = tag;
this.prevCommend = prevCommend;
}

const makeAddHistory = function(model, name, id, status, tag, tempHistory){
let historyForm = new HistoryForm(model, name, id, status, tag, tempHistory);

todoCommonMethod.prototype.todoCount = function () {
let todo = todoList.filter(v => v.status === 'todo').length;
let doing = todoList.filter(v => v.status === 'doing').length;
let done = todoList.filter(v => v.status === 'done').length;
if(undoHistory.length < 3) {
undoHistory.push(historyForm);
} else {
undoHistory.shift();
undoHistory.push(historyForm);
}

Print.printShowAll(todo,doing,done);
}
const makeElseHistory = function(model, idx, tempHistory) {
let todoListData = todoList[idx]
let [name, tag, status, id] = [todoListData['name'], todoListData['tag'], todoListData['status'], todoListData['id']];
let historyForm = new HistoryForm(model, name, id, status, tag, tempHistory);

todoCommonMethod.prototype.showElse = function(input) {
let temp = todoList.filter(v => v.status === input).map((obj)=>{ return ` '${obj.name}, ${obj.id}번'`})
Print.printShowElse(temp.length, temp);
if(undoHistory.length < 3) {
undoHistory.push(historyForm);
} else {
undoHistory.shift();
undoHistory.push(historyForm);
}
}

todoCommonMethod.prototype.todoForm = function (name, tag, status, id) {
this.name = name;
this.tag = [tag];
this.status = status;
this.id = id;
}
const undoModel = function(model) {
let target = undoHistory[undoHistory.length-1];
let [name, tag, status, id] = [target['name'], target['tag'], target['status'], target['id']];

if(model === 'add') {
undoAdd(id);

todoCommonMethod.prototype.makingID = function(){
return Math.floor(Math.random() * 10000);
}
} else if (model === 'update') {
undoUpdate(id, status);

} else if (model === 'delete') {
undoDelete(id, name, tag, status)

todoCommonMethod.prototype.findDataIdObj = function(input){
let target = todoList.filter(v => v["id"] === input);
let targetName = target[0].name;
let targetIdx = todoList.indexOf(target[0]);
return [targetIdx, targetName];
}
}

const undoAdd = function(id) {
todoModel.delete(id)
}
const undoUpdate = function(id, status) {
todoModel.update(id, status)
}
const undoDelete = function(id, name, tag, status) {
let newTodoList = new todoForm(name, tag[0], status, id);
todoList.push(newTodoList);
}



const showAllTimer = (input) => {
setTimeout(todoCommonMethod.prototype.todoCount, input);

const TodoModel = class extends todoCommonMethod{
//class todoModel extends todoCommonMethod{
show(input) {
let status = input[0];
status === 'all' ? this.todoCount() : this.showElse(status);
}
add(input, tempHistory) {
let [name, tag] = input;
let id = this.makingID();
let newTodoList = new todoForm(name, tag, 'todo', id);
makeAddHistory("add", name, id, 'todo', tag, tempHistory);

todoList.push(newTodoList);

Print.printAdd(name,id);
showAllTimer(1000);
}
delete(id, tempHistory) {
let [targetIdx, targetName] = this.findDataIdObj(id)
makeElseHistory('delete', targetIdx, tempHistory);
todoList.splice(targetIdx,1);

Print.printDelete(targetName);
showAllTimer(1000);
}
update(id, status, tempHistory) {
let [targetIdx, targetName] = this.findDataIdObj(id)
makeElseHistory('update', targetIdx, tempHistory);
todoList[targetIdx].status = status;

setTimeout(()=>{Print.printUpdate(targetName, status)}, 1000);
showAllTimer(4000);
}

}


const todoModel = new TodoModel();


//var arr = "add$add$add"

//var arr = "update$9547$doing"
// input & ErrorCheck
todoMain = (answer) => {
if(Error.syntaxError(answer) === false) {
Print.printError(errorMsg.syntaxError);
return
return true;
}

let tempHistory = answer;
let tempArr = answer.match(/\w+/g);
let action = tempArr.shift(0);

if(action === "add") {
todoModel.add(tempArr);
todoModel.add(tempArr, tempHistory);

} else if(action === "delete") {
tempArr[0] = Number(tempArr[0])
let ID = tempArr[0]
Error.unknownIDError(ID)==false ? Print.printError(errorMsg.unknownIDError) : todoModel.delete(ID);
Error.unknownIDError(ID)==false ? Print.printError(errorMsg.unknownIDError) : todoModel.delete(ID, tempHistory);

} else if(action === "update") {
tempArr[0] = Number(tempArr[0])
let ID = tempArr[0]
let status = tempArr[1]
Error.unknownIDError(ID)==false || Error.duplicatedStatusError(ID,status)==false ? Print.printError(errorMsg.unknownID_duplicatedError) : todoModel.update(ID, status)
Error.unknownIDError(ID)==false || Error.duplicatedStatusError(ID,status)==false ? Print.printError(errorMsg.unknownID_duplicatedError) : todoModel.update(ID, status, tempHistory)

} else if(action === "show") {
todoModel.show(tempArr);

} else if(action === 'undo') {
undoModel(undoHistory[undoHistory.length-1]['model']);
undoHistory.pop();

} else if(action === 'redo') {

} else {
Print.printError(errorMsg.ELSE_ERROR);
}

console.log(todoList);
console.log(undoHistory);

const reOrder = () => {
r.prompt()
}
action === 'update' ? setTimeout(reOrder, 5000) : setTimeout(reOrder, 2000);
}

//todoMain(arr)
//todoMain(arr);


r.setPrompt('명령하세요 : ');
Expand All @@ -146,7 +214,9 @@ todoMain = (answer) => {
r.close();
}

todoMain(line);
if(todoMain(line)) {
r.prompt()
}
})

r.on('close', () => {
Expand Down