diff --git a/codemeta_generation.js b/codemeta_generation.js --- a/codemeta_generation.js +++ b/codemeta_generation.js @@ -39,11 +39,9 @@ 'identifier', 'description', 'applicationCategory', - //keywords TODO:keywords array 'releaseNotes', 'funding', 'runtimePlatform', - //softwareRequiremnts, 'operatingSystem', 'developmentStatus', //relatedLink @@ -65,6 +63,7 @@ 'affiliation' ]; + function generatePerson(idPrefix) { var doc = { "@type": "Person", @@ -88,6 +87,21 @@ return persons; } +function generateStringArray(idPrefix,prefix) { + return getIfSet(`#${idPrefix}_${prefix}`); +} + +function generateStringArrays(prefix) { + var array = []; + var nbArray = getNbStringArray(prefix); + + for (let ArrayId = 1; ArrayId <= nbArray; ArrayId++) { + array.push(generateStringArray(`${prefix}_${ArrayId}`,prefix)); + } + + return array; +} + function generateCodemeta() { var inputForm = document.querySelector('#inputForm'); var codemetaText, errorHTML; @@ -101,13 +115,16 @@ // Generate most fields directCodemetaFields.forEach(function (item, index) { - doc[item] = getIfSet('#' + item) + doc[item] = getIfSet('#' + item); }); // Generate dynamic fields doc = Object.assign(doc, { "author": generatePersons('author'), "contributor": generatePersons('contributor'), + "keyword" : generateStringArrays('keyword'), + "software_requirements" : generateStringArrays('software_requirements'), + "related_links" : generateStringArrays('related_links'), }); codemetaText = JSON.stringify(doc, null, 4); @@ -143,6 +160,20 @@ }) } +function importStringArray(prefix, legend, docs) { + if (docs === 'undefined') { + return; + } + var type = 'text'; + if (prefix == 'related_links') {type = 'url';} + docs.forEach(function (doc, index) { + var idPrefix = addStringArray(prefix, legend, 'place_holder', type); + setIfDefined(`#${prefix}_${idPrefix}_${prefix}`, doc); + }); + +} + + function importCodemeta() { var inputForm = document.querySelector('#inputForm'); var codemetaText = document.querySelector('#codemetaText').innerText; @@ -168,6 +199,9 @@ importPersons('author', 'Author', doc['author']) importPersons('contributor', 'Contributor', doc['contributor']) + importStringArray('keyword', 'Keyword', doc['keyword']) + importStringArray('related_links', 'Related links', doc['related_links']) + importStringArray('software_requirements', 'Software Requirements', doc['software_requirements']) setError(""); } diff --git a/dynamic_form.js b/dynamic_form.js --- a/dynamic_form.js +++ b/dynamic_form.js @@ -7,6 +7,7 @@ "use strict"; +/////////////////BEGIN-PERSONS function createPersonFieldset(personPrefix, legend) { // Creates a fieldset containing inputs for informations about a person var fieldset = document.createElement("fieldset") @@ -37,7 +38,7 @@
-
`; @@ -89,11 +90,84 @@ removePerson(prefix) } } +/////////////////END-PERSONS + +/////////////////BEGIN-STRING-ARRAYS +function createStringArrayFieldset(prefix, legend, postfix, placeholder, type) { + // Creates a fieldset containing inputs for informations about an array field + var fieldset = document.createElement(`fieldset_${prefix}`) + fieldset.classList.add(prefix); + fieldset.id = prefix; + + fieldset.innerHTML = ` + ++ + +
+ `; + + return fieldset; +} + +function addStringArrayWithId(container, prefix, legend, id, placeholder, type) { + var fieldset = createStringArrayFieldset(`${prefix}_${id}`, `${legend} #${id}`,`${prefix}`,`${placeholder}`,`${type}` ); + + container.appendChild(fieldset); +} + +function addStringArray(prefix, legend, placeholder, type) { + //console.log(type) + //console.log(placeholder) + //console.log(legend) + //console.log(prefix) + var container = document.querySelector(`#${prefix}_container`); + //console.log(container) + var ArrayId = getNbStringArray(prefix) + 1; + + addStringArrayWithId(container, prefix, legend, ArrayId, placeholder, type); + + setNbStringArray(prefix, ArrayId); + + return ArrayId; +} + +function removeStringArray(prefix) { + var ArrayId = getNbStringArray(prefix); + + document.querySelector(`#${prefix}_${ArrayId}`).remove(); + + setNbPersons(prefix, ArrayId-1); +} + +// Initialize a group of keywords on page load. +// Useful if the page is reloaded. +function initStringArrays(prefix, legend) { + var nbArray = getNbStringArray(prefix); + var ArrayContainer = document.querySelector(`#${prefix}_container`) + + for (let ArrayId = 1; ArrayId <= nbArray; ArrayId++) { + addKeywordWithId(ArrayContainer, prefix, legend, ArrayId); + } +} + +function removeStringArrays(prefix) { + var nbArray = getNbStringArray(prefix); + var ArrayContainer = document.querySelector(`#${prefix}_container`) + + for (let ArrayId = 1; ArrayId <= nbArray; ArrayId++) { + removeStringArray(prefix) + } +} +/////////////////END-STING-ARRAYS function resetForm() { removePersons('author'); removePersons('contributor'); - + removeStringArrays('keyword'); + removeStringArrays('software_requirements'); + removeStringArrays('related_links'); // Reset the form after deleting elements, so nbPersons doesn't get // reset before it's read. document.querySelector('#inputForm').reset(); @@ -117,4 +191,7 @@ initPersons('author', 'Author'); initPersons('contributor', 'Contributor'); + initStringArrays('keyword', 'Keyword'); + initStringArrays('software_requirements', 'Software Requirements'); + initStringArrays('related_links', 'Related Links'); } diff --git a/index.html b/index.html --- a/index.html +++ b/index.html @@ -135,11 +135,48 @@ "Change log: this and that; Bugfixes: that and this." > - - + + + + + diff --git a/utils.js b/utils.js --- a/utils.js +++ b/utils.js @@ -17,6 +17,16 @@ nbField.value = nb; } +function getNbStringArray(prefix) { + var nbField = document.querySelector(`#${prefix}_nb`); + return parseInt(nbField.value, 10); +} + +function setNbStringArray(prefix, nb) { + var nbField = document.querySelector(`#${prefix}_nb`); + nbField.value = nb; +} + function setError(msg) { document.querySelector("#errorMessage").innerHTML = msg; }