<file_sep>app.controller('MainPageController', ['$scope','PhotosService', function($scope, photosService) {
    var vm = this;

    vm.title = 'AngularJS Exercises';
    vm.selectedAlbum = -1;
    vm.albums = []; 
    vm.photos = [];
    vm.number = 1234.567;

    vm.albumClick = function(albumId) {
        photosService.getAlbum(albumId).then(function(photos) {
            vm.photos = photos;
        });
        if (albumId === vm.selectedAlbum) {
            vm.selectedAlbum = -1;
        }
        else {
            vm.selectedAlbum = albumId;
        }
        
    }

    photosService.getAllAlbums().then(function(albums) {
        vm.albums = albums;
    });

}]);<file_sep>var app = angular.module('angular-exercises', ['ngSanitize']);

<file_sep>app.factory('PhotosService',['$http','$q', function($http,$q) {
    var serviceFactory = {};

    var photosUrl = 'https://jsonplaceholder.typicode.com/photos';

    serviceFactory.getAllAlbums = function() {
        var albumsPromise = $http.get(photosUrl).then(function(response) {
            var albums = [];
            var currentAlbumId = 0;
            if (typeof response === 'object') {
                _.forEach(response.data, function(album) {
                   if (!_.find(albums, { 'albumId' : album.albumId })) {
                       albums.push({
                           albumTitle: 'Album: ' + album.albumId,
                           albumId: album.albumId,
                           albumThumbnailUrl: album.thumbnailUrl
                       });
                   }
                }, this);
            }

            return albums;
        },
        function (error) {
            return $q.reject(response.data);
        });

        return albumsPromise;
    }

    serviceFactory.getAlbum = function(albumId) {
        var getAlbumUrl = photosUrl + '?albumId=' + albumId;
        var albumPromise = $http.get(getAlbumUrl).then(function(response) {
            if (typeof response === 'object') {
                return response.data;
            }
            else {
                return $q.reject('Data error');
            }
        },
        function (response) {
            return $q.reject(response.data);
        });

        return albumPromise;
    }

    return serviceFactory;
}]);<file_sep>app.filter('strange', ['$sce', function($sce) {
    function getFract(value) {
        var splited = value.toString().split('.');
        if (_.isArray(splited) && splited.length === 2) {
            return _.toInteger(splited[1]);
        }

    }

    return function(value, precision) {
        var result = '';
        var splited = value.toString().split('.');
        if (_.isArray(splited) && splited.length === 2) {
            var fract = _.toNumber('0.' + splited[1]);
            if (precision) {
                fract = _.round(fract, precision);
            }

            result = splited[0] + '<sup>' + getFract(fract) +'</sup>';
        }

        return $sce.trustAsHtml(result);
    }
}]);<file_sep>app.directive('displayPhoto', [function() {
    var controller = ['$scope', function($scope) {

        $scope.bigImageVisible = false;
        $scope.imageClicked = function() {
            $scope.bigImageVisible = true;

        }
        $scope.closeBigPhoto = function() {
            $scope.bigImageVisible = false;
        }
    }];

    return {
        restrict: 'E',
        scope: {
            photo: '='
        },
        templateUrl: 'app/photo.directive.html',
        controller: controller
    };
}]);