Skip to content

Commit 49b278c

Browse files
IgorMinarpetebacondarwin
authored andcommitted
step-10 image swapping with ng:click
In the phone detail view, clicking on a thumbnail image, changes the main phone image to be the large version of the thumbnail image. - Define mainImageUrl model variable in the PhoneDetailCtrl and set its default value - Create setImage controller method to change mainImageUrl - Register ng:click handler for thumb images to use setImage controller method - Add e2e tests for this feature - Add css to change the mouse cursor when user points at thumnail images
1 parent 471d0f6 commit 49b278c

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

app/css/app.css

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ ul.phone-thumbs img {
5252
padding: 1em;
5353
}
5454

55+
ul.phone-thumbs img:hover {
56+
cursor: pointer;
57+
}
58+
59+
5560
ul.specs {
5661
clear: both;
5762
margin: 0;

app/js/controllers.js

+5
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', '$h
1717
function($scope, $routeParams, $http) {
1818
$http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) {
1919
$scope.phone = data;
20+
$scope.mainImageUrl = data.images[0];
2021
});
22+
23+
$scope.setImage = function(imageUrl) {
24+
$scope.mainImageUrl = imageUrl;
25+
};
2126
}]);

app/partials/phone-detail.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<img ng-src="{{phone.images[0]}}" class="phone">
1+
<img ng-src="{{mainImageUrl}}" class="phone">
22

33
<h1>{{phone.name}}</h1>
44

55
<p>{{phone.description}}</p>
66

77
<ul class="phone-thumbs">
88
<li ng-repeat="img in phone.images">
9-
<img ng-src="{{img}}">
9+
<img ng-src="{{img}}" ng-click="setImage(img)">
1010
</li>
1111
</ul>
1212

test/e2e/scenarios.js

+14
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,19 @@ describe('PhoneCat App', function() {
8383
it('should display nexus-s page', function() {
8484
expect(element(by.binding('phone.name')).getText()).toBe('Nexus S');
8585
});
86+
87+
88+
it('should display the first phone image as the main phone image', function() {
89+
expect(element(by.css('img.phone')).getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/);
90+
});
91+
92+
93+
it('should swap main image if a thumbnail image is clicked on', function() {
94+
element(by.css('.phone-thumbs li:nth-child(3) img')).click();
95+
expect(element(by.css('img.phone')).getAttribute('src')).toMatch(/img\/phones\/nexus-s.2.jpg/);
96+
97+
element(by.css('.phone-thumbs li:nth-child(1) img')).click();
98+
expect(element(by.css('img.phone')).getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/);
99+
});
86100
});
87101
});

test/unit/controllersSpec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ describe('PhoneCat controllers', function() {
3434

3535

3636
describe('PhoneDetailCtrl', function(){
37-
var scope, $httpBackend, ctrl;
37+
var scope, $httpBackend, ctrl,
38+
xyzPhoneData = function() {
39+
return {
40+
name: 'phone xyz',
41+
images: ['image/url1.png', 'image/url2.png']
42+
}
43+
};
44+
3845

3946
beforeEach(inject(function(_$httpBackend_, $rootScope, $routeParams, $controller) {
4047
$httpBackend = _$httpBackend_;
41-
$httpBackend.expectGET('phones/xyz.json').respond({name:'phone xyz'});
48+
$httpBackend.expectGET('phones/xyz.json').respond(xyzPhoneData());
4249

4350
$routeParams.phoneId = 'xyz';
4451
scope = $rootScope.$new();
@@ -50,7 +57,7 @@ describe('PhoneCat controllers', function() {
5057
expect(scope.phone).toBeUndefined();
5158
$httpBackend.flush();
5259

53-
expect(scope.phone).toEqual({name:'phone xyz'});
60+
expect(scope.phone).toEqual(xyzPhoneData());
5461
});
5562
});
5663
});

0 commit comments

Comments
 (0)