154 lines
5.6 KiB
HTML
154 lines
5.6 KiB
HTML
{% extends 'base.html' %}
|
|
|
|
{% block title %}Configure Dashboard - Manufacturing App{% endblock %}
|
|
|
|
{% block content %}
|
|
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
|
|
<h1 class="h2">Configure Dashboard</h1>
|
|
<div class="btn-toolbar mb-2 mb-md-0">
|
|
<a href="{% url 'dashboard:home' %}" class="btn btn-sm btn-outline-secondary">
|
|
<i class="fas fa-arrow-left"></i> Back to Dashboard
|
|
</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="card-title mb-0">Available Widgets</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="row">
|
|
{% for widget in all_widgets %}
|
|
<div class="col-md-6 mb-3">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h6 class="card-title">{{ widget.title }}</h6>
|
|
<p class="card-text">{{ widget.description|truncatewords:15 }}</p>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox"
|
|
id="widget-{{ widget.id }}"
|
|
{% if widget.id in user_widget_ids %}checked{% endif %}
|
|
onchange="toggleWidget({{ widget.id }}, this.checked)">
|
|
<label class="form-check-label" for="widget-{{ widget.id }}">
|
|
{% if widget.id in user_widget_ids %}Enabled{% else %}Disabled{% endif %}
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endfor %}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h5 class="card-title mb-0">Your Dashboard</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<h6>Enabled Widgets ({{ user_widgets|length }})</h6>
|
|
<ul class="list-group">
|
|
{% for user_widget in user_widgets %}
|
|
{% if user_widget.is_visible %}
|
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
|
{{ user_widget.widget.title }}
|
|
<span class="badge bg-primary rounded-pill">
|
|
{{ user_widget.widget.widget_type }}
|
|
</span>
|
|
</li>
|
|
{% endif %}
|
|
{% empty %}
|
|
<li class="list-group-item">No widgets enabled</li>
|
|
{% endfor %}
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
{% block extra_js %}
|
|
<script>
|
|
function toggleWidget(widgetId, isEnabled) {
|
|
const csrfToken = document.querySelector('[name=csrfmiddlewaretoken]').value;
|
|
|
|
// Create FormData for proper Django POST handling
|
|
const formData = new FormData();
|
|
formData.append('csrfmiddlewaretoken', csrfToken);
|
|
|
|
fetch(`/dashboard/toggle-widget/${widgetId}/`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-CSRFToken': csrfToken,
|
|
},
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.status === 'success') {
|
|
// Update the UI without full page reload
|
|
const checkbox = document.getElementById(`widget-${widgetId}`);
|
|
const label = checkbox.nextElementSibling;
|
|
|
|
if (isEnabled) {
|
|
label.textContent = 'Enabled';
|
|
checkbox.checked = true;
|
|
} else {
|
|
label.textContent = 'Disabled';
|
|
checkbox.checked = false;
|
|
}
|
|
|
|
// Show success message
|
|
showMessage('Widget updated successfully!', 'success');
|
|
|
|
// Optionally reload after a short delay to update the right panel
|
|
setTimeout(() => {
|
|
location.reload();
|
|
}, 500);
|
|
|
|
} else {
|
|
// Revert checkbox state
|
|
const checkbox = document.getElementById(`widget-${widgetId}`);
|
|
checkbox.checked = !isEnabled;
|
|
showMessage('Error updating widget', 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
// Revert checkbox state
|
|
const checkbox = document.getElementById(`widget-${widgetId}`);
|
|
checkbox.checked = !isEnabled;
|
|
showMessage('Error updating widget', 'error');
|
|
});
|
|
}
|
|
|
|
function showMessage(message, type) {
|
|
// Remove existing messages
|
|
const existingMessages = document.querySelectorAll('.alert');
|
|
existingMessages.forEach(msg => msg.remove());
|
|
|
|
// Create new message
|
|
const messageDiv = document.createElement('div');
|
|
messageDiv.className = `alert alert-${type === 'success' ? 'success' : 'danger'} alert-dismissible fade show`;
|
|
messageDiv.innerHTML = `
|
|
${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
`;
|
|
|
|
// Insert at the top of the container
|
|
const container = document.querySelector('.container-fluid');
|
|
container.insertBefore(messageDiv, container.firstChild);
|
|
|
|
// Auto-hide after 3 seconds
|
|
setTimeout(() => {
|
|
if (messageDiv.parentNode) {
|
|
messageDiv.remove();
|
|
}
|
|
}, 3000);
|
|
}
|
|
</script>
|
|
{% endblock %} |