From 8b887034e394e82fdf5b52e7722ccc95725d775f Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Thu, 1 Oct 2015 15:54:18 -0700 Subject: [PATCH] feat(rating): user uib- prefix --- src/rating/docs/demo.html | 6 +-- src/rating/rating.js | 40 +++++++++++++--- src/rating/test/rating.spec.js | 88 +++++++++++++++++++++++----------- 3 files changed, 96 insertions(+), 38 deletions(-) diff --git a/src/rating/docs/demo.html b/src/rating/docs/demo.html index ab559661f9..1ad69f7a2e 100644 --- a/src/rating/docs/demo.html +++ b/src/rating/docs/demo.html @@ -1,6 +1,6 @@

Default

- + {{percent}}%
Rate: {{rate}} - Readonly is: {{isReadonly}} - Hovering over: {{overStar || "none"}}
@@ -10,6 +10,6 @@

Default


Custom icons

-
(Rate: {{x}})
-
(Rate: {{y}})
+
(Rate: {{x}})
+
(Rate: {{y}})
diff --git a/src/rating/rating.js b/src/rating/rating.js index f4edf2c912..3343b4a218 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -1,13 +1,13 @@ angular.module('ui.bootstrap.rating', []) -.constant('ratingConfig', { +.constant('uibRatingConfig', { max: 5, stateOn: null, stateOff: null, titles : ['one', 'two', 'three', 'four', 'five'] }) -.controller('RatingController', ['$scope', '$attrs', 'ratingConfig', function($scope, $attrs, ratingConfig) { +.controller('UibRatingController', ['$scope', '$attrs', 'uibRatingConfig', function($scope, $attrs, ratingConfig) { var ngModelCtrl = { $setViewValue: angular.noop }; this.init = function(ngModelCtrl_) { @@ -80,21 +80,47 @@ angular.module('ui.bootstrap.rating', []) }; }]) -.directive('rating', function() { +.directive('uibRating', function() { return { - restrict: 'EA', - require: ['rating', 'ngModel'], + require: ['uibRating', 'ngModel'], scope: { readonly: '=?', onHover: '&', onLeave: '&' }, - controller: 'RatingController', + controller: 'UibRatingController', templateUrl: 'template/rating/rating.html', replace: true, link: function(scope, element, attrs, ctrls) { var ratingCtrl = ctrls[0], ngModelCtrl = ctrls[1]; - ratingCtrl.init( ngModelCtrl ); + ratingCtrl.init(ngModelCtrl); } }; }); + +/* Deprecated rating below */ + +angular.module('ui.bootstrap.rating') + +.value('$ratingSuppressWarning', false) + +.directive('rating', ['$log', '$ratingSuppressWarning', function($log, $ratingSuppressWarning) { + return { + require: ['rating', 'ngModel'], + scope: { + readonly: '=?', + onHover: '&', + onLeave: '&' + }, + controller: 'UibRatingController', + templateUrl: 'template/rating/rating.html', + replace: true, + link: function(scope, element, attrs, ctrls) { + if (!$ratingSuppressWarning) { + $log.warn('rating is now deprecated. Use uib-rating instead.'); + } + var ratingCtrl = ctrls[0], ngModelCtrl = ctrls[1]; + ratingCtrl.init(ngModelCtrl); + } + }; +}]); diff --git a/src/rating/test/rating.spec.js b/src/rating/test/rating.spec.js index c5de00819f..19d235a072 100644 --- a/src/rating/test/rating.spec.js +++ b/src/rating/test/rating.spec.js @@ -6,7 +6,7 @@ describe('rating directive', function() { $compile = _$compile_; $rootScope = _$rootScope_; $rootScope.rate = 3; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); })); @@ -15,14 +15,14 @@ describe('rating directive', function() { } function getStar(number) { - return getStars().eq( number - 1 ); + return getStars().eq(number - 1); } function getState(classOn, classOff) { var stars = getStars(); var state = []; for (var i = 0, n = stars.length; i < n; i++) { - state.push( (stars.eq(i).hasClass(classOn || 'glyphicon-star') && ! stars.eq(i).hasClass(classOff || 'glyphicon-star-empty')) ); + state.push((stars.eq(i).hasClass(classOn || 'glyphicon-star') && ! stars.eq(i).hasClass(classOff || 'glyphicon-star-empty'))); } return state; } @@ -109,7 +109,7 @@ describe('rating directive', function() { }); it('shows different number of icons when `max` attribute is set', function() { - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); expect(getStars().length).toBe(7); @@ -118,7 +118,7 @@ describe('rating directive', function() { it('shows different number of icons when `max` attribute is from scope variable', function() { $rootScope.max = 15; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); expect(getStars().length).toBe(15); expect(element.attr('aria-valuemax')).toBe('15'); @@ -126,7 +126,7 @@ describe('rating directive', function() { it('handles readonly attribute', function() { $rootScope.isReadonly = true; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); expect(getState()).toEqual([true, true, true, false, false]); @@ -146,7 +146,7 @@ describe('rating directive', function() { it('should fire onHover', function() { $rootScope.hoveringOver = jasmine.createSpy('hoveringOver'); - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); getStar(3).trigger('mouseover'); @@ -156,7 +156,7 @@ describe('rating directive', function() { it('should fire onLeave', function() { $rootScope.leaving = jasmine.createSpy('leaving'); - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); element.trigger('mouseleave'); @@ -216,7 +216,7 @@ describe('rating directive', function() { beforeEach(inject(function() { $rootScope.classOn = 'icon-ok-sign'; $rootScope.classOff = 'icon-ok-circle'; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); })); @@ -233,7 +233,7 @@ describe('rating directive', function() { {stateOn: 'heart'}, {stateOff: 'off'} ]; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); })); @@ -256,20 +256,20 @@ describe('rating directive', function() { }); }); - describe('setting ratingConfig', function() { + describe('setting uibRatingConfig', function() { var originalConfig = {}; - beforeEach(inject(function(ratingConfig) { + beforeEach(inject(function(uibRatingConfig) { $rootScope.rate = 5; - angular.extend(originalConfig, ratingConfig); - ratingConfig.max = 10; - ratingConfig.stateOn = 'on'; - ratingConfig.stateOff = 'off'; - element = $compile('')($rootScope); + angular.extend(originalConfig, uibRatingConfig); + uibRatingConfig.max = 10; + uibRatingConfig.stateOn = 'on'; + uibRatingConfig.stateOff = 'off'; + element = $compile('')($rootScope); $rootScope.$digest(); })); - afterEach(inject(function(ratingConfig) { + afterEach(inject(function(uibRatingConfig) { // return it to the original state - angular.extend(ratingConfig, originalConfig); + angular.extend(uibRatingConfig, originalConfig); })); it('should change number of icon elements', function() { @@ -289,16 +289,16 @@ describe('rating directive', function() { describe('shows different title when `max` attribute is greater than the titles array ', function() { var originalConfig = {}; - beforeEach(inject(function(ratingConfig) { + beforeEach(inject(function(uibRatingConfig) { $rootScope.rate = 5; - angular.extend(originalConfig, ratingConfig); - ratingConfig.max = 10; - element = $compile('')($rootScope); + angular.extend(originalConfig, uibRatingConfig); + uibRatingConfig.max = 10; + element = $compile('')($rootScope); $rootScope.$digest(); })); - afterEach(inject(function(ratingConfig) { + afterEach(inject(function(uibRatingConfig) { // return it to the original state - angular.extend(ratingConfig, originalConfig); + angular.extend(uibRatingConfig, originalConfig); })); it('should return the default title for each star', function() { @@ -309,20 +309,52 @@ describe('rating directive', function() { describe('shows custom titles ', function() { it('should return the custom title for each star', function() { $rootScope.titles = [44,45,46]; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); expect(getTitles()).toEqual(['44', '45', '46', '4', '5']); }); it('should return the default title if the custom title is empty', function() { $rootScope.titles = []; - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); expect(getTitles()).toEqual(['one', 'two', 'three', 'four', 'five']); }); it('should return the default title if the custom title is not an array', function() { - element = $compile('')($rootScope); + element = $compile('')($rootScope); $rootScope.$digest(); expect(getTitles()).toEqual(['one', 'two', 'three', 'four', 'five']); }); }); }); + +/* Deprecation tests below */ + +describe('rating deprecation', function() { + beforeEach(module('ui.bootstrap.rating')); + beforeEach(module('template/rating/rating.html')); + + it('should suppress warning', function() { + module(function($provide) { + $provide.value('$ratingSuppressWarning', true); + }); + + inject(function($compile, $log, $rootScope) { + spyOn($log, 'warn'); + + var element = $compile('')($rootScope); + $rootScope.$digest(); + + expect($log.warn.calls.count()).toBe(0); + }); + }); + + it('should give warning by default', inject(function($compile, $log, $rootScope) { + spyOn($log, 'warn'); + + var element = $compile('')($rootScope); + $rootScope.$digest(); + + expect($log.warn.calls.count()).toBe(1); + expect($log.warn.calls.argsFor(0)).toEqual(['rating is now deprecated. Use uib-rating instead.']); + })); +});