starrr.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var slice = [].slice;
  2. (function($, window) {
  3. var Starrr;
  4. window.Starrr = Starrr = (function() {
  5. Starrr.prototype.defaults = {
  6. rating: void 0,
  7. max: 5,
  8. readOnly: false,
  9. emptyClass: 'fa fa-star-o',
  10. fullClass: 'fa fa-star',
  11. change: function(e, value) {}
  12. };
  13. function Starrr($el, options) {
  14. this.options = $.extend({}, this.defaults, options);
  15. this.$el = $el;
  16. this.createStars();
  17. this.syncRating();
  18. if (this.options.readOnly) {
  19. return;
  20. }
  21. this.$el.on('mouseover.starrr', 'a', (function(_this) {
  22. return function(e) {
  23. return _this.syncRating(_this.getStars().index(e.currentTarget) + 1);
  24. };
  25. })(this));
  26. this.$el.on('mouseout.starrr', (function(_this) {
  27. return function() {
  28. return _this.syncRating();
  29. };
  30. })(this));
  31. this.$el.on('click.starrr', 'a', (function(_this) {
  32. return function(e) {
  33. e.preventDefault();
  34. return _this.setRating(_this.getStars().index(e.currentTarget) + 1);
  35. };
  36. })(this));
  37. this.$el.on('starrr:change', this.options.change);
  38. }
  39. Starrr.prototype.getStars = function() {
  40. return this.$el.find('a');
  41. };
  42. Starrr.prototype.createStars = function() {
  43. var j, ref, results;
  44. results = [];
  45. for (j = 1, ref = this.options.max; 1 <= ref ? j <= ref : j >= ref; 1 <= ref ? j++ : j--) {
  46. results.push(this.$el.append("<a href='#' />"));
  47. }
  48. return results;
  49. };
  50. Starrr.prototype.setRating = function(rating) {
  51. if (this.options.rating === rating) {
  52. rating = void 0;
  53. }
  54. this.options.rating = rating;
  55. this.syncRating();
  56. return this.$el.trigger('starrr:change', rating);
  57. };
  58. Starrr.prototype.getRating = function() {
  59. return this.options.rating;
  60. };
  61. Starrr.prototype.syncRating = function(rating) {
  62. var $stars, i, j, ref, results;
  63. rating || (rating = this.options.rating);
  64. $stars = this.getStars();
  65. results = [];
  66. for (i = j = 1, ref = this.options.max; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
  67. results.push($stars.eq(i - 1).removeClass(rating >= i ? this.options.emptyClass : this.options.fullClass).addClass(rating >= i ? this.options.fullClass : this.options.emptyClass));
  68. }
  69. return results;
  70. };
  71. return Starrr;
  72. })();
  73. return $.fn.extend({
  74. starrr: function() {
  75. var args, option;
  76. option = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  77. return this.each(function() {
  78. var data;
  79. data = $(this).data('starrr');
  80. if (!data) {
  81. $(this).data('starrr', (data = new Starrr($(this), option)));
  82. }
  83. if (typeof option === 'string') {
  84. return data[option].apply(data, args);
  85. }
  86. });
  87. }
  88. });
  89. })(window.jQuery, window);