Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 8f6e26b

Browse files
committed
docs(cb-rating-component): cookbook about creating a rating component
1 parent 1a154da commit 8f6e26b

File tree

18 files changed

+337
-0
lines changed

18 files changed

+337
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
describe('Rating compomnent', function () {
2+
3+
beforeAll(function () {
4+
browser.get('');
5+
});
6+
7+
it('should show 5 stars for windstorm', function () {
8+
var windstormRating = element.all(by.tagName('my-hero-rating')).get(0);
9+
var windstormStars = windstormRating.all(by.css('.glyphicon-star'));
10+
expect(windstormStars.count()).toBe(5);
11+
});
12+
13+
it('should show 1 star for bombasto', function() {
14+
var bombastoRating = element.all(by.tagName('my-hero-rating')).get(1);
15+
var bombastoStars = bombastoRating.all(by.css('.glyphicon-star'));
16+
expect(bombastoStars.count()).toBe(1);
17+
});
18+
19+
it('should change the rate on click', function() {
20+
var bombastoRating = element.all(by.tagName('my-hero-rating')).get(1);
21+
var bombastoFourthStar = bombastoRating.all(by.css('.glyphicon')).get(3);
22+
bombastoFourthStar.click().then(function() {
23+
var bombastoStars = bombastoRating.all(by.css('.glyphicon-star'));
24+
expect(bombastoStars.count()).toBe(4);
25+
26+
var div = element.all(by.tagName('div')).get(0);
27+
expect(div.getText()).toEqual('Bombasto rate is 4');
28+
});
29+
});
30+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.js
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
4+
import {HeroRatingComponent} from './rating.component';
5+
6+
@Component({
7+
selector: 'my-app',
8+
template: `
9+
<label>Windstorm: </label>
10+
<my-hero-rating></my-hero-rating>
11+
`,
12+
directives: [HeroRatingComponent]
13+
})
14+
export class AppComponent { }
15+
// #enddocregion
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Component} from 'angular2/core';
2+
3+
import {HeroRatingComponent} from './rating.component';
4+
5+
@Component({
6+
selector: 'my-app',
7+
template: `
8+
<p>
9+
<label>Windstorm: </label>
10+
<my-hero-rating rate="5"></my-hero-rating>
11+
</p>
12+
<p>
13+
<label>Bombasto: </label>
14+
<my-hero-rating [(rate)]="bombasto"></my-hero-rating>
15+
</p>
16+
<div>Bombasto rate is {{bombasto}}</div>
17+
`,
18+
directives: [HeroRatingComponent]
19+
})
20+
export class AppComponent {
21+
bombasto: number = 1;
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {bootstrap} from 'angular2/platform/browser';
2+
import {AppComponent} from './app.component';
3+
4+
bootstrap(AppComponent, [])
5+
.catch((err:any) => console.error(err));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
template: '<div>Rating</div>'
7+
})
8+
export class HeroRatingComponent { }
9+
// #enddocregion
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion
2+
import {Component} from 'angular2/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
// #docregion template
7+
template: `
8+
<template ngFor [ngForOf]="range" #index="index">
9+
<span class="sr-only">(*)</span>
10+
<i class="glyphicon glyphicon-star"></i>
11+
</template>
12+
`
13+
// #enddocregion template
14+
})
15+
export class HeroRatingComponent { }
16+
// #enddocregion
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// #docregion
2+
import {Component, Input} from 'angular2/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
// #docregion template
7+
template: `
8+
<template ngFor [ngForOf]="range" #index="index">
9+
<span class="sr-only">({{ index < rate ? '*' : ' ' }})</span>
10+
<i class="glyphicon {{index < rate ? 'glyphicon-star' : 'glyphicon-star-empty'}}"></i>
11+
</template>
12+
`
13+
// #enddocregion template
14+
})
15+
export class HeroRatingComponent {
16+
@Input() rate: number;
17+
// #docregion update-method
18+
update(value: number) {
19+
this.rate = value;
20+
}
21+
// #enddocregion
22+
}
23+
// #enddocregion
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// #docregion
2+
import {Component, Input, Output, EventEmitter} from 'angular2/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
// #docregion rating-template
7+
template: `
8+
<template ngFor [ngForOf]="range" #index="index">
9+
<span class="sr-only">({{ index < rate ? '*' : ' ' }})</span>
10+
<i class="glyphicon {{index < rate ? 'glyphicon-star' : 'glyphicon-star-empty'}}" (click)="update(index + 1)"></i>
11+
</template>
12+
`
13+
// #enddocregion rating-template
14+
})
15+
export class HeroRatingComponent {
16+
// #docregion range-attribute
17+
range = new Array(5);
18+
// #enddocregion range-attribute
19+
20+
// #docregion rate-input
21+
@Input() rate: number;
22+
// #enddocregion rate-input
23+
// #docregion rate-output
24+
@Output() rateChange: EventEmitter<number> = new EventEmitter<number>();
25+
// #enddocregion rate-output
26+
27+
// #docregion update-method
28+
update(value: number) {
29+
this.rate = value;
30+
this.rateChange.emit(value);
31+
}
32+
// #enddocregion update-method
33+
}
34+
// #enddocregion

public/docs/_examples/cb-rating-component/ts/demo.css

Whitespace-only changes.

0 commit comments

Comments
 (0)