diff --git a/resources/public/css/screen.css b/resources/public/css/screen.css index 47ffd2d..df85a8b 100644 --- a/resources/public/css/screen.css +++ b/resources/public/css/screen.css @@ -13,4 +13,13 @@ html, body { width: 340px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; white-space: pre; +} + +.textOutputContainer { + overflow-x: scroll; +} + +.htmlOutputContainer pre { + overflow-x: scroll; + word-wrap: normal; } \ No newline at end of file diff --git a/resources/public/js/site.js b/resources/public/js/site.js index e69de29..aef920a 100644 --- a/resources/public/js/site.js +++ b/resources/public/js/site.js @@ -0,0 +1,68 @@ +function isHtmlResponse(xhr) { + var contentType = xhr.getResponseHeader('content-type'); + return contentType.indexOf('text/html') !== -1; +} + +function isTextResponse(xhr) { + var contentType = xhr.getResponseHeader('content-type'); + return contentType.indexOf('text/plain') !== -1; +} + +function isJsonResponse(xhr) { + var contentType = xhr.getResponseHeader('content-type'); + return contentType.indexOf('application/json') !== -1; +} + +$(document).ready(function() { + $('form.api-form').submit(function(e) { + e.preventDefault(); + + var form = $(this); + + var params = {}; + form.find('input, select, textarea').filter('[data-fieldname]').each(function() { + var element = $(this); + var fieldName = element.data('fieldname'); + var value = element.val(); // TODO: handle other types of elements (e.g. checkbox, radiobutton) + params[fieldName] = value; + }); + + var textOutputContainer = form.siblings('pre.textOutputContainer'); + var htmlOutputContainer = form.siblings('div.htmlOutputContainer'); + var errorContainer = form.siblings('div.methodErrorContainer'); + + var apiEndpoint = form.data('api-endpoint'); + + textOutputContainer.text(''); + htmlOutputContainer.html(''); + errorContainer.html(''); + errorContainer.hide(); + + var url = context + 'api' + apiEndpoint + '?' + jQuery.param(params); + $.get(url) + .done( + function (data, status, xhr) { + if (isHtmlResponse(xhr)) { + textOutputContainer.hide(); + htmlOutputContainer.html(data); + htmlOutputContainer.show(); + } else { + htmlOutputContainer.hide(); + textOutputContainer.text(data); + textOutputContainer.show(); + } + }) + .fail( + function (response) { + textOutputContainer.hide(); + htmlOutputContainer.hide(); + + errorContainer.html( + 'HTTP ' + response.status + ' ' + response.statusText + ': ' + + response.responseText + ); + errorContainer.show(); + }); + }) + +}); diff --git a/resources/views/methods/text.html b/resources/views/methods/text.html index 196656d..d179277 100644 --- a/resources/views/methods/text.html +++ b/resources/views/methods/text.html @@ -1 +1,36 @@ -
TODO: content for "Text –> ASCII" section
\ No newline at end of file + + + + + \ No newline at end of file