-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Modified layers sorting functionality after adding f-group
- Loading branch information
1 parent
65902f5
commit 3c60249
Showing
52 changed files
with
552 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
...ren-nodes/get-children-nodes.execution.ts → ...ep-children-nodes-and-groups.execution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...c/domain/get-deep-children-nodes-and-groups/get-deep-children-nodes-and-groups.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class GetDeepChildrenNodesAndGroupsRequest { | ||
|
||
constructor( | ||
public fId: string | ||
) { | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
projects/f-flow/src/domain/get-deep-children-nodes-and-groups/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './get-deep-children-nodes-and-groups.execution'; | ||
|
||
export * from './get-deep-children-nodes-and-groups.request'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './sort-node-layers-by-groups'; | ||
|
||
export * from './sort-items-by-parent'; | ||
|
||
export * from './sort-item-layers.execution'; | ||
|
||
export * from './sort-item-layers.request'; |
113 changes: 113 additions & 0 deletions
113
projects/f-flow/src/domain/sort-item-layers/sort-item-layers.execution.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { | ||
FCanvasBase, | ||
FComponentsStore, | ||
FFlowMediator, | ||
FNodeBase, GetDeepChildrenNodesAndGroupsExecution, SortItemLayersExecution, SortItemLayersRequest, SortItemsByParentExecution, | ||
SortNodeLayersExecution, | ||
} from '@foblex/flow'; | ||
import { setupTestModule } from '../test-setup'; | ||
|
||
function createNode(id: string, element: HTMLElement, parentId?: string): FNodeBase { | ||
return { | ||
fId: id, | ||
fParentId: parentId, | ||
hostElement: element, | ||
} as FNodeBase; | ||
} | ||
|
||
function getFCanvasBase(): FCanvasBase { | ||
return { | ||
fGroupsContainer: { | ||
nativeElement: document.createElement('div') as HTMLElement, | ||
}, | ||
fNodesContainer: { | ||
nativeElement: document.createElement('div') as HTMLElement, | ||
} | ||
} as FCanvasBase; | ||
} | ||
|
||
function createElementWithId(id: string): HTMLElement { | ||
const element = document.createElement('div'); | ||
element.id = id; | ||
return element; | ||
} | ||
|
||
describe('SortItemLayersExecution, SortNodeLayersByGroups, SortItemsByParent', () => { | ||
let fMediator: FFlowMediator; | ||
let fComponentsStore: FComponentsStore; | ||
|
||
beforeEach(() => { | ||
setupTestModule([ SortItemLayersExecution, SortItemsByParentExecution, GetDeepChildrenNodesAndGroupsExecution, SortNodeLayersExecution ]); | ||
fMediator = TestBed.inject(FFlowMediator) as jasmine.SpyObj<FFlowMediator>; | ||
fComponentsStore = TestBed.inject(FComponentsStore); | ||
}); | ||
|
||
it('should sort nodes and groups by parents', () => { | ||
fComponentsStore.fCanvas = getFCanvasBase(); | ||
|
||
const group1 = createNode('group1', createElementWithId('group1'), 'group2'); | ||
const group2 = createNode('group2', createElementWithId('group2')); | ||
fComponentsStore.fCanvas.fGroupsContainer.nativeElement.append(group1.hostElement, group2.hostElement); | ||
|
||
const node3 = createNode('node3', createElementWithId('node3'), 'node2'); | ||
const node1 = createNode('node1', createElementWithId('node1'), 'group1'); | ||
const node2 = createNode('node2', createElementWithId('node2'), 'group2'); | ||
fComponentsStore.fCanvas.fNodesContainer.nativeElement.append(node1.hostElement, node2.hostElement, node3.hostElement); | ||
|
||
fComponentsStore.fNodes = [ group1, group2, node1, node2, node3 ]; | ||
|
||
fMediator.send(new SortItemLayersRequest()); | ||
|
||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(0)).toEqual(node2.hostElement); | ||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(1)).toEqual(node3.hostElement); | ||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(2)).toEqual(node1.hostElement); | ||
|
||
expect(fComponentsStore.fCanvas.fGroupsContainer.nativeElement.children.item(0)).toEqual(group2.hostElement); | ||
expect(fComponentsStore.fCanvas.fGroupsContainer.nativeElement.children.item(1)).toEqual(group1.hostElement); | ||
}); | ||
|
||
it('should do nothing if there are nothing to sort', () => { | ||
fComponentsStore.fCanvas = getFCanvasBase(); | ||
|
||
const group1 = createNode('group1', createElementWithId('group1')); | ||
const group2 = createNode('group2', createElementWithId('group2')); | ||
fComponentsStore.fCanvas.fGroupsContainer.nativeElement.append(group1.hostElement, group2.hostElement); | ||
|
||
const node1 = createNode('node1', createElementWithId('node1')); | ||
const node2 = createNode('node2', createElementWithId('node2')); | ||
fComponentsStore.fCanvas.fNodesContainer.nativeElement.append(node1.hostElement, node2.hostElement); | ||
|
||
fComponentsStore.fNodes = [ group1, group2, node1, node2 ]; | ||
|
||
fMediator.send(new SortItemLayersRequest()); | ||
|
||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(0)).toEqual(node1.hostElement); | ||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(1)).toEqual(node2.hostElement); | ||
|
||
expect(fComponentsStore.fCanvas.fGroupsContainer.nativeElement.children.item(0)).toEqual(group1.hostElement); | ||
expect(fComponentsStore.fCanvas.fGroupsContainer.nativeElement.children.item(1)).toEqual(group2.hostElement); | ||
}); | ||
|
||
it('should sort nodes and groups by parents and ignore items with mistakes in parent id', () => { | ||
fComponentsStore.fCanvas = getFCanvasBase(); | ||
|
||
const group1 = createNode('group1', createElementWithId('group1'), 'node1'); | ||
const group2 = createNode('group2', createElementWithId('group2'), 'group1'); | ||
fComponentsStore.fCanvas.fGroupsContainer.nativeElement.append(group1.hostElement, group2.hostElement); | ||
|
||
const node1 = createNode('node1', createElementWithId('node1'), 'group3'); | ||
const node2 = createNode('node2', createElementWithId('node2'), 'group4'); | ||
fComponentsStore.fCanvas.fNodesContainer.nativeElement.append(node2.hostElement, node1.hostElement); | ||
|
||
fComponentsStore.fNodes = [ group1, group2, node1, node2 ]; | ||
|
||
fMediator.send(new SortItemLayersRequest()); | ||
|
||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(0)).toEqual(node2.hostElement); | ||
expect(fComponentsStore.fCanvas.fNodesContainer.nativeElement.children.item(1)).toEqual(node1.hostElement); | ||
|
||
expect(fComponentsStore.fCanvas.fGroupsContainer.nativeElement.children.item(0)).toEqual(group1.hostElement); | ||
expect(fComponentsStore.fCanvas.fGroupsContainer.nativeElement.children.item(1)).toEqual(group2.hostElement); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
projects/f-flow/src/domain/sort-item-layers/sort-item-layers.execution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { SortItemLayersRequest } from './sort-item-layers.request'; | ||
import { Injectable } from '@angular/core'; | ||
import { FExecutionRegister, FFlowMediator, IExecution } from '../../infrastructure'; | ||
import { SortNodeLayersRequest } from './sort-node-layers-by-groups'; | ||
import { SortItemsByParentRequest } from './sort-items-by-parent'; | ||
import { FComponentsStore } from '../../f-storage'; | ||
|
||
@Injectable() | ||
@FExecutionRegister(SortItemLayersRequest) | ||
export class SortItemLayersExecution implements IExecution<SortItemLayersRequest, void> { | ||
|
||
constructor( | ||
private fMediator: FFlowMediator, | ||
private fComponentsStore: FComponentsStore, | ||
) { | ||
} | ||
|
||
public handle(request: SortItemLayersRequest): void { | ||
this.fMediator.send(new SortItemsByParentRequest(this.fComponentsStore.fCanvas!.fGroupsContainer.nativeElement)); | ||
this.fMediator.send(new SortNodeLayersRequest()); | ||
this.fMediator.send(new SortItemsByParentRequest(this.fComponentsStore.fCanvas!.fNodesContainer.nativeElement)); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
projects/f-flow/src/domain/sort-item-layers/sort-item-layers.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class SortItemLayersRequest { | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
projects/f-flow/src/domain/sort-item-layers/sort-items-by-parent/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './sort-items-by-parent.execution'; | ||
|
||
export * from './sort-items-by-parent.request'; |
65 changes: 65 additions & 0 deletions
65
...f-flow/src/domain/sort-item-layers/sort-items-by-parent/sort-items-by-parent.execution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { SortItemsByParentRequest } from './sort-items-by-parent.request'; | ||
import { Injectable } from '@angular/core'; | ||
import { WindowService } from '@foblex/core'; | ||
import { FExecutionRegister, FFlowMediator, IExecution } from '../../../infrastructure'; | ||
import { FComponentsStore } from '../../../f-storage'; | ||
import { FNodeBase } from '../../../f-node'; | ||
import { GetDeepChildrenNodesAndGroupsRequest } from '../../get-deep-children-nodes-and-groups'; | ||
|
||
@Injectable() | ||
@FExecutionRegister(SortItemsByParentRequest) | ||
export class SortItemsByParentExecution implements IExecution<SortItemsByParentRequest, void> { | ||
|
||
private fItemsContainer!: HTMLElement; | ||
|
||
private get fItemsFromContainer(): HTMLElement[] { | ||
return Array.from(this.fItemsContainer.children) as HTMLElement[]; | ||
} | ||
|
||
constructor( | ||
private fComponentsStore: FComponentsStore, | ||
private fMediator: FFlowMediator, | ||
private windowService: WindowService | ||
) { | ||
} | ||
|
||
public handle(request: SortItemsByParentRequest): void { | ||
this.fItemsContainer = request.fItemsContainer; | ||
this.getItems().forEach((parent: FNodeBase) => { | ||
this.moveChildrenItems(this.getSortedChildrenItems(parent), parent); | ||
}); | ||
} | ||
|
||
private getItems(): FNodeBase[] { | ||
return this.fComponentsStore.fNodes.filter((x) => this.fItemsContainer.contains(x.hostElement)); | ||
} | ||
|
||
private getSortedChildrenItems( | ||
parent: FNodeBase, | ||
): HTMLElement[] { | ||
const allElements = this.fItemsFromContainer; | ||
const parentIndex = allElements.indexOf(parent.hostElement); | ||
return this.getChildrenGroups(parent.fId) | ||
.filter((child: HTMLElement) => allElements.indexOf(child) < parentIndex) | ||
.sort((a, b) => allElements.indexOf(a) - allElements.indexOf(b)); | ||
} | ||
|
||
private getChildrenGroups(fId: string): HTMLElement[] { | ||
return this.fMediator.send<FNodeBase[]>(new GetDeepChildrenNodesAndGroupsRequest(fId)) | ||
.filter((x) => this.fItemsContainer.contains(x.hostElement)).map((x) => x.hostElement); | ||
} | ||
|
||
private moveChildrenItems( | ||
sortedChildrenItems: HTMLElement[], | ||
parent: FNodeBase, | ||
): void { | ||
let nextSibling = parent.hostElement.nextElementSibling; | ||
|
||
const fragment = this.windowService.getWindow().document.createDocumentFragment(); | ||
|
||
sortedChildrenItems.forEach((child: HTMLElement) => { | ||
fragment.appendChild(child); // Append automatically removes the element from its current position | ||
}); | ||
this.fItemsContainer.insertBefore(fragment, nextSibling); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...s/f-flow/src/domain/sort-item-layers/sort-items-by-parent/sort-items-by-parent.request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class SortItemsByParentRequest { | ||
|
||
constructor( | ||
public fItemsContainer: HTMLElement // fGroupsContainer || fNodesContainer | ||
) { | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
projects/f-flow/src/domain/sort-item-layers/sort-node-layers-by-groups/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './sort-node-layers.execution'; | ||
|
||
export * from './sort-node-layers.request'; |
Oops, something went wrong.