|
| 1 | + |
| 2 | + |
| 3 | +function handleRatingHtml(el){ |
| 4 | + |
| 5 | + var $ratingBox = $(el); |
| 6 | + var ratingType = $ratingBox.attr('data-workshop-rating-type'); |
| 7 | + |
| 8 | + $.each($(el).find('img'), function(index, element){ |
| 9 | + |
| 10 | + var ratingScore = parseInt($(element).attr('data-rating-score')); |
| 11 | + |
| 12 | + $(element).css('cursor', 'pointer'); |
| 13 | + |
| 14 | + $(element).on('click', function(e){ |
| 15 | + |
| 16 | + // Hide open popover |
| 17 | + $('.popover').hide(); |
| 18 | + |
| 19 | + // Make sure we are logged in |
| 20 | + if(app_store.account === null){ |
| 21 | + window.location = '/login' |
| 22 | + + '?redirect=/workshop/item/' + workshop_item.id + '/' + workshop_item.slug |
| 23 | + + '&msg=workshop-rate'; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Disable rating own account items |
| 28 | + if(ratingType === 'quality' && app_store.account.id === workshop_item.submitter_id){ |
| 29 | + toastr.warning('You can not rate your own workshop items!'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Rate the workshop item |
| 34 | + $.ajax({ |
| 35 | + type: 'POST', |
| 36 | + url: '/workshop/rate/' + workshop_item.id + '/' + ratingType, |
| 37 | + data: { |
| 38 | + score: ratingScore, |
| 39 | + [app_store.csrf.keys.name]: app_store.csrf.name, |
| 40 | + [app_store.csrf.keys.value]: app_store.csrf.value |
| 41 | + }, |
| 42 | + dataType: 'json', // return type data, |
| 43 | + error: function(data){ |
| 44 | + toastr.error('Something went wrong.'); |
| 45 | + }, |
| 46 | + success: function(data){ |
| 47 | + |
| 48 | + // Make sure rating went successful |
| 49 | + if(typeof data.success === 'undefined' || data.success !== true){ |
| 50 | + toastr.error('Something went wrong.'); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Update rating stars HTML |
| 55 | + $ratingBox.html(data.html); |
| 56 | + handleRatingHtml($ratingBox); |
| 57 | + |
| 58 | + // Set template for rating info |
| 59 | + $('#' + ratingType + '-rating-info').html('<span id="' + ratingType + '-rating-score" class="text-muted"></span> / 5' + |
| 60 | + '<span style="margin: 0 3px" class="text-muted"> • </span>' + |
| 61 | + '<span id="' + ratingType + '-rating-count" class="text-muted"></span>' + |
| 62 | + '<span style="margin: 0 3px" class="text-muted"> • </span>' + |
| 63 | + 'Your rating: ' + |
| 64 | + '<span id="' + ratingType + '-rating-self" class="text-muted"></span> ' + // needs a space at the end |
| 65 | + '<a href="#" data-workshop-rating-type="' + ratingType + '" class="rating-remove text-stand-out">Remove</a>' |
| 66 | + ); |
| 67 | + |
| 68 | + // Update rating score |
| 69 | + $('#' + ratingType + '-rating-score').text(data.rating_score); |
| 70 | + |
| 71 | + // Update rating count |
| 72 | + if(data.rating_count === 1){ |
| 73 | + $('#' + ratingType + '-rating-count').text('1 rating'); |
| 74 | + } else { |
| 75 | + $('#' + ratingType + '-rating-count').text('' + data.rating_count + ' ratings'); |
| 76 | + } |
| 77 | + |
| 78 | + // Update self rating |
| 79 | + $('#' + ratingType + '-rating-self').text(ratingScore); |
| 80 | + |
| 81 | + // Update CSRF tokens |
| 82 | + app_store.csrf.name = data.csrf.name; |
| 83 | + app_store.csrf.value = data.csrf.value; |
| 84 | + |
| 85 | + toastr.success('You have successfully rated this workshop item!'); |
| 86 | + |
| 87 | + }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + new bootstrap.Popover(element, { |
| 92 | + 'placement': 'top', |
| 93 | + 'trigger': 'hover', |
| 94 | + 'content': 'Rate this item ' + ratingScore + ' out of 5' |
| 95 | + }); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +$(function(e){ |
| 100 | + |
| 101 | + // Render workshop ratings |
| 102 | + $.each($('[data-workshop-rating-type]'), function(i, el){ |
| 103 | + handleRatingHtml(el); |
| 104 | + }); |
| 105 | + |
| 106 | + // Show 'Show all downloads' and hide 'Downloads list' |
| 107 | + // This is users with javascript disabled can still view all files |
| 108 | + $('#show-all-downloads').show(); |
| 109 | + $('#all-downloads-list').hide(); |
| 110 | + |
| 111 | + // Handle 'Show all downloads' |
| 112 | + $('#show-all-downloads a').on('click', function(e){ |
| 113 | + e.preventDefault(); |
| 114 | + if($('#all-downloads-list').is(':visible') === true){ |
| 115 | + $(this).text('Show all downloads (' + $('#all-downloads-list tbody tr').length + ')'); |
| 116 | + $('#all-downloads-list').slideUp("fast"); |
| 117 | + } else if($('#all-downloads-list').is(':visible') === false) { |
| 118 | + $(this).text('Hide all downloads (' + $('#all-downloads-list tbody tr').length + ')'); |
| 119 | + $('#all-downloads-list').slideDown("fast"); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + // Handle remove rating |
| 124 | + $('body').on('click', function(e){ |
| 125 | + |
| 126 | + let $target = $(e.target); |
| 127 | + if(!$target.hasClass('rating-remove')){ |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + e.preventDefault(); |
| 132 | + |
| 133 | + var ratingType = $target.attr('data-workshop-rating-type'); |
| 134 | + var $ratingBox = $target.parent().parent().parent().find('span:first'); |
| 135 | + |
| 136 | + // Rate the workshop item |
| 137 | + $.ajax({ |
| 138 | + type: 'POST', |
| 139 | + url: '/workshop/rate/' + workshop_item.id + '/' + ratingType + '/remove', |
| 140 | + data: { |
| 141 | + [app_store.csrf.keys.name]: app_store.csrf.name, |
| 142 | + [app_store.csrf.keys.value]: app_store.csrf.value |
| 143 | + }, |
| 144 | + dataType: 'json', // return type data, |
| 145 | + error: function(data){ |
| 146 | + toastr.error('Something went wrong.'); |
| 147 | + }, |
| 148 | + success: function(data){ |
| 149 | + |
| 150 | + // Make sure rating went successful |
| 151 | + if(typeof data.success === 'undefined' || data.success !== true){ |
| 152 | + toastr.error('Something went wrong.'); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + // Update rating stars HTML |
| 157 | + $ratingBox.html(data.html); |
| 158 | + handleRatingHtml($ratingBox); |
| 159 | + |
| 160 | + if(data.rating_count === 0){ |
| 161 | + $('#' + ratingType + '-rating-info').text('Not rated yet'); |
| 162 | + } else { |
| 163 | + // Set template for rating info |
| 164 | + $('#' + ratingType + '-rating-info').html('<span id="' + ratingType + '-rating-score" class="text-muted"></span> out of 5' + |
| 165 | + '<span style="margin: 0 3px" class="text-muted"> • </span>' + |
| 166 | + '<span id="' + ratingType + '-rating-count" class="text-muted"></span>' |
| 167 | + ); |
| 168 | + |
| 169 | + // Update rating score |
| 170 | + $('#' + ratingType + '-rating-score').text(data.rating_score); |
| 171 | + |
| 172 | + // Update rating count |
| 173 | + if(data.rating_count === 1){ |
| 174 | + $('#' + ratingType + '-rating-count').text('1 rating'); |
| 175 | + } else { |
| 176 | + $('#' + ratingType + '-rating-count').text('' + data.rating_count + ' ratings'); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Update CSRF tokens |
| 181 | + app_store.csrf.name = data.csrf.name; |
| 182 | + app_store.csrf.value = data.csrf.value; |
| 183 | + |
| 184 | + toastr.success('You have successfully removed your rating for this workshop item.'); |
| 185 | + |
| 186 | + }, |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments