Skip to content

Commit

Permalink
step-10 image swapping with ng:click
Browse files Browse the repository at this point in the history
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
  • Loading branch information
IgorMinar committed Oct 18, 2012
1 parent f37a97c commit d602c01
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ ul.phone-thumbs img {
padding: 1em;
}

ul.phone-thumbs img:hover {
cursor: pointer;
}


ul.specs {
clear: both;
margin: 0;
Expand Down
5 changes: 5 additions & 0 deletions app/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ function PhoneListCtrl($scope, $http) {
function PhoneDetailCtrl($scope, $routeParams, $http) {
$http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) {
$scope.phone = data;
$scope.mainImageUrl = data.images[0];
});

$scope.setImage = function(imageUrl) {
$scope.mainImageUrl = imageUrl;
}
}

//PhoneDetailCtrl.$inject = ['$scope', '$routeParams', '$http'];
4 changes: 2 additions & 2 deletions app/partials/phone-detail.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<img ng-src="{{phone.images[0]}}" class="phone">
<img ng-src="{{mainImageUrl}}" class="phone">

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

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

<ul class="phone-thumbs">
<li ng-repeat="img in phone.images">
<img ng-src="{{img}}">
<img ng-src="{{img}}" ng-click="setImage(img)">
</li>
</ul>

Expand Down
14 changes: 14 additions & 0 deletions test/e2e/scenarios.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,19 @@ describe('PhoneCat App', function() {
it('should display nexus-s page', function() {
expect(binding('phone.name')).toBe('Nexus S');
});


it('should display the first phone image as the main phone image', function() {
expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.0.jpg');
});


it('should swap main image if a thumbnail image is clicked on', function() {
element('.phone-thumbs li:nth-child(3) img').click();
expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.2.jpg');

element('.phone-thumbs li:nth-child(1) img').click();
expect(element('img.phone').attr('src')).toBe('img/phones/nexus-s.0.jpg');
});
});
});
13 changes: 10 additions & 3 deletions test/unit/controllersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ describe('PhoneCat controllers', function() {


describe('PhoneDetailCtrl', function(){
var scope, $httpBackend, ctrl;
var scope, $httpBackend, ctrl,
xyzPhoneData = function() {
return {
name: 'phone xyz',
images: ['image/url1.png', 'image/url2.png']
}
};


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

$routeParams.phoneId = 'xyz';
scope = $rootScope.$new();
Expand All @@ -48,7 +55,7 @@ describe('PhoneCat controllers', function() {
expect(scope.phone).toBeUndefined();
$httpBackend.flush();

expect(scope.phone).toEqual({name:'phone xyz'});
expect(scope.phone).toEqual(xyzPhoneData());
});
});
});

0 comments on commit d602c01

Please sign in to comment.