- MenuService.reorderMenus() 메서드 추가 - MenuController.reorder() API 엔드포인트 추가 - POST /api/admin/menus/reorder 라우트 추가 - SortableJS 기반 드래그 앤 드롭 UI 구현 - 같은 부모 메뉴 내에서만 순서 변경 가능 (계층 구조 유지)
227 lines
8.2 KiB
PHP
227 lines
8.2 KiB
PHP
@extends('layouts.app')
|
|
|
|
@section('title', '메뉴 관리')
|
|
|
|
@section('content')
|
|
<!-- 페이지 헤더 -->
|
|
<div class="flex justify-between items-center mb-6">
|
|
<h1 class="text-2xl font-bold text-gray-800">메뉴 관리</h1>
|
|
<a href="{{ route('menus.create') }}" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg transition">
|
|
+ 새 메뉴
|
|
</a>
|
|
</div>
|
|
|
|
<!-- 필터 영역 -->
|
|
<div class="bg-white rounded-lg shadow-sm p-4 mb-6">
|
|
<form id="filterForm" class="flex gap-4">
|
|
<!-- 검색 -->
|
|
<div class="flex-1">
|
|
<input type="text"
|
|
name="search"
|
|
placeholder="메뉴명, URL로 검색..."
|
|
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
|
|
</div>
|
|
|
|
<!-- 활성 상태 필터 -->
|
|
<div class="w-48">
|
|
<select name="is_active" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
|
|
<option value="">전체 상태</option>
|
|
<option value="1">활성</option>
|
|
<option value="0">비활성</option>
|
|
</select>
|
|
</div>
|
|
|
|
<!-- 검색 버튼 -->
|
|
<button type="submit" class="bg-gray-600 hover:bg-gray-700 text-white px-6 py-2 rounded-lg transition">
|
|
검색
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- 테이블 영역 (HTMX로 로드) -->
|
|
<div id="menu-table"
|
|
hx-get="/api/admin/menus"
|
|
hx-trigger="load, filterSubmit from:body"
|
|
hx-include="#filterForm"
|
|
hx-headers='{"X-CSRF-TOKEN": "{{ csrf_token() }}"}'
|
|
class="bg-white rounded-lg shadow-sm overflow-hidden">
|
|
<!-- 로딩 스피너 -->
|
|
<div class="flex justify-center items-center p-12">
|
|
<div class="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
|
|
</div>
|
|
</div>
|
|
@endsection
|
|
|
|
@push('scripts')
|
|
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.2/Sortable.min.js"></script>
|
|
<script>
|
|
// 폼 제출 시 HTMX 이벤트 트리거
|
|
document.getElementById('filterForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
|
|
// HTMX 응답 처리 + SortableJS 초기화
|
|
document.body.addEventListener('htmx:afterSwap', function(event) {
|
|
if (event.detail.target.id === 'menu-table') {
|
|
const response = JSON.parse(event.detail.xhr.response);
|
|
if (response.html) {
|
|
event.detail.target.innerHTML = response.html;
|
|
// 테이블 로드 후 SortableJS 초기화
|
|
initMenuSortable();
|
|
}
|
|
}
|
|
});
|
|
|
|
// SortableJS 초기화 함수
|
|
function initMenuSortable() {
|
|
const tbody = document.getElementById('menu-sortable');
|
|
if (!tbody) return;
|
|
|
|
// 기존 인스턴스 제거
|
|
if (tbody.sortableInstance) {
|
|
tbody.sortableInstance.destroy();
|
|
}
|
|
|
|
// SortableJS 초기화
|
|
tbody.sortableInstance = new Sortable(tbody, {
|
|
handle: '.drag-handle',
|
|
animation: 150,
|
|
ghostClass: 'bg-blue-50',
|
|
chosenClass: 'bg-blue-100',
|
|
dragClass: 'shadow-lg',
|
|
// 같은 parent_id 그룹 내에서만 정렬 (계층 구조 유지)
|
|
onMove: function(evt) {
|
|
const draggedParentId = evt.dragged.dataset.parentId || '';
|
|
const relatedParentId = evt.related.dataset.parentId || '';
|
|
// 같은 부모를 가진 항목끼리만 이동 가능
|
|
return draggedParentId === relatedParentId;
|
|
},
|
|
onEnd: function(evt) {
|
|
if (evt.oldIndex === evt.newIndex) return;
|
|
|
|
// 같은 parent_id를 가진 항목들만 추출하여 순서 업데이트
|
|
const movedItem = evt.item;
|
|
const parentId = movedItem.dataset.parentId || '';
|
|
const rows = Array.from(tbody.querySelectorAll('tr.menu-row'));
|
|
|
|
// 같은 부모를 가진 항목들 필터링
|
|
const siblingRows = rows.filter(row => (row.dataset.parentId || '') === parentId);
|
|
|
|
// 순서 데이터 생성
|
|
const items = siblingRows.map((row, index) => ({
|
|
id: parseInt(row.dataset.menuId),
|
|
sort_order: index + 1
|
|
}));
|
|
|
|
// API 호출
|
|
saveMenuOrder(items);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 메뉴 순서 저장 API 호출
|
|
function saveMenuOrder(items) {
|
|
fetch('/api/admin/menus/reorder', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}',
|
|
'Accept': 'application/json'
|
|
},
|
|
body: JSON.stringify({ items: items })
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
// 성공 시 테이블 새로고침하여 정렬 순서 표시 갱신
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
} else {
|
|
alert('메뉴 순서 변경에 실패했습니다: ' + (data.message || '알 수 없는 오류'));
|
|
// 실패 시 테이블 새로고침으로 원래 상태 복구
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
alert('메뉴 순서 변경 중 오류가 발생했습니다.');
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
}
|
|
|
|
// 삭제 확인
|
|
window.confirmDelete = function(id, name) {
|
|
if (confirm(`"${name}" 메뉴를 삭제하시겠습니까?`)) {
|
|
htmx.ajax('DELETE', `/api/admin/menus/${id}`, {
|
|
target: '#menu-table',
|
|
swap: 'none',
|
|
headers: {
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
}
|
|
}).then(() => {
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
}
|
|
};
|
|
|
|
// 복원 확인
|
|
window.confirmRestore = function(id, name) {
|
|
if (confirm(`"${name}" 메뉴를 복원하시겠습니까?`)) {
|
|
htmx.ajax('POST', `/api/admin/menus/${id}/restore`, {
|
|
target: '#menu-table',
|
|
swap: 'none',
|
|
headers: {
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
}
|
|
}).then(() => {
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
}
|
|
};
|
|
|
|
// 영구삭제 확인
|
|
window.confirmForceDelete = function(id, name) {
|
|
if (confirm(`⚠️ 경고: "${name}" 메뉴를 영구 삭제하시겠습니까?\n\n이 작업은 되돌릴 수 없습니다!`)) {
|
|
htmx.ajax('DELETE', `/api/admin/menus/${id}/force`, {
|
|
target: '#menu-table',
|
|
swap: 'none',
|
|
headers: {
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
}
|
|
}).then(() => {
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
}
|
|
};
|
|
|
|
// 활성 토글
|
|
window.toggleActive = function(id) {
|
|
htmx.ajax('POST', `/api/admin/menus/${id}/toggle-active`, {
|
|
target: '#menu-table',
|
|
swap: 'none',
|
|
headers: {
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
}
|
|
}).then(() => {
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
};
|
|
|
|
// 숨김 토글
|
|
window.toggleHidden = function(id) {
|
|
htmx.ajax('POST', `/api/admin/menus/${id}/toggle-hidden`, {
|
|
target: '#menu-table',
|
|
swap: 'none',
|
|
headers: {
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
}
|
|
}).then(() => {
|
|
htmx.trigger('#menu-table', 'filterSubmit');
|
|
});
|
|
};
|
|
|
|
</script>
|
|
<script src="{{ asset('js/menu-tree.js') }}"></script>
|
|
@endpush
|