import {NgModule,Component,AfterViewInit,AfterViewChecked,OnDestroy,Input,Output,EventEmitter,ElementRef} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {CommonModule} from '@angular/common';
import {DomHandler} from '../dom/domhandler';
import {TerminalService} from './terminalservice';
import {Subscription} from 'rxjs/Subscription';
@Component({
selector: 'p-terminal',
template: `
{{welcomeMessage}}
{{prompt}}
{{command.text}}
{{command.response}}
{{prompt}}
`,
providers: [DomHandler]
})
export class Terminal implements AfterViewInit,AfterViewChecked,OnDestroy {
@Input() welcomeMessage: string;
@Input() prompt: string;
@Input() style: any;
@Input() styleClass: string;
commands: any[] = [];
command: string;
container: Element;
commandProcessed: boolean;
subscription: Subscription;
constructor(public el: ElementRef, public domHandler: DomHandler, public terminalService: TerminalService) {
this.subscription = terminalService.responseHandler.subscribe(response => {
this.commands[this.commands.length - 1].response = response;
this.commandProcessed = true;
});
}
ngAfterViewInit() {
this.container = this.domHandler.find(this.el.nativeElement, '.ui-terminal')[0];
}
ngAfterViewChecked() {
if(this.commandProcessed) {
this.container.scrollTop = this.container.scrollHeight;
this.commandProcessed = false;
}
}
@Input()
set response(value: string) {
if(value) {
this.commands[this.commands.length - 1].response = value;
this.commandProcessed = true;
}
}
handleCommand(event: KeyboardEvent) {
if(event.keyCode == 13) {
this.commands.push({text: this.command});
this.terminalService.sendCommand(this.command);
this.command = '';
}
}
focus(element: HTMLElement) {
element.focus();
}
ngOnDestroy() {
if(this.subscription) {
this.subscription.unsubscribe();
}
}
}
@NgModule({
imports: [CommonModule,FormsModule],
exports: [Terminal],
declarations: [Terminal]
})
export class TerminalModule { }