From a4614afd953b28ef06468e102e37d5b53ebfbb46 Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Fri, 31 Aug 2018 00:39:35 +0100 Subject: [PATCH] Refactor Project Group editing to match other objects Currently project groups have a separate "edit" view, which makes it hard to route to them by name. This commit changes this so that editing is done in the detail view, as it is for other objects like Projects or Stories, in order to facilitate routing by name. Depends-On: https://review.openstack.org/#/c/590047/ Change-Id: I7f26838ce255268653cd2f912a4f7bb2a13d2816 --- .../project_group_detail_controller.js | 197 +++++++++++++++- .../project_group_edit_controller.js | 222 ------------------ src/app/project_group/module.js | 15 -- src/app/project_group/template/detail.html | 163 ++++++++++++- src/app/project_group/template/edit.html | 177 -------------- 5 files changed, 356 insertions(+), 418 deletions(-) delete mode 100644 src/app/project_group/controller/project_group_edit_controller.js delete mode 100644 src/app/project_group/template/edit.html diff --git a/src/app/project_group/controller/project_group_detail_controller.js b/src/app/project_group/controller/project_group_detail_controller.js index 1fc3045f..e8f145aa 100644 --- a/src/app/project_group/controller/project_group_detail_controller.js +++ b/src/app/project_group/controller/project_group_detail_controller.js @@ -20,7 +20,8 @@ */ angular.module('sb.project_group').controller('ProjectGroupDetailController', function ($scope, $stateParams, projectGroup, Story, Project, - Preference, SubscriptionList, CurrentUser, Subscription) { + Preference, SubscriptionList, CurrentUser, Subscription, + $q, ProjectGroupItem, ArrayUtil, $log) { 'use strict'; var projectPageSize = Preference.get( @@ -43,6 +44,12 @@ angular.module('sb.project_group').controller('ProjectGroupDetailController', $scope.projects = []; $scope.isSearchingProjects = false; + $scope.editMode = false; + + $scope.toggleEdit = function() { + $scope.editMode = !$scope.editMode; + }; + /** * List the projects in this Project Group */ @@ -205,6 +212,194 @@ angular.module('sb.project_group').controller('ProjectGroupDetailController', }; + /** + * UI flag, are we saving? + * + * @type {boolean} + */ + $scope.isSaving = false; + + /** + * Project typeahead search method. + */ + $scope.searchProjects = function (value) { + var deferred = $q.defer(); + Project.browse({name: value, limit: 10}, + function (results) { + // Dedupe the results. + var idxList = []; + for (var i = 0; i < $scope.projects.length; i++) { + var project = $scope.projects[i]; + if (!!project) { + idxList.push(project.id); + } + } + + for (var j = results.length - 1; j >= 0; j--) { + var resultId = results[j].id; + if (idxList.indexOf(resultId) > -1) { + results.splice(j, 1); + } + } + + deferred.resolve(results); + }, + function (error) { + $log.error(error); + deferred.resolve([]); + }); + return deferred.promise; + }; + + /** + * Formats the project name. + */ + $scope.formatProjectName = function (model) { + if (!!model) { + return model.name; + } + return ''; + }; + + /** + * Remove a project from the list + */ + $scope.removeProject = function (index) { + $scope.projects.splice(index, 1); + }; + + /** + * Save the project and the associated groups + */ + $scope.save = function () { + $scope.isSaving = true; + + ProjectGroupItem.browse({projectGroupId: $scope.projectGroup.id}, + function(results) { + var loadedIds = []; + results.forEach(function (project) { + loadedIds.push(project.id); + }); + var promises = []; + + // Get the desired ID's. + var desiredIds = []; + $scope.projects.forEach(function (project) { + desiredIds.push(project.id); + }); + + // Intersect loaded vs. current to get a list of project + // reference to delete. + var idsToDelete = ArrayUtil.difference( + loadedIds, desiredIds); + idsToDelete.forEach(function (id) { + + // Get a deferred promise... + var removeProjectDeferred = $q.defer(); + + // Construct the item. + var item = new ProjectGroupItem({ + id: id, + projectGroupId: projectGroup.id + }); + + // Delete the item. + item.$delete(function (result) { + removeProjectDeferred.resolve(result); + }, + function (error) { + removeProjectDeferred.reject(error); + } + ); + + promises.push(removeProjectDeferred.promise); + }); + + // Intersect current vs. loaded to get a list of project + // reference to add. + var idsToAdd = ArrayUtil.difference(desiredIds, loadedIds); + idsToAdd.forEach(function (id) { + + // Get a deferred promise... + var addProjectDeferred = $q.defer(); + + // Construct the item. + var item = new ProjectGroupItem({ + id: id, + projectGroupId: projectGroup.id + }); + + // Delete the item. + item.$create(function (result) { + addProjectDeferred.resolve(result); + }, + function (error) { + addProjectDeferred.reject(error); + } + ); + + promises.push(addProjectDeferred.promise); + }); + + + // Save the project group itself. + var deferred = $q.defer(); + promises.push(deferred.promise); + $scope.projectGroup.$update(function (success) { + deferred.resolve(success); + }, function (error) { + $log.error(error); + deferred.reject(error); + }); + + // Roll all the promises into one big happy promise. + $q.all(promises).then( + function () { + $scope.editMode = false; + $scope.isSaving = false; + }, + function (error) { + $log.error(error); + } + ); + } + ); + }; + + /** + * Add project. + */ + $scope.addProject = function () { + $scope.projects.push({}); + }; + + /** + * Insert item into the project list. + */ + $scope.selectNewProject = function (index, model) { + // Put our model into the array + $scope.projects[index] = model; + }; + + /** + * Check that we have valid projects on the list + */ + $scope.checkValidProjects = function () { + if ($scope.projects.length === 0) { + return false; + } + + // check if projects contain a valid project_id + for (var i = 0; i < $scope.projects.length; i++) { + var project = $scope.projects[i]; + if (!project.id) { + return false; + } + } + return true; + }; + + $scope.listProjects(); $scope.filterStories(); diff --git a/src/app/project_group/controller/project_group_edit_controller.js b/src/app/project_group/controller/project_group_edit_controller.js deleted file mode 100644 index 75eeb192..00000000 --- a/src/app/project_group/controller/project_group_edit_controller.js +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. You may obtain - * a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -/** - * New Project Group edit controller. - */ -angular.module('sb.project_group').controller('ProjectGroupEditController', - function ($q, $log, $scope, $state, projectGroup, projects, Project, - ProjectGroupItem, ArrayUtil) { - 'use strict'; - - /** - * The project group we're editing. Resolved by the route. - */ - $scope.projectGroup = projectGroup; - - /** - * The list of projects in this group (Resolved by the route). - */ - $scope.projects = projects; - - /** - * A collection of all project ID's that have been loaded on - * initialization. This list is used to determine the project member - * diff. - */ - var loadedIds = []; - $scope.projects.forEach(function (project) { - loadedIds.push(project.id); - }); - - /** - * UI flag, are we saving? - * - * @type {boolean} - */ - $scope.isSaving = false; - - /** - * Project typeahead search method. - */ - $scope.searchProjects = function (value) { - var deferred = $q.defer(); - Project.browse({name: value, limit: 10}, - function (results) { - // Dedupe the results. - var idxList = []; - for (var i = 0; i < $scope.projects.length; i++) { - var project = $scope.projects[i]; - if (!!project) { - idxList.push(project.id); - } - } - - for (var j = results.length - 1; j >= 0; j--) { - var resultId = results[j].id; - if (idxList.indexOf(resultId) > -1) { - results.splice(j, 1); - } - } - - deferred.resolve(results); - }, - function (error) { - $log.error(error); - deferred.resolve([]); - }); - return deferred.promise; - }; - - /** - * Formats the project name. - */ - $scope.formatProjectName = function (model) { - if (!!model) { - return model.name; - } - return ''; - }; - - /** - * Remove a project from the list - */ - $scope.removeProject = function (index) { - $scope.projects.splice(index, 1); - }; - - /** - * Save the project and the associated groups - */ - $scope.save = function () { - $scope.isSaving = true; - - var promises = []; - - // Get the desired ID's. - var desiredIds = []; - $scope.projects.forEach(function (project) { - desiredIds.push(project.id); - }); - - // Intersect loaded vs. current to get a list of project - // reference to delete. - var idsToDelete = ArrayUtil.difference(loadedIds, desiredIds); - idsToDelete.forEach(function (id) { - - // Get a deferred promise... - var removeProjectDeferred = $q.defer(); - - // Construct the item. - var item = new ProjectGroupItem({ - id: id, - projectGroupId: projectGroup.id - }); - - // Delete the item. - item.$delete(function (result) { - removeProjectDeferred.resolve(result); - }, - function (error) { - removeProjectDeferred.reject(error); - } - ); - - promises.push(removeProjectDeferred.promise); - }); - - // Intersect current vs. loaded to get a list of project - // reference to add. - var idsToAdd = ArrayUtil.difference(desiredIds, loadedIds); - idsToAdd.forEach(function (id) { - - // Get a deferred promise... - var addProjectDeferred = $q.defer(); - - // Construct the item. - var item = new ProjectGroupItem({ - id: id, - projectGroupId: projectGroup.id - }); - - // Delete the item. - item.$create(function (result) { - addProjectDeferred.resolve(result); - }, - function (error) { - addProjectDeferred.reject(error); - } - ); - - promises.push(addProjectDeferred.promise); - }); - - - // Save the project group itself. - var deferred = $q.defer(); - promises.push(deferred.promise); - $scope.projectGroup.$update(function (success) { - deferred.resolve(success); - }, function (error) { - $log.error(error); - deferred.reject(error); - }); - - // Roll all the promises into one big happy promise. - $q.all(promises).then( - function () { - $state.go('sb.project_group.list', {}); - }, - function (error) { - $log.error(error); - } - ); - }; - - /** - * Add project. - */ - $scope.addProject = function () { - $scope.projects.push({}); - }; - - /** - * Insert item into the project list. - */ - $scope.selectNewProject = function (index, model) { - // Put our model into the array - $scope.projects[index] = model; - }; - - /** - * Check that we have valid projects on the list - */ - $scope.checkValidProjects = function () { - if ($scope.projects.length === 0) { - return false; - } - - // check if projects contain a valid project_id - for (var i = 0; i < $scope.projects.length; i++) { - var project = $scope.projects[i]; - if (!project.id) { - return false; - } - } - return true; - }; - - }); diff --git a/src/app/project_group/module.js b/src/app/project_group/module.js index 28f4abe9..e95b84cd 100644 --- a/src/app/project_group/module.js +++ b/src/app/project_group/module.js @@ -58,21 +58,6 @@ angular.module('sb.project_group', deferred.reject(error); }); return deferred.promise; - } - } - }) - .state('sb.project_group.edit', { - url: '/{id:[0-9]+}/edit', - templateUrl: 'app/project_group/template/edit.html', - controller: 'ProjectGroupEditController', - resolve: { - projectGroup: function ($stateParams, ProjectGroup) { - return ProjectGroup.get({id: $stateParams.id}).$promise; - }, - projects: function ($stateParams, ProjectGroupItem) { - return ProjectGroupItem.browse( - {projectGroupId: $stateParams.id} - ).$promise; }, isSuperuser: PermissionResolver .requirePermission('is_superuser', true) diff --git a/src/app/project_group/template/detail.html b/src/app/project_group/template/detail.html index 4f23c60e..7ab3fa84 100644 --- a/src/app/project_group/template/detail.html +++ b/src/app/project_group/template/detail.html @@ -6,7 +6,7 @@ {{projectGroup.title}} - + -
+
+
-
+

@@ -129,3 +130,159 @@
+ + + + + diff --git a/src/app/project_group/template/edit.html b/src/app/project_group/template/edit.html deleted file mode 100644 index eec425e5..00000000 --- a/src/app/project_group/template/edit.html +++ /dev/null @@ -1,177 +0,0 @@ - - -
-
-
-

{{projectGroup.title}} -

-
-
- -
-
-
-
- - -
- - -
- - A project group title is required. - - - A project group title must begin with a letter, and may only - contain letters, numbers, forward slashes, periods, and - dashes. It should not start or end with a separator and - must not contain two or more sequential separators. - - - A project group title must have at least 3 characters. - -
-
-
-
- - -
- - -
- - A project group URL is required. - - - A project group URL must have at least 3 characters. - -
- -
-
-
-
-
-
-
- -
-
-
- - - - - -
-
-
-
-
-
- -
-
- - - Cancel - -
-
-
- - -