提交 b5b20c46 编写于 作者: R Rachel Macfarlane

Move collapseDeepestExpandedLevel action implementation out of tree

上级 5c21818c
......@@ -109,13 +109,6 @@ export interface ITree {
*/
collapseAll(elements?: any[], recursive?: boolean): WinJS.Promise;
/**
* Collapses several elements.
* Collapses all elements at the greatest tree depth that has expanded elements.
* The returned promise returns a boolean for whether the elements were collapsed or not.
*/
collapseDeepestExpandedLevel(): WinJS.Promise;
/**
* Toggles an element's expansion state.
*/
......
......@@ -184,10 +184,6 @@ export class Tree implements _.ITree {
return this.model.collapseAll(elements, recursive);
}
public collapseDeepestExpandedLevel(): WinJS.Promise {
return this.model.collapseDeepestExpandedLevel();
}
public toggleExpansion(element: any, recursive: boolean = false): WinJS.Promise {
return this.model.toggleExpansion(element, recursive);
}
......
......@@ -559,17 +559,6 @@ export class Item {
return result;
}
public getChildren(): Item[] {
var child = this.firstChild;
var results = [];
while (child) {
results.push(child);
child = child.next;
}
return results;
}
private isAncestorOf(item: Item): boolean {
while (item) {
if (item.id === this.id) {
......@@ -1040,27 +1029,6 @@ export class TreeModel {
return WinJS.Promise.join(promises);
}
public collapseDeepestExpandedLevel(): WinJS.Promise {
var levelToCollapse = this.findDeepestExpandedLevel(this.input, 0);
var items = [this.input];
for (var i = 0; i < levelToCollapse; i++) {
items = arrays.flatten(items.map(node => node.getChildren()));
}
var promises = items.map(child => this.collapse(child, false));
return WinJS.Promise.join(promises);
}
private findDeepestExpandedLevel(item: Item, currentLevel: number): number {
var expandedChildren = item.getChildren().filter(child => child.isExpanded());
if (!expandedChildren.length) {
return currentLevel;
}
return Math.max(...expandedChildren.map(child => this.findDeepestExpandedLevel(child, currentLevel + 1)));
}
public toggleExpansion(element: any, recursive: boolean = false): WinJS.Promise {
return this.isExpanded(element) ? this.collapse(element, recursive) : this.expand(element);
}
......
......@@ -613,23 +613,6 @@ suite('TreeModel - Expansion', () => {
});
});
test('collapseDeepestExpandedLevel', () => {
return model.setInput(SAMPLE.DEEP2).then(() => {
return model.expand(SAMPLE.DEEP2.children[0]).then(() => {
return model.expand(SAMPLE.DEEP2.children[0].children[0]).then(() => {
assert(model.isExpanded(SAMPLE.DEEP2.children[0]));
assert(model.isExpanded(SAMPLE.DEEP2.children[0].children[0]));
return model.collapseDeepestExpandedLevel().then(() => {
assert(model.isExpanded(SAMPLE.DEEP2.children[0]));
assert(!model.isExpanded(SAMPLE.DEEP2.children[0].children[0]));
});
});
});
});
});
test('auto expand single child folders', () => {
return model.setInput(SAMPLE.DEEP).then(() => {
return model.expand(SAMPLE.DEEP.children[0]).then(() => {
......
......@@ -233,7 +233,34 @@ export class CollapseDeepestExpandedLevelAction extends Action {
return TPromise.as(null); // Global action disabled if user is in edit mode from another action
}
viewer.collapseDeepestExpandedLevel();
/**
* The hierarchy is FolderMatch, FileMatch, Match. If the top level is FileMatches, then there is only
* one level to collapse so collapse everything. If FolderMatch, check if there are visible grandchildren,
* i.e. if Matches are returned by the navigator, and if so, collapse to them, otherwise collapse all levels.
*/
const navigator = viewer.getNavigator();
let node = navigator.first();
let collapseFileMatchLevel = false;
if (node instanceof FolderMatch) {
while (node = navigator.next()) {
if (node instanceof Match) {
collapseFileMatchLevel = true;
break;
}
}
}
if (collapseFileMatchLevel) {
node = navigator.first();
do {
if (node instanceof FileMatch) {
viewer.collapse(node);
}
} while (node = navigator.next());
} else {
viewer.collapseAll();
}
viewer.clearSelection();
viewer.clearFocus();
viewer.domFocus();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册