/** * created by Menna & Yara */ import { ElementRef, Input, Component, ViewChild, OnInit } from '@angular/core'; import { WindowComponent } from '../kendo-widgets/window/window.component'; import { GeneralGridComponent } from '../general-grid-component/general-grid-component'; import { LookupService } from '../services/lookup-service'; import { Response } from '@angular/http'; import { LookupHomeScreenOptions } from '../types/home-screen-types'; import { GeneralGridOptions } from '../types/grid-types'; @Component({ selector: 'lookup-grid', templateUrl: './lookup-grid-component.html', styleUrls: ['./lookup-grid-component.css'], }) export class LookupGridComponent implements OnInit { @ViewChild('editPopup') private kendoWindowLookupGrid: WindowComponent; @ViewChild('generalGrid') private generalGridComponent: GeneralGridComponent; private _customGridOptions: LookupHomeScreenOptions; private homeGridOptions: GeneralGridOptions; private emptyUpdatingModel = {}; private lookupGridService: LookupService; private editTokenId = ''; private idGlobalForRequest; editPopupWindowOptions = { visible: false, modal: true, width: '40%', title: false, }; get customGridOptions(): LookupHomeScreenOptions { return this._customGridOptions; } set customGridOptions(customGridOptions: LookupHomeScreenOptions) { this._customGridOptions = customGridOptions; } constructor(private lookupService: LookupService) { this.lookupGridService = lookupService; } ngOnInit() { this.homeGridOptions = { dataSource: this.customGridOptions.dataSourceOptions, detailTemplate: this.customGridOptions.detailTemplate, detailInit: this.customGridOptions.detailInit, objectRemoteRestUrl: this.customGridOptions.objectRemoteRestUrl, columns: [{ titleKey: 'code_label', field: 'code', type: 'number', filterable: { cell: { showOperators: false } }, attributes: { style: "text-align: center;" }, headerAttributes: { style: "text-align: center;" } }, { titleKey: 'name_label', field: 'name', type: 'string', filterable: { cell: { operator: "contains" } }, attributes: { style: "text-align: center;" }, headerAttributes: { style: "text-align: center;" } } ].concat(this.customGridOptions.gridColumns), onDataBound: (e) => { $(".update-instance-btn").unbind("click"); let myGrid = $('.k-grid').data('kendoGrid'); var data = myGrid.dataSource.data(), rowData; for (var _i = 0; _i < data.length; _i++) { rowData = data[_i]; var dataItem = myGrid.dataSource.get(rowData.id); if (rowData.actionFlag == true) { var cumulativeErrors = ''; var responseErrors = JSON.stringify(rowData.responseErrors); var responseErrorsJsonObjectMap = JSON.parse(responseErrors); if (responseErrorsJsonObjectMap) { for (let key in responseErrorsJsonObjectMap) { cumulativeErrors = cumulativeErrors + responseErrorsJsonObjectMap[key].message + "\n"; } } var responseGeneralErrors = JSON.stringify(rowData.responseGeneralErrors); var responseGeneralErrorsJsonObjectMap = JSON.parse(responseGeneralErrors); for (let key in responseGeneralErrorsJsonObjectMap) { cumulativeErrors = cumulativeErrors + responseGeneralErrorsJsonObjectMap[key].message + "\n"; } var actionSignsId = "#actionsSigns" + dataItem.uid; var actionsSigns = myGrid.tbody.find("tr[data-uid='" + dataItem.uid + "'] span" + actionSignsId); if (cumulativeErrors != '') { actionsSigns.addClass("fa fa-times"); var toolTipFailureResponse = actionsSigns.find("div ul.uk-nav.uk-nav-dropdown li"); toolTipFailureResponse[0].innerHTML = cumulativeErrors; } } } }, commandsBeforeDelete: [ { name: 'editItem', text: '', iconClass: 'k-icon k-i-pencil', className: 'grid-small-btn', click: (e) => { e.preventDefault(); let myGrid = $('.k-grid').data('kendoGrid'); var currentRow = myGrid.dataItem($(e.target).closest("tr")), id = currentRow.get('id'); var actionSignsId = "#actionsSigns" + currentRow.uid; var actionsSigns = myGrid.tbody.find("tr[data-uid='" + currentRow.uid + "'] span" + actionSignsId); actionsSigns.removeClass("fa fa-check"); actionsSigns.removeClass("fa fa-times"); this.lookupService.editEntity(this.customGridOptions.objectRemoteRestUrl, id, (response) => { $.extend(true, this.customGridOptions.editingModel, currentRow); this.editTokenId = response.tokenId; this.idGlobalForRequest = id; this.kendoWindowLookupGrid.ProtoType.open(); this.kendoWindowLookupGrid.ProtoType.center(); }, (error) => { actionsSigns.addClass("fa fa-times"); var responseBody = error, responseErrors; if (responseBody.details && responseBody.details.localizedMessage) { responseErrors = responseBody.details.localizedMessage; } else if (responseBody.data && responseBody.data.indexOf("userMessage") !== -1) { responseErrors = responseBody.data.substring(responseBody.data.indexOf("userMessage") + 15, responseBody.data.length - 3); } currentRow.set("responseGeneralErrors", [{ 'message': responseErrors }]); currentRow.set("actionFlag", true); } ); } }] }; $('.editPopupBody').html(this.customGridOptions.editPopupWindow.popupTplRef.nativeElement); } getSelectedRows(): any[] { return this.generalGridComponent.selectedItems; } update(e) { if (this.editTokenId != '') { this.lookupService.updateLookupInstance(this.customGridOptions.objectRemoteRestUrl, this.idGlobalForRequest, this.customGridOptions.editingModel, this.editTokenId, (responseUpdate) => { // success this.buildGridDataSource(); this.editTokenId = ''; this.kendoWindowLookupGrid.ProtoType.close(); }, (responseUpdate) => { // failure }); } } cancel(e) { if (this.editTokenId != '') { this.lookupService.cancelEditEntity(this.customGridOptions.objectRemoteRestUrl, this.idGlobalForRequest, this.editTokenId, (Response) => { this.editTokenId = ''; this.kendoWindowLookupGrid.ProtoType.close(); }); } } buildGridDataSource() { let myGrid = $('.k-grid').data('kendoGrid'); this.lookupService.readAll(this.customGridOptions.objectRemoteRestUrl, (response) => { myGrid.dataSource.data(myGrid.dataSource.options.schema.parse(response.data)); }); } }