ng2-tree-hackaday
Version:
angular2 component for visualizing data that can be naturally represented as a tree
55 lines (47 loc) • 1.57 kB
text/typescript
import {
Directive,
ElementRef,
Input,
OnInit,
Output,
EventEmitter,
HostListener,
Inject,
Renderer
} from '@angular/core';
import { NodeEditableEvent, NodeEditableEventAction } from './editable.events';
export class NodeEditableDirective implements OnInit {
/* tslint:disable:no-input-rename */
public nodeValue: string;
/* tslint:enable:no-input-rename */
public valueChanged: EventEmitter<NodeEditableEvent> = new EventEmitter<NodeEditableEvent>(false);
public constructor( private renderer: Renderer,
private elementRef: ElementRef) {
}
public ngOnInit(): void {
const nativeElement = this.elementRef.nativeElement;
this.renderer.invokeElementMethod(nativeElement, 'focus', []);
this.renderer.setElementProperty(nativeElement, 'value', this.nodeValue);
}
public applyNewValue(newNodeValue: string): void {
this.valueChanged.emit({type: 'keyup', value: newNodeValue});
}
public applyNewValueByLoosingFocus(newNodeValue: string): void {
this.valueChanged.emit({type: 'blur', value: newNodeValue});
}
public cancelEditing(): void {
this.valueChanged.emit({
type: 'keyup',
value: this.nodeValue,
action: NodeEditableEventAction.Cancel
});
}
}