From 220cd1cf96a0ba1fd06cecfa6449b5d259fbae25 Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Fri, 20 Mar 2015 22:03:15 -0500 Subject: [PATCH 1/6] feat(rating): Added rounding logic to rating value --- src/rating/rating.js | 6 ++++++ src/rating/test/rating.spec.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/rating/rating.js b/src/rating/rating.js index 55ed9e0d44..9795331d41 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -58,6 +58,12 @@ angular.module('ui.bootstrap.rating', []) this.render = function() { $scope.value = ngModelCtrl.$viewValue; }; + + $scope.$watch('value', function(value) { + if (typeof value !== 'undefined' && value % 1 !== 0) { + $scope.value = Math.round(value); + } + }); }]) .directive('rating', function() { diff --git a/src/rating/test/rating.spec.js b/src/rating/test/rating.spec.js index c841783944..9fab31d5f6 100644 --- a/src/rating/test/rating.spec.js +++ b/src/rating/test/rating.spec.js @@ -74,6 +74,20 @@ describe('rating directive', function () { }); it('changes the number of selected icons when value changes', function() { + $rootScope.rate = 2.1; + $rootScope.$digest(); + + expect(getState()).toEqual([true, true, false, false, false]); + expect(element.attr('aria-valuenow')).toBe('2'); + + $rootScope.rate = 2.5; + $rootScope.$digest(); + + expect(getState()).toEqual([true, true, true, false, false]); + expect(element.attr('aria-valuenow')).toBe('3'); + }); + + it('rounds off the number of stars shown with decimal values', function() { $rootScope.rate = 2; $rootScope.$digest(); From bc7d1b98b26b44b9fe744d95e174ca8d3ba2d1c0 Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Sun, 22 Mar 2015 12:54:45 -0500 Subject: [PATCH 2/6] feat(rating): Moved rounding logic into a formatter --- src/rating/rating.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/rating/rating.js b/src/rating/rating.js index 9795331d41..aab9d749c1 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -12,6 +12,13 @@ angular.module('ui.bootstrap.rating', []) this.init = function(ngModelCtrl_) { ngModelCtrl = ngModelCtrl_; ngModelCtrl.$render = this.render; + + ngModelCtrl.$formatters.push(function(value) { + if (typeof value !== 'undefined' && value % 1 !== 0) { + value = Math.round(value); + } + return value; + }); this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn; this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff; @@ -58,12 +65,7 @@ angular.module('ui.bootstrap.rating', []) this.render = function() { $scope.value = ngModelCtrl.$viewValue; }; - - $scope.$watch('value', function(value) { - if (typeof value !== 'undefined' && value % 1 !== 0) { - $scope.value = Math.round(value); - } - }); + }]) .directive('rating', function() { From 2d65e2cee108364122e9bb741682b2967a688cfc Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Sun, 22 Mar 2015 20:29:08 -0500 Subject: [PATCH 3/6] feat(rating): Checking for number instead of checking if undefined --- src/rating/rating.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rating/rating.js b/src/rating/rating.js index aab9d749c1..12b90adcb7 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -14,7 +14,7 @@ angular.module('ui.bootstrap.rating', []) ngModelCtrl.$render = this.render; ngModelCtrl.$formatters.push(function(value) { - if (typeof value !== 'undefined' && value % 1 !== 0) { + if (typeof value === 'number' && value % 1 !== 0) { value = Math.round(value); } return value; From c9ba273d92d61c4f6f89399a199878241842ace1 Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Sun, 22 Mar 2015 20:48:55 -0500 Subject: [PATCH 4/6] feat(rating): Using angular.isNumber for rounding logic --- src/rating/rating.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rating/rating.js b/src/rating/rating.js index 12b90adcb7..e7300eb52f 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -14,7 +14,7 @@ angular.module('ui.bootstrap.rating', []) ngModelCtrl.$render = this.render; ngModelCtrl.$formatters.push(function(value) { - if (typeof value === 'number' && value % 1 !== 0) { + if (angular.isNumber(value) && value % 1 !== 0) { value = Math.round(value); } return value; From a9f20d0b3ae9479c7e52ec19bcf82a0f0ef6bf89 Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Sun, 22 Mar 2015 23:08:31 -0500 Subject: [PATCH 5/6] feat(rating): Using bitwise instead of modulo to check for decimel --- src/rating/rating.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rating/rating.js b/src/rating/rating.js index e7300eb52f..673c3b2a99 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -14,7 +14,7 @@ angular.module('ui.bootstrap.rating', []) ngModelCtrl.$render = this.render; ngModelCtrl.$formatters.push(function(value) { - if (angular.isNumber(value) && value % 1 !== 0) { + if (angular.isNumber(value) && value << 0 !== value) { value = Math.round(value); } return value; From 3809f3fb82aa73b1aebe49584c46c199620aac14 Mon Sep 17 00:00:00 2001 From: Brian Lewis Date: Mon, 23 Mar 2015 15:09:33 -0500 Subject: [PATCH 6/6] feat(rating): Fixing copy/paste typo in test --- src/rating/test/rating.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rating/test/rating.spec.js b/src/rating/test/rating.spec.js index 9fab31d5f6..cd92771ac9 100644 --- a/src/rating/test/rating.spec.js +++ b/src/rating/test/rating.spec.js @@ -73,7 +73,7 @@ describe('rating directive', function () { expect($rootScope.rate).toBe(3); }); - it('changes the number of selected icons when value changes', function() { + it('rounds off the number of stars shown with decimal values', function() { $rootScope.rate = 2.1; $rootScope.$digest(); @@ -87,7 +87,7 @@ describe('rating directive', function () { expect(element.attr('aria-valuenow')).toBe('3'); }); - it('rounds off the number of stars shown with decimal values', function() { + it('changes the number of selected icons when value changes', function() { $rootScope.rate = 2; $rootScope.$digest();