Code cleaned up

This commit is contained in:
Piotr Pekala
2019-01-09 04:46:10 -08:00
parent 740a042b0e
commit 9925b49725
9 changed files with 58 additions and 46 deletions

View File

@ -48,7 +48,7 @@ import { TextElementFactory } from './helpers/drawings-factory/text-element-fact
import { EllipseElementFactory } from './helpers/drawings-factory/ellipse-element-factory'; import { EllipseElementFactory } from './helpers/drawings-factory/ellipse-element-factory';
import { RectangleElementFactory } from './helpers/drawings-factory/rectangle-element-factory'; import { RectangleElementFactory } from './helpers/drawings-factory/rectangle-element-factory';
import { LineElementFactory } from './helpers/drawings-factory/line-element-factory'; import { LineElementFactory } from './helpers/drawings-factory/line-element-factory';
import { TemporaryTextElementComponent } from './components/temporary-text-element/temporary-text-element.component'; import { TextEditorComponent } from './components/text-editor/text-editor.component';
import { DrawingAddingComponent } from './components/drawing-adding/drawing-adding.component'; import { DrawingAddingComponent } from './components/drawing-adding/drawing-adding.component';
@ -63,7 +63,7 @@ import { DrawingAddingComponent } from './components/drawing-adding/drawing-addi
ExperimentalMapComponent, ExperimentalMapComponent,
DrawingAddingComponent, DrawingAddingComponent,
DrawingResizingComponent, DrawingResizingComponent,
TemporaryTextElementComponent, TextEditorComponent,
...ANGULAR_MAP_DECLARATIONS, ...ANGULAR_MAP_DECLARATIONS,
SelectionControlComponent, SelectionControlComponent,
SelectionSelectComponent, SelectionSelectComponent,

View File

@ -12,5 +12,5 @@
<app-drawing-resizing></app-drawing-resizing> <app-drawing-resizing></app-drawing-resizing>
<app-selection-control></app-selection-control> <app-selection-control></app-selection-control>
<app-selection-select></app-selection-select> <app-selection-select></app-selection-select>
<app-temporary-text-element #temporaryTextElement [svg]="svg"></app-temporary-text-element> <app-text-editor #textEditor [svg]="svg"></app-text-editor>
<app-draggable-selection [svg]="svg"></app-draggable-selection> <app-draggable-selection [svg]="svg"></app-draggable-selection>

Before

Width:  |  Height:  |  Size: 518 B

After

Width:  |  Height:  |  Size: 486 B

View File

@ -20,7 +20,7 @@ import { GraphDataManager } from '../../managers/graph-data-manager';
import { MapSettingsManager } from '../../managers/map-settings-manager'; import { MapSettingsManager } from '../../managers/map-settings-manager';
import { Server } from '../../../models/server'; import { Server } from '../../../models/server';
import { ToolsService } from '../../../services/tools.service'; import { ToolsService } from '../../../services/tools.service';
import { TemporaryTextElementComponent } from '../temporary-text-element/temporary-text-element.component'; import { TextEditorComponent } from '../text-editor/text-editor.component';
@Component({ @Component({
@ -39,7 +39,7 @@ export class D3MapComponent implements OnInit, OnChanges, OnDestroy {
@Input() height = 600; @Input() height = 600;
@ViewChild('svg') svgRef: ElementRef; @ViewChild('svg') svgRef: ElementRef;
@ViewChild('temporaryTextElement') temporaryTextElement: TemporaryTextElementComponent; @ViewChild('textEditor') textEditor: TextEditorComponent;
private parentNativeElement: any; private parentNativeElement: any;
private svg: Selection<SVGSVGElement, any, null, undefined>; private svg: Selection<SVGSVGElement, any, null, undefined>;
@ -168,7 +168,7 @@ export class D3MapComponent implements OnInit, OnChanges, OnDestroy {
this.graphDataManager.setLinks(this.links); this.graphDataManager.setLinks(this.links);
this.graphDataManager.setDrawings(this.drawings); this.graphDataManager.setDrawings(this.drawings);
this.graphLayout.draw(this.svg, this.context); this.graphLayout.draw(this.svg, this.context);
this.temporaryTextElement.activateTextEditing(); this.textEditor.activateTextEditing();
} }
@HostListener('window:resize', ['$event']) @HostListener('window:resize', ['$event'])

View File

@ -1,23 +0,0 @@
import { TemporaryTextElementComponent } from "./temporary-text-element.component";
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
describe('TemporaryTextElementComponent', () => {
let component: TemporaryTextElementComponent;
let fixture: ComponentFixture<TemporaryTextElementComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule
],
providers: [
],
declarations: [
TemporaryTextElementComponent
]
});
}));
});

View File

