This commit is contained in:
pika 2025-04-17 15:36:24 +02:00
parent f5c8e9ee23
commit 3a16f266da
15 changed files with 511 additions and 169 deletions

View file

@ -8,9 +8,15 @@
<button id="save-button" class="inline-flex items-center px-4 py-2 bg-primary text-black rounded-md hover:bg-primary-dark transition-colors">
<i class="mdi mdi-content-save mr-2"></i> Save
</button>
{% if category %}
<a href="{{ url_for('main.view_category', category_id=category.id) }}" class="inline-flex items-center px-4 py-2 bg-gray-700 text-white rounded-md hover:bg-gray-600 transition-colors ml-2">
<i class="mdi mdi-arrow-left mr-2"></i> Back
</a>
{% else %}
<a href="{{ url_for('main.index') }}" class="inline-flex items-center px-4 py-2 bg-gray-700 text-white rounded-md hover:bg-gray-600 transition-colors ml-2">
<i class="mdi mdi-arrow-left mr-2"></i> Back
</a>
{% endif %}
{% if category and not category.is_root %}
<button id="delete-category-btn" class="inline-flex items-center px-4 py-2 bg-red-600 text-white rounded-md hover:bg-red-500 transition-colors ml-2">
<i class="mdi mdi-delete mr-2"></i> Delete
@ -91,7 +97,7 @@
<label for="category-parent" class="block text-sm font-medium text-gray-400 mb-1">Parent Category</label>
<select id="category-parent"
class="w-full px-3 py-2 bg-gray-700 border border-gray-600 rounded-md text-white focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary">
<option value="">None (Root Category)</option>
<option value="">No Parent</option>
<!-- Options will be populated with JavaScript -->
</select>
</div>
@ -202,13 +208,13 @@
<div class="p-6">
<p class="text-gray-300 mb-6">
Are you sure you want to delete <span class="font-semibold text-white">{{ category.name if category else 'this category' }}</span>?
{% if category and (category.documents.count() > 0 or category.children.count() > 0) %}
Are you sure you want to delete <span class="font-semibold text-white">{{ category.name if category is not none else 'this category' }}</span>?
{% if category is not none and (category.documents.count() > 0 or category.children.count() > 0) %}
This category contains {{ category.documents.count() }} document(s) and {{ category.children.count() }} subcategory(ies).
{% endif %}
</p>
{% if category and (category.documents.count() > 0 or category.children.count() > 0) %}
{% if category is not none and (category.documents.count() > 0 or category.children.count() > 0) %}
<div class="mb-6">
<p class="text-white mb-2">What should happen to the contents?</p>
<div class="space-y-3">
@ -266,15 +272,15 @@
fetch('/api/categories')
.then(response => response.json())
.then(categories => {
// Find the root category
const rootCategory = categories.find(c => c.name === 'root');
// Add options recursively
function addCategoryOptions(categories, depth = 0) {
categories.forEach(category => {
// Skip the category being edited to avoid circular references
{% if category %}
if (category.id === {{ category.id }}) return;
// Also skip any root categories as they shouldn't be selectable as parents
{% if category is not none %}
if (category.id === {{ category.id }} || category.is_root) return;
{% else %}
if (category.is_root) return;
{% endif %}
const option = document.createElement('option');
@ -287,23 +293,16 @@
// Select option logic:
// 1. If we have a parent specified, select that parent
// 2. If we're editing an existing category, select its current parent
// 3. If creating a new category, select root by default
let selectThisOption = false;
{% if parent %}
{% if parent is not none %}
if (category.id === {{ parent.id }}) {
selectThisOption = true;
}
{% elif category and category.parent_id %}
{% elif category is not none and category.parent_id is not none %}
if (category.id === {{ category.parent_id }}) {
selectThisOption = true;
}
{% else %}
// If no parent specified and creating new category, default to root
if (category.is_root && !rootSelected) {
selectThisOption = true;
rootSelected = true;
}
{% endif %}
option.selected = selectThisOption;
@ -315,10 +314,7 @@
});
}
// Initialize flag to track if root has been selected
let rootSelected = false;
// Start from root categories
// Start from categories
addCategoryOptions(categories);
})
.catch(error => {
@ -538,7 +534,8 @@
const queryParams = deleteOption === 'delete' ? '' : '?preserve_contents=true';
// Send delete request
fetch(`/api/category/{{ category.id if category else '' }}${queryParams}`, {
{% if category is not none %}
fetch(`/api/category/{{ category.id }}${queryParams}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
@ -568,7 +565,7 @@
// Redirect to home page or parent category
setTimeout(() => {
{% if category and category.parent_id %}
{% if category.parent_id %}
window.location.href = '/category/{{ category.parent_id }}';
{% else %}
window.location.href = '/';
@ -590,6 +587,7 @@
setTimeout(() => notification.remove(), 300);
}, 3000);
});
{% endif %}
});
}
});