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