UNPKG

vpn.email

Version:
166 lines (134 loc) 4.82 kB
$.widget( "metro.rating" , { version: "3.0.0", options: { stars: 5, value: 0, half: true, static: false, showScore: true, scoreTitle: "Current: ", size: 'default', colorRate: false, onRate: function(v, s, w){return true;}, onRated: function(v, s, w){} }, _value: 0, _values: [], _create: function () { var element = this.element, o = this.options; $.each(element.data(), function(key, value){ if (key in o) { try { o[key] = $.parseJSON(value); } catch (e) { o[key] = value; } } }); this._value = parseFloat(o.value); this._values[0] = Math.ceil(o.stars * 1 / 3); this._values[1] = Math.ceil(o.stars * 2 / 3); this._values[2] = o.stars; this._createRating(); this._createEvents(); this._setValue(this._value); this._setScore(this._value); element.data('rating', this); }, _createRating: function(){ var element = this.element, o = this.options; var i, star, stars, score; if (!element.hasClass('rating')) {element.addClass('rating');} switch (o.size) { case 'small': element.addClass('small'); break; case 'large': element.addClass('large'); break; default: break; } if (o.static) { element.addClass('static'); } for (i = 0; i < o.stars; i++) { star = $("<span/>").addClass('star').appendTo(element).data('star-value', i+1); } if (o.showScore) { score = $("<span/>").addClass('score').appendTo(element); } }, _createEvents: function(){ var that = this, element = this.element, o = this.options; var stars; stars = element.find('.star'); stars.on('click', function(e){ if (o.static || element.hasClass('static') || element.data('static')) { return false; } var result, value = $(this).data('star-value'), star = this, rating = that; if (typeof o.onRate === 'function') { if (!o.onRate(value, star, rating)) {return false;} } else { if (typeof window[o.onRate] === 'function') { if (!window[o.onRate](value, star, rating)) {return false;} } else { result = eval("(function(){"+o.onRate+"})"); if (!result.call(value, star, rating)) {return false;} } } if (typeof o.onRated === 'function') { o.onRated(value, star, rating); } else { if (typeof window[o.onRated] === 'function') { window[o.onRated](value, star, rating); } else { result = eval("(function(){"+o.onRated+"})"); result.call(value, star, rating); } } that._value = $(this).data('star-value'); that._setValue(); that._setScore(); e.preventDefault(); e.stopPropagation(); }); }, _setValue: function(){ var stars, o = this.options, element = this.element; if (o.stars) { stars = element.find('.star').removeClass('on half'); var index = Math.floor(this._value) - 1; var half = (this._value - Math.floor(this._value)) * 10 > 0; $(stars[index]).addClass('on'); $(stars[index]).prevAll().addClass('on'); if (half) { $(stars[index]).next().addClass('on half'); } } if (o.colorRate) { element.removeClass('poor regular good'); if (this._value <= this._values[0]) {element.addClass('poor');} else if (this._value > this._values[0] && this._value <= this._values[1]) {element.addClass('regular');} else if (this._value > this._values[1]) {element.addClass('good');} } }, _setScore: function(){ var value = this._value, element = this.element, o = this.options; if (value !== undefined) { element.find(".score").html(o.scoreTitle + value); } }, value: function(value){ if (value !== undefined) { this._value = value; this._setValue(); this._setScore(); } else { return this._value; } }, _destroy: function () { }, _setOption: function ( key, value ) { this._super('_setOption', key, value); } });