mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Clean up js style and remove extra getCookie function (#4123)
This commit is contained in:
parent
5392be4ddb
commit
daccc2b8f3
|
@ -1,15 +1,19 @@
|
||||||
function replaceDocument(docString) {
|
function replaceDocument(docString) {
|
||||||
var doc = document.open("text/html");
|
var doc = document.open("text/html");
|
||||||
|
|
||||||
doc.write(docString);
|
doc.write(docString);
|
||||||
doc.close();
|
doc.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function doAjaxSubmit(e) {
|
function doAjaxSubmit(e) {
|
||||||
var form = $(this);
|
var form = $(this);
|
||||||
var btn = $(this.clk);
|
var btn = $(this.clk);
|
||||||
var method = btn.data('method') || form.data('method') || form.attr('method') || 'GET';
|
var method = (
|
||||||
method = method.toUpperCase()
|
btn.data('method') ||
|
||||||
|
form.data('method') ||
|
||||||
|
form.attr('method') || 'GET'
|
||||||
|
).toUpperCase();
|
||||||
|
|
||||||
if (method === 'GET') {
|
if (method === 'GET') {
|
||||||
// GET requests can always use standard form submits.
|
// GET requests can always use standard form submits.
|
||||||
return;
|
return;
|
||||||
|
@ -18,6 +22,7 @@ function doAjaxSubmit(e) {
|
||||||
var contentType =
|
var contentType =
|
||||||
form.find('input[data-override="content-type"]').val() ||
|
form.find('input[data-override="content-type"]').val() ||
|
||||||
form.find('select[data-override="content-type"] option:selected').text();
|
form.find('select[data-override="content-type"] option:selected').text();
|
||||||
|
|
||||||
if (method === 'POST' && !contentType) {
|
if (method === 'POST' && !contentType) {
|
||||||
// POST requests can use standard form submits, unless we have
|
// POST requests can use standard form submits, unless we have
|
||||||
// overridden the content type.
|
// overridden the content type.
|
||||||
|
@ -29,15 +34,18 @@ function doAjaxSubmit(e) {
|
||||||
|
|
||||||
var url = form.attr('action');
|
var url = form.attr('action');
|
||||||
var data;
|
var data;
|
||||||
|
|
||||||
if (contentType) {
|
if (contentType) {
|
||||||
data = form.find('[data-override="content"]').val() || ''
|
data = form.find('[data-override="content"]').val() || ''
|
||||||
} else {
|
} else {
|
||||||
contentType = form.attr('enctype') || form.attr('encoding')
|
contentType = form.attr('enctype') || form.attr('encoding')
|
||||||
|
|
||||||
if (contentType === 'multipart/form-data') {
|
if (contentType === 'multipart/form-data') {
|
||||||
if (!window.FormData) {
|
if (!window.FormData) {
|
||||||
alert('Your browser does not support AJAX multipart form submissions');
|
alert('Your browser does not support AJAX multipart form submissions');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the FormData API and allow the content type to be set automatically,
|
// Use the FormData API and allow the content type to be set automatically,
|
||||||
// so it includes the boundary string.
|
// so it includes the boundary string.
|
||||||
// See https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
|
// See https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
|
||||||
|
@ -55,15 +63,21 @@ function doAjaxSubmit(e) {
|
||||||
data: data,
|
data: data,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
processData: false,
|
processData: false,
|
||||||
headers: {'Accept': 'text/html; q=1.0, */*'},
|
headers: {
|
||||||
|
'Accept': 'text/html; q=1.0, */*'
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
ret.always(function(data, textStatus, jqXHR) {
|
ret.always(function(data, textStatus, jqXHR) {
|
||||||
if (textStatus != 'success') {
|
if (textStatus != 'success') {
|
||||||
jqXHR = data;
|
jqXHR = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
var responseContentType = jqXHR.getResponseHeader("content-type") || "";
|
var responseContentType = jqXHR.getResponseHeader("content-type") || "";
|
||||||
|
|
||||||
if (responseContentType.toLowerCase().indexOf('text/html') === 0) {
|
if (responseContentType.toLowerCase().indexOf('text/html') === 0) {
|
||||||
replaceDocument(jqXHR.responseText);
|
replaceDocument(jqXHR.responseText);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Modify the location and scroll to top, as if after page load.
|
// Modify the location and scroll to top, as if after page load.
|
||||||
history.replaceState({}, '', url);
|
history.replaceState({}, '', url);
|
||||||
|
@ -77,19 +91,20 @@ function doAjaxSubmit(e) {
|
||||||
window.location = url;
|
window.location = url;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function captureSubmittingElement(e) {
|
function captureSubmittingElement(e) {
|
||||||
var target = e.target;
|
var target = e.target;
|
||||||
var form = this;
|
var form = this;
|
||||||
|
|
||||||
form.clk = target;
|
form.clk = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$.fn.ajaxForm = function() {
|
$.fn.ajaxForm = function() {
|
||||||
var options = {}
|
var options = {}
|
||||||
|
|
||||||
return this
|
return this
|
||||||
.unbind('submit.form-plugin click.form-plugin')
|
.unbind('submit.form-plugin click.form-plugin')
|
||||||
.bind('submit.form-plugin', options, doAjaxSubmit)
|
.bind('submit.form-plugin', options, doAjaxSubmit)
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
function getCookie(name) {
|
function getCookie(name) {
|
||||||
var cookieValue = null;
|
var cookieValue = null;
|
||||||
|
|
||||||
if (document.cookie && document.cookie != '') {
|
if (document.cookie && document.cookie != '') {
|
||||||
var cookies = document.cookie.split(';');
|
var cookies = document.cookie.split(';');
|
||||||
|
|
||||||
for (var i = 0; i < cookies.length; i++) {
|
for (var i = 0; i < cookies.length; i++) {
|
||||||
var cookie = jQuery.trim(cookies[i]);
|
var cookie = jQuery.trim(cookies[i]);
|
||||||
|
|
||||||
// Does this cookie string begin with the name we want?
|
// Does this cookie string begin with the name we want?
|
||||||
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
||||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||||
|
@ -11,6 +14,7 @@ function getCookie(name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cookieValue;
|
return cookieValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +30,7 @@ function sameOrigin(url) {
|
||||||
var protocol = document.location.protocol;
|
var protocol = document.location.protocol;
|
||||||
var sr_origin = '//' + host;
|
var sr_origin = '//' + host;
|
||||||
var origin = protocol + sr_origin;
|
var origin = protocol + sr_origin;
|
||||||
|
|
||||||
// Allow absolute or scheme relative URLs to same origin
|
// Allow absolute or scheme relative URLs to same origin
|
||||||
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
|
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
|
||||||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
|
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
|
||||||
|
|
|
@ -1,24 +1,3 @@
|
||||||
function getCookie(c_name)
|
|
||||||
{
|
|
||||||
// From http://www.w3schools.com/js/js_cookies.asp
|
|
||||||
var c_value = document.cookie;
|
|
||||||
var c_start = c_value.indexOf(" " + c_name + "=");
|
|
||||||
if (c_start == -1) {
|
|
||||||
c_start = c_value.indexOf(c_name + "=");
|
|
||||||
}
|
|
||||||
if (c_start == -1) {
|
|
||||||
c_value = null;
|
|
||||||
} else {
|
|
||||||
c_start = c_value.indexOf("=", c_start) + 1;
|
|
||||||
var c_end = c_value.indexOf(";", c_start);
|
|
||||||
if (c_end == -1) {
|
|
||||||
c_end = c_value.length;
|
|
||||||
}
|
|
||||||
c_value = unescape(c_value.substring(c_start,c_end));
|
|
||||||
}
|
|
||||||
return c_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
// JSON highlighting.
|
// JSON highlighting.
|
||||||
prettyPrint();
|
prettyPrint();
|
||||||
|
@ -33,6 +12,7 @@ $(document).ready(function () {
|
||||||
$('a[data-toggle="tab"]:first').on('shown', function(e) {
|
$('a[data-toggle="tab"]:first').on('shown', function(e) {
|
||||||
$(e.target).parents('.tabbable').addClass('first-tab-active');
|
$(e.target).parents('.tabbable').addClass('first-tab-active');
|
||||||
});
|
});
|
||||||
|
|
||||||
$('a[data-toggle="tab"]:not(:first)').on('shown', function(e) {
|
$('a[data-toggle="tab"]:not(:first)').on('shown', function(e) {
|
||||||
$(e.target).parents('.tabbable').removeClass('first-tab-active');
|
$(e.target).parents('.tabbable').removeClass('first-tab-active');
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user