Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 377b4b7

Browse files
committed
feat(rating): user uib- prefix
Closes #4502
1 parent fdf53e6 commit 377b4b7

File tree

3 files changed

+96
-38
lines changed

3 files changed

+96
-38
lines changed

src/rating/docs/demo.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div ng-controller="RatingDemoCtrl">
22
<h4>Default</h4>
3-
<rating ng-model="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" titles="['one','two','three']" aria-labelledby="default-rating"></rating>
3+
<uib-rating ng-model="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" titles="['one','two','three']" aria-labelledby="default-rating"></uib-rating>
44
<span class="label" ng-class="{'label-warning': percent<30, 'label-info': percent>=30 && percent<70, 'label-success': percent>=70}" ng-show="overStar && !isReadonly">{{percent}}%</span>
55

66
<pre style="margin:15px 0;">Rate: <b>{{rate}}</b> - Readonly is: <i>{{isReadonly}}</i> - Hovering over: <b>{{overStar || "none"}}</b></pre>
@@ -10,6 +10,6 @@ <h4>Default</h4>
1010
<hr />
1111

1212
<h4>Custom icons</h4>
13-
<div ng-init="x = 5"><rating ng-model="x" max="15" state-on="'glyphicon-ok-sign'" state-off="'glyphicon-ok-circle'" aria-labelledby="custom-icons-1"></rating> <b>(<i>Rate:</i> {{x}})</b></div>
14-
<div ng-init="y = 2"><rating ng-model="y" rating-states="ratingStates" aria-labelledby="custom-icons-2"></rating> <b>(<i>Rate:</i> {{y}})</b></div>
13+
<div ng-init="x = 5"><uib-rating ng-model="x" max="15" state-on="'glyphicon-ok-sign'" state-off="'glyphicon-ok-circle'" aria-labelledby="custom-icons-1"></uib-rating> <b>(<i>Rate:</i> {{x}})</b></div>
14+
<div ng-init="y = 2"><uib-rating ng-model="y" rating-states="ratingStates" aria-labelledby="custom-icons-2"></uib-rating> <b>(<i>Rate:</i> {{y}})</b></div>
1515
</div>

src/rating/rating.js

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
angular.module('ui.bootstrap.rating', [])
22

