app/assets/javascripts/api_taster/app.js in api_taster-0.2.1 vs app/assets/javascripts/api_taster/app.js in api_taster-0.2.2

- old
+ new

@@ -9,10 +9,20 @@ storeFormActionFor: function(form) { ApiTaster.formAction = form.attr("action") }, restoreFormActionFor: function(form) { $(form).attr("action", ApiTaster.formAction); + }, + detectContentType: function(response) { + var contentType = response.getResponseHeader("Content-Type"); + var detectedContentType = null + + if (contentType.match(/application\/json/)) { + detectedContentType = 'json'; + } + + return detectedContentType; } }; $.fn.extend({ replaceUrlParams: function(params) { @@ -44,10 +54,18 @@ $(contentElement, container).hide(); $(contentElement + "[ref=" + $(this).attr("id") + "]", container).show(); }); }, + showNavTab: function(name) { + $("ul.nav-tabs li", this).removeClass("active"); + $("ul.nav-tabs li a#response-" + name, this).parent().show().addClass("active"); + + $("pre", this).hide(); + + return $("pre[ref=response-" + name + "]", this).show(); + }, displayOnlySelectedParamsFieldset: function() { $("fieldset", this).hide(); $("fieldset[ref=" + $("ul.nav-tabs li.active a").attr("id") + "]", this).show(); } }); @@ -84,13 +102,18 @@ if ($("#show-api-response-div:visible").length == 0) { $("#show-api-response-div").slideDown(100); } + switch (ApiTaster.detectContentType(xhr)) { + case "json": + $("#show-api-response-div").showNavTab("json").text( + JSON.stringify(JSON.parse(xhr.responseText), null, 2) + ); + break; + } + $("#show-api-response-div pre[ref=response-raw]").text(xhr.responseText); - $("#show-api-response-div pre[ref=response-json]").text( - JSON.stringify(JSON.parse(xhr.responseText), null, 2) - ); prettyPrint(); }); $("#show-api-response-div").enableNavTabsFor("pre");