From 8a9f278d9703762236f5046c3c1396d3cb0b39c4 Mon Sep 17 00:00:00 2001 From: tio-iis Date: Sun, 27 Feb 2022 18:55:50 +0900 Subject: [PATCH] #94 --- index.html | 74 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index c061c25..94e0efe 100644 --- a/index.html +++ b/index.html @@ -146,8 +146,47 @@ return "class-checkbox" } + class Memos { + constructor() { + this.memos = [] + } + + addMemo(title, body) { + const memo = new Memo(title, body) + memo.showErrorOnAddModal() + + if (memoList.children.length > 0) { + Array.from(memoList.children).forEach((tr) => { + if (tr.id == memo.id) { + return + } + + const t = getIdTitleInMemoList(tr.id).innerText + if (title == t) { + validationErrorInAddModal.innerHTML = "すでに登録済みのタイトルです。
" + } + }) + } + + if (validationErrorInAddModal.innerHTML.length > 0) { + validationErrorInAddModal.style.display = "" + return false + } + this.memos.push(memo) + } + } + class Memo { constructor(title, body) { + this.errorStr = "" + if (title.length < 1 || title.length > 30) { + this.errorStr = "タイトルの文字数は1文字以上30文字以下にしてください。
" + } + + if (body.length < 1 || body.length > 100) { + this.errorStr = errorStr + "本文の文字数は1文字以上100文字以下にしてください。
" + } + this.title = title this.body = body @@ -156,6 +195,20 @@ this.createdAt = now this.updatedAt = now } + + getDisplayedUpdatedAt() { + if (this.createdAt = this.updatedAt) { + return "なし" + } + return this.updatedAt + } + showErrorOnAddModal() { + if (this.errorStr.length > 0) { + validationErrorInAddModal.innerHTML = this.errorStr + return false + } + return true + } } function getNow() { @@ -220,14 +273,15 @@ function validateMemo(title, body, errorHTMLElement, memoId) { errorHTMLElement.innerHTML = "" - if (title.length < 1 || title.length > 30) { - errorHTMLElement.innerHTML = "タイトルの文字数は1文字以上30文字以下にしてください。
" - } + //if (title.length < 1 || title.length > 30) { + // errorHTMLElement.innerHTML = "タイトルの文字数は1文字以上30文字以下にしてください。
" + //} - if (body.length < 1 || body.length > 100) { - errorHTMLElement.innerHTML = errorHTMLElement.innerHTML + "本文の文字数は1文字以上100文字以下にしてください。
" - } + //if (body.length < 1 || body.length > 100) { + // errorHTMLElement.innerHTML = errorHTMLElement.innerHTML + "本文の文字数は1文字以上100文字以下にしてください。
" + //} + //すでに登録済みのタイトルがないかをチェックする if (memoList.children.length > 0) { Array.from(memoList.children).forEach((tr) => { if (tr.id == memoId) { @@ -254,13 +308,15 @@ const title = titleInAddModal.value const body = bodyInAddModal.value + const memo = new Memo(title, body) + if (validateMemo(title, body, validationErrorInAddModal, "0") == false) { return } - const createdAt = getNow() - const updatedAt = "なし" - const memoId = dayjs().valueOf() + const createdAt = memo.createdAt + const updatedAt = memo.getDisplayedUpdatedAt() + const memoId = memo.id const tr = document.createElement("tr") tr.setAttribute("id", memoId)