3-
.constant('ratingConfig', {
3+
.constant('uibRatingConfig', {
44
max: 5,
55
stateOn: null,
66
stateOff: null,
77
titles : ['one', 'two', 'three', 'four', 'five']
88
})
99

10-
.controller('RatingController', ['$scope', '$attrs', 'ratingConfig', function($scope, $attrs, ratingConfig) {
10+
.controller('UibRatingController', ['$scope', '$attrs', 'uibRatingConfig', function($scope, $attrs, ratingConfig) {
1111
var ngModelCtrl = { $setViewValue: angular.noop };
1212

1313
this.init = function(ngModelCtrl_) {
@@ -80,21 +80,47 @@ angular.module('ui.bootstrap.rating', [])
8080
};
8181
}])
8282

83-
.directive('rating', function() {
83+
.directive('uibRating', function() {
8484
return {
85-
restrict: 'EA',
86-
require: ['rating', 'ngModel'],
85+
require: ['uibRating', 'ngModel'],
8786
scope: {
8887
readonly: '=?',
8988
onHover: '&',
9089
onLeave: '&'
9190
},
92-
controller: 'RatingController',
91+
controller: 'UibRatingController',
9392
templateUrl: 'template/rating/rating.html',
9493
replace: true,
9594
link: function(scope, element, attrs, ctrls) {
9695
var ratingCtrl = ctrls[0], ngModelCtrl = ctrls[1];
97-
ratingCtrl.init( ngModelCtrl );
96+
ratingCtrl.init(ngModelCtrl);
9897
}
9998
};
10099
});
100+
101+
/* Deprecated rating below */
102+
103+
angular.module('ui.bootstrap.rating')
104+
105+
.value('$ratingSuppressWarning', false)
106+
107+
.directive('rating', ['$log', '$ratingSuppressWarning', function($log, $ratingSuppressWarning) {
108+
return {
109+
require: ['rating', 'ngModel'],
110+
scope: {
111+
readonly: '=?',
112+
onHover: '&',
113+
onLeave: '&'
114+
},
115+
controller: 'UibRatingController',
116+
templateUrl: 'template/rating/rating.html',
117+
replace: true,
118+
link: function(scope, element, attrs, ctrls) {
119+
if (!$ratingSuppressWarning) {
120+
$log.warn('rating is now deprecated. Use uib-rating instead.');
121+
}
122+
var ratingCtrl = ctrls[0], ngModelCtrl = ctrls[1];
123+
ratingCtrl.init(ngModelCtrl);
124+
}
125+
};
126+
}]);

src/rating/test/rating.spec.js

+60-28
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('rating directive', function() {
66
$compile = _$compile_;
77
$rootScope = _$rootScope_;
88
$rootScope.rate = 3;
9-
element = $compile('<rating ng-model="rate"></rating>')($rootScope);
9+
element = $compile('<uib-rating ng-model="rate"></uib-rating>')($rootScope);
1010
$rootScope.$digest();
1111
}));
1212

@@ -15,14 +15,14 @@ describe('rating directive', function() {
1515
}
1616

1717
function getStar(number) {
18-
return getStars().eq( number - 1 );
18+
return getStars().eq(number - 1);
1919
}
2020

2121
function getState(classOn, classOff) {
2222
var stars = getStars();
2323
var state = [];
2424
for (var i = 0, n = stars.length; i < n; i++) {
25-
state.push( (stars.eq(i).hasClass(classOn || 'glyphicon-star') && ! stars.eq(i).hasClass(classOff || 'glyphicon-star-empty')) );
25+
state.push((stars.eq(i).hasClass(classOn || 'glyphicon-star') && ! stars.eq(i).hasClass(classOff || 'glyphicon-star-empty')));
2626
}
2727
return state;
2828
}
@@ -109,7 +109,7 @@ describe('rating directive', function() {
109109
});
110110

111111
it('shows different number of icons when `max` attribute is set', function() {
112-
element = $compile('<rating ng-model="rate" max="7"></rating>')($rootScope);
112+
element = $compile('<uib-rating ng-model="rate" max="7"></uib-rating>')($rootScope);
113113
$rootScope.$digest();
114114

115115
expect(getStars().length).toBe(7);
@@ -118,15 +118,15 @@ describe('rating directive', function() {
118118

119119
it('shows different number of icons when `max` attribute is from scope variable', function() {
120120
$rootScope.max = 15;
121-
element = $compile('<rating ng-model="rate" max="max"></rating>')($rootScope);
121+
element = $compile('<uib-rating ng-model="rate" max="max"></uib-rating>')($rootScope);
122122
$rootScope.$digest();
123123
expect(getStars().length).toBe(15);
124124
expect(element.attr('aria-valuemax')).toBe('15');
125125
});
126126

127127
it('handles readonly attribute', function() {
128128
$rootScope.isReadonly = true;
129-
element = $compile('<rating ng-model="rate" readonly="isReadonly"></rating>')($rootScope);
129+
element = $compile('<uib-rating ng-model="rate" readonly="isReadonly"></uib-rating>')($rootScope);
130130
$rootScope.$digest();
131131

132132
expect(getState()).toEqual([true, true, true, false, false]);
@@ -146,7 +146,7 @@ describe('rating directive', function() {
146146

147147
it('should fire onHover', function() {
148148
$rootScope.hoveringOver = jasmine.createSpy('hoveringOver');
149-
element = $compile('<rating ng-model="rate" on-hover="hoveringOver(value)"></rating>')($rootScope);
149+
element = $compile('<uib-rating ng-model="rate" on-hover="hoveringOver(value)"></uib-rating>')($rootScope);
150150
$rootScope.$digest();
151151

152152
getStar(3).trigger('mouseover');
@@ -156,7 +156,7 @@ describe('rating directive', function() {
156156

157157
it('should fire onLeave', function() {
158158
$rootScope.leaving = jasmine.createSpy('leaving');
159-
element = $compile('<rating ng-model="rate" on-leave="leaving()"></rating>')($rootScope);
159+
element = $compile('<uib-rating ng-model="rate" on-leave="leaving()"></uib-rating>')($rootScope);
160160
$rootScope.$digest();
161161

162162
element.trigger('mouseleave');
@@ -216,7 +216,7 @@ describe('rating directive', function() {
216216
beforeEach(inject(function() {
217217
$rootScope.classOn = 'icon-ok-sign';
218218
$rootScope.classOff = 'icon-ok-circle';
219-
element = $compile('<rating ng-model="rate" state-on="classOn" state-off="classOff"></rating>')($rootScope);
219+
element = $compile('<uib-rating ng-model="rate" state-on="classOn" state-off="classOff"></uib-rating>')($rootScope);
220220
$rootScope.$digest();
221221
}));
222222

@@ -233,7 +233,7 @@ describe('rating directive', function() {
233233
{stateOn: 'heart'},
234234
{stateOff: 'off'}
235235
];
236-
element = $compile('<rating ng-model="rate" rating-states="states"></rating>')($rootScope);
236+
element = $compile('<uib-rating ng-model="rate" rating-states="states"></uib-rating>')($rootScope);
237237
$rootScope.$digest();
238238
}));
239239

@@ -256,20 +256,20 @@ describe('rating directive', function() {
256256
});
257257
});
258258

259-
describe('setting ratingConfig', function() {
259+
describe('setting uibRatingConfig', function() {
260260
var originalConfig = {};
261-
beforeEach(inject(function(ratingConfig) {
261+
beforeEach(inject(function(uibRatingConfig) {
262262
$rootScope.rate = 5;
263-
angular.extend(originalConfig, ratingConfig);
264-
ratingConfig.max = 10;
265-
ratingConfig.stateOn = 'on';
266-
ratingConfig.stateOff = 'off';
267-
element = $compile('<rating ng-model="rate"></rating>')($rootScope);
263+
angular.extend(originalConfig, uibRatingConfig);
264+
uibRatingConfig.max = 10;
265+
uibRatingConfig.stateOn = 'on';
266+
uibRatingConfig.stateOff = 'off';
267+
element = $compile('<uib-rating ng-model="rate"></uib-rating>')($rootScope);
268268
$rootScope.$digest();
269269
}));
270-
afterEach(inject(function(ratingConfig) {
270+
afterEach(inject(function(uibRatingConfig) {
271271
// return it to the original state
272-
angular.extend(ratingConfig, originalConfig);
272+
angular.extend(uibRatingConfig, originalConfig);
273273
}));
274274

275275
it('should change number of icon elements', function() {
@@ -289,16 +289,16 @@ describe('rating directive', function() {
289289

290290
describe('shows different title when `max` attribute is greater than the titles array ', function() {
291291
var originalConfig = {};
292-
beforeEach(inject(function(ratingConfig) {
292+
beforeEach(inject(function(uibRatingConfig) {
293293
$rootScope.rate = 5;
294-
angular.extend(originalConfig, ratingConfig);
295-
ratingConfig.max = 10;
296-
element = $compile('<rating ng-model="rate"></rating>')($rootScope);
294+
angular.extend(originalConfig, uibRatingConfig);
295+
uibRatingConfig.max = 10;
296+
element = $compile('<uib-rating ng-model="rate"></uib-rating>')($rootScope);
297297
$rootScope.$digest();
298298
}));
299-
afterEach(inject(function(ratingConfig) {
299+
afterEach(inject(function(uibRatingConfig) {
300300
// return it to the original state
301-
angular.extend(ratingConfig, originalConfig);
301+
angular.extend(uibRatingConfig, originalConfig);
302302
}));
303303

304304
it('should return the default title for each star', function() {
@@ -309,20 +309,52 @@ describe('rating directive', function() {
309309
describe('shows custom titles ', function() {
310310
it('should return the custom title for each star', function() {
311311
$rootScope.titles = [44,45,46];
312-
element = $compile('<rating ng-model="rate" titles="titles"></rating>')($rootScope);
312+
element = $compile('<uib-rating ng-model="rate" titles="titles"></uib-rating>')($rootScope);
313313
$rootScope.$digest();
314314
expect(getTitles()).toEqual(['44', '45', '46', '4', '5']);
315315
});
316316
it('should return the default title if the custom title is empty', function() {
317317
$rootScope.titles = [];
318-
element = $compile('<rating ng-model="rate" titles="titles"></rating>')($rootScope);
318+
element = $compile('<uib-rating ng-model="rate" titles="titles"></uib-rating>')($rootScope);
319319
$rootScope.$digest();
320320
expect(getTitles()).toEqual(['one', 'two', 'three', 'four', 'five']);
321321
});
322322
it('should return the default title if the custom title is not an array', function() {
323-
element = $compile('<rating ng-model="rate" titles="test"></rating>')($rootScope);
323+
element = $compile('<uib-rating ng-model="rate" titles="test"></uib-rating>')($rootScope);
324324
$rootScope.$digest();
325325
expect(getTitles()).toEqual(['one', 'two', 'three', 'four', 'five']);
326326
});
327327
});
328328
});
329+
330+
/* Deprecation tests below */
331+
332+
describe('rating deprecation', function() {
333+
beforeEach(module('ui.bootstrap.rating'));
334+
beforeEach(module('template/rating/rating.html'));
335+
336+
it('should suppress warning', function() {
337+
module(function($provide) {
338+
$provide.value('$ratingSuppressWarning', true);
339+
});
340+
341+
inject(function($compile, $log, $rootScope) {
342+
spyOn($log, 'warn');
343+
344+
var element = $compile('<rating ng-model="rate"></rating>')($rootScope);
345+
$rootScope.$digest();
346+
347+
expect($log.warn.calls.count()).toBe(0);
348+
});
349+
});
350+
351+
it('should give warning by default', inject(function($compile, $log, $rootScope) {
352+
spyOn($log, 'warn');
353+
354+
var element = $compile('<rating ng-model="rate"></rating>')($rootScope);
355+
$rootScope.$digest();
356+
357+
expect($log.warn.calls.count()).toBe(1);
358+
expect($log.warn.calls.argsFor(0)).toEqual(['rating is now deprecated. Use uib-rating instead.']);
359+
}));
360+
});

0 commit comments

Comments
 (0)