//TODO: Add delete buttons for messages //TODO: Save button deactivation after swap, post or delete //TODO: It sends request, gets responds but doesnt run a function!! //TODO: Warning is displayed twice var clientUUID = ''; var advancedDisplayed = false; var json = {}; var jsonIndex = 0; var lastId = 1; var htable_row = 0; var host = getDomain(); var dataModified = false; // TODO: Remove discard changes const addMessageName = 'addMessage'; const loadMessageName = 'changeMessage'; const removeMessageName = 'removeMessage'; // TODO: Add last given command const C_UUID = 'mock-uuid'; const C_ID = 'last-displayed-id'; const C_ADV = 'advanced-mode'; const setModified = function(){ setDataModified(); } const setOrigin = function(){ setDataOrigin(); } const getUpdate = function(){ updateData(); } const dataRefresh = function(){ getData(); } $('#iconPlus').click(function(){callAddMessage()}); $('#btn-addRow').click(function(){addRow()}); //TODO remove later save onclick init $('#btn-save').click(getUpdate); function getData(){ $.getJSON(host + '/api/mock/'+clientUUID, function(data) { json = data; checkUuid(); console.log(JSON.stringify(json)); console.log("Json received"); refreshData(); }); } function checkUuid(){ if(clientUUID == null || clientUUID == undefined || clientUUID == ''){ clientUUID = json[0].clientUUID; setCookie(); } } function getDomain(){ var url = window.location.href; var arr = url.split("/"); var result = arr[0] + "//" + arr[2]; return result; } function setDataModified(){ if(dataModified) return; dataModified = true; $('#btn-save').removeClass('btn-inactive'); $('#btn-save').addClass('btn-action'); $('#btn-save').click(getUpdate); } //Adding change listener to fields $('.field').change(setModified); function setDataOrigin(){ dataModified = false; $('#btn-save').addClass('btn-inactive'); $('#btn-save').removeClass('btn-action'); $('#btn-save').off(); } const idToDisplay = function(){ let defaultId = json[0].mockedResponseId; if(lastId == undefined || lastId == null) return defaultId; for(let i=0; i 0){ jsonIndex -= 1; return json[jsonIndex].mockedResponseId; } return defaultId; } function refreshData(){ fillMessageList(); console.log("List initiated"); let id = idToDisplay(); console.log('Loading message of id: ' + id); loadMessage(id); console.log("Message loaded"); } function setCookie(){ document.cookie = C_UUID + '=' +clientUUID+';' document.cookie = C_ID + '=' + lastId; document.cookie = C_ADV + '=' + advancedVisibility; } function loadCookies(){ clientUUID = getCookie(C_UUID); lastId = getCookie(C_ID); advancedDisplayed = getCookie(C_ADV) == 'true'; } function getCookie(cname) { var name = cname + '='; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for(var i = 0; i ' + link + ''); $('#httpStatus').val(httpStatus); $('#typeSelector').val(mediaType); $('#bodyEditor').val(body); $('#mockedMessageId').html(id); } function createLink(uuid, id){ var link = host + '/api/mock/r/'+uuid+'/'+id; return link; } function fillHeaderTable(headers){ var innerHTML = $('#httpStatusValues').html(); innerHTML += generateHeaderTable(headers); $('#httpStatusValues').html(innerHTML); $('.tableField').change(function(){setDataModified()}); } //TODO: Add addRow() to generate new rows and populate them with data function generateHeaderTable(headers){ let count = 0; let innerHTML = ''; for(var item in headers){ if( headers.hasOwnProperty(item) ) count++; } var keys = new Array(count); var values = new Array(count); let index = 0; for(var key in Object.keys(headers)){ keys[index++]=Object.keys(headers)[key]; } index = 0; for(var val in headers){ values[index++]=headers[val]; } for(let i=0; i' + '' + '' + '' + '' + '×' + ''; htable_row++; } return innerHTML; } function removeRow(row){ $('#hrow' + row).remove(); setDataModified(); } function addRow(){ var table = $('#httpStatusValues'); var hkey = $('#headerKeyInput'); var hval = $('#headerValueInput'); if(hkey.val() == 'key' || hkey.val() == '' || hval.val() == 'value' || hval.val() == '') return; var innerHtml = '' + '' + '' + '' + '' + 'X' + ''; htable_row++; table.append(innerHtml); hkey.val(''); hval.val(''); setDataModified(); } function fillMessageList(){ $("#listItems").html(''); var innerHTML = ''; for(let i=0; i' + '' + '' + '
Id: '+ id +'
Http-status: '+ httpStatus +'
×
' + '
'; return innerHTML; } const onbuild = function(){ loadCookies(); getData(); if(advancedDisplayed) { changeAdvancedVisibility(); } } $(document).ready(onbuild); function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function generateJson(){ var newJson = { clientUUID: json[jsonIndex].clientUUID, mockedResponseId: json[jsonIndex].mockedResponseId, mediaType: $('#typeSelector').val(), messageBody: $('#bodyEditor').val(), httpStatus: $('#httpStatus').val(), httpHeaders: {}, }; newJson['httpHeaders'] = convertTableToJson(); console.log(JSON.stringify(newJson, null, 2)); json[jsonIndex] = newJson; return newJson; } function convertTableToJson(){ var rows = $('.httpStatusValue'); console.log("Rows: "+rows.length); var obj = {}; var key; for(let i=0; i