Skip to content

Commit

Permalink
Ground work for more flexible fractional ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
antennaio committed Jun 2, 2016
1 parent 663ccda commit 569c74c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
26 changes: 17 additions & 9 deletions jquery.barrating.js
Expand Up @@ -207,6 +207,11 @@
}
};

// return rounded fraction of a value (14.4 -> 40, 0.99 -> 90)
var fraction = function(value) {
return Math.round(((Math.floor(value * 10) / 10) % 1) * 100);
};

// remove all classes from elements
var resetStyle = function() {
self.$widget.find('a').removeClass();
Expand All @@ -216,6 +221,9 @@
var applyStyle = function() {
var $a = self.$widget.find('a[data-rating-value="' + ratingValue() + '"]');
var initialRating = getData('userOptions').initialRating;
var baseValue = $.isNumeric(ratingValue()) ? ratingValue() : 0;
var f = fraction(initialRating);
var $all, $fractional;

resetStyle();

Expand All @@ -224,18 +232,18 @@
.addClass('br-selected');

if (!getData('ratingMade') && $.isNumeric(initialRating)) {
// compare initial rating to rating value or zero
var baseValue = $.isNumeric(ratingValue()) ? ratingValue() : 0;
if ((initialRating <= baseValue) || !f) {
return;
}

if (initialRating > baseValue) {
var $all = self.$widget.find('a');
$all = self.$widget.find('a');

var $fractionalA = ($a.length) ?
$a[(getData('userOptions').reverse) ? 'prev' : 'next']() :
$all[(getData('userOptions').reverse) ? 'last' : 'first']();
$fractional = ($a.length) ?
$a[(getData('userOptions').reverse) ? 'prev' : 'next']() :
$all[(getData('userOptions').reverse) ? 'last' : 'first']();

$fractionalA.addClass('br-half');
}
$fractional.addClass('br-fractional');
$fractional.addClass('br-fractional-' + f);
}
};

Expand Down
8 changes: 5 additions & 3 deletions test/jquery.barrating-spec.js
Expand Up @@ -110,7 +110,8 @@ describe('bar rating plugin on set fractional value', function () {
});

it('should set .br-half class', function () {
expect($('.br-widget a:nth-child(4)').hasClass('br-half')).to.equal(true);
expect($('.br-widget a:nth-child(4)').hasClass('br-fractional')).to.equal(true);
expect($('.br-widget a:nth-child(4)').hasClass('br-fractional-30')).to.equal(true);
});

});
Expand All @@ -120,7 +121,7 @@ describe('bar rating plugin on set fractional value < 1', function () {
before(function () {
createSelect();
$('#rating')
.barrating('show', { initialRating: 0.3 });
.barrating('show', { initialRating: 0.99 });
});

after(function () {
Expand All @@ -129,7 +130,8 @@ describe('bar rating plugin on set fractional value < 1', function () {
});

it('should set .br-half class', function () {
expect($('.br-widget a:first').hasClass('br-half')).to.equal(true);
expect($('.br-widget a:first').hasClass('br-fractional')).to.equal(true);
expect($('.br-widget a:first').hasClass('br-fractional-90')).to.equal(true);
});

});
Expand Down

0 comments on commit 569c74c

Please sign in to comment.