PageSourceSearch

https://tschmoderer.github.io/assets/js/assets/hal-bibliography-integrator/hal.js

js tschmoderer.github.io collected 2026-10-03 10:06:16 UTC 12,694 bytes, 413 lines download raw bytes

1'use strict';
2
3const hal_api_url = "https://api.archives-ouvertes.fr/search";
4
5async function callHALAPI(parameters, debug = false) {
6    try {
7        // Definition des paramètres de la recherche
8        const param = new URLSearchParams(parameters);
9
10        // Instanciation de l'url 
11        var url = new URL(hal_api_url);
12        url.search = param.toString();
13
14        if (debug) {
15            console.dir(url);
16        }
17
18        // Appel de l'API
19        const response = await fetch(url);
20        const data = await response.json();
21
22        // Return 
23        return data.response.docs;
24    } catch (error) {
25        console.log("Error in call on HAL API on url : " + url.toString());
26        console.error(error);
27    }
28}
29
30const hal_module_name = "hal-bibliography-integrator";
31
32const hal_helpers = {
33    "THESE": {
34        "icon": "fa-graduation-cap",
35        "title_en": "Thesis",
36    },
37
38    "ART": {
39        "icon": "fa-newspaper",
40        "title_en": "Journal articles",
41    },
42
43    "UNDEFINED": {
44        "icon": "fa-file-pen",
45        "title_en": "Preprint",
46    },
47
48    "COMM": {
49        "icon": "fa-microphone",
50        "title_en": "Communications",
51    },
52    "POSTER": {
53        "icon": "fa-image",
54        "title_en": "Poster",
55    },
56    "OUV": {
57        "icon": "fa-book",
58        "title_en": "Book",
59    },
60    "COUV": {
61        "icon": "fa-book",
62        "title_en": "Book Chapters",
63    },
64    "LECTURE": {
65        "icon": "fa-book-open",
66        "title_en": "Lectures",
67    },
68
69    "PATENT": {
70        "icon": "fa-lightbulb",
71        "title_en": "Patents",
72    },
73
74    "SOFTWARE": {
75        "icon": "fa-microchip",
76        "title_en": "Softwares",
77    },
78    "PROCEEDINGS": {
79        "icon": "fa-file",
80        "title_en": "Proceedings",
81    }
82};
83
84var globalHalData = {};
85
86const eventNameHalDone = "halMainDone";
87const eventNameArtDone = "halArticleDone";
88
89function collapse(event) {
90    event.preventDefault();
91    const elem = event.target.closest("button");
92    const elem2 = document.getElementById(elem.getAttribute("data-target").replace('#', ''));
93    if (elem2.style.display === "none") {
94        elem2.style.display = "block";
95        elem.querySelector(".icon-drop_down").classList.remove("fa-rotate-by");
96    } else {
97        elem2.style.display = "none";
98        elem.querySelector(".icon-drop_down").classList.add("fa-rotate-by");
99    }
100}
101
102// Fonction pour gérer les popups de citations
103function copyCitation(idDoc, debug = false) {
104    const id = "hal-citation-biblatex-" + idDoc;
105    const elem = document.getElementById(id);
106
107    // Copy the text inside the text field
108    navigator.clipboard.writeText(elem.innerText);
109
110    if (debug) {
111        console.log("Copy text to clipboard");
112        console.log(elem.innerText);
113    }
114
115    // Display green message for 3 second
116    const success = elem.parentElement.querySelector("[id='hal-copy-success']");
117    success.classList.remove('hidden');
118    success.classList.add('visible');
119
120    setTimeout(function () {
121        success.classList.add('hidden');
122        success.classList.remove('visible');
123    }, 3000);
124}
125
126function trigger_hal(eventName) {
127    const event = new Event(eventName);
128    document.dispatchEvent(event);
129}
130
131function trigger_hal_article_end() {
132    trigger_hal(eventNameArtDone);
133}
134
135function trigger_hal_end() {
136    trigger_hal(eventNameHalDone);
137}
138
139function create_spinner(id = null) {
140    const spinner = document.createElement("div");
141    spinner.classList = "hal-spinner";
142    if (id) {
143        spinner.id = id;
144    }
145
146    const ellipsis = document.createElement("div");
147    ellipsis.classList.add("lds-ellipsis");
148    for (let i = 1; i <= 4; i++) {
149        ellipsis.appendChild(document.createElement("div"));
150    }
151
152    spinner.appendChild(ellipsis);
153    return spinner;
154}
155
156
157function initialHTML(type) {
158    const container = document.createElement("div");
159    container.classList.add('hal-list');
160    container.id = `hal-${type}`;
161    container.setAttribute('name', `hal-${type}`);
162
163    const button = document.createElement("button");
164    button.id = `hal-btn-${type}`;
165    button.classList.add("hal-btn");
166    button.setAttribute("data-target", `#${type}`);
167
168    const icon = document.createElement("i");
169    icon.classList.add('hal-icon', 'fa-solid', hal_helpers[type]["icon"]);
170    button.appendChild(icon);
171
172    const title = document.createElement("a");
173    title.id = `hal-${type}-card-title`;
174    title.textContent = hal_helpers[type]["title_en"];
175    button.appendChild(title);
176
177    const caretIcon = document.createElement("i");
178    caretIcon.classList.add('hal-icon', 'icon-drop_down', 'fa-solid', 'fa-caret-down');
179    button.appendChild(caretIcon);
180
181    button.addEventListener("click", collapse);
182
183    container.appendChild(button);
184
185    const spinner = create_spinner(`hal-${type}-spinner`);
186
187    container.appendChild(spinner);
188
189    const content = document.createElement("div");
190    content.classList.add("hal-content");
191    content.id = type;
192
193    const table = document.createElement("table");
194    table.classList.add("hal-results-table");
195
196    const tbody = document.createElement("tbody");
197    tbody.id = `hal-${type}-table`;
198
199    table.appendChild(tbody);
200    content.appendChild(table);
201    container.appendChild(content);
202
203    return container;
204}
205
206async function genListPubli(id, type, debug = false) {
207    const param = {
208        q: `authIdHal_s: ${id}`,
209        rows: "10000",
210        fl: [
211            "title_s",
212            "halId_s",
213            "citationRef_s",
214            "defenseDateY_i",
215            "journalTitle_s",
216            "authFullName_s",
217            "publicationDate_tdate",
218            "fileMain_s",
219            "thumbId_i",
220            "label_bibtex",
221            "en_keyword_s",
222            "journalIssn_s",
223            "doiId_s",
224        ],
225        fq: `docType_s: ${type}`,
226        sort: "publicationDate_tdate desc",
227        wt: "json",
228    };
229
230    return callHALAPI(param).then(data => {
231        // Sauvegarde des data dans une variable global pour les plugins 
232        globalHalData[type] = data;
233        // Set title 
234        document.getElementById(`hal-${type}-card-title`).innerText = `${hal_helpers[type]["title_en"]} (${data.length})`;
235
236        // Complete list 
237        var tab = document.getElementById(`hal-${type}-table`);
238        for (const p of data) {
239            if (debug) {
240                console.log(p);
241            }
242
243            const row = document.createElement("tr");
244            row.id = `row-${p.halId_s}`;
245
246            const cell = document.createElement("td");
247            cell.classList.add("d-sm-table-cell");
248
249            if (p.thumbId_i) {
250                const link = document.createElement("a");
251                link.href = p.fileMain_s;
252                link.target = "_blank";
253
254                const mediaDiv = document.createElement("div");
255                mediaDiv.classList.add("hal-media", "d-sm-block");
256
257                const image = document.createElement("img");
258                image.src = `https://thumb.ccsd.cnrs.fr/${p.thumbId_i}/thumb`;
259                image.alt = "Image document";
260
261                mediaDiv.appendChild(image);
262                link.appendChild(mediaDiv);
263                cell.appendChild(link);
264            }
265
266            const titleCell = document.createElement("td");
267            titleCell.classList.add("hal-title");
268
269            const titleLink = document.createElement("a");
270            titleLink.href = `https://hal.science/${p.halId_s}`;
271            titleLink.target = "_blank";
272
273            const titleHeading = document.createElement("h3");
274            titleHeading.classList.add("title-results");
275            titleHeading.textContent = p.title_s[0];
276
277            titleLink.appendChild(titleHeading);
278            titleCell.appendChild(titleLink);
279
280
281            let nbit = p.authFullName_s.length;
282            for (const author of p.authFullName_s) {
283                const authorLink = document.createElement("a");
284                authorLink.href = `https://hal.science/search/?q=*&authFullName_s=${author}`;
285                authorLink.alt = "Documents de l auteur";
286                authorLink.target = "_blank";
287                authorLink.textContent = author;
288
289                titleCell.appendChild(authorLink);
290
291                if (--nbit) {
292                    titleCell.appendChild(document.createTextNode(" ; "));
293                }
294            }
295
296            const citationDiv = document.createElement("div");
297            citationDiv.classList.add("citation-results");
298            citationDiv.innerHTML = p.citationRef_s;
299
300            titleCell.appendChild(document.createElement("br"));
301            titleCell.appendChild(citationDiv);
302
303            const exportDiv = document.createElement("div");
304            exportDiv.classList.add("export-result");
305            if (p.thumbId_i) {
306                const pdfLink = document.createElement("a");
307                pdfLink.href = p.fileMain_s;
308                pdfLink.target = "_blank";
309                pdfLink.classList.add("hal-export-pdf");
310
311                const pdfIcon = document.createElement("i");
312                pdfIcon.classList.add("fa-regular", "fa-file-pdf");
313
314                pdfLink.appendChild(pdfIcon);
315                exportDiv.appendChild(pdfLink);
316            }
317
318            const citeLink = document.createElement("a");
319            citeLink.classList.add("hal-export-cite");
320            citeLink.addEventListener("click", () => { copyCitation(p.halId_s, debug); });
321            citeLink.title = "Copy BibLatex Citation";
322
323            const quoteIcon = document.createElement("i");
324            quoteIcon.classList.add("fa-solid", "fa-quote-right");
325
326            const copySuccess = document.createElement("a");
327            copySuccess.id = "hal-copy-success";
328            copySuccess.classList.add("hal-citation-copy-success", "hidden");
329            copySuccess.textContent = "BibLatex citation copied";
330
331            const bibtexCitation = document.createElement("p");
332            bibtexCitation.classList.add("hal-biblatex-citation");
333            bibtexCitation.id = `hal-citation-biblatex-${p.halId_s}`;
334            bibtexCitation.textContent = p.label_bibtex;
335
336            citeLink.appendChild(quoteIcon);
337            citeLink.appendChild(copySuccess);
338            citeLink.appendChild(bibtexCitation);
339            exportDiv.appendChild(citeLink);
340
341            titleCell.appendChild(exportDiv);
342
343            row.appendChild(cell);
344            row.appendChild(titleCell);
345
346            tab.appendChild(row);
347        };
348
349        // Remove loader
350        document.getElementById("hal-" + type + "-spinner").style.display = "none";
351        document.getElementById("hal-" + type).style.display = "block";
352        if ("onLoad" in hal_integrator_config && hal_integrator_config["onLoad"].toLowerCase() === "collapsed") {
353            document.getElementById(type).style.display = "none";
354            document.getElementById("hal-btn-" + type).querySelector(".icon-drop_down").classList.add("fa-rotate-by");
355        }
356
357        // Update mathjax
358        MathJax.typeset([document.getElementById(type)]);
359
360        // If article trigger event 
361        if (type === "ART") {
362            trigger_hal_article_end();
363        }
364    }).catch(error => console.error(error));
365}
366
367function create_hal_publication_list(idhal, pubTypeList, debug = false) {
368    // Récupère la div ou placer la liste des publications 
369    var hal_publi_div = document.getElementById(hal_module_name);
370
371    // Si elle n'existe pas on ne fait rien 
372    if (hal_publi_div === null) {
373        if (debug) {
374            console.log("No HAL publication div on this page");
375        }
376        return false;
377    }
378
379    if (debug) {
380        console.log("Create a list of publications in ");
381        console.log(hal_publi_div);
382    }
383
384    // Sinon pour chaque type de publication on créé la liste
385    var apiCalls = [];
386    for (const p of pubTypeList) {
387        hal_publi_div.appendChild(initialHTML(p));
388        apiCalls.push(genListPubli(idhal, p, debug));
389    }
390
391    Promise.all(apiCalls).then(() => {
392        trigger_hal_end();
393    });
394
395    return true;
396};
397
398function make_hal(id, pubType, debug) {
399    if ((typeof debug !== "undefined") && (debug)) {
400        console.log(id);
401        console.log(pubType);
402    } else {
403        var debug = false;
404    }
405
406    create_hal_publication_list(id, pubType, debug);
407}
408
409if (typeof hal_integrator_config !== 'undefined') {
410    if (!("doit" in hal_integrator_config) || (hal_integrator_config["doit"])) {
411        document.addEventListener("DOMContentLoaded", make_hal(hal_integrator_config["id"], hal_integrator_config["typeList"], hal_integrator_config["debug"]), false);
412    }
413}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.