send_sample_request.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. define([
  2. 'jquery',
  3. 'lodash'
  4. ], function($, _) {
  5. var initDynamic = function() {
  6. // Button send
  7. $(".sample-request-send").off("click");
  8. $(".sample-request-send").on("click", function(e) {
  9. e.preventDefault();
  10. var $root = $(this).parents("article");
  11. var group = $root.data("group");
  12. var name = $root.data("name");
  13. var version = $root.data("version");
  14. sendSampleRequest(group, name, version, $(this).data("sample-request-type"));
  15. });
  16. // Button clear
  17. $(".sample-request-clear").off("click");
  18. $(".sample-request-clear").on("click", function(e) {
  19. e.preventDefault();
  20. var $root = $(this).parents("article");
  21. var group = $root.data("group");
  22. var name = $root.data("name");
  23. var version = $root.data("version");
  24. clearSampleRequest(group, name, version);
  25. });
  26. }; // initDynamic
  27. function sendSampleRequest(group, name, version, type)
  28. {
  29. var $root = $('article[data-group="' + group + '"][data-name="' + name + '"][data-version="' + version + '"]');
  30. // Optional header
  31. var header = {};
  32. $root.find(".sample-request-header:checked").each(function(i, element) {
  33. var group = $(element).data("sample-request-header-group-id");
  34. $root.find("[data-sample-request-header-group=\"" + group + "\"]").each(function(i, element) {
  35. var key = $(element).data("sample-request-header-name");
  36. var value = element.value;
  37. if ( ! element.optional && element.defaultValue !== '') {
  38. value = element.defaultValue;
  39. }
  40. header[key] = value;
  41. });
  42. });
  43. // create JSON dictionary of parameters
  44. var param = {};
  45. var paramType = {};
  46. $root.find(".sample-request-param:checked").each(function(i, element) {
  47. var group = $(element).data("sample-request-param-group-id");
  48. $root.find("[data-sample-request-param-group=\"" + group + "\"]").not(function(){
  49. return $(this).val() == "" && $(this).is("[data-sample-request-param-optional='true']");
  50. }).each(function(i, element) {
  51. var key = $(element).data("sample-request-param-name");
  52. var value = element.value;
  53. if ( ! element.optional && element.defaultValue !== '') {
  54. value = element.defaultValue;
  55. }
  56. param[key] = value;
  57. paramType[key] = $(element).next().text();
  58. });
  59. });
  60. // grab user-inputted URL
  61. var url = $root.find(".sample-request-url").val();
  62. // Insert url parameter
  63. var pattern = pathToRegexp(url, null);
  64. var matches = pattern.exec(url);
  65. for (var i = 1; i < matches.length; i++) {
  66. var key = matches[i].substr(1);
  67. if (param[key] !== undefined) {
  68. url = url.replace(matches[i], encodeURIComponent(param[key]));
  69. // remove URL parameters from list
  70. delete param[key];
  71. }
  72. } // for
  73. $root.find(".sample-request-response").fadeTo(250, 1);
  74. $root.find(".sample-request-response-json").html("Loading...");
  75. refreshScrollSpy();
  76. _.each( param, function( val, key ) {
  77. var t = paramType[ key ].toLowerCase();
  78. if ( t === 'object' || t === 'array' ) {
  79. try {
  80. param[ key ] = JSON.parse( val );
  81. } catch (e) {
  82. }
  83. }
  84. });
  85. // send AJAX request, catch success or error callback
  86. var ajaxRequest = {
  87. url : url,
  88. headers : header,
  89. data : param,
  90. type : type.toUpperCase(),
  91. success : displaySuccess,
  92. error : displayError
  93. };
  94. $.ajax(ajaxRequest);
  95. function displaySuccess(data, status, jqXHR) {
  96. var jsonResponse;
  97. try {
  98. jsonResponse = JSON.parse(jqXHR.responseText);
  99. jsonResponse = JSON.stringify(jsonResponse, null, 4);
  100. } catch (e) {
  101. jsonResponse = data;
  102. }
  103. $root.find(".sample-request-response-json").html(jsonResponse);
  104. refreshScrollSpy();
  105. };
  106. function displayError(jqXHR, textStatus, error) {
  107. var message = "Error " + jqXHR.status + ": " + error;
  108. var jsonResponse;
  109. try {
  110. jsonResponse = JSON.parse(jqXHR.responseText);
  111. jsonResponse = JSON.stringify(jsonResponse, null, 4);
  112. } catch (e) {
  113. jsonResponse = escape(jqXHR.responseText);
  114. }
  115. if (jsonResponse)
  116. message += "<br>" + jsonResponse;
  117. // flicker on previous error to make clear that there is a new response
  118. if($root.find(".sample-request-response").is(":visible"))
  119. $root.find(".sample-request-response").fadeTo(1, 0.1);
  120. $root.find(".sample-request-response").fadeTo(250, 1);
  121. $root.find(".sample-request-response-json").html(message);
  122. refreshScrollSpy();
  123. };
  124. }
  125. function clearSampleRequest(group, name, version)
  126. {
  127. var $root = $('article[data-group="' + group + '"][data-name="' + name + '"][data-version="' + version + '"]');
  128. // hide sample response
  129. $root.find(".sample-request-response-json").html("");
  130. $root.find(".sample-request-response").hide();
  131. // reset value of parameters
  132. $root.find(".sample-request-param").each(function(i, element) {
  133. element.value = "";
  134. });
  135. // restore default URL
  136. var $urlElement = $root.find(".sample-request-url");
  137. $urlElement.val($urlElement.prop("defaultValue"));
  138. refreshScrollSpy();
  139. }
  140. function refreshScrollSpy()
  141. {
  142. $('[data-spy="scroll"]').each(function () {
  143. $(this).scrollspy("refresh");
  144. });
  145. }
  146. function escapeHtml(str) {
  147. var div = document.createElement("div");
  148. div.appendChild(document.createTextNode(str));
  149. return div.innerHTML;
  150. }
  151. /**
  152. * Exports.
  153. */
  154. return {
  155. initDynamic: initDynamic
  156. };
  157. });