From 7bdd7c5f82b80f9d3154ec820f1b6ea405e2a068 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 8 Apr 2015 11:54:04 -0400 Subject: [PATCH] Remove some code duplication by moving the robot and team creation dialogs to the create service --- static/directives/new-header-bar.html | 4 +-- static/js/directives/ui/entity-search.js | 36 ++++-------------------- static/js/directives/ui/header-bar.js | 35 ++++------------------- static/js/services/create-service.js | 35 ++++++++++++++++++++++- 4 files changed, 48 insertions(+), 62 deletions(-) diff --git a/static/directives/new-header-bar.html b/static/directives/new-header-bar.html index 453462176..68dfa504a 100644 --- a/static/directives/new-header-bar.html +++ b/static/directives/new-header-bar.html @@ -62,9 +62,9 @@ New Repository - +
  • diff --git a/static/js/directives/ui/entity-search.js b/static/js/directives/ui/entity-search.js index 3362197bc..38a68bb1e 100644 --- a/static/js/directives/ui/entity-search.js +++ b/static/js/directives/ui/entity-search.js @@ -92,40 +92,16 @@ angular.module('quay').directive('entitySearch', function () { }; $scope.createTeam = function() { - if (!$scope.isAdmin) { return; } - - bootbox.prompt('Enter the name of the new team', function(teamname) { - if (!teamname) { return; } - - var regex = new RegExp(TEAM_PATTERN); - if (!regex.test(teamname)) { - bootbox.alert('Invalid team name'); - return; - } - - CreateService.createOrganizationTeam(ApiService, $scope.namespace, teamname, function(created) { - $scope.setEntity(created.name, 'team', false, created.avatar); - $scope.teams[teamname] = created; - }); + CreateService.askCreateTeam($scope.namespace, function(created) { + $scope.setEntity(created.name, 'team', false, created.avatar); + $scope.teams[teamname] = created; }); }; $scope.createRobot = function() { - if (!$scope.isAdmin) { return; } - - bootbox.prompt('Enter the name of the new robot account', function(robotname) { - if (!robotname) { return; } - - var regex = new RegExp(ROBOT_PATTERN); - if (!regex.test(robotname)) { - bootbox.alert('Invalid robot account name'); - return; - } - - CreateService.createRobotAccount(ApiService, $scope.isOrganization, $scope.namespace, robotname, function(created) { - $scope.setEntity(created.name, 'user', true, created.avatar); - $scope.robots.push(created); - }); + CreateService.askCreateRobot($scope.namespace, function(created) { + $scope.setEntity(created.name, 'user', true, created.avatar); + $scope.robots.push(created); }); }; diff --git a/static/js/directives/ui/header-bar.js b/static/js/directives/ui/header-bar.js index 330e73790..67f170833 100644 --- a/static/js/directives/ui/header-bar.js +++ b/static/js/directives/ui/header-bar.js @@ -188,25 +188,12 @@ angular.module('quay').directive('headerBar', function () { $scope.createRobot = function(context) { var namespace = $scope.getNamespace(context); - if (!namespace || !UserService.isNamespaceAdmin(namespace)) { return; } - - var isorg = UserService.isOrganization(namespace); - bootbox.prompt('Enter the name of the new robot account', function(robotname) { - if (!robotname) { return; } - - var regex = new RegExp(ROBOT_PATTERN); - if (!regex.test(robotname)) { - bootbox.alert('Invalid robot account name'); - return; + CreateService.askCreateRobot(function(created) { + if (isorg) { + $location.url('/organization/' + namespace + '?tab=robots'); + } else { + $location.url('/user/' + namespace + '?tab=robots'); } - - CreateService.createRobotAccount(ApiService, isorg, namespace, robotname, function(created) { - if (isorg) { - $location.url('/organization/' + namespace + '?tab=robots'); - } else { - $location.url('/user/' + namespace + '?tab=robots'); - } - }); }); }; @@ -214,18 +201,8 @@ angular.module('quay').directive('headerBar', function () { var namespace = $scope.getNamespace(context); if (!namespace || !UserService.isNamespaceAdmin(namespace)) { return; } - bootbox.prompt('Enter the name of the new team', function(teamname) { - if (!teamname) { return; } - - var regex = new RegExp(TEAM_PATTERN); - if (!regex.test(teamname)) { - bootbox.alert('Invalid team name'); - return; - } - - CreateService.createOrganizationTeam(ApiService, namespace, teamname, function(created) { + CreateService.askCreateTeam(function(created) { $location.url('/organization/' + namespace + '/teams/' + teamname); - }); }); }; } diff --git a/static/js/services/create-service.js b/static/js/services/create-service.js index 25c308e22..b6092728b 100644 --- a/static/js/services/create-service.js +++ b/static/js/services/create-service.js @@ -1,7 +1,7 @@ /** * Service which exposes various methods for creating entities on the backend. */ -angular.module('quay').factory('CreateService', ['ApiService', function(ApiService) { +angular.module('quay').factory('CreateService', ['ApiService', 'UserService', function(ApiService, UserService) { var createService = {}; createService.createRobotAccount = function(ApiService, is_org, orgname, name, callback) { @@ -24,5 +24,38 @@ angular.module('quay').factory('CreateService', ['ApiService', function(ApiServi .then(callback, ApiService.errorDisplay('Cannot create team')); }; + createService.askCreateRobot = function(namespace, callback) { + if (!namespace || !UserService.isNamespaceAdmin(namespace)) { return; } + + var isorg = UserService.isOrganization(namespace); + bootbox.prompt('Enter the name of the new robot account', function(robotname) { + if (!robotname) { return; } + + var regex = new RegExp(ROBOT_PATTERN); + if (!regex.test(robotname)) { + bootbox.alert('Invalid robot account name'); + return; + } + + createService.createRobotAccount(ApiService, isorg, namespace, robotname, callback); + }); + }; + + createService.askCreateTeam = function(namespace, callback) { + if (!namespace || !UserService.isNamespaceAdmin(namespace)) { return; } + + bootbox.prompt('Enter the name of the new team', function(teamname) { + if (!teamname) { return; } + + var regex = new RegExp(TEAM_PATTERN); + if (!regex.test(teamname)) { + bootbox.alert('Invalid team name'); + return; + } + + CreateService.createOrganizationTeam(ApiService, namespace, teamname, callback); + }); + }; + return createService; }]);