send_sample_request_backup.js 5.2 KB

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