mirror of
https://github.com/Dvorinka/PPve.git
synced 2026-06-05 04:52:58 +00:00
Add files via upload
This commit is contained in:
+77
-75
@@ -464,13 +464,13 @@
|
||||
|
||||
<script>
|
||||
// Get token and check authentication
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
window.location.href = '/login.html';
|
||||
}
|
||||
}
|
||||
|
||||
// Show notification to user
|
||||
function showNotification(message, type = 'info') {
|
||||
// Show notification to user
|
||||
function showNotification(message, type = 'info') {
|
||||
const notification = document.createElement('div');
|
||||
notification.className = `notification ${type}`;
|
||||
|
||||
@@ -493,11 +493,11 @@ function showNotification(message, type = 'info') {
|
||||
notification.classList.add('fade-out');
|
||||
setTimeout(() => notification.remove(), 300);
|
||||
}, delay);
|
||||
}
|
||||
}
|
||||
|
||||
// Override fetch to include token (but NOT for FormData requests)
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async function(resource, init = {}) {
|
||||
// Override fetch to include token
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async function(resource, init = {}) {
|
||||
// Add token to headers if it's an API request
|
||||
if (typeof resource === 'string' && resource.startsWith('/api/')) {
|
||||
const headers = new Headers(init.headers || {});
|
||||
@@ -505,8 +505,8 @@ window.fetch = async function(resource, init = {}) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
|
||||
// Only set content type if not FormData (FormData sets its own)
|
||||
if (!headers.has('Content-Type') && init.body && !(init.body instanceof FormData)) {
|
||||
// Set content type if not set
|
||||
if (!headers.has('Content-Type') && init.body) {
|
||||
headers.set('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
@@ -514,15 +514,15 @@ window.fetch = async function(resource, init = {}) {
|
||||
}
|
||||
|
||||
return originalFetch(resource, init);
|
||||
};
|
||||
};
|
||||
|
||||
// Image handling
|
||||
document.getElementById('uploadImageBtn').addEventListener('click', function() {
|
||||
// Image handling
|
||||
document.getElementById('uploadImageBtn').addEventListener('click', function() {
|
||||
document.getElementById('bannerImage').click();
|
||||
});
|
||||
});
|
||||
|
||||
// Handle image upload
|
||||
function handleImageUpload(event) {
|
||||
// Handle image upload
|
||||
function handleImageUpload(event) {
|
||||
const fileInput = event.target;
|
||||
const file = fileInput.files[0];
|
||||
|
||||
@@ -582,36 +582,36 @@ function handleImageUpload(event) {
|
||||
};
|
||||
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
}
|
||||
|
||||
// Logout functionality
|
||||
document.getElementById('logoutBtn').addEventListener('click', function() {
|
||||
// Logout functionality
|
||||
document.getElementById('logoutBtn').addEventListener('click', function() {
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/';
|
||||
});
|
||||
});
|
||||
|
||||
// DOM Elements
|
||||
const bannerText = document.getElementById('bannerText');
|
||||
const bannerVisible = document.getElementById('bannerVisible');
|
||||
const bannerBgColor = document.getElementById('bannerBgColor');
|
||||
const bannerTextColor = document.getElementById('bannerTextColor');
|
||||
const bannerTextAlign = document.getElementById('bannerTextAlign');
|
||||
const bannerFontSize = document.getElementById('bannerFontSize');
|
||||
const bannerPadding = document.getElementById('bannerPadding');
|
||||
const bannerMargin = document.getElementById('bannerMargin');
|
||||
const bannerBorderRadius = document.getElementById('bannerBorderRadius');
|
||||
const bannerPreview = document.getElementById('bannerPreview');
|
||||
const bannerPreviewContent = bannerPreview.querySelector('.banner-preview-content');
|
||||
const bannerPreviewText = bannerPreview.querySelector('.banner-preview-text');
|
||||
const bannerPreviewBg = bannerPreview.querySelector('.banner-preview-bg');
|
||||
const bgColorPreview = document.getElementById('bgColorPreview');
|
||||
const textColorPreview = document.getElementById('textColorPreview');
|
||||
const saveBannerBtn = document.getElementById('saveBannerBtn');
|
||||
const stylePresets = document.querySelectorAll('.style-preset');
|
||||
let currentImage = null;
|
||||
// DOM Elements
|
||||
const bannerText = document.getElementById('bannerText');
|
||||
const bannerVisible = document.getElementById('bannerVisible');
|
||||
const bannerBgColor = document.getElementById('bannerBgColor');
|
||||
const bannerTextColor = document.getElementById('bannerTextColor');
|
||||
const bannerTextAlign = document.getElementById('bannerTextAlign');
|
||||
const bannerFontSize = document.getElementById('bannerFontSize');
|
||||
const bannerPadding = document.getElementById('bannerPadding');
|
||||
const bannerMargin = document.getElementById('bannerMargin');
|
||||
const bannerBorderRadius = document.getElementById('bannerBorderRadius');
|
||||
const bannerPreview = document.getElementById('bannerPreview');
|
||||
const bannerPreviewContent = bannerPreview.querySelector('.banner-preview-content');
|
||||
const bannerPreviewText = bannerPreview.querySelector('.banner-preview-text');
|
||||
const bannerPreviewBg = bannerPreview.querySelector('.banner-preview-bg');
|
||||
const bgColorPreview = document.getElementById('bgColorPreview');
|
||||
const textColorPreview = document.getElementById('textColorPreview');
|
||||
const saveBannerBtn = document.getElementById('saveBannerBtn');
|
||||
const stylePresets = document.querySelectorAll('.style-preset');
|
||||
let currentImage = null;
|
||||
|
||||
// Preset styles
|
||||
const presets = {
|
||||
// Preset styles
|
||||
const presets = {
|
||||
info: {
|
||||
backgroundColor: '#cce5ff',
|
||||
textColor: '#004085',
|
||||
@@ -632,10 +632,10 @@ const presets = {
|
||||
textColor: '#721c24',
|
||||
textAlign: 'center'
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Load current banner
|
||||
async function loadBanner() {
|
||||
// Load current banner
|
||||
async function loadBanner() {
|
||||
try {
|
||||
const response = await fetch('/api/banner');
|
||||
if (!response.ok) {
|
||||
@@ -708,9 +708,9 @@ async function loadBanner() {
|
||||
console.error('Chyba při načítání banneru:', error);
|
||||
showNotification('Chyba při načítání banneru', 'error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function saveBanner(event) {
|
||||
async function saveBanner(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const form = document.getElementById('bannerForm');
|
||||
@@ -750,7 +750,7 @@ async function saveBanner(event) {
|
||||
console.log(key, value);
|
||||
}
|
||||
|
||||
// Create headers object - DO NOT set Content-Type for FormData!
|
||||
// Create headers object
|
||||
const headers = {};
|
||||
|
||||
// Add Authorization header if token exists
|
||||
@@ -759,10 +759,12 @@ async function saveBanner(event) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// Send request with FormData (browser will set correct Content-Type with boundary)
|
||||
// Note: Don't set Content-Type header when using FormData with files
|
||||
// The browser will set it automatically with the correct boundary
|
||||
|
||||
const response = await fetch('/api/banner/update', {
|
||||
method: 'POST',
|
||||
headers: headers, // No Content-Type header!
|
||||
headers: headers,
|
||||
body: formData
|
||||
});
|
||||
|
||||
@@ -815,10 +817,10 @@ async function saveBanner(event) {
|
||||
submitButton.innerHTML = originalButtonText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update color previews
|
||||
function updateColorPreviews() {
|
||||
// Update color previews
|
||||
function updateColorPreviews() {
|
||||
const bgColorPreview = document.getElementById('bgColorPreview');
|
||||
const textColorPreview = document.getElementById('textColorPreview');
|
||||
|
||||
@@ -829,10 +831,10 @@ function updateColorPreviews() {
|
||||
if (textColorPreview) {
|
||||
textColorPreview.style.backgroundColor = document.getElementById('bannerTextColor').value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove image
|
||||
function removeImage() {
|
||||
// Remove image
|
||||
function removeImage() {
|
||||
const bannerImage = document.getElementById('bannerImage');
|
||||
const imagePreview = document.getElementById('imagePreview');
|
||||
const imagePreviewContainer = document.getElementById('imagePreviewContainer');
|
||||
@@ -872,10 +874,10 @@ function removeImage() {
|
||||
const event = new Event('change');
|
||||
bannerImage.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update banner preview
|
||||
function updateBannerPreview() {
|
||||
// Update banner preview
|
||||
function updateBannerPreview() {
|
||||
const bannerPreview = document.getElementById('bannerPreview');
|
||||
const bannerPreviewText = bannerPreview?.querySelector('.banner-preview-text');
|
||||
const bannerPreviewBg = bannerPreview?.querySelector('.banner-preview-bg');
|
||||
@@ -966,10 +968,10 @@ function updateBannerPreview() {
|
||||
|
||||
// Make sure the preview is visible
|
||||
bannerPreview.style.visibility = 'visible';
|
||||
}
|
||||
}
|
||||
|
||||
// Apply preset
|
||||
function applyPreset(preset) {
|
||||
// Apply preset
|
||||
function applyPreset(preset) {
|
||||
const style = presets[preset];
|
||||
if (!style) return;
|
||||
|
||||
@@ -979,32 +981,32 @@ function applyPreset(preset) {
|
||||
|
||||
updateColorPreviews();
|
||||
updateBannerPreview();
|
||||
}
|
||||
}
|
||||
|
||||
// Event Listeners
|
||||
bannerBgColor.addEventListener('input', () => {
|
||||
// Event Listeners
|
||||
bannerBgColor.addEventListener('input', () => {
|
||||
updateColorPreviews();
|
||||
updateBannerPreview();
|
||||
});
|
||||
});
|
||||
|
||||
bannerTextColor.addEventListener('input', () => {
|
||||
bannerTextColor.addEventListener('input', () => {
|
||||
updateColorPreviews();
|
||||
updateBannerPreview();
|
||||
});
|
||||
});
|
||||
|
||||
[bannerText, bannerTextAlign, bannerFontSize, bannerPadding, bannerVisible].forEach(el => {
|
||||
[bannerText, bannerTextAlign, bannerFontSize, bannerPadding, bannerVisible].forEach(el => {
|
||||
el.addEventListener('change', updateBannerPreview);
|
||||
el.addEventListener('input', updateBannerPreview);
|
||||
});
|
||||
});
|
||||
|
||||
stylePresets.forEach(preset => {
|
||||
stylePresets.forEach(preset => {
|
||||
preset.addEventListener('click', () => applyPreset(preset.dataset.preset));
|
||||
});
|
||||
});
|
||||
|
||||
saveBannerBtn.addEventListener('click', saveBanner);
|
||||
saveBannerBtn.addEventListener('click', saveBanner);
|
||||
|
||||
// Load banner when page loads
|
||||
document.addEventListener('DOMContentLoaded', loadBanner);
|
||||
// Load banner when page loads
|
||||
document.addEventListener('DOMContentLoaded', loadBanner);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -156,20 +156,20 @@ func UpdateBannerHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Log form values for debugging
|
||||
log.Printf("Form values: %+v", r.Form)
|
||||
|
||||
// Parse style as JSON string
|
||||
styleJSON := r.FormValue("style")
|
||||
var style BannerStyle
|
||||
if err := json.Unmarshal([]byte(styleJSON), &style); err != nil {
|
||||
log.Printf("Error parsing style JSON: %v", err)
|
||||
http.Error(w, "Error parsing style data: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new banner with parsed style
|
||||
// Create a new banner with default values
|
||||
newBanner := BannerContent{
|
||||
Text: r.FormValue("text"),
|
||||
Link: r.FormValue("link"),
|
||||
Style: style,
|
||||
Style: BannerStyle{
|
||||
BackgroundColor: r.FormValue("style[backgroundColor]"),
|
||||
TextColor: r.FormValue("style[textColor]"),
|
||||
TextAlign: r.FormValue("style[textAlign]"),
|
||||
FontSize: r.FormValue("style[fontSize]"),
|
||||
Padding: r.FormValue("style[padding]"),
|
||||
Margin: r.FormValue("style[margin]"),
|
||||
BorderRadius: r.FormValue("style[borderRadius]"),
|
||||
IsVisible: r.FormValue("style[isVisible]") == "true",
|
||||
},
|
||||
}
|
||||
|
||||
// Log the banner data for debugging
|
||||
|
||||
Reference in New Issue
Block a user