ツール管理へ戻る

v3 のソース

<!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%, 960px);
      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: 680px;
      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;
    }

    tbody tr:last-child td {
      border-bottom: 1px solid #cbd5e1;
    }

    thead th {
      background: #eff6ff;
      color: #1e3a8a;
      font-weight: 700;
    }

    tbody tr:nth-child(even) {
      background: #f8fafc;
    }

    tfoot th,
    tfoot td {
      border-bottom: 0;
      background: #eaf2ff;
      color: #1e3a8a;
      font-weight: 750;
    }

    .column-number {
      width: 72px;
      text-align: center;
    }

    .column-count {
      width: 150px;
    }

    .column-time {
      width: 190px;
    }

    .column-subtotal {
      width: 200px;
    }

    .column-action {
      width: 100px;
      text-align: center;
    }

    input {
      width: 100%;
      min-width: 80px;
      padding: 9px;
      border: 1px solid #9ca3af;
      border-radius: 6px;
      font: inherit;
      font-size: 1rem;
    }

    input:focus {
      outline: 3px solid #bfdbfe;
      border-color: #2563eb;
    }

    .unit {
      display: block;
      margin-top: 4px;
      color: #526071;
      font-size: 0.85rem;
    }

    .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;
    }

    @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-number">
          <col class="column-count">
          <col class="column-time">
          <col class="column-subtotal">
          <col class="column-action">
        </colgroup>
        <thead>
          <tr>
            <th scope="col" class="column-number">No.</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="3">合計</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>
  </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");

    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) {
      const subtotalCell = row.querySelector(".subtotal");
      subtotalCell.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;
        setSubtotal(row, subtotal);
        totalMinutes += subtotal;
      }

      grandTotalCell.textContent = formatDuration(totalMinutes);
    }

    function addWorkRow() {
      const row = document.createElement("tr");

      const numberCell = document.createElement("th");
      numberCell.scope = "row";
      numberCell.className = "column-number";

      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 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 = "分";
      timeCell.append(timeInput, timeUnit);

      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(numberCell, countCell, timeCell, subtotalCell, actionCell);
      workList.append(row);

      renumberRows();
      message.textContent = "";
      countInput.focus();
      updateTotals();
    }

    function renumberRows() {
      const rows = workList.querySelectorAll("tr");

      rows.forEach((row, index) => {
        row.querySelector("th").textContent = String(index + 1);
      });
    }

    function calculateAndValidate() {
      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();
    }

    addWorkButton.addEventListener("click", addWorkRow);
    calculateButton.addEventListener("click", calculateAndValidate);

    workList.addEventListener("input", () => {
      message.textContent = "";
      updateTotals();
    });

    addWorkRow();
  </script>
</body>
</html>