Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,37 @@
>delete
</li>

<li
nz-menu-item
*ngIf="canAddCommentBoxToOperator()"
(click)="onAddCommentBoxToOperator()">
<span
nz-icon
nzType="message"
nzTheme="outline"></span
>add comment
</li>
<li
nz-menu-item
*ngIf="canAnchorCommentBoxes()"
(click)="onAnchorCommentBoxes()">
<span
nz-icon
nzType="pushpin"
nzTheme="outline"></span
>anchor to operator
Comment thread
sshiv012 marked this conversation as resolved.
Resolved
</li>
<li
nz-menu-item
*ngIf="canDetachCommentBoxes()"
(click)="onDetachCommentBoxes()">
<span
nz-icon
nzType="disconnect"
nzTheme="outline"></span
>detach from operator
</li>

<li
nz-menu-item
*ngIf="highlightedOperatorIds.length === 1 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ describe("ContextMenuComponent", () => {
hasLinkWithID: vi.fn(),
bundleActions: vi.fn(),
getSubDAG: vi.fn(),
getCommentBox: vi.fn(),
hasCommentBox: vi.fn(),
};

const workflowActionServiceSpy = {
Expand All @@ -89,6 +91,10 @@ describe("ContextMenuComponent", () => {
getWorkflowMetadata: vi.fn(),
getTexeraGraph: vi.fn(),
deleteLinkWithID: vi.fn(),
anchorCommentBox: vi.fn(),
detachCommentBox: vi.fn(),
addCommentBoxToOperator: vi.fn(),
isCommentBoxWithinAnchorReach: vi.fn(),
};
workflowActionServiceSpy.getJointGraphWrapper.mockReturnValue(jointGraphWrapperSpy);
workflowActionServiceSpy.getWorkflowModificationEnabledStream.mockReturnValue(of(true));
Expand Down Expand Up @@ -821,6 +827,139 @@ describe("ContextMenuComponent", () => {
expect(spy).toHaveBeenCalledTimes(1);
});
});
describe("comment box anchoring", () => {
const norm = (s: string | null) => (s ?? "").replace(/\s+/g, " ").trim().toLowerCase();
const ANCHOR = "anchor to operator";
const DETACH = "detach from operator";
const ADD_COMMENT = "add comment";
let anchors: Record<string, string | undefined>;

function renderedLabels(): string[] {
return fixture.debugElement.queryAll(By.css("li[nz-menu-item]")).map(li => norm(li.nativeElement.textContent));
}

function clickItem(label: string): void {
const item = fixture.debugElement
.queryAll(By.css("li[nz-menu-item]"))
.find(li => norm(li.nativeElement.textContent) === label);
if (!item) throw new Error(`menu item "${label}" not rendered; present: [${renderedLabels().join(" | ")}]`);
item.nativeElement.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
}

function select(operatorIDs: string[], commentBoxIDs: string[]): void {
jointGraphWrapperSpy.getCurrentHighlightedOperatorIDs.mockReturnValue(operatorIDs);
jointGraphWrapperSpy.getCurrentHighlightedCommentBoxIDs.mockReturnValue(commentBoxIDs);
highlightedOperatorsSubject.next(operatorIDs);
highlightedCommentBoxesSubject.next(commentBoxIDs);
fixture.detectChanges();
}

beforeEach(() => {
anchors = { "box-floating": undefined, "box-anchored": "op1" };
// "execute to this operator" renders beside the anchoring items for a lone selected operator.
validationWorkflowService.validateOperator.mockReturnValue({ isValid: true });
(workflowActionService.getTexeraGraph().hasCommentBox as any).mockImplementation((id: string) => id in anchors);
(workflowActionService.getTexeraGraph().getCommentBox as any).mockImplementation((id: string) => ({
...mockCommentBox,
commentBoxID: id,
anchoredOperatorID: anchors[id],
}));
workflowActionService.isCommentBoxWithinAnchorReach.mockReturnValue(true);
component.isWorkflowModifiable = true;
});

it("offers no anchor when a selected comment box is too far from the operator to stay anchored", () => {
workflowActionService.isCommentBoxWithinAnchorReach.mockImplementation(
(commentBoxID: string) => commentBoxID !== "box-anchored"
);
select(["op2"], ["box-floating", "box-anchored"]);
expect(renderedLabels()).not.toContain(ANCHOR);
select(["op2"], ["box-floating"]);
expect(renderedLabels()).toContain(ANCHOR);
expect(workflowActionService.isCommentBoxWithinAnchorReach).toHaveBeenCalledWith("box-floating", "op2");
});

it("anchors every selected comment box to the one selected operator, in one bundled action", () => {
select(["op2"], ["box-floating", "box-anchored"]);
const bundle = workflowActionService.getTexeraGraph().bundleActions as any;
bundle.mockClear();

clickItem(ANCHOR);

expect(bundle).toHaveBeenCalledTimes(1);
expect(workflowActionService.anchorCommentBox).toHaveBeenCalledWith("box-floating", "op2");
expect(workflowActionService.anchorCommentBox).toHaveBeenCalledWith("box-anchored", "op2");
});

it("offers no anchor without exactly one operator and at least one comment box selected", () => {
select(["op1", "op2"], ["box-floating"]);
expect(renderedLabels()).not.toContain(ANCHOR);
select(["op1"], []);
expect(renderedLabels()).not.toContain(ANCHOR);
select([], ["box-floating"]);
expect(renderedLabels()).not.toContain(ANCHOR);
});

it("offers no anchor or detach while the workflow cannot be modified", () => {
component.isWorkflowModifiable = false;
select(["op1"], ["box-anchored"]);
expect(renderedLabels()).not.toContain(ANCHOR);
select([], ["box-anchored"]);
expect(renderedLabels()).not.toContain(DETACH);
});

it("offers no anchor or detach under a structure lock", () => {
component.structureLocked = true;
select(["op1"], ["box-anchored"]);
expect(renderedLabels()).not.toContain(ANCHOR);
select([], ["box-anchored"]);
expect(renderedLabels()).not.toContain(DETACH);
});

it("detaches only the anchored comment boxes among the selection", () => {
select([], ["box-floating", "box-anchored"]);

clickItem(DETACH);

expect(workflowActionService.detachCommentBox).toHaveBeenCalledTimes(1);
expect(workflowActionService.detachCommentBox).toHaveBeenCalledWith("box-anchored");
});

it("offers no detach when every selected comment box floats", () => {
select([], ["box-floating"]);
expect(renderedLabels()).not.toContain(DETACH);
});

it("ignores a selected comment box that has just been deleted", () => {
select([], ["box-deleted"]);
expect(renderedLabels()).not.toContain(DETACH);
expect(workflowActionService.getTexeraGraph().getCommentBox).not.toHaveBeenCalledWith("box-deleted");
});

it("adds a comment box to the one selected operator", () => {
select(["op1"], []);

clickItem(ADD_COMMENT);

expect(workflowActionService.addCommentBoxToOperator).toHaveBeenCalledTimes(1);
expect(workflowActionService.addCommentBoxToOperator).toHaveBeenCalledWith("op1");
});

it("offers adding a comment only for exactly one selected operator and nothing else", () => {
select(["op1", "op2"], []);
expect(renderedLabels()).not.toContain(ADD_COMMENT);
select(["op1"], ["box-floating"]);
expect(renderedLabels()).not.toContain(ADD_COMMENT);
select([], ["box-floating"]);
expect(renderedLabels()).not.toContain(ADD_COMMENT);
});

it("offers adding a comment only while the workflow can be modified", () => {
component.isWorkflowModifiable = false;
select(["op1"], []);
expect(renderedLabels()).not.toContain(ADD_COMMENT);
});
});
});

