mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-29 09:35:38 -05:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
171
src/sql/parts/fileBrowser/fileBrowserTreeView.ts
Normal file
171
src/sql/parts/fileBrowser/fileBrowserTreeView.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IErrorMessageService } from 'sql/parts/connection/common/connectionManagement';
|
||||
import { FileBrowserDataSource } from 'sql/parts/fileBrowser/fileBrowserDataSource';
|
||||
import { FileBrowserController } from 'sql/parts/fileBrowser/fileBrowserController';
|
||||
import { FileBrowserRenderer } from 'sql/parts/fileBrowser/fileBrowserRenderer';
|
||||
import { IFileBrowserService } from 'sql/parts/fileBrowser/common/interfaces';
|
||||
import { FileNode } from 'sql/parts/fileBrowser/common/fileNode';
|
||||
import errors = require('vs/base/common/errors');
|
||||
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
|
||||
import * as DOM from 'vs/base/browser/dom';
|
||||
import nls = require('vs/nls');
|
||||
import { DefaultFilter, DefaultAccessibilityProvider, DefaultController, DefaultDragAndDrop } from 'vs/base/parts/tree/browser/treeDefaults';
|
||||
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { attachListStyler } from 'vs/platform/theme/common/styler';
|
||||
import { IThemeService } from 'vs/platform/theme/common/themeService';
|
||||
import { ITree } from 'vs/base/parts/tree/browser/tree';
|
||||
|
||||
/**
|
||||
* Implements tree view for file browser
|
||||
*/
|
||||
export class FileBrowserTreeView {
|
||||
private _tree: ITree;
|
||||
private _toDispose: IDisposable[] = [];
|
||||
|
||||
constructor(
|
||||
@IInstantiationService private _instantiationService: IInstantiationService,
|
||||
@IFileBrowserService private _fileBrowserService: IFileBrowserService,
|
||||
@IErrorMessageService private _errorMessageService: IErrorMessageService,
|
||||
@IThemeService private _themeService: IThemeService
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the view body
|
||||
*/
|
||||
public renderBody(container: HTMLElement, rootNode: FileNode, selectedNode: FileNode, expandedNodes: FileNode[]): void {
|
||||
if (!this._tree) {
|
||||
DOM.addClass(container, 'show-file-icons');
|
||||
this._tree = this.createFileBrowserTree(container, this._instantiationService);
|
||||
this._toDispose.push(this._tree.addListener('selection', (event) => this.onSelected(event)));
|
||||
this._toDispose.push(this._fileBrowserService.onExpandFolder(fileNode => this._tree.refresh(fileNode)));
|
||||
this._toDispose.push(attachListStyler(this._tree, this._themeService));
|
||||
this._tree.DOMFocus();
|
||||
}
|
||||
|
||||
if (rootNode) {
|
||||
this._tree.setInput(rootNode).then(() => {
|
||||
if (expandedNodes) {
|
||||
this._tree.expandAll(expandedNodes);
|
||||
}
|
||||
if (selectedNode) {
|
||||
this._tree.select(selectedNode);
|
||||
this._tree.setFocus(selectedNode);
|
||||
}
|
||||
this._tree.getFocus();
|
||||
}, errors.onUnexpectedError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a file browser tree
|
||||
*/
|
||||
public createFileBrowserTree(treeContainer: HTMLElement, instantiationService: IInstantiationService): Tree {
|
||||
const dataSource = instantiationService.createInstance(FileBrowserDataSource);
|
||||
const renderer = instantiationService.createInstance(FileBrowserRenderer);
|
||||
const controller = instantiationService.createInstance(FileBrowserController);
|
||||
const dnd = new DefaultDragAndDrop();
|
||||
const filter = new DefaultFilter();
|
||||
const sorter = null;
|
||||
const accessibilityProvider = new DefaultAccessibilityProvider();
|
||||
|
||||
return new Tree(treeContainer, {
|
||||
dataSource, renderer, controller, dnd, filter, sorter, accessibilityProvider
|
||||
}, {
|
||||
indentPixels: 10,
|
||||
twistiePixels: 12,
|
||||
ariaLabel: nls.localize({ key: 'regTreeAriaLabel', comment: ['FileBrowserTree'] }, 'File browser tree')
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the tree
|
||||
*/
|
||||
public refreshTree(rootNode: FileNode): void {
|
||||
let selectedElement: any;
|
||||
let targetsToExpand: any[];
|
||||
|
||||
// Focus
|
||||
this._tree.DOMFocus();
|
||||
|
||||
if (this._tree) {
|
||||
let selection = this._tree.getSelection();
|
||||
if (selection && selection.length === 1) {
|
||||
selectedElement = <any>selection[0];
|
||||
}
|
||||
targetsToExpand = this._tree.getExpandedElements();
|
||||
}
|
||||
|
||||
if (rootNode) {
|
||||
this._tree.setInput(rootNode).then(() => {
|
||||
// Make sure to expand all folders that were expanded in the previous session
|
||||
if (targetsToExpand) {
|
||||
this._tree.expandAll(targetsToExpand);
|
||||
}
|
||||
if (selectedElement) {
|
||||
this._tree.select(selectedElement);
|
||||
this._tree.setFocus(selectedElement);
|
||||
}
|
||||
this._tree.getFocus();
|
||||
}, errors.onUnexpectedError);
|
||||
}
|
||||
}
|
||||
|
||||
private onSelected(event: any) {
|
||||
let selection = this._tree.getSelection();
|
||||
|
||||
if (selection && selection.length > 0 && (selection[0] instanceof FileNode)) {
|
||||
let isMouseOrigin = event.payload && (event.payload.origin === 'mouse');
|
||||
let isSingleClick = isMouseOrigin && event.payload.originalEvent && event.payload.originalEvent.detail === 1;
|
||||
let isDoubleClick = isMouseOrigin && event.payload.originalEvent && event.payload.originalEvent.detail === 2;
|
||||
if (isSingleClick) {
|
||||
this.onClickedCallback(event.selection[0]);
|
||||
} else if (isDoubleClick) {
|
||||
this.onDoublieClickedCallback(event.selection[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public onClickedCallback: any;
|
||||
public setOnClickedCallback(fn: any) {
|
||||
this.onClickedCallback = fn;
|
||||
}
|
||||
|
||||
public onDoublieClickedCallback: any;
|
||||
public setOnDoubleClickedCallback(fn: any) {
|
||||
this.onDoublieClickedCallback = fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the layout of the view
|
||||
*/
|
||||
public layout(height?: number): void {
|
||||
this._tree.layout(height);
|
||||
}
|
||||
|
||||
/**
|
||||
* set the visibility of the view
|
||||
*/
|
||||
public setVisible(visible: boolean): void {
|
||||
if (visible) {
|
||||
this._tree.onVisible();
|
||||
} else {
|
||||
this._tree.onHidden();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dispose the file browser tree view
|
||||
*/
|
||||
public dispose(): void {
|
||||
if (this._tree) {
|
||||
this._tree.dispose();
|
||||
}
|
||||
this._toDispose = dispose(this._toDispose);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user