@lidorsystems/integralui-web
Version:
IntegralUI Web - Advanced UI Components for Angular
346 lines (286 loc) • 12.6 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, ViewEncapsulation } from '@angular/core';
import { IntegralUITreeGrid } from '../../integralui/components/integralui.treegrid';
export class TreeGridContextMenuSample {
applicationRef: ViewContainerRef;
treegrid: IntegralUITreeGrid;
public columns: Array<any>;
public rows: Array<any>;
// Add Remove
private columnCount: number = 3;
private rowCount: number = 3;
// Editing
private isEditActive: boolean = false;
public editCell: any = null;
private originalText: string = '';
public editorFocused: boolean = false;
// Column Menu settings
public columnMenu: any = {
appRef: null,
items: [
{ id: 3, text: "Add Column" },
{ id: 4, text: "Insert Column Before" },
{ id: 5, text: "Insert Column After" },
{ id: 6, type: "separator" },
{ id: 7, text: "Remove Column" }
]
}
// Row Menu settings
public rowMenu: any = {
appRef: null,
items: [
{ id: 1, text: "Edit" },
{ id: 2, type: "separator" },
{ id: 3, text: "Add Row" },
{ id: 4, text: "Insert Row Before" },
{ id: 5, text: "Insert Row After" },
{ id: 6, type: "separator" },
{ id: 7, text: "Remove Row" }
]
}
public treegridStyle: any = {
general: {
normal: 'treegrid-cmn-normal'
}
}
constructor(){
this.columns = [
{ id: 1, headerText: "Header1", footerText: "Footer1", width: 200 },
{ id: 2, headerText: "Header2", footerText: "Footer2", width: 250 },
{ id: 3, headerText: "Header3", footerText: "Footer3", width: 120 }
];
this.rows = [
{
id: 1,
text: "Item1",
cells: [{ cid: 1, text: "Item11" }, { cid: 2, text: "Item12" }, { cid: 3, text: "Item13" }],
rows: [
{ id: 11, pid: 1, text: "Item11", cells: [{ cid: 1, text: "Item111" }, { cid: 2, text: "Item112" }, { cid: 3, text: "Item113" }] }
]
},
{ id: 2, text: "Row2", cells: [{ cid: 1, text: "Item21" }, { cid: 2, text: "Item22" }, { cid: 3, text: "Item23" }] }
];
}
ngAfterViewInit(){
// This is required in order for Context Menu component to appear
// The menu is added as a child of specified app component
this.columnMenu.appRef = this.applicationRef;
this.rowMenu.appRef = this.applicationRef;
}
// ContextMenu events ----------------------------------------------------------------
columnMenuOpening(e: any, column: any){
this.treegrid.selectedColumn = column;
}
columnMenuItemClick(e: any){
if (e.item){
// Action depends on selected menu option
switch (e.item.id){
case 3:
this.treegrid.addColumn(this.createNewColumn());
break;
case 4:
this.treegrid.insertColumnBefore(this.createNewColumn(), this.treegrid.selectedColumn);
break;
case 5:
this.treegrid.insertColumnAfter(this.createNewColumn(), this.treegrid.selectedColumn);
break;
case 7:
this.treegrid.removeColumn(this.treegrid.selectedColumn);
break;
}
this.treegrid.updateLayout();
}
}
rowMenuItemClick(e: any, cell: any){
if (e.item){
// Action depends on selected menu option
switch (e.item.id){
case 1:
this.showEditor(cell);
break;
case 3:
this.treegrid.addRow(this.createNewRow(), this.treegrid.selectedRow);
break;
case 4:
this.treegrid.insertRowBefore(this.createNewRow(), this.treegrid.selectedRow);
break;
case 5:
this.treegrid.insertRowAfter(this.createNewRow(), this.treegrid.selectedRow);
break;
case 7:
this.treegrid.removeRow(this.treegrid.selectedRow);
break;
}
this.treegrid.updateLayout();
}
}
// Add Remove Columns ----------------------------------------------------------------
createNewColumn(){
this.columnCount++;
return { id: this.columnCount, headerText: "Header" + this.columnCount, footerText: "Footer" + this.columnCount };
}
// Add Remove Rows ----------------------------------------------------------------
createNewRow(){
this.rowCount++;
let newRow: any = {
text: "Row" + this.rowCount,
cells: []
}
for (let j = 1; j <= this.columns.length; j++){
let colId: any = this.columns[j-1].id;
newRow.cells.push({ cid: colId, text: "Item" + this.rowCount + colId });
}
return newRow;
}
// Editing ---------------------------------------------------------------------------
showEditor(cell: any){
// A timeout is required in this case, because when edit option from context menu is selected
// there is a small delay prior context menu closes and focus is transfered from context menu to the cell
// In other cases (when context menu is not used), the timout is not needed
let self = this;
let editTimeout = setTimeout(function(){
self.originalText = cell.text;
self.isEditActive = true;
self.editCell = cell;
self.editorFocused = true;
clearTimeout(editTimeout);
}, 150);
}
// 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);
}
}
closeEditor(){
this.editCell = null;
this.originalText = '';
this.editorFocused = false;
}
editorKeyDown(e: any){
if (this.editCell){
switch (e.keyCode){
case 13: // ENTER
this.closeEditor();
break;
case 27: // ESCAPE
this.editCell.text = this.originalText;
this.closeEditor();
break;
}
}
}
editorLostFocus(){
if (this.editCell)
this.editCell.text = this.originalText;
this.closeEditor();
}
// Returns the level of the row in tree hierearchy
getRowLevel(row: any){
let level: number = 0;
let parent: any = this.treegrid.getRowParent(row);
while (parent){
level++;
parent = this.treegrid.getRowParent(parent);
}
return level;
}
// Calculates the width of treegrid cell
getCellWidth(cell: any){
let cellWidth: number = 100;
for (let j = 0; j < this.columns.length; j++){
if (this.columns[j].id == cell.cid){
cellWidth = this.columns[j].width;
break;
}
}
let cellPadding: number = 4;
if (cell.cid == 1){
let row = this.treegrid.findRowById(cell.rid);
let level: number = this.getRowLevel(row);
cellPadding = 23 + level*15;
}
cellWidth -= cellPadding;
return cellWidth;
}
}