@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
111 lines • 4.65 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2017 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { Container } from 'inversify';
import { TreeWidget, TreeProps, defaultTreeProps } from './tree-widget.js';
import { TreeModelImpl, TreeModel } from './tree-model.js';
import { TreeImpl, Tree } from './tree.js';
import { TreeSelectionService } from './tree-selection.js';
import { TreeSelectionServiceImpl } from './tree-selection-impl.js';
import { TreeExpansionService, TreeExpansionServiceImpl } from './tree-expansion.js';
import { TreeNavigationService } from './tree-navigation.js';
import { TreeDecoratorService, NoopTreeDecoratorService } from './tree-decorator.js';
import { TreeSearch } from './tree-search.js';
import { FuzzySearch } from './fuzzy-search.js';
import { SearchBox, SearchBoxFactory } from './search-box.js';
import { SearchBoxDebounce } from './search-box-debounce.js';
import { TreeFocusService, TreeFocusServiceImpl } from './tree-focus-service.js';
export function isTreeServices(candidate) {
if (candidate) {
const maybeServices = candidate;
for (const key of Object.keys(maybeServices)) {
// This key is in both TreeProps and TreeContainerProps, so we have to handle it separately
if (key === 'search' && typeof maybeServices[key] === 'boolean') {
return false;
}
if (key in defaultImplementations) {
return true;
}
}
}
return false;
}
export function createTreeContainer(parent, props) {
const child = new Container({ defaultScope: 'Singleton' });
child.parent = parent;
const overrideServices = isTreeServices(props) ? props : { props: props };
for (const key of Object.keys(serviceIdentifiers)) {
if (key === 'props') {
const { service, identifier } = getServiceAndIdentifier(key, overrideServices);
child.bind(identifier).toConstantValue({
...defaultImplementations.props,
...service
});
}
else if (key === 'searchBoxFactory') {
const { service, identifier } = getServiceAndIdentifier(key, overrideServices);
child.bind(identifier).toFactory(context => service(context));
}
else {
const { service, identifier } = getServiceAndIdentifier(key, overrideServices);
child.bind(service).toSelf().inSingletonScope();
if (identifier !== service) {
child.bind(identifier).toService(service);
}
}
}
return child;
}
function getServiceAndIdentifier(key, overrides) {
const override = overrides[key];
const service = override ?? defaultImplementations[key];
return {
service,
identifier: serviceIdentifiers[key]
};
}
const defaultSearchBoxFactoryFactory = () => options => {
const debounce = new SearchBoxDebounce(options);
return new SearchBox(options, debounce);
};
const defaultImplementations = {
tree: TreeImpl,
selectionService: TreeSelectionServiceImpl,
expansionService: TreeExpansionServiceImpl,
navigationService: TreeNavigationService,
model: TreeModelImpl,
widget: TreeWidget,
search: TreeSearch,
fuzzy: FuzzySearch,
decoratorService: NoopTreeDecoratorService,
focusService: TreeFocusServiceImpl,
props: defaultTreeProps,
searchBoxFactory: defaultSearchBoxFactoryFactory,
};
const serviceIdentifiers = {
tree: Tree,
selectionService: TreeSelectionService,
expansionService: TreeExpansionService,
navigationService: TreeNavigationService,
model: TreeModel,
widget: TreeWidget,
props: TreeProps,
search: TreeSearch,
fuzzy: FuzzySearch,
searchBoxFactory: SearchBoxFactory,
decoratorService: TreeDecoratorService,
focusService: TreeFocusService
};
//# sourceMappingURL=tree-container.js.map