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 @@ -82,6 +82,50 @@ describe("UserDatasetVersionFiletreeComponent (browser)", () => {
expect(buttonRect.bottom).toBeLessThanOrEqual(rowRect.bottom);
});

// Measured on a deployment: clicking the only file left its row's class at exactly
// "node-content-wrapper", so nothing told the user which file they had picked. Needs real rows,
// which the virtual scroll only renders here.
it("highlights the row of the clicked file", async () => {
await renderTree(makeFlatFileNodes(5));

const rowAt = (index: number): HTMLElement =>
fixture.nativeElement.querySelectorAll(".node-content-wrapper")[index] as HTMLElement;
rowAt(1).click();
fixture.detectChanges();

expect(rowAt(1).classList.contains("node-content-wrapper-active")).toBe(true);
expect(rowAt(0).classList.contains("node-content-wrapper-active")).toBe(false);
// The class alone is invisible unless the component styles it. The library transitions
// background-color and box-shadow over 150ms, so the computed values only reach the highlight
// once that has run.
await vi.waitFor(() => {
const highlighted = getComputedStyle(rowAt(1));
expect(highlighted.backgroundColor).toBe("rgb(230, 247, 255)");
expect(highlighted.boxShadow).toBe("rgb(24, 144, 255) 0px 0px 0px 1px inset");
// blue-8, not the ring's primary blue: the name is text, and the primary blue sits at
// 2.95:1 on this fill, under the 4.5:1 text needs.
expect(highlighted.color).toBe("rgb(0, 80, 179)");
});
// The ring takes no layout space: the row's content box is still the full node height, where a
// 1px border would have left 2px less for the icon buttons under the app's border-box sizing.
expect(rowAt(1).clientHeight).toBe(component.TREE_NODE_HEIGHT_PX);
});

// The highlight is feedback for a pick, so where no file can be picked (the dataset picker in
// version mode, the model picker) a click must leave the row exactly as it was.
it("leaves the row untouched where files cannot be picked", async () => {
component.isFileSelectable = false;
await renderTree(makeFlatFileNodes(5));

const row = fixture.nativeElement.querySelectorAll(".node-content-wrapper")[1] as HTMLElement;
const before = getComputedStyle(row).backgroundColor;
row.click();
fixture.detectChanges();

expect(row.classList.contains("node-content-wrapper-active")).toBe(false);
expect(getComputedStyle(row).backgroundColor).toBe(before);
});

// Both hosts create the tree empty and fill it when data arrives; the
// height change must trigger a viewport re-measure or the 0px initial
// measurement sticks and the tree stays blank.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@
display: flex;
align-items: center;
}

/* The picked file has to read as picked. The library's own active colours (#beebff behind a 1px
grey inset shadow) belong to no palette this app uses, so the row takes the ant blue-1 (#e6f7ff)
fill the dashboard's selected list rows and the workspace's selected cards share, with ant's
primary blue as the ring, as the workspace's selected cards ring. The name is ant's blue-8: the
primary blue reads at 2.95:1 on this fill, under the 4.5:1 that text needs (WCAG 1.4.3), while
blue-8 reads at 6.8:1. The :hover copy keeps this fill on the picked row rather than the
library's hover tint. */
:host ::ng-deep .file-tree-container .node-content-wrapper.node-content-wrapper-active,
:host ::ng-deep .file-tree-container .node-content-wrapper.node-content-wrapper-active:hover {
background-color: #e6f7ff;
color: #0050b3;
/* An inset shadow rather than a border: with the row's height pinned to --tree-node-height for
the virtual scroll, a border would eat 2px of the content box the row's icon buttons and
centring rely on; a shadow takes no layout space at all. */
box-shadow: inset 0 0 0 1px #1890ff;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { ComponentFixture } from "@angular/core/testing";
import { KEYS } from "@ali-hm/angular-tree-component";
import { UserDatasetVersionFiletreeComponent } from "./user-dataset-version-filetree.component";
import { DatasetFileNode } from "../../../../../../common/type/datasetVersionFileTree";
import { createFiletreeFixture, FILE_COUNT, makeFlatFileNodes } from "./user-dataset-version-filetree.test-utils";
Expand Down Expand Up @@ -109,38 +110,138 @@ describe("UserDatasetVersionFiletreeComponent", () => {
expect(component.tree.treeModel.roots[0].isExpanded).toBe(true);
});

it("toggles expansion without emitting selectedTreeNode when a folder is clicked", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));

// The folder branch delegates to TOGGLE_EXPANDED, which only calls
// node.toggleExpanded().
let toggleCalls = 0;
const onClick = component.fileTreeDisplayOptions.actionMapping!.mouse!.click!;
const folderNode = {
hasChildren: true,
toggleExpanded: () => {
toggleCalls++;
},
data: { name: "dir", type: "directory", parentDir: "/owner/dataset/v1" },
} as never;
onClick(undefined as never, folderNode, undefined as never);