describe("ContextMenuComponent onDelete with real WorkflowActionService", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,86 @@ export class ContextMenuComponent {
});
}

/** A comment box is added to exactly one selected operator, with nothing else selected. */
public canAddCommentBoxToOperator(): boolean {
return (
this.canModify &&
this.highlightedOperatorIds.length === 1 &&
this.highlightedCommentBoxIds.length === 0 &&
!this.hasHighlightedLinks()
);
}

/** Adds a comment box beside the selected operator, anchored to it. */
public onAddCommentBoxToOperator(): void {
if (!this.canAddCommentBoxToOperator()) {
return;
}
this.workflowActionService.addCommentBoxToOperator(this.highlightedOperatorIds[0]);
}

/**
* Anchoring takes the selected comment boxes and exactly one selected operator to anchor them to,
* each box close enough to the operator that its next drop does not detach it again.
*/
public canAnchorCommentBoxes(): boolean {
return (
this.canModify &&
this.highlightedOperatorIds.length === 1 &&
this.highlightedCommentBoxIds.length > 0 &&
!this.hasHighlightedLinks() &&
this.highlightedCommentBoxIds.every(commentBoxID =>
this.workflowActionService.isCommentBoxWithinAnchorReach(commentBoxID, this.highlightedOperatorIds[0])
)
);
}

/** Anchors every selected comment box to the selected operator. */
public onAnchorCommentBoxes(): void {
Comment thread
sshiv012 marked this conversation as resolved.
Resolved
if (!this.canAnchorCommentBoxes()) {
return;
}
const operatorID = this.highlightedOperatorIds[0];
const commentBoxIDs = Array.from(this.highlightedCommentBoxIds);
this.workflowActionService
.getTexeraGraph()
.bundleActions(() =>
commentBoxIDs.forEach(commentBoxID => this.workflowActionService.anchorCommentBox(commentBoxID, operatorID))
);
}

private getSelectedAnchoredCommentBoxIDs(): string[] {
const texeraGraph = this.workflowActionService.getTexeraGraph();
// The highlight stream can still name a comment box that has just been deleted.
return this.highlightedCommentBoxIds.filter(
commentBoxID =>
texeraGraph.hasCommentBox(commentBoxID) &&
texeraGraph.getCommentBox(commentBoxID).anchoredOperatorID !== undefined
);
}

public canDetachCommentBoxes(): boolean {
return (
this.canModify &&
this.highlightedOperatorIds.length === 0 &&
!this.hasHighlightedLinks() &&
this.getSelectedAnchoredCommentBoxIDs().length > 0
);
}

/** Detaches the selected anchored comment boxes, so they float again. */
public onDetachCommentBoxes(): void {
if (!this.canDetachCommentBoxes()) {
return;
}
const commentBoxIDs = this.getSelectedAnchoredCommentBoxIDs();
this.workflowActionService
.getTexeraGraph()
.bundleActions(() =>
commentBoxIDs.forEach(commentBoxID => this.workflowActionService.detachCommentBox(commentBoxID))
);
}

private registerWorkflowModifiableChangedHandler() {
this.workflowActionService
.getWorkflowModificationEnabledStream()
Expand Down
Loading
Loading