Skip to content

Commit

Permalink
tree: node.collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Jul 10, 2018
1 parent 47e4319 commit 5fd4be9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/vs/base/browser/ui/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ interface ITreeListTemplateData<T> {
templateData: T;
}

function renderTwistie<T>(node: ITreeNode<T>, twistie: HTMLElement): void {
if (node.children.length === 0 && !node.collapsible) {
twistie.innerText = '';
} else {
twistie.innerText = node.collapsed ? '▹' : '◢';
}
}

class TreeRenderer<T, TTemplateData> implements IRenderer<ITreeNode<T>, ITreeListTemplateData<TTemplateData>> {

readonly templateId: string;
Expand Down Expand Up @@ -92,8 +100,8 @@ class TreeRenderer<T, TTemplateData> implements IRenderer<ITreeNode<T>, ITreeLis
this.renderedNodes.set(node, templateData);
templateData.elementDisposable = toDisposable(() => this.renderedNodes.delete(node));

templateData.twistie.innerText = node.children.length === 0 ? '' : (node.collapsed ? '▹' : '◢');
templateData.twistie.style.width = `${10 + node.depth * 10}px`;
renderTwistie(node, templateData.twistie);

this.renderer.renderElement(node.element, index, templateData.templateData);
}
Expand All @@ -109,7 +117,7 @@ class TreeRenderer<T, TTemplateData> implements IRenderer<ITreeNode<T>, ITreeLis
return;
}

templateData.twistie.innerText = node.children.length === 0 ? '' : (node.collapsed ? '▹' : '◢');
renderTwistie(node, templateData.twistie);
}

dispose(): void {
Expand Down
12 changes: 10 additions & 2 deletions src/vs/base/browser/ui/tree/treeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Emitter, Event } from 'vs/base/common/event';
export interface ITreeElement<T> {
readonly element: T;
readonly children?: IIterator<ITreeElement<T>> | ITreeElement<T>[];
readonly collapsible?: boolean;
readonly collapsed?: boolean;
}

Expand All @@ -20,6 +21,7 @@ export interface ITreeNode<T> {
readonly element: T;
readonly children: IMutableTreeNode<T>[];
readonly depth: number;
readonly collapsible: boolean;
readonly collapsed: boolean;
readonly visibleCount: number;
}
Expand Down Expand Up @@ -61,15 +63,16 @@ function getTreeElementIterator<T>(elements: IIterator<ITreeElement<T>> | ITreeE

function treeElementToNode<T>(treeElement: ITreeElement<T>, parent: IMutableTreeNode<T>, visible: boolean, treeListElements: ITreeNode<T>[]): IMutableTreeNode<T> {
const depth = parent.depth + 1;
const { element, collapsed } = treeElement;
const node = { parent, element, children: [], depth, collapsed: !!collapsed, visibleCount: 0 };
const { element, collapsible, collapsed } = treeElement;
const node = { parent, element, children: [], depth, collapsible: !!collapsible, collapsed: !!collapsed, visibleCount: 0 };

if (visible) {
treeListElements.push(node);
}

const children = getTreeElementIterator(treeElement.children);
node.children = collect(map(children, el => treeElementToNode(el, node, visible && !treeElement.collapsed, treeListElements)));
node.collapsible = node.collapsible || node.children.length > 0;
node.visibleCount = 1 + getVisibleCount(node.children);

return node;
Expand All @@ -89,6 +92,7 @@ export class TreeModel<T> {
element: undefined,
children: [],
depth: 0,
collapsible: false,
collapsed: false,
visibleCount: 1
};
Expand Down Expand Up @@ -131,6 +135,10 @@ export class TreeModel<T> {
private _setCollapsed(location: number[], collapsed?: boolean | undefined): boolean {
const { node, listIndex, visible } = this.findNode(location);

if (!node.collapsible) {
return false;
}

if (typeof collapsed === 'undefined') {
collapsed = !node.collapsed;
}
Expand Down
2 changes: 1 addition & 1 deletion test/tree/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function getTree(fsPath, level) {

const childNames = await fs.readdir(fsPath);
const children = await Promise.all(childNames.map(async childName => await getTree(path.join(fsPath, childName), level + 1)));
return { element, collapsed: false, children };
return { element, collapsible: true, collapsed: false, children };
}

app.use(serve('public'));
Expand Down

0 comments on commit 5fd4be9

Please sign in to comment.