$.holdReady(true); if (typeof lang_id == "undefined") lang_id = 1; $.get(base_url + '/global_include/templates/global/rating_v2/rating.cfg',function(content){ var config = content.split(/\[.\]/).filter(Boolean); config = config[parseInt(lang_id-1)].split(/\n/).filter(Boolean); $.each(config,function(i,v){ eval(v); }) $.holdReady(false); }) $(function () { $('.gSRh').each(function() { var _option = $(this).data('options'); try { _option = $.parseJSON(window.atou(_option)); } catch { _option = $.parseJSON(_option); } $(this).addRating({ fieldName : $(this).attr('name'), fieldId : $(this).attr('id'), icon: $(this).attr('icon') || 'star', options: _option || [], selectedRatings : _option.rate.avgrate }) showrate($(this), _option); }) }); (function ($) { $.fn.addRating = function (options) { var obj = this; var settings = $.extend({ max: 5, half: true, fieldName: 'rating', fieldId: 'rating', icon: 'star', selectedRatings:0, }, options); this.settings = settings; // create the stars for (var i = 1; i <= settings.max; i++) { var star = $('') .attr('mticon',true) .html(this.settings.icon + '_border') .data('rating', i) .appendTo(this) .on('click', function () { if (settings.options.disable == undefined || !settings.options.disable) obj.setRating($(this).data('rating')); }) .on('mouseover', function() { if (settings.options.disable == undefined || !settings.options.disable) obj.showRating($(this).data('rating'), false); }) .on('mouseleave', function() { if (settings.options.disable == undefined || !settings.options.disable) obj.showRating(settings.selectedRatings, false); }); } $(this).append(''); obj.showRating(settings.selectedRatings, true); }; $.fn.setRating = function (numRating) { var obj = this; var _current = $('#' + obj.settings.fieldId).find('input').val(); $('#' + obj.settings.fieldId).find('input').val(numRating); $.when(common_request({ url : obj.settings.options.url, data : { "star":numRating, "item_id": obj.settings.options.recordid }, }, 'rest')) .then(function(res) { if (res == 1) { global_message({type:'error', message: global_message_success_rating}); ++obj.settings.options.rate.count; obj.settings.options.rate.rate = parseInt(obj.settings.options.rate.rate) + parseInt(numRating); obj.settings.options.rate.avgrate = ((parseFloat(obj.settings.options.rate.rate) + parseInt(numRating)) / obj.settings.options.rate.count).toFixed(1); showrate($(obj), obj.settings.options); obj.showRating(numRating, true); } else if (res == 'repeat') { $('#' + obj.settings.fieldId).find('input').val(_current); obj.showRating(_current, true); global_message({type:'error', message: global_message_repeat_rating}); } }) }; $.fn.showRating = function (numRating, force) { var obj = this; if ($('#' + obj.settings.fieldId).val() == '' || force) { $(obj).find('i').each(function () { var icon = obj.settings.icon + '_border'; $(this).removeClass('selected'); if ($(this).data('rating') <= numRating) { icon = obj.settings.icon; $(this).addClass('selected'); } if (obj.settings.half && numRating < $(this).data('rating') && numRating > ($(this).data('rating') - 1)) { icon = obj.settings.icon + '_half'; $(this).addClass('half-selected'); } $(this).html(icon); }) } } }(jQuery)); function showrate(el, params) { if (params.showrate) { if (params.templates) { result = params.templates.replace(/\(\([a-z]+\)\)/gi, function(x) { var str = x.replace('((','').replace('))',''); v = str.replace(str, params.rate[str]) return v; }) } else { result = "
"; } el.find('.show_rating').remove(); el.append(result) } }