Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Restructured some code
Browse files Browse the repository at this point in the history
  • Loading branch information
spoenemann committed May 17, 2018
1 parent 82a714f commit 689eb60
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
16 changes: 6 additions & 10 deletions depgraph-navigator/src/browser/graph/graph-sprotty-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { ContainerModule, Container } from 'inversify';
import { ContainerModule, Container, interfaces } from 'inversify';
import {
TYPES, ConsoleLogger, LogLevel, SGraphFactory, configureModelElement, SGraph,
SGraphView, HtmlRoot, HtmlRootView, PreRenderedElement, PreRenderedView, SLabel,
Expand All @@ -22,21 +22,15 @@ import { NpmDependencyGraphGenerator } from './npm-dependencies';
import { ResolveNodesHandler } from './resolve-nodes';
import { DependencyNodeView, DependencyEdgeView } from './graph-views';
import { popupModelFactory } from './popup-info';
import { ElkGraphLayout, ElkFactory } from './graph-layout';
import { ElkGraphLayout } from './graph-layout';
import { DependencyGraphFilter } from './graph-filter';

export interface ContainerFactoryArguments {
elkFactory: ElkFactory
graphGenerator?: { new(...args: any[]): IGraphGenerator }
}

export default (args: ContainerFactoryArguments) => {
export default (additionalBindings?: interfaces.ContainerModuleCallBack) => {
const depGraphModule = new ContainerModule((bind, unbind, isBound, rebind) => {
bind(DependencyGraphFilter).toSelf();
bind(ResolveNodesHandler).toSelf();
bind(ElkFactory).toConstantValue(args.elkFactory);
bind(ElkGraphLayout).toSelf();
bind(IGraphGenerator).to(args.graphGenerator || NpmDependencyGraphGenerator).inSingletonScope();
bind(IGraphGenerator).to(NpmDependencyGraphGenerator).inSingletonScope();
bind(TYPES.ModelSource).to(DepGraphModelSource).inSingletonScope();
bind(TYPES.PopupModelFactory).toConstantValue(popupModelFactory);
rebind(TYPES.ILogger).to(ConsoleLogger).inSingletonScope();
Expand All @@ -50,6 +44,8 @@ export default (args: ContainerFactoryArguments) => {
configureModelElement(context, 'compartment', SCompartment, SCompartmentView);
configureModelElement(context, 'html', HtmlRoot, HtmlRootView);
configureModelElement(context, 'pre-rendered', PreRenderedElement, PreRenderedView);
if (additionalBindings)
additionalBindings(bind, unbind, isBound, rebind);
});

const container = new Container();
Expand Down
8 changes: 6 additions & 2 deletions depgraph-navigator/src/browser/widget/diagram-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { overrideViewerOptions, KeyTool, TYPES } from "sprotty/lib";
import { DiagramConfiguration, TheiaKeyTool } from "theia-sprotty/lib";
import { DepGraphModelSource } from "../graph/model-source";
import { IGraphGenerator } from "../graph/graph-generator";
import { NodeModulesGraphGenerator } from "./diagram-manager";
import { ElkFactory } from "../graph/graph-layout";
import { NodeModulesGraphGenerator } from "./node-modules";
import containerFactory from '../graph/graph-sprotty-config';
import elkFactory from '../graph/elk-bundled';

Expand All @@ -27,7 +28,10 @@ export class DepGraphDiagramConfiguration implements DiagramConfiguration {
readonly diagramType: string = 'dependency-graph';

createContainer(widgetId: string): Container {
const container = containerFactory({ elkFactory, graphGenerator: NodeModulesGraphGenerator });
const container = containerFactory((bind, unbind, isBound, rebind) => {
bind(ElkFactory).toConstantValue(elkFactory);
rebind(IGraphGenerator).to(NodeModulesGraphGenerator).inSingletonScope();
});
container.rebind(KeyTool).to(TheiaKeyTool).inSingletonScope();
overrideViewerOptions(container, {
baseDiv: widgetId
Expand Down
32 changes: 1 addition & 31 deletions depgraph-navigator/src/browser/widget/diagram-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@

import { injectable, inject } from "inversify";
import URI from "@theia/core/lib/common/uri";
import { Path } from "@theia/core/lib/common";
import { OpenerOptions } from "@theia/core/lib/browser";
import { FileSystem } from "@theia/filesystem/lib/common";
import { DiagramManagerImpl, DiagramWidget } from "theia-sprotty/lib";
import { DepGraphModelSource } from '../graph/model-source';
import { DependencyGraphNodeSchema } from "../graph/graph-model";
import { VersionMetadata } from "../graph/registry-metadata";
import { NpmDependencyGraphGenerator } from "../graph/npm-dependencies";
import { NodeModulesGraphGenerator } from "./node-modules";

@injectable()
export class DepGraphDiagramManager extends DiagramManagerImpl {
Expand Down Expand Up @@ -61,30 +58,3 @@ export class DepGraphDiagramManager extends DiagramManagerImpl {
}

}

@injectable()
export class NodeModulesGraphGenerator extends NpmDependencyGraphGenerator {

fileSystem?: FileSystem;
startUri?: URI;

protected async getMetadata(node: DependencyGraphNodeSchema): Promise<VersionMetadata> {
const startUri = this.startUri;
if (this.fileSystem && startUri) {
const segs = startUri.path.toString().split(Path.separator);
while (segs.length > 0 && segs[segs.length - 1] && segs[segs.length - 1] !== '..') {
segs.splice(segs.length - 1, 1);
const packageUri = startUri.withPath(`${segs.join('/')}/node_modules/${node.name}/package.json`);
try {
const { content } = await this.fileSystem.resolveContent(packageUri.toString());
return JSON.parse(content);
} catch {
// Try the parent directory
}
}
}
// Fall back to the remote package registry
return super.getMetadata(node);
}

}
43 changes: 43 additions & 0 deletions depgraph-navigator/src/browser/widget/node-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2018 TypeFox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*/

import { injectable } from "inversify";
import URI from "@theia/core/lib/common/uri";
import { Path } from "@theia/core/lib/common";
import { FileSystem } from "@theia/filesystem/lib/common";
import { DependencyGraphNodeSchema } from "../graph/graph-model";
import { VersionMetadata } from "../graph/registry-metadata";
import { NpmDependencyGraphGenerator } from "../graph/npm-dependencies";

@injectable()
export class NodeModulesGraphGenerator extends NpmDependencyGraphGenerator {

fileSystem?: FileSystem;
startUri?: URI;

protected async getMetadata(node: DependencyGraphNodeSchema): Promise<VersionMetadata> {
const startUri = this.startUri;
if (this.fileSystem && startUri) {
const segs = startUri.path.toString().split(Path.separator);
while (segs.length > 0 && segs[segs.length - 1] && segs[segs.length - 1] !== '..') {
segs.splice(segs.length - 1, 1);
const packageUri = startUri.withPath(`${segs.join('/')}/node_modules/${node.name}/package.json`);
try {
const { content } = await this.fileSystem.resolveContent(packageUri.toString());
return JSON.parse(content);
} catch {
// Try the parent directory
}
}
}
// Fall back to the remote package registry
return super.getMetadata(node);
}

}
7 changes: 5 additions & 2 deletions standalone-app/src/frontend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import * as faGithub from '@fortawesome/fontawesome-free-brands/faGithub';
import fontawesome from '@fortawesome/fontawesome';
import { TYPES } from 'sprotty/lib';
import {
containerFactory, DepGraphModelSource, REGISTRY_URL, NpmDependencyGraphGenerator, IGraphGenerator
containerFactory, DepGraphModelSource, REGISTRY_URL, NpmDependencyGraphGenerator,
IGraphGenerator, ElkFactory
} from 'depgraph-navigator/lib/browser';
import elkFactory from 'depgraph-navigator/lib/browser/graph/elk-webworker';

Expand All @@ -30,7 +31,9 @@ jQuery(() => {

//---------------------------------------------------------
// Create sprotty container and initialize model source
const container = containerFactory({ elkFactory });
const container = containerFactory((bind, unbind, isBound, rebind) => {
bind(ElkFactory).toConstantValue(elkFactory);
});
const modelSource = container.get<DepGraphModelSource>(TYPES.ModelSource);
modelSource.loadIndicator = loading => {
loadingIndicator.css({ visibility: loading ? 'visible' : 'hidden' });
Expand Down

0 comments on commit 689eb60

Please sign in to comment.