ツール管理へ戻る

v4 → v5 の差分

--- v4
+++ v5
@@ -23,7 +23,7 @@
     }
 
     main {
-      width: min(100%, 1100px);
+      width: min(100%, 1200px);
       margin: 0 auto;
       padding: 28px;
       background: #fff;
@@ -51,7 +51,7 @@
 
     table {
       width: 100%;
-      min-width: 900px;
+      min-width: 960px;
       border-collapse: collapse;
       table-layout: fixed;
     }
@@ -80,12 +80,25 @@
       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 {
@@ -129,6 +142,30 @@
       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;
@@ -211,6 +248,18 @@
       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;
@@ -230,12 +279,13 @@
   <main>
     <h1>総作業時間計算</h1>
     <p class="description">
-      作業名、件数、1件あたりの時間を入力してください。小計と合計は入力に合わせて更新されます。
+      作業名、件数、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">
@@ -245,6 +295,7 @@
         </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>
@@ -256,7 +307,7 @@
         <tbody id="work-list"></tbody>
         <tfoot>
           <tr>
-            <th scope="row" colspan="4">合計</th>
+            <th scope="row" colspan="5">合計</th>
             <td id="grand-total">0分</td>
             <td></td>
           </tr>
@@ -274,6 +325,7 @@
     </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>
@@ -284,6 +336,9 @@
     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", {
@@ -351,13 +406,40 @@
     }
 
     function renumberRows() {
-      for (const [index, row] of [...workList.querySelectorAll("tr")].entries()) {
-        row.querySelector(".row-number").textContent = String(index + 1);
-      }
+      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";
@@ -426,7 +508,15 @@
       });
 
       actionCell.append(removeButton);
-      row.append(numberCell, nameCell, countCell, timeCell, subtotalCell, actionCell);
+      row.append(
+        dragCell,
+        numberCell,
+        nameCell,
+        countCell,
+        timeCell,
+        subtotalCell,
+        actionCell
+      );
       workList.append(row);
 
       renumberRows();
@@ -435,7 +525,75 @@
       updateTotals();
     }
 
-    function calculateAndValidate() {
+    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");
 
@@ -478,10 +636,7 @@
       }
 
       updateTotals();
-    }
-
-    addWorkButton.addEventListener("click", addWorkRow);
-    calculateButton.addEventListener("click", calculateAndValidate);
+    });
 
     workList.addEventListener("input", () => {
       message.textContent = "";