From ad30665ca2d9be6597377b8d4d6e3129c968746a Mon Sep 17 00:00:00 2001 From: Oleksii Kurinnyi Date: Thu, 28 Apr 2016 14:06:06 +0300 Subject: [PATCH] CHE-1087: [dashboard] improve workspace name exists error message Signed-off-by: Oleksii Kurinnyi --- .../create-project.controller.js | 2 + .../create-project/create-project.html | 2 + .../create-workspace.controller.js | 2 + .../create-workspace/create-workspace.html | 2 + ...ique-workspace-name-validator.directive.js | 64 +++++++++ .../unique-workspace-name-validator.spec.js | 127 ++++++++++++++++++ .../components/validator/validator-config.js | 4 +- 7 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 dashboard/src/components/validator/unique-workspace-name-validator.directive.js create mode 100644 dashboard/src/components/validator/unique-workspace-name-validator.spec.js diff --git a/dashboard/src/app/projects/create-project/create-project.controller.js b/dashboard/src/app/projects/create-project/create-project.controller.js index bc3f1d81517..99f1b1032e5 100755 --- a/dashboard/src/app/projects/create-project/create-project.controller.js +++ b/dashboard/src/app/projects/create-project/create-project.controller.js @@ -154,6 +154,8 @@ export class CreateProjectCtrl { this.projectName = null; this.projectDescription = null; this.defaultWorkspaceName = null; + + cheAPI.cheWorkspace.getWorkspaces(); } /** diff --git a/dashboard/src/app/projects/create-project/create-project.html b/dashboard/src/app/projects/create-project/create-project.html index 65e95628a31..77db6aa2190 100644 --- a/dashboard/src/app/projects/create-project/create-project.html +++ b/dashboard/src/app/projects/create-project/create-project.html @@ -169,6 +169,7 @@ che-place-holder="Name of the workspace" ng-model="createProjectCtrl.workspaceName" required + unique-workspace-name="createProjectCtrl.workspaceName" ng-minlength="3" ng-maxlength="20" ng-pattern="/^[A-Za-z0-9_\-\.]+$/"> @@ -178,6 +179,7 @@
The name has to be more then 3 characters long.
The name has to be less than 20 characters long.
+
This workspace name is already used.
diff --git a/dashboard/src/app/workspaces/create-workspace/create-workspace.controller.js b/dashboard/src/app/workspaces/create-workspace/create-workspace.controller.js index 189a443bc78..5e285aae25e 100644 --- a/dashboard/src/app/workspaces/create-workspace/create-workspace.controller.js +++ b/dashboard/src/app/workspaces/create-workspace/create-workspace.controller.js @@ -54,6 +54,8 @@ export class CreateWorkspaceCtrl { this.recipeScript = null; this.importWorkspace = ''; this.defaultWorkspaceName = null; + + cheAPI.cheWorkspace.fetchWorkspaces(); } /** diff --git a/dashboard/src/app/workspaces/create-workspace/create-workspace.html b/dashboard/src/app/workspaces/create-workspace/create-workspace.html index 250e3863c06..f8f7e732212 100644 --- a/dashboard/src/app/workspaces/create-workspace/create-workspace.html +++ b/dashboard/src/app/workspaces/create-workspace/create-workspace.html @@ -44,6 +44,7 @@ che-place-holder="Name of the workspace" ng-model="createWorkspaceCtrl.workspaceName" required + unique-workspace-name="createWorkspaceCtrl.workspaceName" ng-minlength="3" ng-maxlength="20" ng-pattern="/^[A-Za-z0-9_\-\.]+$/"> @@ -53,6 +54,7 @@
The name has to be more then 3 characters long.
The name has to be less than 20 characters long.
+
This workspace name is already used.
diff --git a/dashboard/src/components/validator/unique-workspace-name-validator.directive.js b/dashboard/src/components/validator/unique-workspace-name-validator.directive.js new file mode 100644 index 00000000000..ce461896a63 --- /dev/null +++ b/dashboard/src/components/validator/unique-workspace-name-validator.directive.js @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2015-2016 Codenvy, S.A. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Codenvy, S.A. - initial API and implementation + */ +'use strict'; + +/** + * Defines a directive for checking if the workspace name is not already taken + * @author Oleksii Kurinnyi + */ +export class UniqueWorkspaceNameValidator { + + /** + * Default constructor that is using resource + * @ngInject for Dependency injection + */ + constructor (cheAPI, $q) { + this.cheAPI = cheAPI; + this.$q = $q; + this.restrict='A'; + this.require = 'ngModel'; + } + + /** + * Check that the name of workspace is unique + */ + link($scope, element, attributes, ngModel) { + + // validate only input element + if ('input' === element[0].localName) { + + ngModel.$asyncValidators.uniqueWorkspaceName = (modelValue) => { + + // create promise + var deferred = this.$q.defer(); + + let workspaces = this.cheAPI.getWorkspace().getWorkspaces(); + if (workspaces.length) { + + for (let i=0; i' + + '' + + '' + ); + $compiler(element)($scope); + form = $scope.form; + + form.name.$setViewValue(nameWorkspace1); + + // check form (expect invalid) + expect(form.name.$invalid).toBe(true); + expect(form.name.$valid).toBe(false); + }); + + it('workspace not yet defined', function() { + + // setup tests objects + var idWorkspace1 = 'idOfMyWorkspace1'; + var nameWorkspace1 = 'testWorkspace1'; + var workspace1 = apiBuilder.getWorkspaceBuilder().withName(nameWorkspace1).withId(idWorkspace1).build(); + + factoryWorkspace.fetchWorkspaces(); + + // add into backend + cheBackend.addWorkspaces([workspace1]); + + // setup backend + cheBackend.setup(); + + // flush HTTP backend + httpBackend.flush(); + + $scope.model = { workspaceName: null }; + + var element = angular.element( + '
' + + '' + + '
' + ); + $compiler(element)($scope); + form = $scope.form; + + form.name.$setViewValue('dummyWorkspace'); + + // check form valid + expect(form.name.$invalid).toBe(false); + expect(form.name.$valid).toBe(true); + + }); + }); +}); diff --git a/dashboard/src/components/validator/validator-config.js b/dashboard/src/components/validator/validator-config.js index c8ea827afee..571528037cf 100644 --- a/dashboard/src/components/validator/validator-config.js +++ b/dashboard/src/components/validator/validator-config.js @@ -12,6 +12,7 @@ import {GitUrlValidator} from './git-url-validator.directive'; import {UniqueProjectNameValidator} from './unique-project-name-validator.directive'; +import {UniqueWorkspaceNameValidator} from './unique-workspace-name-validator.directive'; export class ValidatorConfig { @@ -19,7 +20,8 @@ export class ValidatorConfig { constructor(register) { register.directive('gitUrl', GitUrlValidator) - .directive('uniqueProjectName', UniqueProjectNameValidator); + .directive('uniqueProjectName', UniqueProjectNameValidator) + .directive('uniqueWorkspaceName', UniqueWorkspaceNameValidator); } }