Co-authored-by: Adam Bem <adam.bem@zoho.eu> Reviewed-on: #216 Reviewed-by: Mikolaj Widla <widlam@noreply.example.com>
		
			
				
	
	
		
			123 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
const tools = new Map();
 | 
						|
 | 
						|
/**
 | 
						|
 * Get address of Mock Services
 | 
						|
 *
 | 
						|
 * @function
 | 
						|
 * @name getMockHost
 | 
						|
 * @kind function
 | 
						|
 * @returns {string}
 | 
						|
 */
 | 
						|
function getMockHost() {
 | 
						|
    return window.location.protocol + "//" + window.location.hostname + ":8097";
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Function called after page is loaded
 | 
						|
 * 
 | 
						|
 * @function
 | 
						|
 * @name init
 | 
						|
 * @kind function
 | 
						|
 * @returns {void}
 | 
						|
 */
 | 
						|
function init() {
 | 
						|
    
 | 
						|
    tools.set("xpath", "tools/xpath.html");
 | 
						|
    tools.set("xsd", "tools/xsd.html");
 | 
						|
    tools.set("xslt", "tools/xslt.html");
 | 
						|
    tools.set("xmlform", "tools/xmlFormatter.html");
 | 
						|
    tools.set("jsonform", "tools/jsonFormatter.html");
 | 
						|
    tools.set("mock", "tools/mock.html");
 | 
						|
 | 
						|
    changeActiveTools('XML');
 | 
						|
    var toolUrl = window.location.search.substring(1);
 | 
						|
    if (tools.has(toolUrl))
 | 
						|
        changeTool(toolUrl, false);
 | 
						|
    else
 | 
						|
        loadLastPage();
 | 
						|
    
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Function that updates list of tools depending on chosen category
 | 
						|
 * 
 | 
						|
 * @function
 | 
						|
 * @name changeActiveTools
 | 
						|
 * @kind function
 | 
						|
 * @param {any} activeClass class of elements that have to be shown
 | 
						|
 * @param {any} activeCategoryButton class of category button that has to be active
 | 
						|
 */
 | 
						|
function changeActiveTools(activeCategoryButton) {
 | 
						|
    let toolList = document.getElementById("toolList").children;
 | 
						|
    let categoryToClass = new Map([["XML",  "xmlTool"],
 | 
						|
                                   ["JSON", "jsonTool"],
 | 
						|
                                   ["REST", "restTool"]]);
 | 
						|
    
 | 
						|
    let activeClass = categoryToClass.get(activeCategoryButton.toUpperCase());
 | 
						|
    if(activeClass == null) return;
 | 
						|
 | 
						|
    for (i = 0; i < toolList.length; i++) {
 | 
						|
        if (toolList[i].classList.contains(activeClass))
 | 
						|
            toolList[i].style.display = "block";
 | 
						|
        else
 | 
						|
            toolList[i].style.display = "none";
 | 
						|
    }
 | 
						|
 | 
						|
    let categoryList = document.getElementById("menu").children;
 | 
						|
 | 
						|
    for (i = 0; i < categoryList.length; i++) {
 | 
						|
        if (categoryList[i].innerText == activeCategoryButton)
 | 
						|
            categoryList[i].classList.add("active");
 | 
						|
        else
 | 
						|
            categoryList[i].classList.remove("active");
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/**
 | 
						|
 * This function changes active tool.
 | 
						|
 * Optional updateURL can be set to false to stop changing URL. 
 | 
						|
 * This helps avoiding endless reload loop when loading page.
 | 
						|
 * 
 | 
						|
 * @function
 | 
						|
 * @name changeTool
 | 
						|
 * @kind function
 | 
						|
 * @param {any} tool
 | 
						|
 * @param {boolean} updateURL?
 | 
						|
 * @returns {void}
 | 
						|
 */
 | 
						|
function changeTool(tool, updateURL = true) {
 | 
						|
    if (! tools.has(tool)) return;
 | 
						|
    const url = tools.get(tool);
 | 
						|
    if (updateURL) document.location.search = tool;
 | 
						|
    
 | 
						|
 | 
						|
    switch (tool) { // XML category is default.
 | 
						|
        case "jsonform":
 | 
						|
            changeActiveTools('JSON');
 | 
						|
            break;
 | 
						|
        case "mock":
 | 
						|
            changeActiveTools('REST');
 | 
						|
            break;
 | 
						|
    }
 | 
						|
 | 
						|
    localStorage.setItem("lastPage", tool);
 | 
						|
    document.getElementById("iframe").src = url;
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Function that loads last used tool and sets active category accordingly
 | 
						|
 * 
 | 
						|
 * @function
 | 
						|
 * @name loadLastPage
 | 
						|
 * @kind function
 | 
						|
 * @returns {void}
 | 
						|
 */
 | 
						|
function loadLastPage() {
 | 
						|
    var lastPage = localStorage.getItem("lastPage");
 | 
						|
    if (lastPage == null) {
 | 
						|
        lastPage = "xpath";
 | 
						|
    }
 | 
						|
    changeTool(lastPage, false);
 | 
						|
} |