Skip to content

Commit e76920a

Browse files
Move django-csrf.js and rtd-import.js into core's static-src directory
1 parent 7de59e9 commit e76920a

File tree

7 files changed

+139
-136
lines changed

7 files changed

+139
-136
lines changed

gulpfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var gulp = require('gulp'),
77
/* Applications with static sources */
88
var sources = {
99
'readthedocs/core': [
10-
'readthedocs/core/static-src/**/readthedocs-doc-embed.js'
10+
'readthedocs/core/static-src/**/readthedocs-doc-embed.js',
11+
'readthedocs/core/static-src/**/projectimport.js',
1112
],
1213
'readthedocs/projects': [
1314
'readthedocs/projects/static-src/**/tools.js'

media/javascript/django-csrf.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

media/javascript/rtd-import.js

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var $ = require('jquery');
2+
3+
4+
function csrfSafeMethod(method) {
5+
// these HTTP methods do not require CSRF protection
6+
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
7+
}
8+
9+
$.ajaxSetup({
10+
beforeSend: function(xhr, settings) {
11+
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
12+
function getCookie(name) {
13+
var cookieValue = null;
14+
if (document.cookie && document.cookie != '') {
15+
var cookies = document.cookie.split(';');
16+
for (var i = 0; i < cookies.length; i++) {
17+
var cookie = jQuery.trim(cookies[i]);
18+
// Does this cookie string begin with the name we want?
19+
if (cookie.substring(0, name.length + 1) == (name + '=')) {
20+
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
21+
break;
22+
}
23+
}
24+
}
25+
return cookieValue;
26+
}
27+
var csrftoken = getCookie('csrftoken');
28+
29+
xhr.setRequestHeader("X-CSRFToken", csrftoken);
30+
}
31+
}
32+
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
$ = require('jquery');
2+
require('./django-csrf.js');
3+
4+
5+
$(function() {
6+
var input = $('#id_repo'),
7+
repo = $('#id_repo_type');
8+
9+
input.blur(function () {
10+
var val = input.val(),
11+
type;
12+
13+
switch(true) {
14+
case /^hg/.test(val):
15+
type = 'hg';
16+
break;
17+
18+
case /^bzr/.test(val):
19+
case /launchpad/.test(val):
20+
type = 'bzr';
21+
break;
22+
23+
case /trunk/.test(val):
24+
case /^svn/.test(val):
25+
type = 'svn';
26+
break;
27+
28+
default:
29+
case /github/.test(val):
30+
case /(^git|\.git$)/.test(val):
31+
type = 'git';
32+
break;
33+
}
34+
35+
repo.val(type);
36+
});
37+
38+
$('[data-sync-repositories]').each(function () {
39+
var $button = $(this);
40+
var target = $(this).attr('data-target');
41+
42+
$button.on('click', function () {
43+
var url = $button.attr('data-sync-repositories');
44+
$.ajax({
45+
method: 'POST',
46+
url: url,
47+
success: function (data) {
48+
$button.attr('disabled', true);
49+
watchProgress(data.url);
50+
},
51+
error: function () {
52+
onError();
53+
}
54+
});
55+
$('.sync-repositories').addClass('hide');
56+
$('.sync-repositories-progress').removeClass('hide');
57+
});
58+
59+
function watchProgress(url) {
60+
setTimeout(function () {
61+
$.ajax({
62+
method: 'GET',
63+
url: url,
64+
success: function (data) {
65+
if (data.finished) {
66+
if (data.success) {
67+
onSuccess();
68+
} else {
69+
onError();
70+
}
71+
} else {
72+
watchProgress(url);
73+
}
74+
},
75+
error: onError
76+
});
77+
}, 2000);
78+
}
79+
80+
function onSuccess(url) {
81+
$.ajax({
82+
method: 'GET',
83+
url: window.location.href,
84+
success: function (data) {
85+
var $newContent = $(data).find(target);
86+
$('body').find(target).replaceWith($newContent);
87+
$('.sync-repositories').addClass('hide');
88+
$('.sync-repositories-progress').addClass('hide');
89+
$('.sync-repositories-success').removeClass('hide');
90+
},
91+
error: onError
92+
});
93+
}
94+
95+
function onError() {
96+
$('.sync-repositories').addClass('hide');
97+
$('.sync-repositories-progress').addClass('hide');
98+
$('.sync-repositories-error').removeClass('hide');
99+
}
100+
});
101+
});

readthedocs/templates/projects/project_import_bitbucket.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends "base.html" %}
22
{% load i18n %}
3+
{% load static %}
34

45
{% block title %}{% trans "Import a BitBucket project" %}{% endblock %}
56

@@ -9,8 +10,7 @@
910
{% endblock %}
1011

1112
{% block extra_scripts %}
12-
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/django-csrf.js"></script>
13-
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/rtd-import.js"></script>
13+
<script type="text/javascript" src="{% static "core/js/projectimport.js" %}"></script>
1414
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/jquery.markitup.pack.js"></script>
1515
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/sets/sphinx/editor.js"></script>
1616
<script type="text/javascript">

readthedocs/templates/projects/project_import_github.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends "base.html" %}
22
{% load i18n %}
3+
{% load static %}
34

45
{% block title %}{% trans "Import a GitHub project" %}{% endblock %}
56

@@ -9,8 +10,7 @@
910
{% endblock %}
1011

1112
{% block extra_scripts %}
12-
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/django-csrf.js"></script>
13-
<script type="text/javascript" src="{{ MEDIA_URL }}javascript/rtd-import.js"></script>
13+
<script type="text/javascript" src="{% static "core/js/projectimport.js" %}"></script>
1414
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/jquery.markitup.pack.js"></script>
1515
<script type="text/javascript" src="{{ MEDIA_URL }}lib/markitup/sets/sphinx/editor.js"></script>
1616
<script type="text/javascript">

0 commit comments

Comments
 (0)