@lidorsystems/integralui-web
Version:
IntegralUI Web - Advanced UI Components for Angular
216 lines (186 loc) • 8.98 kB
text/typescript
/*
Copyright © 2016-2019 Lidor Systems. All rights reserved.
This file is part of the "IntegralUI Web" Library.
The contents of this file are subject to the IntegralUI Web License, and may not be used except in compliance with the License.
A copy of the License should have been installed in the product's root installation directory or it can be found at
http://www.lidorsystems.com/products/web/studio/license-agreement.aspx.
This SOFTWARE is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language
governing rights and limitations under the License. Any infringement will be prosecuted under applicable laws.
*/
import { Component, ViewContainerRef, ViewChild, ViewChildren, ViewEncapsulation } from '@angular/core';
import { IntegralUITreeView } from '../../integralui/components/integralui.treeview';
export class TreeViewAddItemsDynamicallySample {
applicationRef: ViewContainerRef;
treeview: IntegralUITreeView;
public items: Array<any>;
private isEditActive: boolean = false;
public editItem: any = null;
private originalText: string = '';
public editorFocused: boolean = false;
public hoverItem: any = null;
public ctrlStyle: any = {
general: {
normal: 'trw-add-dynamic'
}
}
private itemCount: number = 1;
constructor(){
this.items = [
{ text: "Item 1" }
];
}
// Add/Remove ------------------------------------------------------------------------
createNewItem(){
this.itemCount++;
return { text: "Item " + this.itemCount };
}
addRoot(){
let item: any = this.createNewItem();
this.treeview.addItem(item);
this.showEditor(item);
}
addChild(parent: any){
let item: any = this.createNewItem();
this.treeview.addItem(item, parent);
this.showEditor(item);
}
deleteItem(item: any){
if (item)
this.treeview.removeItem(item);
}
// Edit ------------------------------------------------------------------------------
// Selects the whole text in the text editor
selectContent(e: any){
if (e.target){
setTimeout(function(){
e.target.setSelectionRange(0, e.target.value.length);
}, 1);
}
}
showEditor(item: any){
this.originalText = item.text;
this.isEditActive = true;
this.editItem = item;
this.editorFocused = true;
item.allowDrag = false;
}
closeEditor(){
if (this.editItem)
this.editItem.allowDrag = true;
this.editItem = null;
this.originalText = '';
this.editorFocused = false;
}
editorKeyDown(e: any){
if (this.editItem){
switch (e.keyCode){
case 13: // ENTER
this.closeEditor();
break;
case 27: // ESCAPE
this.editItem.text = this.originalText;
this.closeEditor();
break;
}
}
}
editorLostFocus(){
if (this.editItem)
this.editItem.text = this.originalText;
this.closeEditor();
}
}