Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
daccc2b8
Commit
daccc2b8
authored
May 16, 2016
by
José Padilla
Committed by
Tom Christie
May 16, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up js style and remove extra getCookie function (#4123)
parent
5392be4d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
175 additions
and
175 deletions
+175
-175
rest_framework/static/rest_framework/js/ajax-form.js
+92
-77
rest_framework/static/rest_framework/js/csrf.js
+37
-32
rest_framework/static/rest_framework/js/default.js
+46
-66
No files found.
rest_framework/static/rest_framework/js/ajax-form.js
View file @
daccc2b8
function
replaceDocument
(
docString
)
{
function
replaceDocument
(
docString
)
{
var
doc
=
document
.
open
(
"text/html"
);
var
doc
=
document
.
open
(
"text/html"
);
doc
.
write
(
docString
);
doc
.
close
();
}
doc
.
write
(
docString
);
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'
)
||
if
(
method
===
'GET'
)
{
form
.
data
(
'method'
)
||
// GET requests can always use standard form submits.
form
.
attr
(
'method'
)
||
'GET'
).
toUpperCase
();
if
(
method
===
'GET'
)
{
// GET requests can always use standard form submits.
return
;
}
var
contentType
=
form
.
find
(
'input[data-override="content-type"]'
).
val
()
||
form
.
find
(
'select[data-override="content-type"] option:selected'
).
text
();
if
(
method
===
'POST'
&&
!
contentType
)
{
// POST requests can use standard form submits, unless we have
// overridden the content type.
return
;
}
// At this point we need to make an AJAX form submission.
e
.
preventDefault
();
var
url
=
form
.
attr
(
'action'
);
var
data
;
if
(
contentType
)
{
data
=
form
.
find
(
'[data-override="content"]'
).
val
()
||
''
}
else
{
contentType
=
form
.
attr
(
'enctype'
)
||
form
.
attr
(
'encoding'
)
if
(
contentType
===
'multipart/form-data'
)
{
if
(
!
window
.
FormData
)
{
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,
// so it includes the boundary string.
// See https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
contentType
=
false
;
data
=
new
FormData
(
form
[
0
]);
}
else
{
contentType
=
'application/x-www-form-urlencoded; charset=UTF-8'
data
=
form
.
serialize
();
}
}
}
var
contentType
=
var
ret
=
$
.
ajax
({
form
.
find
(
'input[data-override="content-type"]'
).
val
()
||
url
:
url
,
form
.
find
(
'select[data-override="content-type"] option:selected'
).
text
();
method
:
method
,
if
(
method
===
'POST'
&&
!
contentType
)
{
data
:
data
,
// POST requests can use standard form submits, unless we have
contentType
:
contentType
,
// overridden the content type.
processData
:
false
,
return
;
headers
:
{
'Accept'
:
'text/html; q=1.0, */*'
},
});
ret
.
always
(
function
(
data
,
textStatus
,
jqXHR
)
{
if
(
textStatus
!=
'success'
)
{
jqXHR
=
data
;
}
}
// At this point we need to make an AJAX form submission.
var
responseContentType
=
jqXHR
.
getResponseHeader
(
"content-type"
)
||
""
;
e
.
preventDefault
();
if
(
responseContentType
.
toLowerCase
().
indexOf
(
'text/html'
)
===
0
)
{
replaceDocument
(
jqXHR
.
responseText
);
var
url
=
form
.
attr
(
'action'
);
try
{
var
data
;
// Modify the location and scroll to top, as if after page load.
if
(
contentType
)
{
history
.
replaceState
({},
''
,
url
);
data
=
form
.
find
(
'[data-override="content"]'
).
val
()
||
''
scroll
(
0
,
0
);
}
catch
(
err
)
{
// History API not supported, so redirect.
window
.
location
=
url
;
}
}
else
{
}
else
{
contentType
=
form
.
attr
(
'enctype'
)
||
form
.
attr
(
'encoding'
)
// Not HTML content. We can't open this directly, so redirect.
if
(
contentType
===
'multipart/form-data'
)
{
window
.
location
=
url
;
if
(
!
window
.
FormData
)
{
alert
(
'Your browser does not support AJAX multipart form submissions'
);
return
;
}
// Use the FormData API and allow the content type to be set automatically,
// so it includes the boundary string.
// See https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
contentType
=
false
;
data
=
new
FormData
(
form
[
0
]);
}
else
{
contentType
=
'application/x-www-form-urlencoded; charset=UTF-8'
data
=
form
.
serialize
();
}
}
}
});
var
ret
=
$
.
ajax
({
return
ret
;
url
:
url
,
method
:
method
,
data
:
data
,
contentType
:
contentType
,
processData
:
false
,
headers
:
{
'Accept'
:
'text/html; q=1.0, */*'
},
});
ret
.
always
(
function
(
data
,
textStatus
,
jqXHR
)
{
if
(
textStatus
!=
'success'
)
{
jqXHR
=
data
;
}
var
responseContentType
=
jqXHR
.
getResponseHeader
(
"content-type"
)
||
""
;
if
(
responseContentType
.
toLowerCase
().
indexOf
(
'text/html'
)
===
0
)
{
replaceDocument
(
jqXHR
.
responseText
);
try
{
// Modify the location and scroll to top, as if after page load.
history
.
replaceState
({},
''
,
url
);
scroll
(
0
,
0
);
}
catch
(
err
)
{
// History API not supported, so redirect.
window
.
location
=
url
;
}
}
else
{
// Not HTML content. We can't open this directly, so redirect.
window
.
location
=
url
;
}
});
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
.
unbind
(
'submit.form-plugin click.form-plugin'
)
return
this
.
bind
(
'submit.form-plugin'
,
options
,
doAjaxSubmit
)
.
unbind
(
'submit.form-plugin click.form-plugin'
)
.
bind
(
'click.form-plugin'
,
options
,
captureSubmittingElement
);
.
bind
(
'submit.form-plugin'
,
options
,
doAjaxSubmit
)
.
bind
(
'click.form-plugin'
,
options
,
captureSubmittingElement
);
};
};
rest_framework/static/rest_framework/js/csrf.js
View file @
daccc2b8
function
getCookie
(
name
)
{
function
getCookie
(
name
)
{
var
cookieValue
=
null
;
var
cookieValue
=
null
;
if
(
document
.
cookie
&&
document
.
cookie
!=
''
)
{
var
cookies
=
document
.
cookie
.
split
(
';'
);
if
(
document
.
cookie
&&
document
.
cookie
!=
''
)
{
for
(
var
i
=
0
;
i
<
cookies
.
length
;
i
++
)
{
var
cookies
=
document
.
cookie
.
split
(
';'
);
var
cookie
=
jQuery
.
trim
(
cookies
[
i
]);
// Does this cookie string begin with the name we want?
for
(
var
i
=
0
;
i
<
cookies
.
length
;
i
++
)
{
if
(
cookie
.
substring
(
0
,
name
.
length
+
1
)
==
(
name
+
'='
))
{
var
cookie
=
jQuery
.
trim
(
cookies
[
i
]);
cookieValue
=
decodeURIComponent
(
cookie
.
substring
(
name
.
length
+
1
));
break
;
// Does this cookie string begin with the name we want?
}
if
(
cookie
.
substring
(
0
,
name
.
length
+
1
)
==
(
name
+
'='
))
{
}
cookieValue
=
decodeURIComponent
(
cookie
.
substring
(
name
.
length
+
1
));
break
;
}
}
}
return
cookieValue
;
}
return
cookieValue
;
}
}
function
csrfSafeMethod
(
method
)
{
function
csrfSafeMethod
(
method
)
{
// these HTTP methods do not require CSRF protection
// these HTTP methods do not require CSRF protection
return
(
/^
(
GET|HEAD|OPTIONS|TRACE
)
$/
.
test
(
method
));
return
(
/^
(
GET|HEAD|OPTIONS|TRACE
)
$/
.
test
(
method
));
}
}
function
sameOrigin
(
url
)
{
function
sameOrigin
(
url
)
{
// test that a given url is a same-origin URL
// test that a given url is a same-origin URL
// url could be relative or scheme relative or absolute
// url could be relative or scheme relative or absolute
var
host
=
document
.
location
.
host
;
// host + port
var
host
=
document
.
location
.
host
;
// host + port
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
return
(
url
==
origin
||
url
.
slice
(
0
,
origin
.
length
+
1
)
==
origin
+
'/'
)
||
// Allow absolute or scheme relative URLs to same origin
(
url
==
sr_origin
||
url
.
slice
(
0
,
sr_origin
.
length
+
1
)
==
sr_origin
+
'/'
)
||
return
(
url
==
origin
||
url
.
slice
(
0
,
origin
.
length
+
1
)
==
origin
+
'/'
)
||
// or any other URL that isn't scheme relative or absolute i.e relative.
(
url
==
sr_origin
||
url
.
slice
(
0
,
sr_origin
.
length
+
1
)
==
sr_origin
+
'/'
)
||
!
(
/^
(\/\/
|http:|https:
)
.*/
.
test
(
url
));
// or any other URL that isn't scheme relative or absolute i.e relative.
!
(
/^
(\/\/
|http:|https:
)
.*/
.
test
(
url
));
}
}
var
csrftoken
=
getCookie
(
window
.
drf
.
csrfCookieName
);
var
csrftoken
=
getCookie
(
window
.
drf
.
csrfCookieName
);
$
.
ajaxSetup
({
$
.
ajaxSetup
({
beforeSend
:
function
(
xhr
,
settings
)
{
beforeSend
:
function
(
xhr
,
settings
)
{
if
(
!
csrfSafeMethod
(
settings
.
type
)
&&
sameOrigin
(
settings
.
url
))
{
if
(
!
csrfSafeMethod
(
settings
.
type
)
&&
sameOrigin
(
settings
.
url
))
{
// Send the token to same-origin, relative URLs only.
// Send the token to same-origin, relative URLs only.
// Send the token only if the method warrants CSRF protection
// Send the token only if the method warrants CSRF protection
// Using the CSRFToken value acquired earlier
// Using the CSRFToken value acquired earlier
xhr
.
setRequestHeader
(
"X-CSRFToken"
,
csrftoken
);
xhr
.
setRequestHeader
(
"X-CSRFToken"
,
csrftoken
);
}
}
}
}
});
});
rest_framework/static/rest_framework/js/default.js
View file @
daccc2b8
function
getCookie
(
c_name
)
$
(
document
).
ready
(
function
()
{
{
// JSON highlighting.
// From http://www.w3schools.com/js/js_cookies.asp
prettyPrint
();
var
c_value
=
document
.
cookie
;
var
c_start
=
c_value
.
indexOf
(
" "
+
c_name
+
"="
);
// Bootstrap tooltips.
if
(
c_start
==
-
1
)
{
$
(
'.js-tooltip'
).
tooltip
({
c_start
=
c_value
.
indexOf
(
c_name
+
"="
);
delay
:
1000
,
}
container
:
'body'
if
(
c_start
==
-
1
)
{
});
c_value
=
null
;
}
else
{
// Deal with rounded tab styling after tab clicks.
c_start
=
c_value
.
indexOf
(
"="
,
c_start
)
+
1
;
$
(
'a[data-toggle="tab"]:first'
).
on
(
'shown'
,
function
(
e
)
{
var
c_end
=
c_value
.
indexOf
(
";"
,
c_start
);
$
(
e
.
target
).
parents
(
'.tabbable'
).
addClass
(
'first-tab-active'
);
if
(
c_end
==
-
1
)
{
});
c_end
=
c_value
.
length
;
}
$
(
'a[data-toggle="tab"]:not(:first)'
).
on
(
'shown'
,
function
(
e
)
{
c_value
=
unescape
(
c_value
.
substring
(
c_start
,
c_end
));
$
(
e
.
target
).
parents
(
'.tabbable'
).
removeClass
(
'first-tab-active'
);
}
});
return
c_value
;
}
$
(
'a[data-toggle="tab"]'
).
click
(
function
()
{
document
.
cookie
=
"tabstyle="
+
this
.
name
+
"; path=/"
;
$
(
document
).
ready
(
function
()
{
});
// JSON highlighting.
prettyPrint
();
// Store tab preference in cookies & display appropriate tab on load.
var
selectedTab
=
null
;
// Bootstrap tooltips.
var
selectedTabName
=
getCookie
(
'tabstyle'
);
$
(
'.js-tooltip'
).
tooltip
({
delay
:
1000
,
if
(
selectedTabName
)
{
container
:
'body'
selectedTabName
=
selectedTabName
.
replace
(
/
[^
a-z-
]
/g
,
''
);
});
}
// Deal with rounded tab styling after tab clicks.
if
(
selectedTabName
)
{
$
(
'a[data-toggle="tab"]:first'
).
on
(
'shown'
,
function
(
e
)
{
selectedTab
=
$
(
'.form-switcher a[name='
+
selectedTabName
+
']'
);
$
(
e
.
target
).
parents
(
'.tabbable'
).
addClass
(
'first-tab-active'
);
}
});
$
(
'a[data-toggle="tab"]:not(:first)'
).
on
(
'shown'
,
function
(
e
)
{
if
(
selectedTab
&&
selectedTab
.
length
>
0
)
{
$
(
e
.
target
).
parents
(
'.tabbable'
).
removeClass
(
'first-tab-active'
);
// Display whichever tab is selected.
});
selectedTab
.
tab
(
'show'
);
}
else
{
$
(
'a[data-toggle="tab"]'
).
click
(
function
(){
// If no tab selected, display rightmost tab.
document
.
cookie
=
"tabstyle="
+
this
.
name
+
"; path=/"
;
$
(
'.form-switcher a:first'
).
tab
(
'show'
);
});
}
// Store tab preference in cookies & display appropriate tab on load.
$
(
window
).
load
(
function
()
{
var
selectedTab
=
null
;
$
(
'#errorModal'
).
modal
(
'show'
);
var
selectedTabName
=
getCookie
(
'tabstyle'
);
});
if
(
selectedTabName
)
{
selectedTabName
=
selectedTabName
.
replace
(
/
[^
a-z-
]
/g
,
''
);
}
if
(
selectedTabName
)
{
selectedTab
=
$
(
'.form-switcher a[name='
+
selectedTabName
+
']'
);
}
if
(
selectedTab
&&
selectedTab
.
length
>
0
)
{
// Display whichever tab is selected.
selectedTab
.
tab
(
'show'
);
}
else
{
// If no tab selected, display rightmost tab.
$
(
'.form-switcher a:first'
).
tab
(
'show'
);
}
$
(
window
).
load
(
function
(){
$
(
'#errorModal'
).
modal
(
'show'
);
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment