こんにちは、プログラマーVTuberの衣亥栖ティオです。
今回は以下の動画の補足説明をします。
動画内で実装したソースコードも載せています。
以下が今回実装したソースコードです。
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<title>メモ帳アプリ</title>
</head>
<body>
<div>
<h3>メモの登録</h3>
<button id="id-add-button">メモを登録する</button>
</div>
<div>
<h3>メモの一覧</h3>
<button id="id-delete-button">削除する</button>
<table class="table">
<thead>
<tr>
<th scope="col"><input type="checkbox" id="id-delete-all-memos"></th>
<th scope="col">タイトル</th>
<th scope="col">作成日</th>
<th scope="col">更新日</th>
<th scope="col">編集</th>
</tr>
</thead>
<tbody id="id-memo-list">
</tbody>
</table>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">編集</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="input-group mb-3">
<span class="input-group-text" id="inputGroup-sizing-default">タイトル</span>
<input type="text" id="id-modal-title" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-default">
<input type="hidden" id="id-memo-id">
</div>
<div class="form-floating">
<textarea class="form-control" style="height: 150px;" placeholder="Leave a comment here"
id="id-modal-body"></textarea>
<label for="id-modal-body">本文</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" id="id-update-button" class="btn btn-primary">更新</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="id-add-modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="id-add-modal-label">登録</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="input-group mb-3">
<span class="input-group-text" id="inputGroup-sizing-default">タイトル</span>
<input type="text" id="id-add-modal-title" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-default">
</div>
<div class="form-floating">
<textarea class="form-control" style="height: 150px;" placeholder="Leave a comment here"
id="id-add-modal-body"></textarea>
<label for="id-add-modal-body">本文</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" id="id-add-modal-button" class="btn btn-primary">登録</button>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj"
crossorigin="anonymous"></script>
</body>
<script>
const myModal = new bootstrap.Modal(document.getElementById('exampleModal'))
const addModal = new bootstrap.Modal(document.getElementById('id-add-modal'))
const addButtonInModal = document.getElementById("id-add-modal-button")
addButtonInModal.addEventListener("click", (event) => {
const title = document.getElementById("id-add-modal-title").value
const body = document.getElementById("id-add-modal-body").value
const now = new Date()
const createdAt = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()
const updatedAt = "なし"
const memoId = now.getTime()
const tr = document.createElement("tr")
tr.setAttribute("id", memoId)
const editButtonId = "id-edit-button-" + memoId
const idTitleInList = "id-title-in-list-" + memoId
const updatedAtId = "id-updated-at-" + memoId
tr.innerHTML = '<td><input class="class-checkbox" data-memo-id="' + memoId + '" type="checkbox"></td><td id="' + idTitleInList + '">' + title + '</td><td>' + createdAt + '</td><td id="' + updatedAtId + '">' + updatedAt + '</td><td><button type="button" data-memo-id="' + memoId + '" id="' + editButtonId + '" class="btn btn-primary">編集</button></td>'
const memoList = document.getElementById("id-memo-list")
memoList.appendChild(tr)
const inputForBody = document.createElement("input")
inputForBody.setAttribute("id", "id-body-in-list-" + memoId)
inputForBody.setAttribute("type", "hidden")
inputForBody.value = body
tr.appendChild(inputForBody)
const editButton = document.getElementById(editButtonId)
editButton.addEventListener("click", (event) => {
const memoId = event.currentTarget.dataset.memoId
document.getElementById("id-memo-id").value = memoId
const title = document.getElementById("id-title-in-list-" + memoId).innerText
const titleElement = document.getElementById("id-modal-title")
titleElement.value = title
const body = document.getElementById("id-body-in-list-" + memoId).value
const bodyElement = document.getElementById("id-modal-body")
bodyElement.value = body
document.getElementById("exampleModalLabel").innerText = "編集"
myModal.show()
})
addModal.hide()
})
const addButton = document.getElementById("id-add-button")
addButton.addEventListener("click", (event) => {
addModal.show()
})
const deleteButton = document.getElementById("id-delete-button")
deleteButton.addEventListener("click", (event) => {
const checkboxes = document.getElementsByClassName("class-checkbox")
Array.from(checkboxes).forEach((checkbox) => {
if (checkbox.checked == false) {
return false
}
const memoId = checkbox.dataset.memoId
document.getElementById(memoId).remove()
})
})
const deleteAllMemos = document.getElementById("id-delete-all-memos")
deleteAllMemos.addEventListener("change", (event) => {
const checkboxes = document.getElementsByClassName("class-checkbox")
if (checkboxes.length == 0) {
return
}
Array.from(checkboxes).forEach((checkbox) => {
if (deleteAllMemos.checked == true) {
checkbox.checked = true
} else {
checkbox.checked = false
}
})
})
const updateButton = document.getElementById("id-update-button")
updateButton.addEventListener("click", (event) => {
const memoId = document.getElementById("id-memo-id").value
const modalTitleElement = document.getElementById("id-modal-title")
const titleElementInMemoList = document.getElementById("id-title-in-list-" + memoId)
titleElementInMemoList.innerText = modalTitleElement.value
document.getElementById("id-body-in-list-" + memoId).value = document.getElementById("id-modal-body").value
const now = new Date()
const updatedAt = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()
document.getElementById("id-updated-at-" + memoId).innerText = updatedAt
myModal.hide()
})
</script>
</html>
メモの登録は専用のモーダルを用意する
前回のブログでメモを登録するためのモーダルを編集用のモーダルと一緒にするかどうかを説明しました。
最終的にはメモを登録するための専用のモーダルを用意しています。
実際にこの方針で実装してみると、プログラムがとてもシンプルになったと思っています。
同じようなモーダルのHTMLが2つあるので、モーダル部分を修正する場合、2つのHTMLを修正しなければいけませんが、
今の所そういった予定はないので、一旦このまま進めようと思います。
動画中で "inputGroup-sizing-default" という id が編集用のモーダル内のHTMLと登録用のモーダルのHTMLに存在します。
<span class="input-group-text" id="inputGroup-sizing-default">タイトル</span>
id の値は本来HTML上でユニークでなければいけません(重複してはいけません)。
しかし、動画中では重複しても問題ないと判断し、特に修正しませんでした。
以下のように "inputGroup-sizing-default" という id は aria-describedby という属性で指定されています。
<div class="input-group mb-3">
<span class="input-group-text" id="inputGroup-sizing-default">タイトル</span>
<input type="text" id="id-add-modal-title" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-default">
</div>
aria-describedby の説明は以下です。
値が重複したとしても今回のメモ帳アプリが動かなくなることはないと思い、重複を許容しています。
aria-describedby 属性の使用 - アクセシビリティ | MDN
開発方針について
動画の最後の方でも言及したのですが、
私がアプリケーションを開発するときに "まずは動くものを雑に作って、それが完成したら細かいところを作り込む" という方針を取ります。
動くものを最初に作ることによって、
プログラムの全体像(どこが複雑になりそうか? このまま作り続けて問題ないプログラムになっているか?)を早い段階で確認できたり、
メモ帳アプリ自体の使い勝手も確認することができます。
これはプロダクト開発における "MVP(Minimum Viable Product)" という考えをプログラミングにも応用したものです。
【起業家必見】MVP開発とは何か? – techpartner
プログラムを書く→修正する→書く→修正する→書く→修正する ... というサイクルを早く回すことによって、
プログラムの品質を段階的に向上させていくことができます。
動くものが早くできるので、利用者に使ってもらって、良し悪しを確認してもらうこともできます。