repo list: generic refresh stars function

This commit is contained in:
Jimmy Zelinskie 2015-02-25 16:19:54 -05:00
parent fb0d3d69c2
commit 6e1877084d

View file

@ -63,7 +63,8 @@
'repository': repo.name
};
ApiService.createStar(data).then(function(result) {
updateReposAfterStar(repo);
repo.is_starred = true;
refreshStars();
}, ApiService.errorDisplay('Could not star repository'));
};
@ -72,50 +73,21 @@
'repository': repo.namespace + '/' + repo.name
};
ApiService.deleteStar(null, data).then(function(result) {
updateReposAfterUnstar(repo);
repo.is_starred = false;
refreshStars();
}, ApiService.errorDisplay('Could not unstar repository'));
};
// Finds a repository within the list of namespaces attached to $scope.
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;
}
// Add a starred repository to the list starred repository list and make
// sure it appears starred elsewhere on the page.
var updateReposAfterStar = function(repository) {
$scope.starred_repositories.value.push(repository);
var repo = findRepoInList(repository.namespace, repository.name);
if (repo != null) {
repo.is_starred = true;
}
}
// Remove a repository from the starred repository list and make sure that
// it doesn't appear starred elsewhere on the page.
var updateReposAfterUnstar = 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;
}
var refreshStars = function() {
var starred = [];
$scope.namespaces.map(function(namespace) {
namespace.repositories.value.map(function(repository) {
if (repository.is_starred) {
starred.push(repository);
}
});
});
$scope.starred_repositories.value = starred;
};
var loadStarredRepos = function() {