@ -0,0 +1,40 @@
import { TextEditorComponent } from "./text-editor.component";
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { DrawingsEventSource } from '../../events/drawings-event-source';
import { ToolsService } from '../../../services/tools.service';
import { Context } from '../../models/context';
import { Renderer2 } from '@angular/core';
describe('TemporaryTextElementComponent', () => {
let component: TextEditorComponent;
let fixture: ComponentFixture<TextEditorComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule
],
providers: [
{ provide: DrawingsEventSource, useClass: DrawingsEventSource },
{ provide: ToolsService, useClass: ToolsService },
{ provide: Context, useClass: Context },
{ provide: Renderer2, useClass: Renderer2 }
],
declarations: [
TextEditorComponent
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TextEditorComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -9,18 +9,16 @@ import { Subscription } from 'rxjs';
@Component({ @Component({
selector: 'app-temporary-text-element', selector: 'app-text-editor',
templateUrl: './temporary-text-element.component.html', templateUrl: './text-editor.component.html',
styleUrls: ['./temporary-text-element.component.scss'] styleUrls: ['./text-editor.component.scss']
}) })
export class TemporaryTextElementComponent implements OnInit, OnDestroy { export class TextEditorComponent implements OnInit, OnDestroy {
@ViewChild('temporaryTextElement') temporaryTextElement: ElementRef; @ViewChild('temporaryTextElement') temporaryTextElement: ElementRef;
@Input('svg') svg: SVGSVGElement; @Input('svg') svg: SVGSVGElement;
private isActive: boolean = true;
private leftPosition: string = '0px'; private leftPosition: string = '0px';
private topPosition: string = '0px'; private topPosition: string = '0px';
private display: string = 'none';
private innerText: string = ''; private innerText: string = '';
private editingDrawingId: string; private editingDrawingId: string;
@ -40,39 +38,36 @@ export class TemporaryTextElementComponent implements OnInit, OnDestroy {
ngOnInit(){ ngOnInit(){
this.textAddingSubscription = this.toolsService.isTextAddingToolActivated.subscribe((isActive: boolean) => { this.textAddingSubscription = this.toolsService.isTextAddingToolActivated.subscribe((isActive: boolean) => {
isActive ? this.activate() : this.deactivate() isActive ? this.activateTextAdding() : this.deactivateTextAdding()
}); });
this.activateTextEditing(); this.activateTextEditing();
} }
activate(){ activateTextAdding(){
let addTextListener = (event: MouseEvent) => { let addTextListener = (event: MouseEvent) => {
this.leftPosition = event.clientX.toString() + 'px'; this.leftPosition = event.clientX.toString() + 'px';
this.topPosition = (event.clientY + window.pageYOffset).toString() + 'px'; this.topPosition = (event.clientY + window.pageYOffset).toString() + 'px';
this.renderer.setStyle(this.temporaryTextElement.nativeElement, 'display', 'initial'); this.renderer.setStyle(this.temporaryTextElement.nativeElement, 'display', 'initial');
this.temporaryTextElement.nativeElement.focus(); this.temporaryTextElement.nativeElement.focus();
// //this.renderer.selectRootElement('#temporaryElement').focus();
let textListener = () => { let textListener = () => {
this.drawingsEventSource.textAdded.emit(new TextAddedDataEvent(this.temporaryTextElement.nativeElement.innerText.replace(/\n$/, ""), event.clientX, event.clientY)); this.drawingsEventSource.textAdded.emit(new TextAddedDataEvent(this.temporaryTextElement.nativeElement.innerText.replace(/\n$/, ""), event.clientX, event.clientY));
this.deactivate(); this.deactivateTextAdding();
this.innerText = ''; this.innerText = '';
this.temporaryTextElement.nativeElement.removeEventListener("focusout", this.textListener); this.temporaryTextElement.nativeElement.removeEventListener("focusout", this.textListener);
this.renderer.setStyle(this.temporaryTextElement.nativeElement, 'display', 'none'); this.renderer.setStyle(this.temporaryTextElement.nativeElement, 'display', 'none');
this.display = 'none';
} }
this.textListener = textListener; this.textListener = textListener;
this.temporaryTextElement.nativeElement.addEventListener('focusout', this.textListener); this.temporaryTextElement.nativeElement.addEventListener('focusout', this.textListener);
} }
this.deactivate(); this.deactivateTextAdding();
this.mapListener = addTextListener; this.mapListener = addTextListener;
this.svg.addEventListener('click', this.mapListener as EventListenerOrEventListenerObject); this.svg.addEventListener('click', this.mapListener as EventListenerOrEventListenerObject);
} }
deactivate(){ deactivateTextAdding(){
this.svg.removeEventListener('click', this.mapListener as EventListenerOrEventListenerObject); this.svg.removeEventListener('click', this.mapListener as EventListenerOrEventListenerObject);
} }
@ -119,6 +114,6 @@ export class TemporaryTextElementComponent implements OnInit, OnDestroy {
} }
ngOnDestroy(){ ngOnDestroy(){
this.toolsService.isTextAddingToolActivated.unsubscribe(); this.textAddingSubscription.unsubscribe();
} }
} }

View File

@ -147,7 +147,7 @@
[server]="server" [server]="server"
[project]="project" [project]="project"
[selectedDrawing]="selectedDrawing" [selectedDrawing]="selectedDrawing"
(drawingSaved)="onDrawingSaved($event)"> (drawingSaved)="onDrawingSaved()">
</app-drawing-added> </app-drawing-added>
<app-drawing-dragged [server]="server"></app-drawing-dragged> <app-drawing-dragged [server]="server"></app-drawing-dragged>
<app-drawing-resized [server]="server"></app-drawing-resized> <app-drawing-resized [server]="server"></app-drawing-resized>
@ -158,6 +158,6 @@
<app-text-added <app-text-added
[server]="server" [server]="server"
[project]="project" [project]="project"
(drawingSaved)="onDrawingSaved($event)"> (drawingSaved)="onDrawingSaved()">
</app-text-added> </app-text-added>
<app-text-edited [server]="server"></app-text-edited> <app-text-edited [server]="server"></app-text-edited>