ツール管理へ戻るv5 のソース
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>総作業時間計算</title>
<style>
:root {
color-scheme: light;
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
color: #1f2937;
background: #f3f4f6;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
padding: 32px 20px;
}
main {
width: min(100%, 1200px);
margin: 0 auto;
padding: 28px;
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 16px;
box-shadow: 0 8px 24px rgb(31 41 55 / 8%);
}
h1 {
margin: 0 0 8px;
font-size: 1.6rem;
}
.description {
margin: 0 0 22px;
color: #4b5563;
line-height: 1.6;
}
.table-wrap {
overflow-x: auto;
border: 1px solid #cbd5e1;
border-radius: 8px;
}
table {
width: 100%;
min-width: 960px;
border-collapse: collapse;
table-layout: fixed;
}
th,
td {
padding: 10px;
border-right: 1px solid #cbd5e1;
border-bottom: 1px solid #cbd5e1;
text-align: left;
vertical-align: middle;
}
th:last-child,
td:last-child {
border-right: 0;
}
thead th {
background: #eff6ff;
color: #1e3a8a;
font-weight: 700;
}
tbody tr:nth-child(even) {
background: #f8fafc;
}
tbody tr.dragging {
opacity: 0.45;
}
tbody tr.drop-target {
box-shadow: inset 0 3px 0 #2563eb;
}
tfoot th,
tfoot td {
border-bottom: 0;
background: #eaf2ff;
color: #1e3a8a;
font-weight: 750;
}
.column-drag {
width: 54px;
text-align: center;
}
.column-number {
width: 60px;
text-align: center;
}
.column-name {
width: 190px;
}
.column-count {
width: 150px;
}
.column-time {
width: 235px;
}
.column-subtotal {
width: 190px;
}
.column-action {
width: 100px;
text-align: center;
}
input {
width: 100%;
min-width: 0;
padding: 9px;
border: 1px solid #9ca3af;
border-radius: 6px;
font: inherit;
font-size: 1rem;
}
input:focus {
outline: 3px solid #bfdbfe;
border-color: #2563eb;
}
.drag-handle {
width: 34px;
height: 36px;
padding: 0;
border: 1px solid #cbd5e1;
border-radius: 6px;
background: #fff;
color: #334155;
font: inherit;
font-size: 1.2rem;
line-height: 1;
cursor: grab;
touch-action: none;
}
.drag-handle:active {
cursor: grabbing;
}
.drag-handle:focus-visible {
outline: 3px solid #93c5fd;
outline-offset: 2px;
}
.time-input-row {
display: flex;
align-items: center;
gap: 8px;
}
.time-input-row input {
flex: 1 1 auto;
}
.unit {
flex: 0 0 auto;
color: #526071;
}
.subtotal {
font-weight: 650;
white-space: nowrap;
}
.remove-button {
padding: 7px 10px;
border: 1px solid #b42318;
border-radius: 6px;
background: #fff;
color: #b42318;
font: inherit;
cursor: pointer;
}
.remove-button:hover {
background: #fef3f2;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 16px;
}
button.primary-button,
button.secondary-button {
padding: 11px 16px;
border-radius: 8px;
font: inherit;
font-weight: 700;
cursor: pointer;
}
.primary-button {
border: 0;
background: #1d4ed8;
color: #fff;
}
.primary-button:hover {
background: #1e40af;
}
.secondary-button {
border: 1px solid #1d4ed8;
background: #fff;
color: #1d4ed8;
}
.secondary-button:hover {
background: #eff6ff;
}
button:focus-visible {
outline: 3px solid #93c5fd;
outline-offset: 3px;
}
.message {
min-height: 1.5em;
margin: 12px 0 0;
color: #b91c1c;
font-size: 0.95rem;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 600px) {
body {
padding: 16px 10px;
}
main {
padding: 22px 16px;
}
h1 {
font-size: 1.4rem;
}
}
</style>
</head>
<body>
<main>
<h1>総作業時間計算</h1>
<p class="description">
作業名、件数、1件あたりの時間を入力してください。左端の「↕」をドラッグすると、作業の順序を変更できます。
</p>
<div class="table-wrap">
<table aria-label="作業時間一覧">
<colgroup>
<col class="column-drag">
<col class="column-number">
<col class="column-name">
<col class="column-count">
<col class="column-time">
<col class="column-subtotal">
<col class="column-action">
</colgroup>
<thead>
<tr>
<th scope="col" class="column-drag"><span class="sr-only">並べ替え</span></th>
<th scope="col" class="column-number">No.</th>
<th scope="col">作業名</th>
<th scope="col">作業件数</th>
<th scope="col">1件あたりの時間</th>
<th scope="col">小計</th>
<th scope="col" class="column-action">操作</th>
</tr>
</thead>
<tbody id="work-list"></tbody>
<tfoot>
<tr>
<th scope="row" colspan="5">合計</th>
<td id="grand-total">0分</td>
<td></td>
</tr>
</tfoot>
</table>
</div>
<div class="actions">
<button id="add-work" class="secondary-button" type="button">
新規登録
</button>
<button id="calculate" class="primary-button" type="button">
合計を計算
</button>
</div>
<p id="message" class="message" role="alert" aria-live="polite"></p>
<p id="reorder-status" class="sr-only" aria-live="polite"></p>
</main>
<script>
"use strict";
const workList = document.getElementById("work-list");
const addWorkButton = document.getElementById("add-work");
const calculateButton = document.getElementById("calculate");
const grandTotalCell = document.getElementById("grand-total");
const message = document.getElementById("message");
const reorderStatus = document.getElementById("reorder-status");
let draggedRow = null;
function formatNumber(value) {
return new Intl.NumberFormat("ja-JP", {
maximumFractionDigits: 2
}).format(value);
}
function formatDuration(totalMinutes) {
const totalHours = Math.floor(totalMinutes / 60);
const remainingMinutes = totalMinutes - totalHours * 60;
if (totalHours === 0) {
return `${formatNumber(totalMinutes)}分`;
}
if (remainingMinutes === 0) {
return `${formatNumber(totalHours)}時間`;
}
return `${formatNumber(totalHours)}時間${formatNumber(remainingMinutes)}分`;
}
function setSubtotal(row, minutes) {
row.querySelector(".subtotal").textContent = formatDuration(minutes);
}
function updateTotals() {
let totalMinutes = 0;
for (const row of workList.querySelectorAll("tr")) {
const countInput = row.querySelector('input[name="count"]');
const timeInput = row.querySelector('input[name="minutes"]');
if (countInput.value === "" || timeInput.value === "") {
setSubtotal(row, 0);
continue;
}
const count = countInput.valueAsNumber;
const minutesPerItem = timeInput.valueAsNumber;
if (
!Number.isFinite(count) ||
!Number.isFinite(minutesPerItem) ||
count < 0 ||
!Number.isInteger(count) ||
minutesPerItem < 0
) {
setSubtotal(row, 0);
continue;
}
const subtotal = count * minutesPerItem;
if (!Number.isFinite(subtotal)) {
setSubtotal(row, 0);
continue;
}
setSubtotal(row, subtotal);
totalMinutes += subtotal;
}
grandTotalCell.textContent = formatDuration(totalMinutes);
}
function renumberRows() {
const rows = [...workList.querySelectorAll("tr")];
rows.forEach((row, index) => {
const number = index + 1;
row.querySelector(".row-number").textContent = String(number);
const nameInput = row.querySelector('input[name="workName"]');
const countInput = row.querySelector('input[name="count"]');
const timeInput = row.querySelector('input[name="minutes"]');
const dragHandle = row.querySelector(".drag-handle");
const removeButton = row.querySelector(".remove-button");
nameInput.setAttribute("aria-label", `作業 ${number} の作業名`);
countInput.setAttribute("aria-label", `作業 ${number} の作業件数`);
timeInput.setAttribute("aria-label", `作業 ${number} の1件あたりの時間`);
dragHandle.setAttribute("aria-label", `作業 ${number} をドラッグして並べ替え`);
removeButton.setAttribute("aria-label", `作業 ${number} を削除`);
});
}
function addWorkRow() {
const row = document.createElement("tr");
row.draggable = false;
const dragCell = document.createElement("td");
dragCell.className = "column-drag";
const dragHandle = document.createElement("button");
dragHandle.type = "button";
dragHandle.className = "drag-handle";
dragHandle.textContent = "↕";
dragHandle.draggable = true;
dragHandle.setAttribute("aria-label", "作業をドラッグして並べ替え");
dragCell.append(dragHandle);
const numberCell = document.createElement("th");
numberCell.scope = "row";
numberCell.className = "column-number row-number";
const nameCell = document.createElement("td");
const nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.name = "workName";
nameInput.maxLength = 100;
nameInput.placeholder = "例:資料作成";
nameInput.setAttribute("aria-label", "作業名");
nameCell.append(nameInput);
const countCell = document.createElement("td");
const countInput = document.createElement("input");
countInput.type = "number";
countInput.name = "count";
countInput.min = "0";
countInput.step = "1";
countInput.inputMode = "numeric";
countInput.placeholder = "例:8";
countInput.setAttribute("aria-label", "作業件数");
countInput.required = true;
countCell.append(countInput);
const timeCell = document.createElement("td");
const timeRow = document.createElement("div");
timeRow.className = "time-input-row";
const timeInput = document.createElement("input");
timeInput.type = "number";
timeInput.name = "minutes";
timeInput.min = "0";
timeInput.step = "any";
timeInput.inputMode = "decimal";
timeInput.placeholder = "例:15";
timeInput.setAttribute("aria-label", "1件あたりの時間");
timeInput.required = true;
const timeUnit = document.createElement("span");
timeUnit.className = "unit";
timeUnit.textContent = "分";
timeRow.append(timeInput, timeUnit);
timeCell.append(timeRow);
const subtotalCell = document.createElement("td");
subtotalCell.className = "subtotal";
subtotalCell.textContent = "0分";
const actionCell = document.createElement("td");
actionCell.className = "column-action";
const removeButton = document.createElement("button");
removeButton.type = "button";
removeButton.className = "remove-button";
removeButton.textContent = "削除";
removeButton.setAttribute("aria-label", "この作業を削除");
removeButton.addEventListener("click", () => {
row.remove();
renumberRows();
message.textContent = "";
updateTotals();
});
actionCell.append(removeButton);
row.append(
dragCell,
numberCell,
nameCell,
countCell,
timeCell,
subtotalCell,
actionCell
);
workList.append(row);
renumberRows();
message.textContent = "";
nameInput.focus();
updateTotals();
}
function getDropPosition(targetRow, pointerY) {
const rect = targetRow.getBoundingClientRect();
return pointerY < rect.top + rect.height / 2 ? "before" : "after";
}
workList.addEventListener("dragstart", (event) => {
const handle = event.target.closest(".drag-handle");
if (!handle) {
event.preventDefault();
return;
}
draggedRow = handle.closest("tr");
draggedRow.classList.add("dragging");
if (event.dataTransfer) {
event.dataTransfer.effectAllowed = "move";
event.dataTransfer.setData("text/plain", "");
}
});
workList.addEventListener("dragover", (event) => {
if (!draggedRow) {
return;
}
const targetRow = event.target.closest("tr");
if (!targetRow || targetRow === draggedRow) {
return;
}
event.preventDefault();
for (const row of workList.querySelectorAll("tr")) {
row.classList.remove("drop-target");
}
targetRow.classList.add("drop-target");
const position = getDropPosition(targetRow, event.clientY);
if (position === "before") {
workList.insertBefore(draggedRow, targetRow);
} else {
workList.insertBefore(targetRow, draggedRow);
}
});
workList.addEventListener("drop", (event) => {
if (draggedRow) {
event.preventDefault();
}
});
workList.addEventListener("dragend", () => {
for (const row of workList.querySelectorAll("tr")) {
row.classList.remove("dragging", "drop-target");
}
if (draggedRow) {
renumberRows();
reorderStatus.textContent = "作業の順序を変更しました。";
draggedRow = null;
}
updateTotals();
});
addWorkButton.addEventListener("click", addWorkRow);
calculateButton.addEventListener("click", () => {
message.textContent = "";
const rows = workList.querySelectorAll("tr");
if (rows.length === 0) {
message.textContent = "作業を1件以上登録してください。";
addWorkButton.focus();
return;
}
for (const row of rows) {
const countInput = row.querySelector('input[name="count"]');
const timeInput = row.querySelector('input[name="minutes"]');
if (countInput.value === "") {
message.textContent = "すべての作業の件数を入力してください。";
countInput.focus();
return;
}
if (timeInput.value === "") {
message.textContent = "すべての作業の時間を入力してください。";
timeInput.focus();
return;
}
const count = countInput.valueAsNumber;
const minutesPerItem = timeInput.valueAsNumber;
if (!Number.isFinite(count) || count < 0 || !Number.isInteger(count)) {
message.textContent = "作業件数は0以上の整数で入力してください。";
countInput.focus();
return;
}
if (!Number.isFinite(minutesPerItem) || minutesPerItem < 0) {
message.textContent = "1件あたりの時間は0以上で入力してください。";
timeInput.focus();
return;
}
}
updateTotals();
});
workList.addEventListener("input", () => {
message.textContent = "";
updateTotals();
});
addWorkRow();
</script>
</body>
</html>