expect(toggleCalls).toBe(1);
expect(emitted).toEqual([]);
});
// Clicks go through the library's own mouseAction dispatch on real tree nodes, so what these
// pin is the state a row's node-content-wrapper-active class is bound to -- not only that the
// handler was reached.
describe("clicking a node", () => {
function renderAndClick(nodes: DatasetFileNode[], rootIndex: number): any {
component.fileTreeNodes = nodes;
fixture.detectChanges();
const node = component.tree.treeModel.roots[rootIndex];
node.mouseAction("click", new MouseEvent("click"));
return node;
}

it("emits selectedTreeNode when a leaf node is clicked", () => {
component.fileTreeNodes = makeFlatFileNodes(1);
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));
const activeNames = (): string[] =>
component.tree.treeModel.activeNodes.map((node: any) => node.data.name as string);

// The handler only reads hasChildren and data; tree and $event are unused.
const onClick = component.fileTreeDisplayOptions.actionMapping!.mouse!.click!;
const leafNode = { hasChildren: false, data: component.fileTreeNodes[0] } as never;
onClick(undefined as never, leafNode, undefined as never);
it("emits selectedTreeNode and makes the file the active node", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));

expect(emitted).toEqual([component.fileTreeNodes[0]]);
const files = makeFlatFileNodes(3);
const node = renderAndClick(files, 1);

expect(emitted).toEqual([files[1]]);
expect(node.isActive).toBe(true);
expect(activeNames()).toEqual([files[1].name]);
});

// ACTIVATE, not TOGGLE_ACTIVE: a second click on the file the user already picked must not
// strip the highlight off a selection that still stands.
it("keeps the file active when the same file is clicked again, and emits it again", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));
const files = makeFlatFileNodes(3);
const node = renderAndClick(files, 1);

node.mouseAction("click", new MouseEvent("click"));

expect(node.isActive).toBe(true);
expect(activeNames()).toEqual([files[1].name]);
expect(emitted).toEqual([files[1], files[1]]);
});

// The library maps Enter and Space to TOGGLE_ACTIVE by default: Enter on the picked file would
// strip its highlight while the host's selection stands, and on another row would mark a file
// the host never heard of. Both keys now do what a click does.
it("activates and emits on Enter or Space, as on a click, without toggling the pick off", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));
const files = makeFlatFileNodes(3);
component.fileTreeNodes = files;
fixture.detectChanges();
const keys = component.fileTreeDisplayOptions.actionMapping!.keys!;
const node = component.tree.treeModel.roots[1];

keys[KEYS.ENTER](component.tree.treeModel, node, new KeyboardEvent("keydown"));
expect(node.isActive).toBe(true);
keys[KEYS.SPACE](component.tree.treeModel, node, new KeyboardEvent("keydown"));

expect(node.isActive).toBe(true);
expect(activeNames()).toEqual([files[1].name]);
expect(emitted).toEqual([files[1], files[1]]);
});

// Every mouse action focuses the tree, but only a file click focuses a node: after a hover or
// a folder click the tree holds focus with no focused node, and Enter then reaches the key
// action with null, as it does the library's own (which guard with `node &&`).
it("does nothing on Enter when the tree is focused but no node is", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));
const files = makeFlatFileNodes(3);
component.fileTreeNodes = files;
fixture.detectChanges();
const treeModel = component.tree.treeModel;
treeModel.roots[1].mouseAction("mouseOver", new MouseEvent("mouseover"));
expect(treeModel.isFocused).toBe(true);
expect(treeModel.getFocusedNode()).toBeNull();
const enter = new KeyboardEvent("keydown");
Object.defineProperty(enter, "keyCode", { value: KEYS.ENTER });

expect(() => component.tree.onKeydown(enter)).not.toThrow();

expect(activeNames()).toEqual([]);
expect(emitted).toEqual([]);
});

// A host that browses a version's files without being able to hold one (the dataset picker in
// version mode, the model picker) gets no trace of a click: a blue row there would promise a
// pick nothing takes. Folders still open.
it("neither activates nor emits a file where files cannot be picked, and still expands a folder", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));
component.isFileSelectable = false;

const files = makeFlatFileNodes(3);
const node = renderAndClick(files, 1);
expect(emitted).toEqual([]);
// Never activated at all, so the library leaves the flag unset rather than false.
expect(node.isActive).toBeFalsy();
expect(activeNames()).toEqual([]);

const folder = renderAndClick([folderWith(makeFlatFileNodes(2))], 0);
expect(folder.isExpanded).toBe(true);
});

// Every host that never binds the input (the dataset and model pages, the file selector) keeps
// the click it has today.
it("picks files by default, for a host that never binds the input", () => {
expect(component.isFileSelectable).toBe(true);
});

it("moves the highlight to the newly clicked file", () => {
const files = makeFlatFileNodes(3);
renderAndClick(files, 1);

component.tree.treeModel.roots[2].mouseAction("click", new MouseEvent("click"));

expect(activeNames()).toEqual([files[2].name]);
});

