-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolbox): refactor context toolbar into a better and reusable to…
…olbox
- Loading branch information
Showing
24 changed files
with
731 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Routes, RouterModule } from '@angular/router'; | ||
import { ModuleWithProviders } from '@angular/core'; | ||
|
||
import { AppToolComponent } from './tool.component'; | ||
|
||
const routes: Routes = [ | ||
{ | ||
path: 'tool', | ||
component: AppToolComponent | ||
} | ||
]; | ||
|
||
export const AppToolRoutingModule: ModuleWithProviders = RouterModule.forChild( | ||
routes | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<mat-card> | ||
<mat-card-subtitle>Common</mat-card-subtitle> | ||
<mat-card-title>Tool</mat-card-title> | ||
<mat-card-content> | ||
See the <a href="https://github.com/infra-geo-ouverte/igo2-lib/tree/master/demo/src/app/common/tool">code of this example</a> | ||
</mat-card-content> | ||
|
||
<igo-panel [title]="panelTitle"> | ||
<button | ||
mat-icon-button | ||
panelLeftButton | ||
(click)="toolbox.activatePreviousTool()" | ||
*ngIf="activeTool$ | async"> | ||
<mat-icon>arrow_back</mat-icon> | ||
</button> | ||
|
||
<button | ||
mat-icon-button | ||
panelRightButton | ||
(click)="toolbox.deactivateTool()" | ||
*ngIf="activeTool$ | async"> | ||
<mat-icon>menu</mat-icon> | ||
</button> | ||
|
||
<igo-toolbox [toolbox]="toolbox" [animate]="true"></igo-toolbox> | ||
</igo-panel> | ||
|
||
<button | ||
mat-flat-button | ||
type="button" | ||
color="primary" | ||
(click)="activateSalutationTool()"> | ||
Activate Salutation Tool | ||
</button> | ||
|
||
</mat-card> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pre, | ||
code { | ||
font-family: monospace, monospace; | ||
} | ||
pre { | ||
overflow: auto; | ||
} | ||
pre > code { | ||
display: block; | ||
padding: 1rem; | ||
word-wrap: normal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
Component, | ||
OnInit, | ||
OnDestroy, | ||
Input, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef | ||
} from '@angular/core'; | ||
|
||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
import { | ||
OnUpdateInputs, | ||
Tool, | ||
Toolbox, | ||
ToolComponent, | ||
ToolService | ||
} from '@igo2/common'; | ||
|
||
@ToolComponent({ | ||
name: 'salutation', | ||
title: 'Salutation', | ||
icon: 'person', | ||
options: {name: 'Jack'} | ||
}) | ||
@Component({ | ||
selector: 'app-salutation-tool', | ||
template: ` | ||
<p>Hello, my name is {{name}}.</p> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class AppSalutationToolComponent implements OnUpdateInputs { | ||
|
||
@Input() name: string; | ||
|
||
constructor(private cdRef: ChangeDetectorRef) {} | ||
|
||
onUpdateInputs() { | ||
this.cdRef.detectChanges(); | ||
} | ||
|
||
} | ||
|
||
@ToolComponent({ | ||
name: 'about', | ||
title: 'About', | ||
icon: 'info' | ||
}) | ||
@Component({ | ||
selector: 'app-about-tool', | ||
template: ` | ||
<p>I'm a tool inside a toolbox.</p> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class AppAboutToolComponent {} | ||
|
||
@Component({ | ||
selector: 'app-tool', | ||
templateUrl: './tool.component.html', | ||
styleUrls: ['./tool.component.scss'] | ||
}) | ||
export class AppToolComponent implements OnInit, OnDestroy { | ||
|
||
toolbox = new Toolbox(); | ||
|
||
get activeTool$(): BehaviorSubject<Tool> { | ||
return this.toolbox.activeTool$; | ||
} | ||
|
||
get panelTitle(): string { | ||
return this.activeTool$.value ? this.activeTool$.value.title : 'Toolbox'; | ||
} | ||
|
||
constructor(private toolService: ToolService) {} | ||
|
||
ngOnInit() { | ||
this.toolbox.setTools(this.toolService.getTools()); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.toolbox.destroy(); | ||
} | ||
|
||
activateSalutationTool() { | ||
this.toolbox.activateTool('salutation', {name: 'Bob'}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { MatButtonModule, MatIconModule, MatCardModule } from '@angular/material'; | ||
|
||
import { IgoPanelModule, IgoToolModule } from '@igo2/common'; | ||
|
||
import { | ||
AppToolComponent, | ||
AppSalutationToolComponent, | ||
AppAboutToolComponent | ||
} from './tool.component'; | ||
import { AppToolRoutingModule } from './tool-routing.module'; | ||
|
||
@NgModule({ | ||
declarations: [ | ||
AppToolComponent, | ||
AppSalutationToolComponent, | ||
AppAboutToolComponent | ||
], | ||
imports: [ | ||
CommonModule, | ||
AppToolRoutingModule, | ||
MatButtonModule, | ||
MatIconModule, | ||
MatCardModule, | ||
IgoPanelModule, | ||
IgoToolModule.forRoot() | ||
], | ||
exports: [AppToolComponent], | ||
entryComponents: [ | ||
AppSalutationToolComponent, | ||
AppAboutToolComponent | ||
] | ||
}) | ||
export class AppToolModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './shared'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './tool.interface'; | ||
export * from './tool.service'; | ||
export * from './tool-component'; | ||
export * from './toolbox'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Tool } from './tool.interface'; | ||
import { ToolService } from './tool.service'; | ||
|
||
export function ToolComponent(tool: Partial<Tool>): (cls: any) => any { | ||
return function (compType: any) { | ||
ToolService.register(Object.assign({}, tool, { | ||
component: compType | ||
} as Tool)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface Tool { | ||
name: string; | ||
component: any; | ||
title?: string; | ||
icon?: string; | ||
iconImage?: string; | ||
tooltip?: string; | ||
options?: { [key: string]: any }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
|
||
import { ToolService } from './tool.service'; | ||
|
||
describe('ToolService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [HttpClientModule], | ||
providers: [ToolService] | ||
}); | ||
}); | ||
|
||
it('should ...', inject([ToolService], (service: ToolService) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
import { Tool } from './tool.interface'; | ||
|
||
/** | ||
* Service where runtime tool configurations are registered | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ToolService { | ||
|
||
static tools: {[key: string]: Tool} = {}; | ||
|
||
static register(tool: Tool) { | ||
ToolService.tools[tool.name] = tool; | ||
} | ||
|
||
constructor() {} | ||
|
||
/** | ||
* Return a tool | ||
* @param name Tool name | ||
* @returns tool Tool | ||
*/ | ||
getTool(name: string): Tool { | ||
return ToolService.tools[name]; | ||
} | ||
|
||
/** | ||
* Return all tools | ||
* @returns tTols | ||
*/ | ||
getTools(): Tool[] { | ||
return Object.values(ToolService.tools); | ||
} | ||
|
||
} |
Oops, something went wrong.