Co-authored-by: mikolaj widla <mikolaj.widla@gmail.com> Co-authored-by: Adam Bem <adam.bem@zoho.eu> Co-authored-by: Adam Bem <bema@noreply.example.com> Co-authored-by: Artur Kołecki <koleckiartur@icloud.com> Reviewed-on: R11/release11-tools-web#82
205 lines
6.6 KiB
JavaScript
205 lines
6.6 KiB
JavaScript
var defaultStrings = [];
|
|
const color_grey = "#6b6b6b";
|
|
const color_red = "#ff8f8f";
|
|
|
|
//Remove default text and set color to black
|
|
function clearDefaultContent(element, text) {
|
|
if (element.value == text) {
|
|
element.value = "";
|
|
element.style.color = "#000000";
|
|
element.style.backgroundColor = "#ffffff";
|
|
}
|
|
}
|
|
|
|
function clearDataField(){
|
|
document.getElementById("xmlArea").value = "";
|
|
document.getElementById("transformArea").value = "";
|
|
}
|
|
|
|
function fillDefaultXML(element) {
|
|
if(element.classList.contains("active")){
|
|
const serverAddress = window.location.protocol + "//" + window.location.hostname + ":8086";
|
|
clearDefaultContent(document.getElementById("xmlArea"), "Insert XML here");
|
|
fetch(serverAddress + "/assets/samples/sampleXml.xml")
|
|
.then(response => response.text())
|
|
.then((exampleData) => {
|
|
document.getElementById("xmlArea").value = exampleData;
|
|
})
|
|
}
|
|
}
|
|
|
|
//Set default text in grey
|
|
function setDefaultContent(element, text) {
|
|
if (element.value == "") {
|
|
var id = element.getAttribute('id');
|
|
if (!defaultStrings.includes(text)) {
|
|
defaultStrings.push(text);
|
|
}
|
|
if (id == "xmlArea") {
|
|
element.style.color = color_grey;
|
|
element.value = text;
|
|
}
|
|
if (id == "transformArea") {
|
|
element.style.color = color_grey;
|
|
element.value = text;
|
|
}
|
|
if (id == "jsonArea") {
|
|
element.style.color = color_grey;
|
|
element.value = text;
|
|
}
|
|
}
|
|
}
|
|
|
|
function hideList(collList) {
|
|
for (i = 0; i < collList.length; i++) {
|
|
if (collList[i].nextElementSibling !== null) {
|
|
collList[i].nextElementSibling.style.maxHeight = null;
|
|
collList[i].nextElementSibling.classList.toggle("collapsibleDataExpanded", false);
|
|
}
|
|
collList[i].style.display = 'none';
|
|
collList[i].classList.remove("collapsibleActive");
|
|
}
|
|
}
|
|
|
|
function checkDefault(text){
|
|
return defaultStrings.includes(text);
|
|
}
|
|
|
|
function showList(collList) {
|
|
for (i = 0; i < collList.length; i++) {
|
|
collList[i].style.display = 'block';
|
|
}
|
|
}
|
|
|
|
function smoothFoldElement(element, toogleState, toggleParrent){
|
|
if (toogleState) {
|
|
console.log("DUPA");
|
|
if(toggleParrent){
|
|
element.parentElement.style.maxHeight = "0px";
|
|
}
|
|
|
|
element.classList.toggle("active", false);
|
|
var subLists = collapsibleData.getElementsByClassName("collapsibleData");
|
|
for (j = 0; j < subLists.length; j++) {
|
|
subLists[j].style.maxHeight = null;
|
|
}
|
|
} else {
|
|
collapsibleData.parentElement.style.maxHeight = (collapsibleData.parentElement.scrollHeight) + "px";
|
|
collapsibleData.classList.toggle("active", true);
|
|
if (collapsibleData.parentElement.classList.contains("collapsibleData") && collapsibleData.parentElement.classList.contains("active")) {
|
|
collapsibleData.parentElement.style.maxHeight = (collapsibleData.parentElement.scrollHeight + collapsibleData.scrollHeight) + "px";
|
|
}
|
|
}
|
|
}
|
|
|
|
//Set tooltip info, function is called by onClick handlers
|
|
function refreshTooltip() {
|
|
var resizeList = document.getElementsByClassName("collapsibleData");
|
|
console.log("collDataList: " + resizeList.length)
|
|
document.getElementById("processorTooltipInfo").innerText = procInfo;
|
|
document.getElementById("xsltelementsheader").innerText = XSLTheader;
|
|
}
|
|
|
|
|
|
|
|
function performRequest(endpoint, checkXML, checkTransform){
|
|
var xmlData = document.getElementById(sourceId).value.trim();
|
|
var transformData = document.getElementById(targetId).value.trim();
|
|
|
|
var port = 8081
|
|
if (getProcessor() == "libxml") {
|
|
port = 8082
|
|
}
|
|
|
|
var empty = false;
|
|
if (defaultStrings.includes(xmlData) && checkXML) {
|
|
document.getElementById(sourceId).style.backgroundColor = color_red;
|
|
xmlData = "";
|
|
empty = true;
|
|
}
|
|
if (defaultStrings.includes(transformData) && checkTransform) {
|
|
document.getElementById(targetId).style.backgroundColor = color_red;
|
|
empty = true;
|
|
}
|
|
if (!empty) {
|
|
restRequest(port, endpoint, xmlData, transformData).then(function(result) {
|
|
document.getElementById("resultArea").value = result.result;
|
|
document.getElementById("procinfo").innerText = ' Computed using '.concat(" ", result.processor);
|
|
if (result.status = "OK") {
|
|
document.getElementById("procinfo").innerText = document.getElementById("procinfo").innerText.concat(" in ", result.time, "ms");
|
|
procinfo.style.color = "#30aa58";
|
|
} else {
|
|
procinfo.style.color = "#aa3030";
|
|
}
|
|
});
|
|
}else{
|
|
document.getElementById("resultArea").value = "No data provided!";
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
function performFormatRequest(endpoint, checkXML, sourceId, targetId){
|
|
const port = 8082;
|
|
var xmlData = document.getElementById(sourceId).value.trim();
|
|
|
|
var empty = false;
|
|
if (defaultStrings.includes(xmlData) && checkXML) {
|
|
document.getElementById(sourceId).style.backgroundColor = color_red;
|
|
xmlData = "";
|
|
empty = true;
|
|
}
|
|
|
|
if (!empty) {
|
|
restRequest(port, endpoint, xmlData, "").then(function(result) {
|
|
document.getElementById(targetId).value = result.result;
|
|
|
|
});
|
|
}else{
|
|
document.getElementById(targetId).value = "No data provided!";
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//Form REST request, send and return received data
|
|
async function restRequest(port, endpoint, xmlData, transformData) {
|
|
const escapeChar = "specialEscapeChar";
|
|
|
|
const addr = window.location.protocol + "//" + window.location.hostname + ":" + port + "/" + endpoint;
|
|
|
|
if(defaultStrings.includes(xmlData)){
|
|
xmlData = "<empty/>";
|
|
}
|
|
|
|
// var data = xmlData.concat(escapeChar, transformData);
|
|
|
|
// const url = addr.concat("?escapechar=", escapeChar, "&processor=", getProcInfo());
|
|
|
|
var jsonData = JSON.stringify({
|
|
"data" : xmlData,
|
|
"process" : transformData,
|
|
"processor" : getProcessor(),
|
|
"version" : getVersion()
|
|
});
|
|
// console.log(jsonData);
|
|
var init = {
|
|
headers: new Headers({
|
|
}),
|
|
body: jsonData,
|
|
// body: data,
|
|
method: "POST"
|
|
};
|
|
var request = new Request(addr, init);
|
|
|
|
|
|
var result = await fetch(request).then(response => {
|
|
return response.text().then(function(text) {
|
|
return JSON.parse(text);
|
|
});
|
|
|
|
});
|
|
return result;
|
|
}
|