" id="requirementText" rows="10" cols="30" class="required" value="" type="text"></textarea>
</p>
<p>
<b>*Make sure to add keyword entered in KEYWORD field in the requirement in CAPS.</b>
</p>
</fieldset>
</div>
</div>
</form>
<button style="background-color: #008CBA; color: white; border: 2px solid black; width: 100%; font-size: 24px;" onclick="formDataToObject(document.getElementById('req'))">Convert to RST</button>
<textarea style="width: 100%; margin: 8px 0; box-sizing: border-box; padding: 12px 20px;" rows="10" cols="30" id="formatted" readonly>
Formatted RST will Show here.
</textarea>
<script>
//define template
var template = $('#sections .section:first').clone();
//define counter
var sectionsCount = 1;
var url = "https://wiki.onap.org/download/attachments/60887371/needs.json?api=v2"
$('body').on('click', '#generate', function(e) {
myFunction();
});
function myFunction() {
get(url).then(JSON.parse).then(function(data) {
console.log("It worked!", data);
return data;
}).catch(function(err) {
// Instead, this happens:
console.log("It failed!", err);
}).then(function(data) {
var parsedData = parseIDs(data);
var finalIDs = createIDs(1, parsedData);
console.log(finalIDs);
addSection(finalIDs);
})
}
function get(url) {
// Return a new promise.
return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
resolve(req.response);
} else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(req.statusText));
}
};
// Handle network errors
req.onerror = function() {
reject(Error("Network Error"));
};
// Make the request
req.send();
});
}
function parseIDs(jsonText) {
var result = new Set();
var version = jsonText.current_version;
result.add(Object.keys(jsonText["versions"][version]["needs"]));
console.log(result);
return result;
}
function createIDs(numberOfIDs, parsedData) {
var addedIDs = [];
for (x = 0; x < numberOfIDs; x++) {
var new_id = "";
do {
new_id = (Math.floor(Math.random() * 999999));
if (new_id < 100000) {
new_id = ('000000' + new_id).slice(-6);
}
new_id = ("R-" + new_id);
}
while (parsedData.has(new_id));
addedIDs[x] = new_id;
}
return addedIDs;
}
function addSection(finalIDs) {
document.getElementById('id').value = finalIDs;
}
function formDataToObject(elForm) {
//if (!elForm instanceof Element) return;
var fields = elForm.querySelectorAll('input, select, textarea'),
o = {};
for (var i = 0, imax = fields.length; i < imax; ++i) {
var field = fields[i],
sKey = field.name || field.id;
if (field.type === 'button' || field.type === 'image' || field.type === 'submit' || !sKey) continue;
switch (field.type) {
case 'checkbox':
o[sKey] = +field.checked;
break;
case 'radio':
if (o[sKey] === undefined) o[sKey] = '';
if (field.checked) o[sKey] = field.value;
break;
case 'select-multiple':
var a = [];
for (var j = 0, jmax = field.options.length; j < jmax; ++j) {
if (field.options[j].selected) a.push(field.options[j].value);
}
o[sKey] = a;
break;
default:
o[sKey] = field.value;
}
}
//formcheck();
buildRST(o);
return o;
}
function buildRST(o) {
var rst = ".. req:: |