From 93841338976473ac354c94ef0a4949f1a41313ca Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 8 Jan 2015 16:03:06 -0500 Subject: [PATCH] update starring without reloading repos --- static/js/controllers.js | 77 +++++++++++++++++++++++++++------- static/partials/repo-list.html | 8 ++-- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/static/js/controllers.js b/static/js/controllers.js index 6cd17c30c..41a97c58f 100644 --- a/static/js/controllers.js +++ b/static/js/controllers.js @@ -239,12 +239,14 @@ function RepoListCtrl($scope, $sanitize, Restangular, UserService, ApiService) { // Grab user namespaces UserService.load(function() { var user = UserService.currentUser(); - $scope.namespaces = [user]; - for (var i = 0; i < user.organizations.length; i++) { - $scope.namespaces.push(user.organizations[i]); + if (!user.anonymous) { + $scope.namespaces = [user]; + for (var i = 0; i < user.organizations.length; i++) { + $scope.namespaces.push(user.organizations[i]); + } + loadStarredRepos(); + loadRepos(); } - loadStarredRepos(); - loadRepos(); }); @@ -254,34 +256,74 @@ function RepoListCtrl($scope, $sanitize, Restangular, UserService, ApiService) { // loadRepos(); //}); + $scope.toggleStar = function(repo) { + if (repo.is_starred) { + unstarRepo(repo); + } else { + starRepo(repo); + } + } - $scope.starRepo = function(repo) { + var starRepo = function(repo) { var data = { 'namespace': repo.namespace, 'repository': repo.name }; ApiService.createStar(data).then(function(result) { - loadStarredRepos(); - loadRepos(); + addToStarred(repo); }, function(result) { - loadStarredRepos(); - loadRepos(); + // TODO(jzelinskie): have some kind of pop-up for star failure }); }; - $scope.unstarRepo = function(repo) { + var unstarRepo = function(repo) { var data = { 'repository': repo.namespace + '/' + repo.name }; ApiService.deleteStar(null, data).then(function(result) { - loadStarredRepos(); - loadRepos(); + removeFromStarred(repo); }, function(result) { - loadStarredRepos(); - loadRepos(); + // TODO(jzelinskie): have some kind of pop-up for star failure }); }; + var findRepoInList = function(repoNamespace, repoName) { + var namespaceIndex = $scope.namespaces.map(function (n) { + return n.username || n.name; + }).indexOf(repoNamespace); + + var namespace = $scope.namespaces[namespaceIndex] + + var repoIndex = namespace.repositories.value.map(function (r) { + return r.namespace + '/' + r.name; + }).indexOf(repoNamespace + '/' + repoName); + + return repoIndex != -1 ? namespace.repositories.value[repoIndex] : null; + } + + var addToStarred = function(repository) { + $scope.starred_repositories.value.push(repository); + + var repo = findRepoInList(repository.namespace, repository.name); + if (repo != null) { + repo.is_starred = true; + } + } + + var removeFromStarred = function(repository) { + // Remove from the starred listings + var index = $scope.starred_repositories.value.map(function (r) { + return r.namespace + '/' + r.name; + }).indexOf(repository.namespace + '/' + repository.name); + $scope.starred_repositories.value.splice(index, 1); + + // Set repo from the normal listings to unstarred. + var repo = findRepoInList(repository.namespace, repository.name); + if (repo != null) { + repo.is_starred = false; + } + }; + $scope.movePublicPage = function(increment) { if ($scope.publicPageCount == null) { return; @@ -305,7 +347,10 @@ function RepoListCtrl($scope, $sanitize, Restangular, UserService, ApiService) { } $scope.starred_repositories = ApiService.listStarredReposAsResource().get(function(resp) { - return resp.repositories; + return resp.repositories.map(function (repo) { + repo.is_starred = true; + return repo; + }); }); }; diff --git a/static/partials/repo-list.html b/static/partials/repo-list.html index 532d976eb..b0e1ddc3d 100644 --- a/static/partials/repo-list.html +++ b/static/partials/repo-list.html @@ -1,4 +1,4 @@ -
+
@@ -106,11 +106,9 @@
- - +
-