From 2ee324ce6261e784cdb982412fd71d177354cc0a Mon Sep 17 00:00:00 2001 From: Uthpala Herath Date: Wed, 2 Oct 2024 02:56:42 -0400 Subject: [PATCH] Updated autoupdates.js to fill in authors from RIS file. --- .../materials/javascript/autoupdates.js | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/materials/static/materials/javascript/autoupdates.js b/materials/static/materials/javascript/autoupdates.js index 895f58a..30decb1 100644 --- a/materials/static/materials/javascript/autoupdates.js +++ b/materials/static/materials/javascript/autoupdates.js @@ -20,6 +20,7 @@ function handleFileSelect() { let year = ''; let doi = ''; let isbn = ''; + let authors = []; // Iterate over lines and extract data lines.forEach(line => { @@ -48,7 +49,9 @@ function handleFileSelect() { else if (line.startsWith('SN -')) { isbn += line.replace(/^SN\s*-\s*/, '').trim(); } - // Add more fields as needed + else if (line.startsWith('AU -')) { + authors.push(line.replace(/^AU\s*-\s*/, '').trim()); // Collect all authors + } }); // Now set the values of the form inputs @@ -77,7 +80,6 @@ function handleFileSelect() { yearInput.value = year.trim(); } - // Set the DOI and ISBN with a comma and space separator if both are present const doiIsbnInput = document.querySelector('[name="doi_isbn"]'); if (doiIsbnInput) { if (doi && isbn) { @@ -87,8 +89,33 @@ function handleFileSelect() { } } + const addAuthorButton = document.querySelector('#add-more-authors-btn'); // Button to add more authors + + // Fill authors one by one + authors.forEach((author, index) => { + if (index > 0) { + // Click the "Add more authors" button for additional authors + addAuthorButton.click(); + } + + const authorParts = author.split(', '); + const lastName = authorParts[0]; + const firstName = authorParts.length > 1 ? authorParts[1] : ''; + + // Populate the respective author fields dynamically + const firstNameField = document.querySelector(`[name="first-name-${index + 1}"]`); + const lastNameField = document.querySelector(`[name="last-name-${index + 1}"]`); + + if (firstNameField && lastNameField) { + firstNameField.value = firstName; + lastNameField.value = lastName; + } + }); }; reader.readAsText(file); // Read the file as text } } + +// Attach the function to the input file change event +document.querySelector('[name="inputRISFile"]').addEventListener('change', handleFileSelect);