/
_script.html
53 lines (52 loc) · 2.1 KB
/
_script.html
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
<script>
(function($) {
$('html').ajaxSend(
function(event, xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
}
});
})(jQuery);
</script>
<script type="text/javascript" src="{{ STATIC_URL }}agon_ratings/js/jquery.raty.js"></script>
<script type="text/javascript">
$(function () {
// Assumptions:
// 1. you have a div with the id of "user_rating" where you want the stars to go
// 2. you have a container with the class .overall_rating where the new average rating will go
$("#user_rating").raty({
start: "{{ the_user_rating }}",
click: function(score, evt) {
var current_rating = 0;
if (score != null) {
current_rating = score;
}
$.ajax({
url: "{{ post_url }}",
type: "POST",
data: {
"rating": current_rating
{% if category %}
,"category": "{{ category }}"
{% endif %}
},
statusCode: {
403: function(jqXHR, textStatus, errorThrown) {
// Invalid rating was posted or invalid category was sent
console.log(errorThrown);
},
200: function(data, textStatus, jqXHR) {
{% if category %}
$(".overall_rating.category-{{ category }}").text(data["overall_rating"]);
{% else %}
$(".overall_rating").text(data["overall_rating"]);
{% endif %}
}
}
});
},
cancel: true,
path: "{{ STATIC_URL }}agon_ratings/img/"
});
});
</script>