2015-03-25 19:31:05 +00:00
|
|
|
/**
|
|
|
|
* Element for managing the teams of an organization.
|
|
|
|
*/
|
|
|
|
angular.module('quay').directive('teamsManager', function () {
|
|
|
|
var directiveDefinitionObject = {
|
|
|
|
priority: 0,
|
|
|
|
templateUrl: '/static/directives/teams-manager.html',
|
|
|
|
replace: false,
|
|
|
|
transclude: false,
|
|
|
|
restrict: 'C',
|
|
|
|
scope: {
|
|
|
|
'organization': '=organization'
|
|
|
|
},
|
|
|
|
controller: function($scope, $element, ApiService, CreateService) {
|
|
|
|
$scope.TEAM_PATTERN = TEAM_PATTERN;
|
|
|
|
$scope.teamRoles = [
|
|
|
|
{ 'id': 'member', 'title': 'Member', 'kind': 'default' },
|
|
|
|
{ 'id': 'creator', 'title': 'Creator', 'kind': 'success' },
|
|
|
|
{ 'id': 'admin', 'title': 'Admin', 'kind': 'primary' }
|
|
|
|
];
|
|
|
|
|
2015-03-30 21:55:04 +00:00
|
|
|
$scope.members = {};
|
|
|
|
$scope.orderedTeams = [];
|
|
|
|
|
|
|
|
var loadTeamMembers = function() {
|
|
|
|
if (!$scope.organization) { return; }
|
|
|
|
|
|
|
|
for (var name in $scope.organization.teams) {
|
|
|
|
if (!$scope.organization.teams.hasOwnProperty(name)) { continue; }
|
|
|
|
loadMembersOfTeam(name);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var loadMembersOfTeam = function(name) {
|
|
|
|
var params = {
|
|
|
|
'orgname': $scope.organization.name,
|
|
|
|
'teamname': name
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.members[name] = {};
|
|
|
|
|
|
|
|
ApiService.getOrganizationTeamMembers(null, params).then(function(resp) {
|
|
|
|
$scope.members[name].members = resp.members;
|
|
|
|
}, function() {
|
|
|
|
delete $scope.members[name];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var loadOrderedTeams = function() {
|
2015-04-07 22:33:43 +00:00
|
|
|
if (!$scope.organization || !$scope.organization.ordered_teams) { return; }
|
2015-03-30 21:55:04 +00:00
|
|
|
|
|
|
|
$scope.orderedTeams = [];
|
|
|
|
$scope.organization.ordered_teams.map(function(name) {
|
|
|
|
$scope.orderedTeams.push($scope.organization.teams[name]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.$watch('organization', loadOrderedTeams);
|
|
|
|
$scope.$watch('organization', loadTeamMembers);
|
|
|
|
|
2015-03-25 19:31:05 +00:00
|
|
|
$scope.setRole = function(role, teamname) {
|
|
|
|
var previousRole = $scope.organization.teams[teamname].role;
|
|
|
|
$scope.organization.teams[teamname].role = role;
|
|
|
|
|
|
|
|
var params = {
|
|
|
|
'orgname': $scope.organization.name,
|
|
|
|
'teamname': teamname
|
|
|
|
};
|
|
|
|
|
|
|
|
var data = $scope.organization.teams[teamname];
|
|
|
|
|
|
|
|
var errorHandler = ApiService.errorDisplay('Cannot update team', function(resp) {
|
|
|
|
$scope.organization.teams[teamname].role = previousRole;
|
|
|
|
});
|
|
|
|
|
|
|
|
ApiService.updateOrganizationTeam(data, params).then(function(resp) {
|
|
|
|
}, errorHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.createTeam = function(teamname) {
|
|
|
|
if (!teamname) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.organization.teams[teamname]) {
|
|
|
|
$('#team-' + teamname).removeClass('highlight');
|
|
|
|
setTimeout(function() {
|
|
|
|
$('#team-' + teamname).addClass('highlight');
|
|
|
|
}, 10);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var orgname = $scope.organization.name;
|
|
|
|
CreateService.createOrganizationTeam(ApiService, orgname, teamname, function(created) {
|
|
|
|
$scope.organization.teams[teamname] = created;
|
2015-03-30 21:55:04 +00:00
|
|
|
$scope.members[teamname] = {};
|
|
|
|
$scope.members[teamname].members = [];
|
|
|
|
$scope.organization.ordered_teams.push(teamname);
|
|
|
|
$scope.orderedTeams.push(created);
|
2015-03-25 19:31:05 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.askDeleteTeam = function(teamname) {
|
|
|
|
bootbox.confirm('Are you sure you want to delete team ' + teamname + '?', function(resp) {
|
|
|
|
if (resp) {
|
|
|
|
$scope.deleteTeam(teamname);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.deleteTeam = function(teamname) {
|
|
|
|
var params = {
|
|
|
|
'orgname': $scope.organization.name,
|
|
|
|
'teamname': teamname
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.deleteOrganizationTeam(null, params).then(function() {
|
2015-03-30 21:55:04 +00:00
|
|
|
var index = $scope.organization.ordered_teams.indexOf(teamname);
|
|
|
|
if (index >= 0) {
|
|
|
|
$scope.organization.ordered_teams.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOrderedTeams();
|
2015-03-25 19:31:05 +00:00
|
|
|
delete $scope.organization.teams[teamname];
|
|
|
|
}, ApiService.errorDisplay('Cannot delete team'));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return directiveDefinitionObject;
|
|
|
|
});
|