@lidorsystems/integralui-web
Version:
IntegralUI Web - Advanced UI Components for Angular
453 lines (393 loc) • 17.1 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, enableProdMode, ViewContainerRef, ViewChild, ViewChildren, ViewEncapsulation } from '@angular/core';
import { IntegralUICommonService } from '../../integralui/services/integralui.common.service';
import { IntegralUIGrid } from '../../integralui/components/integralui.grid';
enableProdMode();
export class GridAddRowDynamicallySample {
// Get a reference to the Grid component
grid: IntegralUIGrid;
// An Array object that holds all column objects shown in the Grid
public columns: Array<any>;
// An Array object that holds all row objects shown in the Grid
public rows: Array<any>;
// Edit variables
private currentEditRow: any = null;
public currentEditRowID: any = null;
public currentEditCell: any = null;
private isNewRow: boolean = false;
public gridStyle: any = {
general: {
normal: 'grid-ardyn-normal'
}
}
constructor(private commonService?: IntegralUICommonService){
this.columns = [
{ id: 2, headerText: "Title", width: 290},
{ id: 3, headerText: "Year", headerAlignment: "center", contentAlignment: "center", width: 70 },
{ id: 4, headerText: "Genre", headerAlignment: "center", contentAlignment: "center", width: 150 },
{ id: 6, headerText: "Released", headerAlignment: "center", contentAlignment: "center", width: 120 },
{ id: 7, contentAlignment: "center", width: 100, fixedWidth: true }
];
this.rows = [
{
id: 1,
text: "Gravity",
cells: [{ cid: 2, text: "Gravity" }, { cid: 3, text: "2013" }, { cid: 4, text: "Sci-Fi" }, { cid: 6, text: "04 Oct 2013" }, { cid: 7 }]
},
{
id: 2,
text: "Prometheus",
cells: [{ cid: 2, text: "Prometheus" }, { cid: 3, text: "2012" }, { cid: 4, text: "Sci-Fi" }, { cid: 6, text: "08 Jun 2012" }, { cid: 7 }]
},
{
id: 3,
text: "The Dark Knight Rises",
cells: [{ cid: 2, text: "The Dark Knight Rises" }, { cid: 3, text: "2012" }, { cid: 4, text: "Action" }, { cid: 6, text: "20 Jul 2012" }, { cid: 7 }]
},
];
}
// Prevent dblclick and mousedown events to bubble up from clicks on active buttons
// This stops the expand.collapse of rows when doubleclicked
onButtonsDblClick(e: any){
e.stopPropagation();
}
onButtonsMouseDown(e: any){
e.stopPropagation();
}
// Returns the level of the row in tree hierearchy
getRowLevel(row: any){
let level: number = 0;
let parent: any = this.grid.getRowParent(row);
while (parent){
level++;
parent = this.grid.getRowParent(parent);
}
return level;
}
// Calculates the width of grid 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 == 2){
let row = this.grid.findRowById(cell.rid);
let level: number = this.getRowLevel(row);
cellPadding = 23 + level*15;
}
cellWidth -= cellPadding;
return cellWidth;
}
// 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);
}
}
// Creates a new row object
createNewRow(){
let row: any = {
id: this.commonService.getUniqueId(),
cells: [
{ cid: 2, text: "", saved: false },
{ cid: 3, text: "", saved: false },
{ cid: 4, text: "", saved: false },
{ cid: 6, text: "", saved: false },
{ cid: 7, saved: false }
]
}
for (let j = 0; j < row.cells.length; j++)
row.cells[j].rid = row.id;
this.isNewRow = true;
return row;
}
// Adds a new row as root
addRow(){
if (this.currentEditRow)
this.cancelEdit(this.currentEditRow.id);
this.insertRowInGrid();
}
// Inserts the created row at specific position and updates the grid layout
insertRowInGrid(selRow?: any){
let row: any = this.createNewRow();
this.currentEditRow = row;
this.currentEditRowID = row.id;
this.currentEditCell = row.cells[0];
this.grid.insertRowAt(row, 0, selRow);
this.grid.updateLayout();
}
// Confirms the changes and saves the row
saveKeyDown(e: any, id: any){
if (e.keyCode === 13) // ENTER
this.saveRow(id);
}
saveRow(id: any){
let row = this.grid.findRowById(id);
if (row){
this.updateEditStatus(row, true);
for (let j = 0; j < row.cells.length; j++)
row.cells[j].text = row.cells[j].editText;
if (this.isNewRow)
this.moveRowToEnd(row);
}
this.resetEditor();
this.isNewRow = false;
}
// Cancels the edit process and closes the editor
cancelKeyDown(e: any, id: any){
if (e.keyCode === 13) // ENTER
this.cancelEdit(id);
}
cancelEdit(id: any){
if (this.isNewRow)
this.removeRow(id);
this.resetEditor();
this.isNewRow = false;
}
// Sets the row in edit mode and opens the editor
editRow(id: any){
this.resetEditor();
let row = this.grid.findRowById(id);
if (row){
this.updateEditStatus(row);
this.currentEditRow = row;
this.currentEditRowID = row.id;
this.currentEditCell = row.cells[0];
}
}
// Cancels the edit process when ESCAPE key is pressed
editorKeyDown(e: any){
if (this.currentEditRow){
switch (e.keyCode){
case 27: // ESCAPE
this.cancelEdit(this.currentEditRow.id);
break;
}
}
}
editorMouseDown(e: any, cell: any){
this.currentEditCell = cell;
e.stopPropagation();
}
editorTouch(e: any, cell: any){
this.currentEditCell = cell;
e.stopPropagation();
}
// Removes a row from the grid
removeRow(id: any){
let row = this.grid.findRowById(id);
if (row){
this.grid.removeRow(row);
this.grid.updateLayout();
}
}
// Resets the variables for editing
resetEditor(){
this.updateEditStatus(this.currentEditRow, true);
this.currentEditRow = null;
this.currentEditRowID = null;
this.currentEditCell = null;
}
// Updates the status of row, for edit or normal mode
updateEditStatus(row: any, flag?: boolean){
let status: boolean = flag ? true : false;
if (row)
for (let j = 0; j < row.cells.length; j++){
row.cells[j].saved = status;
// If row is in edit mode, copy the text value of grid cell to the cell editor
if (!status)
row.cells[j].editText = row.cells[j].text;
}
}
// Moves a row at end of the list
moveRowToEnd(row: any){
if (row){
let list: Array<any> = this.rows;
if (list){
list.splice(list.length-1, 0, list.splice(0, 1)[0]);
this.grid.updateLayout();
}
}
}
}