Spaces:
Sleeping
Sleeping
let selectedMeetings = []; | |
let currentURL = null; | |
function toggleDropdown() { | |
const dropdown = document.getElementById("dropdownContent"); | |
dropdown.style.display = (dropdown.style.display === "none") ? "block" : "none"; | |
} | |
document.getElementById('dropbtn').addEventListener('click', () => { | |
toggleDropdown() | |
}) | |
document.addEventListener('mousedown', (e) => { | |
if (document.getElementById("dropdownContent").style.display == "block" && e.target.className != "dropdown-content" && e.target.tagName != "INPUT" && e.target.tagName != "LABEL") { | |
document.getElementById("dropdownContent").style.display = "none"; | |
} | |
}) | |
document.getElementById('tdocs-wg-option').addEventListener('change', async (e) => { | |
let wg = e.target.value; | |
const dropdownContent = document.getElementById('dropdownContent'); | |
const dropbtn = document.getElementById('dropbtn'); | |
if (wg != "ALL") { | |
dropdownContent.innerHTML = '<label style="display:none;"><input type="checkbox" checked value="ALL">Index all meetings</label>'; | |
const response = await fetch(`/get_meetings/${wg}`, { | |
method: "GET" | |
}); | |
const responseJson = await response.json(); | |
const meetings = responseJson.meetings; | |
currentURL = responseJson.url; | |
for (const meet of meetings) { | |
const label = document.createElement('label'); | |
const checkbox = document.createElement('input'); | |
checkbox.type = "checkbox"; | |
checkbox.value = meet; | |
label.appendChild(checkbox); | |
label.appendChild(document.createTextNode(meet)); | |
dropdownContent.appendChild(label); | |
dropdownContent.appendChild(document.createElement('br')); | |
} | |
dropbtn.removeAttribute('disabled'); | |
// après création, ajoute les listeners de gestion sur chaque checkbox | |
initCheckboxListeners(); | |
// Initialise l'état initial | |
updateDropbtnLabel(); | |
} else { | |
dropdownContent.innerHTML = '<label style="display:none;"><input type="checkbox" checked value="ALL">Index all meetings</label>'; | |
dropbtn.setAttribute('disabled', 'true'); | |
dropbtn.textContent = "Index all meetings"; | |
} | |
}); | |
function disableButtons() { | |
document.getElementById("spec-3gpp-btn").setAttribute('disabled', 'disabled') | |
document.getElementById("spec-etsi-btn").setAttribute('disabled', 'disabled') | |
document.getElementById("tdocs-btn").setAttribute('disabled', 'disabled') | |
} | |
function enableButtons() { | |
document.getElementById("spec-3gpp-btn").removeAttribute('disabled') | |
document.getElementById("spec-etsi-btn").removeAttribute('disabled') | |
document.getElementById("tdocs-btn").removeAttribute('disabled') | |
} | |
function initCheckboxListeners() { | |
const dropdownContent = document.getElementById('dropdownContent'); | |
const dropbtn = document.getElementById('dropbtn'); | |
function updateState() { | |
const checkboxes = dropdownContent.querySelectorAll('input[type="checkbox"]'); | |
const allCheckbox = dropdownContent.querySelector('input[value="ALL"]'); | |
const checkedBoxes = Array.from(checkboxes).filter(cb => cb.checked && cb !== allCheckbox); | |
if (checkedBoxes.length === 0) { | |
allCheckbox.checked = true; | |
dropbtn.textContent = "Index all meetings"; | |
selectedMeetings = ["ALL"]; | |
} else { | |
if (allCheckbox.checked) { | |
allCheckbox.checked = false; // décocher ALL si autre(s) cochée(s) | |
} | |
if (checkedBoxes.length === 1) { | |
dropbtn.textContent = checkedBoxes[0].value; | |
} else { | |
dropbtn.textContent = `${checkedBoxes.length} meetings sélectionnés`; | |
} | |
selectedMeetings = checkedBoxes.map(cb => cb.value); | |
} | |
console.log(selectedMeetings); | |
console.log(currentURL); | |
} | |
const checkboxes = dropdownContent.querySelectorAll('input[type="checkbox"]'); | |
checkboxes.forEach(cb => cb.addEventListener('change', updateState)); | |
updateState(); // mise à jour initiale | |
} | |
function updateDropbtnLabel() { | |
const dropdownContent = document.getElementById('dropdownContent'); | |
const checkboxes = dropdownContent.querySelectorAll('input[type="checkbox"]'); | |
const allCheckbox = dropdownContent.querySelector('input[value="ALL"]'); | |
const dropbtn = document.getElementById('dropbtn'); | |
const checkedBoxes = Array.from(checkboxes).filter(cb => cb.checked && cb !== allCheckbox); | |
if (checkedBoxes.length === 0) { | |
allCheckbox.checked = true; | |
dropbtn.textContent = "Index all meetings"; | |
} else if (checkedBoxes.length === 1) { | |
allCheckbox.checked = false; | |
dropbtn.textContent = checkedBoxes[0].value; | |
} else { | |
allCheckbox.checked = false; | |
dropbtn.textContent = `${checkedBoxes.length} meetings sélectionnés`; | |
} | |
} | |
const progress = document.getElementById('indexProgression') | |
document.getElementById('tdocs-btn').addEventListener('click', async () => { | |
disableButtons() | |
let response = null; | |
if (currentURL) { | |
if (!selectedMeetings.includes("ALL")) { | |
response = await fetch("/index_tdocs/meeting", { | |
method: "POST", | |
body: JSON.stringify({ | |
wg: document.getElementById("tdocs-wg-option").value, | |
meetings: selectedMeetings | |
}), | |
headers: { | |
"Content-Type": "application/json", | |
"Accept": "text/event-stream", | |
}, | |
}); | |
} else { | |
response = await fetch("/index_tdocs/working_group", { | |
method: "POST", | |
body: JSON.stringify({ | |
wg: document.getElementById("tdocs-wg-option").value | |
}), | |
headers: { | |
"Content-Type": "application/json", | |
"Accept": "text/event-stream", | |
}, | |
}); | |
} | |
} else { | |
response = await fetch("/index_tdocs/all", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
"Accept": "text/event-stream", | |
}, | |
}); | |
} | |
const reader = response.body.getReader(); | |
const decoder = new TextDecoder("utf-8"); | |
let buffer = ""; | |
while (true) { | |
const { | |
done, | |
value | |
} = await reader.read(); | |
if (done) break; | |
buffer += decoder.decode(value, { | |
stream: true | |
}); | |
let events = buffer.split("\n\n"); // séparer les événements | |
buffer = events.pop(); // garder le reste pour le prochain chunk | |
for (const rawEvent of events) { | |
const event = {}; | |
rawEvent.split("\n").forEach((line) => { | |
const [key, ...rest] = line.split(":"); | |
if (key) event[key.trim()] = rest.join(":").trim(); | |
}); | |
// dispatch selon event.event ou par défaut message | |
switch (event.event) { | |
case "progress": | |
console.log("[progress]", event.data); | |
progress.value = event.data; | |
break; | |
case "get-maximum": | |
console.log("[new-max]", event.data); | |
progress.max = event.data | |
break; | |
case "info": | |
console.log("[info]", event.data); | |
document.getElementById('infoText').textContent = event.data | |
break; | |
case "end": | |
console.log("[end]", event.data); | |
document.getElementById('infoText').textContent = "Indexation successful" | |
break; | |
default: | |
if (event.data) { | |
console.log("[message]", event.data); | |
}; | |
} | |
} | |
} | |
enableButtons() | |
}); | |
document.getElementById('spec-3gpp-btn').addEventListener('click', () => { | |
disableButtons() | |
logMessage(`Started re-indexing 3GPP Specifications`); | |
fetch("/index_specs/3gpp", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json" | |
} | |
}) | |
.then(resp => resp.text()) | |
.then(data => { | |
enableButtons() | |
}) | |
}); | |
document.getElementById('spec-etsi-btn').addEventListener('click', () => { | |
logMessage('Started re-indexing ETSI Specifications'); | |
disableButtons() | |
fetch("/index_specs/etsi", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json" | |
} | |
}) | |
.then(resp => resp.text()) | |
.then(data => { | |
enableButtons() | |
}) | |
}); |