mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-08 07:33:23 +03:00
Fallback to more widely supported JS feature sets. (#4961)
This commit is contained in:
parent
9ccdc4366f
commit
7a8fb262f2
|
@ -7,7 +7,7 @@ function normalizeHTTPHeader (str) {
|
||||||
.replace(/(Md5)/g, function ($1) { return 'MD5' })
|
.replace(/(Md5)/g, function ($1) { return 'MD5' })
|
||||||
}
|
}
|
||||||
|
|
||||||
let responseDisplay = 'data'
|
var responseDisplay = 'data'
|
||||||
const coreapi = window.coreapi
|
const coreapi = window.coreapi
|
||||||
const schema = window.schema
|
const schema = window.schema
|
||||||
|
|
||||||
|
@ -28,6 +28,39 @@ $('#language-control li').click(function (event) {
|
||||||
codeBlocks.filter('[data-language="' + language +'"]').removeClass("hide")
|
codeBlocks.filter('[data-language="' + language +'"]').removeClass("hide")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function formEntries (form) {
|
||||||
|
// Polyfill for new FormData(form).entries()
|
||||||
|
var formData = new FormData(form)
|
||||||
|
if (formData.entries !== undefined) {
|
||||||
|
return formData.entries()
|
||||||
|
}
|
||||||
|
|
||||||
|
var entries = []
|
||||||
|
|
||||||
|
for (var {name, type, value, files, checked, selectedOptions} of Array.from(form.elements)) {
|
||||||
|
if (!name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'file') {
|
||||||
|
for (var file of files) {
|
||||||
|
entries.push([name, file])
|
||||||
|
}
|
||||||
|
} else if (type === 'select-multiple' || type === 'select-one') {
|
||||||
|
for (var elm of Array.from(selectedOptions)) {
|
||||||
|
entries.push([name, elm.value])
|
||||||
|
}
|
||||||
|
} else if (type === 'checkbox') {
|
||||||
|
if (checked) {
|
||||||
|
entries.push([name, value])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
entries.push([name, value])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return entries
|
||||||
|
}
|
||||||
|
|
||||||
// API Explorer
|
// API Explorer
|
||||||
$('form.api-interaction').submit(function(event) {
|
$('form.api-interaction').submit(function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
@ -36,23 +69,23 @@ $('form.api-interaction').submit(function(event) {
|
||||||
const key = form.data("key");
|
const key = form.data("key");
|
||||||
var params = {};
|
var params = {};
|
||||||
|
|
||||||
const formData = new FormData(form.get()[0]);
|
const entries = formEntries(form.get()[0]);
|
||||||
for (var [paramKey, paramValue] of formData.entries()) {
|
for (var [paramKey, paramValue] of entries) {
|
||||||
var elem = form.find("[name=" + paramKey + "]")
|
var elem = form.find("[name=" + paramKey + "]")
|
||||||
var dataType = elem.data('type') || 'string'
|
var dataType = elem.data('type') || 'string'
|
||||||
|
|
||||||
if (dataType === 'integer' && paramValue) {
|
if (dataType === 'integer' && paramValue) {
|
||||||
let value = parseInt(paramValue)
|
var value = parseInt(paramValue)
|
||||||
if (!isNaN(value)) {
|
if (!isNaN(value)) {
|
||||||
params[paramKey] = value
|
params[paramKey] = value
|
||||||
}
|
}
|
||||||
} else if (dataType === 'number' && paramValue) {
|
} else if (dataType === 'number' && paramValue) {
|
||||||
let value = parseFloat(paramValue)
|
var value = parseFloat(paramValue)
|
||||||
if (!isNaN(value)) {
|
if (!isNaN(value)) {
|
||||||
params[paramKey] = value
|
params[paramKey] = value
|
||||||
}
|
}
|
||||||
} else if (dataType === 'boolean' && paramValue) {
|
} else if (dataType === 'boolean' && paramValue) {
|
||||||
let value = {
|
var value = {
|
||||||
'true': true,
|
'true': true,
|
||||||
'false': false
|
'false': false
|
||||||
}[paramValue.toLowerCase()]
|
}[paramValue.toLowerCase()]
|
||||||
|
@ -86,7 +119,7 @@ $('form.api-interaction').submit(function(event) {
|
||||||
|
|
||||||
function requestCallback(request) {
|
function requestCallback(request) {
|
||||||
// Fill in the "GET /foo/" display.
|
// Fill in the "GET /foo/" display.
|
||||||
let parser = document.createElement('a');
|
var parser = document.createElement('a');
|
||||||
parser.href = request.url;
|
parser.href = request.url;
|
||||||
const method = request.options.method
|
const method = request.options.method
|
||||||
const path = parser.pathname + parser.hash + parser.search
|
const path = parser.pathname + parser.hash + parser.search
|
||||||
|
@ -111,7 +144,7 @@ $('form.api-interaction').submit(function(event) {
|
||||||
|
|
||||||
// Fill in the Raw HTTP response display.
|
// Fill in the Raw HTTP response display.
|
||||||
var panelText = 'HTTP/1.1 ' + response.status + ' ' + response.statusText + '\n';
|
var panelText = 'HTTP/1.1 ' + response.status + ' ' + response.statusText + '\n';
|
||||||
response.headers.forEach((header, key) => {
|
response.headers.forEach(function(header, key) {
|
||||||
panelText += normalizeHTTPHeader(key) + ': ' + header + '\n'
|
panelText += normalizeHTTPHeader(key) + ': ' + header + '\n'
|
||||||
})
|
})
|
||||||
if (responseText) {
|
if (responseText) {
|
||||||
|
@ -121,7 +154,7 @@ $('form.api-interaction').submit(function(event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate a client to make the outgoing request.
|
// Instantiate a client to make the outgoing request.
|
||||||
let options = {
|
var options = {
|
||||||
requestCallback: requestCallback,
|
requestCallback: requestCallback,
|
||||||
responseCallback: responseCallback,
|
responseCallback: responseCallback,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
let codec = new window.coreapi.codecs.CoreJSONCodec()
|
var codec = new window.coreapi.codecs.CoreJSONCodec()
|
||||||
let coreJSON = window.atob('{{ schema }}')
|
var coreJSON = window.atob('{{ schema }}')
|
||||||
window.schema = codec.decode(coreJSON)
|
window.schema = codec.decode(coreJSON)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user