diff --git a/index.html b/index.html index 454e682..78222ff 100644 --- a/index.html +++ b/index.html @@ -1,69 +1,132 @@
Most fields are optional. Mandatory fields will be highlighted when generating Codemeta.
+ - + + + diff --git a/script.js b/script.js index 13d0449..88d57be 100644 --- a/script.js +++ b/script.js @@ -1,129 +1,164 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ +"use strict"; + function emptyToUndefined(v) { if (v == null || v == "") return undefined; else return v; } function getIfSet(query) { return emptyToUndefined(document.querySelector(query).value); } -function getNbAuthors() { - var nbAuthorsField = document.querySelector('#nbAuthors'); - return parseInt(nbAuthorsField.value, 10); +function getNbPersons(prefix) { + var nbField = document.querySelector(`#${prefix}_nb`); + return parseInt(nbField.value, 10); } -function addAuthorWithId(authorId) { +function createPersonFieldset(personPrefix, legend) { + // Creates a fieldset containing inputs for informations about a person var fieldset = document.createElement("fieldset") - fieldset.classList.add("author"); - fieldset.id = `author_${authorId}` + fieldset.classList.add("person"); + fieldset.id = personPrefix; fieldset.innerHTML = ` - +- - + +
- - + +
- - + +
- - + +
`; - var authorsFieldset = document.querySelector('#authors'); - authorsFieldset.appendChild(fieldset); + return fieldset; } -function addAuthor() { - var authorId = getNbAuthors() + 1; +function addPersonWithId(container, legend, prefix, id) { + var fieldset = createPersonFieldset(`${prefix}_${id}`, `${legend} #${id}`); + + container.appendChild(fieldset); +} - addAuthorWithId(authorId); +function addPerson(legend, prefix) { + var container = document.querySelector(`#${prefix}_container`); + var personId = getNbPersons(prefix) + 1; - var nbAuthorsField = document.querySelector('#nbAuthors'); - nbAuthorsField.value = authorId; + addPersonWithId(container, legend, prefix, personId); + + var nbPersonsField = container.querySelector(`#${prefix}_nb`); + nbPersonsField.value = personId; } -function removeAuthor() { - var authorId = getNbAuthors(); +function removePerson(prefix) { + var personId = getNbPersons(prefix); - document.querySelector(`#author_${authorId}`).remove(); + document.querySelector(`#${prefix}_${personId}`).remove(); - var nbAuthorsField = document.querySelector('#nbAuthors'); - nbAuthorsField.value = authorId-1; + var nbPersonsField = document.querySelector(`#${prefix}_nb`); + nbPersonsField.value = personId-1; +} + +// Names of codemeta properties with a matching HTML field name +const directCodemetaFields = [ + 'codeRepository', + 'contIntegration', + 'dateCreated', + 'datePublished', + 'dateModified', + 'issueTracker', + 'name', + 'version', +]; + +function generatePerson(idPrefix) { + return { + "@type": "Person", + "givenName": getIfSet(`#${idPrefix}_givenName`), + "familyName": getIfSet(`#${idPrefix}_familyName`), + "email": getIfSet(`#${idPrefix}_email`), + "@id": getIfSet(`#${idPrefix}_id`), + } +} + +function generatePersons(prefix) { + var persons = []; + var nbPersons = getNbPersons(prefix); + + for (let personId = 1; personId <= nbPersons; personId++) { + persons.push(generatePerson(`${prefix}_${personId}`)); + } + + return persons; } function generateCodemeta() { var inputForm = document.querySelector('#inputForm'); var codemetaText; if (inputForm.checkValidity()) { - authors = []; - - var nbAuthors = getNbAuthors(); - - for (let authorId = 1; authorId <= nbAuthors; authorId++) { - authors.push({ - "@type": "Person", - "givenName": getIfSet(`#author_givenName_${authorId}`), - "familyName": getIfSet(`#author_familyName_${authorId}`), - "email": getIfSet(`#author_email_${authorId}`), - "@id": getIfSet(`#author_id_${authorId}`), - }) - } - var doc = { "@context": "https://doi.org/10.5063/schema/codemeta-2.0", "@type": "SoftwareSourceCode", - "name": getIfSet('#name'), - "codeRepository": getIfSet('#codeRepository'), - "dateCreated": getIfSet('#dateCreated'), "license": "https://spdx.org/licenses/" + getIfSet('#license'), - "author": authors, }; + directCodemetaFields.forEach(function (item, index) { + doc[item] = getIfSet('#' + item) + }); + + doc = Object.assign(doc, { + "author": generatePersons('author'), + "contributor": generatePersons('contributor'), + }); + codemetaText = JSON.stringify(doc, null, 4); } else { codemetaText = "invalid input (see error above)"; inputForm.reportValidity(); } document.querySelector('#codemetaText').textContent = codemetaText; } -function initAuthors() { - var nbAuthors = getNbAuthors(); +function initPersons(prefix, legend) { + var nbAuthors = getNbPersons(prefix); + var personContainer = document.querySelector(`#${prefix}_container`) - for (let authorId = 1; authorId <= nbAuthors; authorId++) { - addAuthorWithId(authorId); + for (let personId = 1; personId <= nbAuthors; personId++) { + addPersonWithId(personContainer, legend, prefix, personId); } } function initCallbacks() { - document.querySelector('#addAuthor') - .addEventListener('click', addAuthor); - document.querySelector('#removeAuthor') - .addEventListener('click', removeAuthor); document.querySelector('#generateCodemeta') .addEventListener('click', generateCodemeta); document.querySelector('#inputForm') .addEventListener('change', generateCodemeta); - initAuthors() + initPersons('author', 'Author'); + initPersons('contributor', 'Contributor'); } diff --git a/style.css b/style.css index 09a12fd..01647cf 100644 --- a/style.css +++ b/style.css @@ -1,14 +1,23 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ -#authors .author { +.person { display: inline-block; } +#fieldsetSoftwareItself, #fieldsetDevelopmentCommunity, #fieldsetCurrentVersion { + display: inline-block; +} + +input[type=URL] { + /* URLs are longer than the other fields */ + min-width: 20em; +} + #codemetaText { width: 100%; }