angular.module('degreedPublic').directive('dgClientLogos', [ '$interval', '$timeout', 'Power2', function ($interval, $timeout, Power2) { return { templateUrl: '/scripts/public/clientLogos/partials/clientLogos.html', link: function (scope, el, attrs) { scope.getClientLogos().then(function (response) { var count = scope.index, logos = scope.logos, spotIndex = 0; setupNewSpotEl(count, spotIndex, logos); }); function animateElements(imgEl, spotIndex, count, logos) { // removed 3rd party lib audit } function getCount(count, logos) { if (count === logos.length - 1) { return 0; } else { count += 1; return count; } } function setupNewSpotEl(count, spotIndex, logos) { var img, newLogo, newImg; scope.resetAvailableSpots(); spotIndex = scope.pickRandomAvailSpot(scope.availableSpots); //Pick the next image from our list of logos, if at end, reset. count = getCount(count, logos); img = el.find('img').eq(spotIndex); newLogo = logos[count]; newImg = '
'; animateElements(newImg, spotIndex, count, logos); } }, controller: [ '$scope', 'ClientLogosSvc', 'helperSvc', function ($scope, ClientLogosSvc, helperSvc) { var ctrl = this; //On scope to share with directive to improve performance. //Sharing in controller caused a serious memory leak. $scope.availableSpots = []; $scope.logos = []; $scope.index = 7; $scope.getClientLogos = getClientLogos; $scope.resetAvailableSpots = resetAvailableSpots; $scope.pickRandomAvailSpot = pickRandomAvailSpot; ctrl.$onInit = function () { resetAvailableSpots(); }; function getClientLogos() { return ClientLogosSvc.getLogos().then(function (response) { //move to api response angular.forEach(response.data, function (logo) { logo.Url = helperSvc.paths.getBlobUrl( '~/content/img/public/home/clientlogos/' + logo.LogoId + '.svg?v=1' ); logo.Url2x = helperSvc.paths.getBlobUrl( '~/content/img/public/home/clientlogos/' + logo.LogoId + '.svg?v=1' ); }); $scope.logos = response.data; return response; }); } function resetAvailableSpots() { if ($scope.availableSpots.length === 0) { for (var i = 0; i < $scope.index + 1; i++) { $scope.availableSpots.push(i); } } return $scope.availableSpots; } function pickRandomAvailSpot(spots) { if (spots.length > 0) { var indexVal = spots[Math.floor(Math.random() * (spots.length - 1))]; return removeItem(spots.indexOf(indexVal), spots); } } function removeItem(index, spots) { return spots.splice(index, 1)[0]; } }, ], }; }, ]);