File size: 8,821 Bytes
cf82bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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()
    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', () => {
    disableButtons()
    fetch("/index_specs/etsi", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            }
        })
        .then(resp => resp.text())
        .then(data => {
            enableButtons()
        })
});