it("toggles a folder's expansion, activating nothing and emitting nothing", () => {
const emitted: DatasetFileNode[] = [];
component.selectedTreeNode.subscribe((n: DatasetFileNode) => emitted.push(n));

const folder = renderAndClick([folderWith(makeFlatFileNodes(2))], 0);
expect(folder.isExpanded).toBe(true);

folder.mouseAction("click", new MouseEvent("click"));
expect(folder.isExpanded).toBe(false);

expect(activeNames()).toEqual([]);
expect(emitted).toEqual([]);
});
});

it("emits deletedTreeNode when a node deletion is requested", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
DatasetFileNode,
getRelativePathFromDatasetFileNode,
} from "../../../../../../common/type/datasetVersionFileTree";
import { ITreeOptions, TREE_ACTIONS, TreeModule } from "@ali-hm/angular-tree-component";
import { ITreeOptions, KEYS, TREE_ACTIONS, TreeModule } from "@ali-hm/angular-tree-component";
import { NgIf } from "@angular/common";
import { ɵNzTransitionPatchDirective } from "ng-zorro-antd/core/transition-patch";
import { NzIconDirective } from "ng-zorro-antd/icon";
Expand Down Expand Up @@ -104,6 +104,35 @@ export class UserDatasetVersionFiletreeComponent implements AfterViewInit, OnDes
@Input()
public isExpandAllAfterViewInit = false;

// Whether a file in this tree is an answer the host can take. A host that browses the files of a
// version without holding one (the dataset picker in version mode, the model picker) leaves
// nothing for a click to mean, and a highlighted row there would promise a pick that is never
// made. Defaults to true, which is what every host that never binds it gets.
@Input()
public isFileSelectable = true;

// One handler for a click and for Enter/Space on the focused node: the library maps the keys to
// TOGGLE_ACTIVE by default, which would strip the highlight off the picked file, or mark a file
// the host never heard of.
private readonly activateOrExpand = (tree: any, node: any, $event: any): void => {
// A key can arrive with no node: any mouse action, a hover included, focuses the tree, and
// only a file click also focuses a node, so Enter after hovering a row or opening a folder
// dispatches with null. The library's own key actions guard the same way.
if (!node) {
return;
}
if (node.hasChildren) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node can be null on the key path: hovering a row or clicking a folder focuses the tree without focusing a node, so Enter/Space then throws Cannot read properties of null (reading 'hasChildren'). The library default guards with node &&.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right -- any mouse action focuses the tree (mouseAction calls setFocus(true) for the hover too) and only a file click focuses a node, so Enter after a hover or a folder click reached the handler with null. It returns on no node now, as the library's TOGGLE_ACTIVE does with node &&. The spec hovers a row, sends Enter through the tree's own onKeydown, and expects no throw, no active node and no emit; without the guard it throws exactly this TypeError (f324dce).

TREE_ACTIONS.TOGGLE_EXPANDED(tree, node, $event);
} else if (this.isFileSelectable) {
// Overriding the library's click replaced its default (TOGGLE_ACTIVE) with the emit alone,
// so the tree never had an active node and the clicked row looked like every other.
// ACTIVATE rather than TOGGLE_ACTIVE: a second click on the picked file leaves it
// highlighted, since the selection it emits stands either way.
TREE_ACTIONS.ACTIVATE(tree, node, $event);
this.selectedTreeNode.emit(node.data);
}
};

@ViewChild("tree") tree: any;
@ViewChild("container") container?: ElementRef<HTMLElement>;

Expand All @@ -126,15 +155,8 @@ export class UserDatasetVersionFiletreeComponent implements AfterViewInit, OnDes
useVirtualScroll: true,
nodeHeight: this.TREE_NODE_HEIGHT_PX,
actionMapping: {
mouse: {
click: (tree: any, node: any, $event: any) => {
if (node.hasChildren) {
TREE_ACTIONS.TOGGLE_EXPANDED(tree, node, $event);
} else {
this.selectedTreeNode.emit(node.data);
}
},
},
mouse: { click: this.activateOrExpand },
keys: { [KEYS.ENTER]: this.activateOrExpand, [KEYS.SPACE]: this.activateOrExpand },
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<texera-user-dataset-version-filetree
[isExpandAllAfterViewInit]="true"
[fileTreeNodes]="fileTree"
[isFileSelectable]="fileMode"
(selectedTreeNode)="onFileSelected($event)">
</texera-user-dataset-version-filetree>
<br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export class DatasetSelectionModalComponent implements OnInit {
}
}

/** Whether a file is the answer here (file mode) or only browsed (version mode); see the tree's isFileSelectable. */
get fileMode(): boolean {
return this.data.fileMode;
}

onFileSelected(node: DatasetFileNode) {
if (this.data.fileMode) {
this.selectedPath = getFullPathFromDatasetFileNode(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
<br />
<texera-user-dataset-version-filetree
[isExpandAllAfterViewInit]="true"
[fileTreeNodes]="fileTree">
[fileTreeNodes]="fileTree"
[isFileSelectable]="false">
</texera-user-dataset-version-filetree>
<br />
<button
Expand Down
Loading