comparison gpp/downloads/static/js/rating.js @ 312:88b2b9cb8c1f

Fixing #142; cut over to the django.contrib.staticfiles app.
author Brian Neal <bgneal@gmail.com>
date Thu, 27 Jan 2011 02:56:10 +0000
parents
children
comparison
equal deleted inserted replaced
311:b1c39788e511 312:88b2b9cb8c1f
1 function dlRatingOver(event)
2 {
3 var div = $(this).parent('div');
4 var stars = $('img', div);
5 for (var i = 0; i <= event.data; ++i)
6 {
7 var star = $(stars[i]);
8 star.attr('src', '/static/icons/stars/rating_over.gif');
9 }
10 }
11
12 function dlRatingOut(event)
13 {
14 var div = $(this).parent('div');
15 var stars = $('img', div);
16 for (var i = 0; i <= event.data; ++i)
17 {
18 var star = $(stars[i]);
19 star.attr('src', '/static/icons/stars/rating_' + star.attr('class') + '.gif');
20 }
21 }
22
23 function dlRatingClick(event)
24 {
25 var star = $(this);
26 var id = star.attr('id');
27 if (id.match(/star-(\d+)-(\d+)/))
28 {
29 $.ajax({
30 url: '/downloads/rate/',
31 type: 'POST',
32 data: { id: RegExp.$1, rating: parseInt(RegExp.$2) + 1},
33 dataType: 'text',
34 success: function(rating) {
35 rating = parseFloat(rating);
36 if (rating < 0)
37 {
38 alert("You've already rated this download.");
39 return;
40 }
41 alert('Thanks for rating this download!');
42 var div = star.parent('div');
43 var stars = $('img', div);
44 rating = parseFloat(rating);
45 for (var i = 0; i < 5; ++i)
46 {
47 var s = $(stars[i]);
48 s.removeClass(s.attr('class'));
49 if (rating >= 1.0)
50 {
51 s.attr('src', '/static/icons/stars/rating_on.gif');
52 s.addClass('on')
53 rating -= 1.0;
54 }
55 else if (rating >= 0.5)
56 {
57 s.attr('src', '/static/icons/stars/rating_half.gif');
58 s.addClass('half')
59 rating = 0;
60 }
61 else
62 {
63 s.attr('src', '/static/icons/stars/rating_off.gif');
64 s.addClass('off')
65 }
66 }
67 },
68 error: function (xhr, textStatus, ex) {
69 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
70 xhr.responseText);
71 }
72 });
73 }
74 }
75
76 $(document).ready(function() {
77 $('.rating').each(function(n) {
78 var div = $(this);
79 var id = div.attr('id');
80 var numeric_id = -1;
81 if (id.match(/rating-(\d+)/))
82 {
83 numeric_id = RegExp.$1;
84 }
85 var rating = div.html();
86 div.html('');
87 for (var i = 0; i < 5; ++i)
88 {
89 var star = $('<img />');
90 if (rating >= 1)
91 {
92 star.attr('src', '/static/icons/stars/rating_on.gif');
93 star.addClass('on')
94 --rating;
95 }
96 else if (rating >= 0.5)
97 {
98 star.attr('src', '/static/icons/stars/rating_half.gif');
99 star.addClass('half')
100 rating = 0;
101 }
102 else
103 {
104 star.attr('src', '/static/icons/stars/rating_off.gif');
105 star.addClass('off')
106 }
107 star.attr('alt', 'star');
108 star.attr('id', 'star-' + numeric_id + '-' + i);
109 star.bind('mouseover', i, dlRatingOver);
110 star.bind('mouseout', i, dlRatingOut);
111 star.click(dlRatingClick);
112 div.append(star);
113 }
114 });
115 });