starrr.js
124 lines
| 1 | var __slice = [].slice; |
| 2 | |
| 3 | (function(jQuery, window) { |
| 4 | var Starrr; |
| 5 | window.Starrr = Starrr = (function() { |
| 6 | Starrr.prototype.defaults = { |
| 7 | rating: void 0, |
| 8 | numStars: 5, |
| 9 | emptyStarClass: 'fa fa-star-o', |
| 10 | fullStarClass: 'fa fa-star', |
| 11 | change: function(e, value) {} |
| 12 | }; |
| 13 | |
| 14 | function Starrr($el, options) { |
| 15 | var i, _, _ref; |
| 16 | this.options = jQuery.extend({}, this.defaults, options); |
| 17 | this.$el = $el; |
| 18 | _ref = this.defaults; |
| 19 | for (i in _ref) { |
| 20 | _ = _ref[i]; |
| 21 | if (this.$el.data(i.toLowerCase()) != null) { |
| 22 | this.options[i] = this.$el.data(i.toLowerCase()); |
| 23 | } |
| 24 | } |
| 25 | if (this.$el.data('connected-input')) { |
| 26 | this.$connectedInput = jQuery("[name=\"" + (this.$el.data('connected-input')) + "\"]"); |
| 27 | this.options.rating = this.$connectedInput.val() ? parseInt(this.$connectedInput.val(), 10) : void 0; |
| 28 | } |
| 29 | this.createStars(); |
| 30 | this.syncRating(); |
| 31 | if (this.$connectedInput && this.$connectedInput.is(':disabled')) { |
| 32 | return; |
| 33 | } |
| 34 | this.$el.on('mouseover.starrr', 'i', (function(_this) { |
| 35 | return function(e) { |
| 36 | return _this.syncRating(_this.getStars().index(e.currentTarget) + 1); |
| 37 | }; |
| 38 | })(this)); |
| 39 | this.$el.on('mouseout.starrr', (function(_this) { |
| 40 | return function() { |
| 41 | return _this.syncRating(); |
| 42 | }; |
| 43 | })(this)); |
| 44 | this.$el.on('click.starrr', 'i', (function(_this) { |
| 45 | return function(e) { |
| 46 | return _this.setRating(_this.getStars().index(e.currentTarget) + 1); |
| 47 | }; |
| 48 | })(this)); |
| 49 | this.$el.on('starrr:change', this.options.change); |
| 50 | if (this.$connectedInput != null) { |
| 51 | this.$el.on('starrr:change', (function(_this) { |
| 52 | return function(e, value) { |
| 53 | _this.$connectedInput.val(value); |
| 54 | return _this.$connectedInput.trigger('focusout'); |
| 55 | }; |
| 56 | })(this)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | Starrr.prototype.getStars = function() { |
| 61 | return this.$el.find('i'); |
| 62 | }; |
| 63 | |
| 64 | Starrr.prototype.createStars = function() { |
| 65 | var _i, _ref, _results; |
| 66 | _results = []; |
| 67 | for (_i = 1, _ref = this.options.numStars; 1 <= _ref ? _i <= _ref : _i >= _ref; 1 <= _ref ? _i++ : _i--) { |
| 68 | _results.push(this.$el.append("<i class='" + this.options.emptyStarClass + "'></i>")); |
| 69 | } |
| 70 | return _results; |
| 71 | }; |
| 72 | |
| 73 | Starrr.prototype.setRating = function(rating) { |
| 74 | if (this.options.rating === rating) { |
| 75 | rating = void 0; |
| 76 | } |
| 77 | this.options.rating = rating; |
| 78 | this.syncRating(); |
| 79 | return this.$el.trigger('starrr:change', rating); |
| 80 | }; |
| 81 | |
| 82 | Starrr.prototype.getRating = function() { |
| 83 | return this.options.rating; |
| 84 | }; |
| 85 | |
| 86 | Starrr.prototype.syncRating = function(rating) { |
| 87 | var i, _i, _j, _ref, _ref1; |
| 88 | rating || (rating = this.options.rating); |
| 89 | if (rating) { |
| 90 | for (i = _i = 0, _ref = rating - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { |
| 91 | this.getStars().eq(i).removeClass(this.options.emptyStarClass).addClass(this.options.fullStarClass); |
| 92 | } |
| 93 | } |
| 94 | if (rating && rating < this.options.numStars) { |
| 95 | for (i = _j = rating, _ref1 = this.options.numStars - 1; rating <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = rating <= _ref1 ? ++_j : --_j) { |
| 96 | this.getStars().eq(i).removeClass(this.options.fullStarClass).addClass(this.options.emptyStarClass); |
| 97 | } |
| 98 | } |
| 99 | if (!rating) { |
| 100 | return this.getStars().removeClass(this.options.fullStarClass).addClass(this.options.emptyStarClass); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | return Starrr; |
| 105 | |
| 106 | })(); |
| 107 | return jQuery.fn.extend({ |
| 108 | starrr: function() { |
| 109 | var args, option; |
| 110 | option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; |
| 111 | return this.each(function() { |
| 112 | var data; |
| 113 | data = jQuery(this).data('starrr'); |
| 114 | if (!data) { |
| 115 | jQuery(this).data('starrr', (data = new Starrr(jQuery(this), option))); |
| 116 | } |
| 117 | if (typeof option === 'string') { |
| 118 | return data[option].apply(data, args); |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | }); |
| 123 | })(window.jQuery, window); |
| 124 |