Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Refactor determineParent method to make it easier readable and satisfy rubocop

Henriette Dingerdeb38cf5009e
diff --git a/frontend/src/app/components/wp-table/drag-and-drop/actions/hierarchy-drag-action.service.ts b/frontend/src/app/components/wp-table/drag-and-drop/actions/hierarchy-drag-action.service.ts
index 86ba7271140..0bcef4cb88d 100644
--- a/frontend/src/app/components/wp-table/drag-and-drop/actions/hierarchy-drag-action.service.ts
+++ b/frontend/src/app/components/wp-table/drag-and-drop/actions/hierarchy-drag-action.service.ts
@@ -38,38 +38,33 @@ export class HierarchyDragActionService extends TableDragActionService {
*/
private determineParent(el:HTMLElement):Promise<string|null> {
let previous = el.previousElementSibling;
+ var parent = null;
- if (previous === null) {
- return Promise.resolve(null);
- }
-
- // If the previous element is a relation row,
- // skip it until we find the real previous sibling
- const isRelationRow = previous.className.indexOf(relationRowClass()) >= 0;
- if (isRelationRow) {
- let relationRoot = this.findRelationRowRoot(previous);
- if (relationRoot == null) {
- return Promise.resolve(null);
+ if (previous !== null && !this.isFlatList(previous)) {
+ // If the previous element is a relation row,
+ // skip it until we find the real previous sibling
+ const isRelationRow = previous.className.indexOf(relationRowClass()) >= 0;
+ if (isRelationRow) {
+ let relationRoot = this.findRelationRowRoot(previous);
+ if (relationRoot == null) {
+ return Promise.resolve(null);
+ }
+ previous = relationRoot;
}
- previous = relationRoot;
- }
-
- // When there is no hierarchy group at all, we're at a flat list
- const inGroup = previous.className.indexOf(hierarchyGroupClass('')) >= 0;
- const isRoot = previous.className.indexOf(hierarchyRootClass('')) >= 0;
- if (!(inGroup || isRoot)) {
- return Promise.resolve(null);
- }
- // If the sibling is a hierarchy root, return this one as new parent
- let previousWpId = (previous as HTMLElement).dataset.workPackageId!;
- if (previous.classList.contains(hierarchyRootClass(previousWpId))) {
- return Promise.resolve(previousWpId);
+ let previousWpId = (previous as HTMLElement).dataset.workPackageId!;
+ if (this.isHiearchyRoot(previous, previousWpId)) {
+ // If the sibling is a hierarchy root, return that sibling as new parent.
+ parent = previousWpId;
+ }
+ else {
+ // If the sibling is no hierarchy root, return it's parent.
+ // Thus, the dropped element will get the same hierarchy level as the sibling
+ parent = this.loadParentOfWP(previousWpId);
+ }
}
- // If the sibling is no hierarchy root, return it's parent.
- // Thus, the dropped element will get the same hierarchy level as the sibling
- return this.loadParentOfWP(previousWpId);
+ return Promise.resolve(parent);
}
private findRelationRowRoot(el:Element):Element|null {
@@ -84,6 +79,17 @@ export class HierarchyDragActionService extends TableDragActionService {
return null;
}
+ private isFlatList(previous:Element):boolean {
+ const inGroup = previous.className.indexOf(hierarchyGroupClass('')) >= 0;
+ const isRoot = previous.className.indexOf(hierarchyRootClass('')) >= 0;
+
+ return !(inGroup || isRoot);
+ }
+
+ private isHiearchyRoot(previous:Element, previousWpId:string):boolean {
+ return previous.classList.contains(hierarchyRootClass(previousWpId));
+ }
+
private loadParentOfWP(wpId:string):Promise<string|null> {
return this.wpCacheService.require(wpId)
.then((wp:WorkPackageResource) => {