Files
links/templates/base.html
T
2024-09-08 19:18:53 +10:00

173 lines
7.3 KiB
HTML

{% load i18n %}
{% load static %}
<!DOCTYPE html>
<html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% trans "URL Manager" %}</title>
<link href="{% static 'css/dist/styles.css' %}" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
<style>
/* ... other styles ... */
#search-results {
background-color: white;
border: 1px solid #e2e8f0;
border-top: none;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
max-height: 200px;
overflow-y: auto;
}
#search-results div {
padding: 0.5rem 1rem;
cursor: pointer;
color: #4a5568;
}
#search-results div:hover {
background-color: #f7fafc;
}
.language-selector {
position: relative;
cursor: pointer;
}
.language-options {
display: none;
position: absolute;
top: 100%;
right: 0;
background-color: white;
border: 1px solid #e2e8f0;
border-radius: 0.25rem;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
min-width: 150px; /* Increased width */
}
.language-selector:hover .language-options {
display: block;
}
.language-option {
padding: 0.5rem 1rem;
color: #4a5568;
}
.language-option:hover {
background-color: #f7fafc;
}
</style>
</head>
<body class="bg-gray-100">
<nav class="bg-blue-600 text-white p-4 fixed w-full top-0 z-10">
<div class="container mx-auto flex justify-between items-center">
<a href="{% url 'link_list' %}" class="text-xl font-bold">{% trans "URL Manager" %}</a>
<div class="flex items-center space-x-4"> <!-- Added space-x-4 for more spacing -->
<div class="relative">
<input type="text" id="search-input" class="bg-white text-gray-800 rounded px-4 py-2 w-64" placeholder="{% trans 'Search aliases...' %}"> <!-- Increased width and padding -->
<div id="search-results" class="absolute top-full left-0 right-0 bg-white border border-gray-300 rounded mt-1 hidden"></div>
</div>
<div class="language-selector">
<span class="flex items-center">
<i class="fas fa-globe mr-2"></i> <!-- Increased margin -->
{% get_current_language as LANGUAGE_CODE %}
{% get_language_info for LANGUAGE_CODE as current_language %}
{{ current_language.code|upper }}
</span>
<div class="language-options">
<form action="{% url 'set_language' %}" method="post">
{% csrf_token %}
<input name="next" type="hidden" value="{{ request.path }}">
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<button type="submit" name="language" value="{{ language.code }}" class="language-option w-full text-left">
{{ language.name_local }} ({{ language.code|upper }})
</button>
{% endfor %}
</form>
</div>
</div>
</div>
</div>
</nav>
<div class="container mx-auto mt-20 p-4">
{% if messages %}
{% for message in messages %}
<div class="bg-{{ message.tags }}-100 border border-{{ message.tags }}-400 text-{{ message.tags }}-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline">{{ message }}</span>
</div>
{% endfor %}
{% endif %}
{% block content %}
{% endblock %}
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const searchInput = document.getElementById('search-input');
const searchResults = document.getElementById('search-results');
let currentFocus = -1;
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
searchInput.addEventListener('input', debounce(function(e) {
const query = e.target.value.trim();
console.log('Search query:', query); // Debug log
if (query.length === 0) {
searchResults.style.display = 'none';
return;
}
const url = `/search/?q=${encodeURIComponent(query)}`;
console.log('Fetching from URL:', url); // Debug log
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log('Search results:', data); // Debug log
searchResults.innerHTML = '';
if (Array.isArray(data) && data.length > 0) {
data.forEach(item => {
if (item && typeof item === 'object' && 'alias' in item && 'url' in item) {
const div = document.createElement('div');
div.textContent = item.alias;
div.addEventListener('click', function() {
window.location.href = item.url;
});
searchResults.appendChild(div);
} else {
console.warn('Invalid item in search results:', item);
}
});
searchResults.style.display = 'block';
} else {
searchResults.innerHTML = '<div class="p-2">{% trans "No results found" %}</div>';
searchResults.style.display = 'block';
}
})
.catch(error => {
console.error('Error:', error);
searchResults.style.display = 'none';
});
}, 300));
// ... (rest of the script for keyboard navigation, if needed)
});
</script>
</body>
</html>