-
Notifications
You must be signed in to change notification settings - Fork 992
/
qa.js
executable file
·131 lines (123 loc) · 3.78 KB
/
qa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
$(function () {
function getCookie(name) {
// Function to get any cookie available in the session.
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// 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;
};
function csrfSafeMethod(method) {
// These HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function toogleVote(voteIcon, vote, data, isAnswer) {
var idPrefix = isAnswer ? 'answer' : 'question';
var isOwner = data.is_owner;
if (isOwner === false) {
if (vote === "U") {
voteIcon.addClass('voted');
voteIcon.siblings(`#${idPrefix}DownVote`).removeClass('voted');
} else {
voteIcon.addClass('voted');
voteIcon.siblings(`#${idPrefix}UpVote`).removeClass('voted');
}
voteIcon.siblings(`#${idPrefix}Votes`).text(data.votes);
}
}
var csrftoken = getCookie('csrftoken');
var page_title = $(document).attr("title");
// This sets up every ajax call with proper headers.
$.ajaxSetup({
beforeSend: function (xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$("#publish").click(function () {
// function to operate the Publish button in the question form, marking
// the question status as published.
$("input[name='status']").val("O");
$("#question-form").submit();
});
$("#draft").click(function () {
// Function to operate the Draft button in the question form, marking
// the question status as draft.
$("input[name='status']").val("D");
$("#question-form").submit();
});
$(".question-vote").click(function () {
// Vote on a question.
var voteIcon = $(this);
var question = $(this).closest(".question").attr("question-id");
if ($(this).hasClass("up-vote")) {
vote = "U";
} else {
$('#questionDownVote').addClass('voted');
$('#questionUpVote').removeClass('voted');
}
$.ajax({
url: '/qa/question/vote/',
data: {
'question': question,
'value': vote
},
type: 'post',
cache: false,
success: function (data) {
toogleVote(voteIcon, vote, data, false);
}
});
});
$(".answer-vote").click(function () {
// Vote on an answer.
var voteIcon = $(this);
var answer = $(this).closest(".answer").attr("answer-id");
if ($(this).hasClass("up-vote")) {
vote = "U";
} else {
$('#answerDownVote').addClass('voted');
$('#answerUpVote').removeClass('voted');
}
$.ajax({
url: '/qa/answer/vote/',
data: {
'answer': answer,
'value': vote
},
type: 'post',
cache: false,
success: function (data) {
toogleVote(voteIcon, vote, data, true);
}
});
});
$("#acceptAnswer").click(function () {
// Mark an answer as accepted.
var span = $(this);
var answer = $(this).closest(".answer").attr("answer-id");
$.ajax({
url: '/qa/accept-answer/',
data: {
'answer': answer
},
type: 'post',
cache: false,
success: function (data) {
$("#acceptAnswer").removeClass("accepted");
$("#acceptAnswer").prop("title", "Click to accept the answer");
$("#acceptAnswer").addClass("accepted");
$("#acceptAnswer").prop("title", "Click to unaccept the answer");
}
});
});
});