File "code017.php"
Full Path: /home/analogde/www/Projet/liste/code017.php
File size: 6.42 KB
MIME-type: text/html
Charset: utf-8
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dev</title>
<style>
.context-menu {
display: none;
position: absolute;
background-color: #fff;
border: 1px solid #ccc;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
z-index: 1000;
}
.context-menu ul {
list-style: none;
padding: 0;
margin: 0;
}
.context-menu ul li {
padding: 10px;
cursor: pointer;
}
.context-menu ul li:hover {
background-color: #f0f0f0;
}
.list-item-span {
display: inline-block;
padding: 5px;
cursor: pointer;
}
.list-title {
font-weight: bold;
margin-bottom: 10px;
display: block;
cursor: pointer;
}
.list-content {
display: none;
margin-left: 20px;
}
.context-menu-link {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 200px; /* Ajustez cette valeur selon vos besoins */
}
.context-menu-link a {
text-decoration: none;
}
</style>
</head>
<body>
<h1>OH Tree Viewer avec menu contextuel</h1>
<div id="listContainer"></div>
<div id="contextMenu" class="context-menu">
<ul>
<li onclick="handleOption1()">Option 1</li>
<li onclick="handleOption2()">Option 2</li>
<li onclick="handleOption3()">Option 3</li>
<li id="contextMenuLink" class="context-menu-link"><a href="#">Lien vers une autre page</a></li>
</ul>
</div>
<script>
const contextMenu = document.getElementById('contextMenu');
const contextMenuLink = document.getElementById('contextMenuLink').querySelector('a');
// Fonction pour générer la liste à partir des données JSON
function generateList(data)
{
const listContainer = document.getElementById('listContainer');
listContainer.innerHTML = '';
Object.keys(data).forEach(listName => {
const listDiv = document.createElement('div');
listDiv.className = 'list-container';
const listTitle = document.createElement('span');
listTitle.className = 'list-title';
listTitle.textContent = `${listName} (${data[listName].length} éléments)`;
listTitle.setAttribute('data-list', listName.replace(/\s+/g, ''));
listDiv.appendChild(listTitle);
const listContent = document.createElement('ul');
listContent.id = listName.replace(/\s+/g, '');
listContent.className = 'list-content';
data[listName].forEach(element => {
const listItem = document.createElement('li');
const listItemSpan = document.createElement('span');
listItemSpan.className = 'list-item-span';
listItemSpan.textContent = element;
listItem.appendChild(listItemSpan);
listContent.appendChild(listItem);
});
listDiv.appendChild(listContent);
listContainer.appendChild(listDiv);
});
// Ajouter les événements aux titres de liste
const listTitles = document.querySelectorAll('.list-title');
listTitles.forEach(title => {
title.addEventListener('click', (event) => {
const listId = title.getAttribute('data-list');
const listContent = document.getElementById(listId);
if (listContent.style.display === 'none' || listContent.style.display === '') {
listContent.style.display = 'block';
} else {
listContent.style.display = 'none';
}
});
});
// Ajouter les événements aux éléments de liste
const listItemSpans = document.querySelectorAll('.list-item-span');
listItemSpans.forEach(span => {
span.addEventListener('contextmenu', (event) => {
event.preventDefault();
const listTitle = span.closest('.list-container').querySelector('.list-title').textContent;
const itemText = span.textContent;
contextMenuLink.href = `otherpage.php?list=${encodeURIComponent(listTitle)}&item=${encodeURIComponent(itemText)}`;
contextMenu.style.display = 'block';
contextMenu.style.left = `${event.pageX}px`;
contextMenu.style.top = `${event.pageY}px`;
});
});
}
// Fonction pour récupérer les données JSON depuis le serveur
function fetchData()
{
const xhr = new XMLHttpRequest();
xhr.open('GET', 'fetch_data.php', true);
xhr.onreadystatechange = function ()
{
if (xhr.readyState === 4 && xhr.status === 200)
{
const data = JSON.parse(xhr.responseText);
generateList(data);
}
};
xhr.send();
}
// Appel initial pour récupérer les données
fetchData();
document.addEventListener('click', (event) => {
if (!contextMenu.contains(event.target) && !event.target.matches('.list-item-span')) {
contextMenu.style.display = 'none';
}
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' || event.keyCode === 27) {
contextMenu.style.display = 'none';
}
});
function handleOption1() {
alert('Option 1 sélectionnée');
contextMenu.style.display = 'none';
}
function handleOption2() {
alert('Option 2 sélectionnée');
contextMenu.style.display = 'none';
}
function handleOption3() {
alert('Option 3 sélectionnée');
contextMenu.style.display = 'none';
}
</script>
</body>
</html>