mirror of
https://github.com/wahyd4/links.git
synced 2026-08-09 05:06:16 +10:00
225 lines
8.8 KiB
HTML
225 lines
8.8 KiB
HTML
{% load i18n %}
|
|
<!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="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<style>
|
|
body { padding-top: 60px; }
|
|
.table-responsive { margin-top: 20px; }
|
|
/* 移除链接下划线 */
|
|
a { text-decoration: none; }
|
|
/* 鼠标悬停时显示下划线 */
|
|
a:hover { text-decoration: underline; }
|
|
.message-container {
|
|
position: fixed;
|
|
top: 60px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
z-index: 1000;
|
|
}
|
|
.message {
|
|
padding: 10px 20px;
|
|
border-radius: 5px;
|
|
margin-bottom: 10px;
|
|
opacity: 1;
|
|
transition: opacity 0.5s ease-in-out;
|
|
}
|
|
.message.error {
|
|
background-color: #f8d7da;
|
|
border-color: #f5c6cb;
|
|
color: #721c24;
|
|
}
|
|
.search-results {
|
|
position: absolute;
|
|
top: 100%;
|
|
left: 0;
|
|
right: 0;
|
|
background-color: white;
|
|
border: 1px solid #ddd;
|
|
border-top: none;
|
|
max-height: 300px;
|
|
overflow-y: auto;
|
|
display: none;
|
|
}
|
|
.search-result-item {
|
|
padding: 10px;
|
|
cursor: pointer;
|
|
}
|
|
.search-result-item:hover, .search-result-item.active {
|
|
background-color: #f8f9fa;
|
|
}
|
|
</style>
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
|
|
</head>
|
|
<body>
|
|
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="{% url 'link_list' %}">{% trans "URL Manager" %}</a>
|
|
<div class="d-flex">
|
|
<div class="position-relative me-2">
|
|
<input type="text" id="search-input" class="form-control" placeholder="{% trans 'Search aliases...' %}">
|
|
<div id="search-results" class="search-results"></div>
|
|
</div>
|
|
<form action="{% url 'set_language' %}" method="post" class="form-inline">
|
|
{% csrf_token %}
|
|
<input name="next" type="hidden" value="{{ request.path }}">
|
|
<select name="language" onchange="this.form.submit()" class="form-select">
|
|
{% get_current_language as LANGUAGE_CODE %}
|
|
{% get_available_languages as LANGUAGES %}
|
|
{% get_language_info_list for LANGUAGES as languages %}
|
|
{% for language in languages %}
|
|
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
|
|
{{ language.name_local }}
|
|
</option>
|
|
{% endfor %}
|
|
</select>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="message-container">
|
|
{% if messages %}
|
|
{% for message in messages %}
|
|
<div class="message {% if message.tags %}{{ message.tags }}{% endif %}">
|
|
{{ message }}
|
|
</div>
|
|
{% endfor %}
|
|
{% endif %}
|
|
</div>
|
|
|
|
<div class="container">
|
|
{% block content %}
|
|
{% endblock %}
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
// 消息自动消失
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
var messages = document.querySelectorAll('.message');
|
|
messages.forEach(function(message) {
|
|
setTimeout(function() {
|
|
message.style.opacity = '0';
|
|
setTimeout(function() {
|
|
message.remove();
|
|
}, 500);
|
|
}, 5000);
|
|
});
|
|
});
|
|
|
|
// Search functionality
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const searchInput = document.getElementById('search-input');
|
|
const searchResults = document.getElementById('search-results');
|
|
let currentFocus = -1;
|
|
|
|
if (!searchInput || !searchResults) {
|
|
console.error('Search input or results container not found');
|
|
return;
|
|
}
|
|
|
|
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.classList.add('search-result-item');
|
|
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="search-result-item">{% trans "No results found" %}</div>';
|
|
searchResults.style.display = 'block';
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
searchResults.style.display = 'none';
|
|
});
|
|
}, 300));
|
|
|
|
searchInput.addEventListener('keydown', function(e) {
|
|
const items = searchResults.getElementsByClassName('search-result-item');
|
|
if (e.keyCode === 40) { // Down arrow
|
|
currentFocus++;
|
|
addActive(items);
|
|
} else if (e.keyCode === 38) { // Up arrow
|
|
currentFocus--;
|
|
addActive(items);
|
|
} else if (e.keyCode === 13) { // Enter
|
|
e.preventDefault();
|
|
if (currentFocus > -1) {
|
|
if (items) items[currentFocus].click();
|
|
}
|
|
}
|
|
});
|
|
|
|
function addActive(items) {
|
|
if (!items) return false;
|
|
removeActive(items);
|
|
if (currentFocus >= items.length) currentFocus = 0;
|
|
if (currentFocus < 0) currentFocus = (items.length - 1);
|
|
items[currentFocus].classList.add('active');
|
|
}
|
|
|
|
function removeActive(items) {
|
|
for (let i = 0; i < items.length; i++) {
|
|
items[i].classList.remove('active');
|
|
}
|
|
}
|
|
|
|
function debounce(func, wait) {
|
|
let timeout;
|
|
return function executedFunction(...args) {
|
|
const context = this;
|
|
const later = () => {
|
|
clearTimeout(timeout);
|
|
func.apply(context, args);
|
|
};
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(later, wait);
|
|
};
|
|
}
|
|
|
|
document.addEventListener('click', function(e) {
|
|
if (e.target !== searchInput && e.target !== searchResults) {
|
|
searchResults.style.display = 'none';
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|