annotate downloads/static/js/rating.js @ 697:67f8d49a9377

Cleaned up the code a bit. Separated the S3 stuff out into its own class. This class maybe should be in core. Still want to do some kind of context manager around the temporary file we are creating to ensure it gets deleted.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 21:02:58 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@312 1 function dlRatingOver(event)
bgneal@312 2 {
bgneal@312 3 var div = $(this).parent('div');
bgneal@312 4 var stars = $('img', div);
bgneal@312 5 for (var i = 0; i <= event.data; ++i)
bgneal@312 6 {
bgneal@312 7 var star = $(stars[i]);
bgneal@312 8 star.attr('src', '/static/icons/stars/rating_over.gif');
bgneal@312 9 }
bgneal@312 10 }
bgneal@312 11
bgneal@312 12 function dlRatingOut(event)
bgneal@312 13 {
bgneal@312 14 var div = $(this).parent('div');
bgneal@312 15 var stars = $('img', div);
bgneal@312 16 for (var i = 0; i <= event.data; ++i)
bgneal@312 17 {
bgneal@312 18 var star = $(stars[i]);
bgneal@312 19 star.attr('src', '/static/icons/stars/rating_' + star.attr('class') + '.gif');
bgneal@312 20 }
bgneal@312 21 }
bgneal@312 22
bgneal@312 23 function dlRatingClick(event)
bgneal@312 24 {
bgneal@312 25 var star = $(this);
bgneal@312 26 var id = star.attr('id');
bgneal@312 27 if (id.match(/star-(\d+)-(\d+)/))
bgneal@312 28 {
bgneal@312 29 $.ajax({
bgneal@312 30 url: '/downloads/rate/',
bgneal@312 31 type: 'POST',
bgneal@312 32 data: { id: RegExp.$1, rating: parseInt(RegExp.$2) + 1},
bgneal@312 33 dataType: 'text',
bgneal@312 34 success: function(rating) {
bgneal@312 35 rating = parseFloat(rating);
bgneal@312 36 if (rating < 0)
bgneal@312 37 {
bgneal@312 38 alert("You've already rated this download.");
bgneal@312 39 return;
bgneal@312 40 }
bgneal@312 41 alert('Thanks for rating this download!');
bgneal@312 42 var div = star.parent('div');
bgneal@312 43 var stars = $('img', div);
bgneal@312 44 rating = parseFloat(rating);
bgneal@312 45 for (var i = 0; i < 5; ++i)
bgneal@312 46 {
bgneal@312 47 var s = $(stars[i]);
bgneal@312 48 s.removeClass(s.attr('class'));
bgneal@312 49 if (rating >= 1.0)
bgneal@312 50 {
bgneal@312 51 s.attr('src', '/static/icons/stars/rating_on.gif');
bgneal@312 52 s.addClass('on')
bgneal@312 53 rating -= 1.0;
bgneal@312 54 }
bgneal@312 55 else if (rating >= 0.5)
bgneal@312 56 {
bgneal@312 57 s.attr('src', '/static/icons/stars/rating_half.gif');
bgneal@312 58 s.addClass('half')
bgneal@312 59 rating = 0;
bgneal@312 60 }
bgneal@312 61 else
bgneal@312 62 {
bgneal@312 63 s.attr('src', '/static/icons/stars/rating_off.gif');
bgneal@312 64 s.addClass('off')
bgneal@312 65 }
bgneal@312 66 }
bgneal@312 67 },
bgneal@312 68 error: function (xhr, textStatus, ex) {
bgneal@312 69 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 70 xhr.responseText);
bgneal@312 71 }
bgneal@312 72 });
bgneal@312 73 }
bgneal@312 74 }
bgneal@312 75
bgneal@312 76 $(document).ready(function() {
bgneal@312 77 $('.rating').each(function(n) {
bgneal@312 78 var div = $(this);
bgneal@312 79 var id = div.attr('id');
bgneal@312 80 var numeric_id = -1;
bgneal@312 81 if (id.match(/rating-(\d+)/))
bgneal@312 82 {
bgneal@312 83 numeric_id = RegExp.$1;
bgneal@312 84 }
bgneal@312 85 var rating = div.html();
bgneal@312 86 div.html('');
bgneal@312 87 for (var i = 0; i < 5; ++i)
bgneal@312 88 {
bgneal@312 89 var star = $('<img />');
bgneal@312 90 if (rating >= 1)
bgneal@312 91 {
bgneal@312 92 star.attr('src', '/static/icons/stars/rating_on.gif');
bgneal@312 93 star.addClass('on')
bgneal@312 94 --rating;
bgneal@312 95 }
bgneal@312 96 else if (rating >= 0.5)
bgneal@312 97 {
bgneal@312 98 star.attr('src', '/static/icons/stars/rating_half.gif');
bgneal@312 99 star.addClass('half')
bgneal@312 100 rating = 0;
bgneal@312 101 }
bgneal@312 102 else
bgneal@312 103 {
bgneal@312 104 star.attr('src', '/static/icons/stars/rating_off.gif');
bgneal@312 105 star.addClass('off')
bgneal@312 106 }
bgneal@312 107 star.attr('alt', 'star');
bgneal@312 108 star.attr('id', 'star-' + numeric_id + '-' + i);
bgneal@312 109 star.bind('mouseover', i, dlRatingOver);
bgneal@312 110 star.bind('mouseout', i, dlRatingOut);
bgneal@312 111 star.click(dlRatingClick);
bgneal@312 112 div.append(star);
bgneal@312 113 }
bgneal@312 114 });
bgneal@312 115 });