1(function ($) { 2 3 var defaults = { 4 avgRateAttr: "data-average_rating", 5 totalRatingsAttr: "data-rates_count", 6 idAttr: "data-media_id", 7 loadAll: true, 8 starSelector: '.ratings_stars', 9 widgetSelector: '.rate_widget', 10 onRate: function() {} 11 }; 12 13 var settings; 14 15 var methods = { 16 init: function (options) { 17 18 settings = $.extend( defaults, options ); 19 20 if(settings.starSelector) methods._loadSelectors(this); 21 22 if(settings.loadAll) { 23 methods._loadAll(this); 24 } 25 26 return true; 27 }, 28 29 load: function () { 30 methods._loadAll(this); 31 }, 32 33 disable: function () { 34 this.data('rating_data', {disabled: true}); 35 }, 36 37 _loadAll: function (obj) { 38 obj.find(settings.widgetRating).each(function (i) { 39 var $this = $(this); 40 methods._loadRatings($this); 41 }); 42 }, 43 44 _isDisabled: function (obj) { 45 if(obj.data('rating_data') && obj.data('rating_data').disabled) return true; 46 else return false; 47 }, 48 49 _starsToBoost: function (rate) { 50 if (!rate) return 0; 51 rate --; 52 var map = [[-15,-10],[-10,-5],[5,10],[15,20],[25,30]]; 53 return Math.floor(Math.random() * (map[rate][1] - map[rate][0] + 1)) + map[rate][0]; 54 }, 55 56 _loadSelectors: function (obj) { 57 // Handles the mouseover 58 var starSelector = settings.widgetSelector + ' ' + settings.starSelector; 59 var onRate = settings.onRate; 60 61 $(obj).on('mouseover', starSelector, function() { 62 if(methods._isDisabled($(this).parent())) return false; 63 //alert('starSelec'); 64 $(this).prevAll().andSelf().addClass('ratings_over'); 65 var $boost_val = $(this).parents('.imageview_bottom_rating').find('.boost_val'), 66 $current_val = $(this).parents('.imageview_bottom_rating').find('.current_val'); 67 if ($boost_val.length > 0) { 68 var boost_val = methods._starsToBoost($(this).index()+1); 69 $boost_val.html(boost_val>0?'+'+boost_val:boost_val); 70 $current_val.html(boost_val+$current_val.data('value')>100?100:( 71 boost_val+$current_val.data('value')<1?0:boost_val+$current_val.data('value') 72 )); 73 window.boost_val = boost_val; 74 } 75 //$(this).nextAll().removeClass('ratings_vote'); 76 $(this).nextAll().attr('class', function(i, c){ return c.replace(/\bratings_vote\S*/g, ''); }); 77 }); 78 // Handles the mouseout 79 $(obj).on('mouseout', starSelector, function() { 80 if(methods._isDisabled($(this).parent())) return false; 81 $(this).prevAll().andSelf().removeClass('ratings_over'); 82 methods._loadRatings($(this).parent()); 83 $(this).parents('.imageview_bottom_rating').find('.boost_val').html('+0'); 84 var $current_val = $(this).parents('.imageview_bottom_rating').find('.current_val'); 85 $current_val.html($current_val.data('value')); 86 }); 87 // Handles the click 88 $(obj).on('click', starSelector, function() { 89 if(methods._isDisabled($(this).parent())) return false; 90 if(typeof onRate == 'function') { 91 var $current_val = $(this).parents('.imageview_bottom_rating').find('.current_val'); 92 $current_val.data('value', parseInt($current_val.text())); 93 onRate($(this).attr('class').split(' ')[0].split('_')[1], $(this)); 94 } 95 }); 96 }, 97 98 _loadRatings: function (obj) { 99 if(!obj) return; 100 var count = obj.attr(settings.totalRatingsAttr); 101 var orig_avg = obj.attr(settings.avgRateAttr); 102 var avg = Math.floor(orig_avg); 103 var avg_diff = orig_avg - avg; 104 var label = (count > 0) 105 ? orig_avg + " picture rating <span class='rates_count'> (" + count + ")<span>" 106 : "No rating"; 107 108 obj.find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote'); 109 //obj.find('.star_' + avg).nextAll().removeClass('ratings_vote'); 110 111 obj.find('.star_' + avg).nextAll().attr('class', function(i, c){ return c.replace(/\bratings_vote\S*/g, ''); }); 112 113 //calculate half star 114 if( avg_diff > 0.29) 115 obj.find('.star_' + avg).next().addClass('ratings_vote_half'); 116 117 //update ratings_value and ratings_count value 118 if($('#ratings_count').length > 0 ) $('#ratings_count').html(count); 119 if($('#ratings_value').length > 0 ) $('#ratings_value').html(orig_avg); 120 if($('#average_rating').length > 0 ) $('#average_rating').html(label); 121 }, 122 123 destroy: function () { 124 } 125 }; 126 127 $.fn.rating = function (method) { 128 129 if (methods[method]) { 130 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 131 } else if (typeof method === 'object' || !method) { 132 return methods.init.apply(this, arguments); 133 } else { 134 $.error('Method ' + method + ' does not exist on jQuery.PIRating'); 135 } 136 137 }; 138 139})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.