2018-12-03 12:33:03 +01:00
import { Component , ViewChild , OnInit , ElementRef } from '@angular/core' ;
2019-03-07 16:43:10 +01:00
import { ActivatedRoute , Router } from '@angular/router' ;
import { HelpContentService } from '../../services/help-content.service' ;
import { FormGroup } from '@angular/forms' ;
import { EntityFormComponent } from './entity-form.component' ;
import { CheckEntity , Entity } from '../../domain/entity' ;
import { Community } from '../../domain/community' ;
2018-10-30 16:31:16 +01:00
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties' ;
import { Session } from '../../openaireLibrary/login/utils/helper.class' ;
2018-11-13 16:26:49 +01:00
import { LoginErrorCodes } from '../../openaireLibrary/login/utils/guardHelper.class' ;
2019-04-24 13:44:46 +02:00
import { HelperFunctions } from "../../openaireLibrary/utils/HelperFunctions.class" ;
2018-10-30 16:31:16 +01:00
@Component ( {
selector : 'entities' ,
templateUrl : './entities.component.html' ,
} )
export class EntitiesComponent implements OnInit {
// @ViewChild(ModalFormComponent)
// @ViewChild('saveModal')
// public modal:ModalFormComponent;
//
// @ViewChild('updateModal')
// public updateModal:ModalFormComponent;
@ViewChild ( 'AlertModalSaveEntity' ) alertModalSaveEntity ;
@ViewChild ( 'AlertModalUpdateEntity' ) alertModalUpdateEntity ;
@ViewChild ( 'AlertModalDeleteEntities' ) alertModalDeleteEntities ;
private selectedEntities : string [ ] = [ ] ;
@ViewChild ( EntityFormComponent )
2019-03-07 16:43:10 +01:00
public formComponent : EntityFormComponent ;
2018-10-30 16:31:16 +01:00
2019-03-07 16:43:10 +01:00
public checkboxes : CheckEntity [ ] = [ ] ;
2018-10-30 16:31:16 +01:00
2019-03-07 16:43:10 +01:00
public entities : Entity [ ] = [ ] ;
2018-10-30 16:31:16 +01:00
2019-03-07 16:43:10 +01:00
public formGroup : FormGroup ;
2018-10-30 16:31:16 +01:00
2019-03-07 16:43:10 +01:00
private searchText : RegExp = new RegExp ( '' ) ;
public keyword = '' ;
2018-10-30 16:31:16 +01:00
public communities : Community [ ] = [ ] ;
public selectedCommunityPid : string ;
@ViewChild ( 'AlertModalRelatedPages' ) alertModalRelatedPages ;
public toggleIds : string [ ] ;
public toggleStatus : boolean ;
2019-03-07 16:43:10 +01:00
public properties : EnvProperties = null ;
2018-10-30 16:31:16 +01:00
2019-03-07 16:43:10 +01:00
public showLoading = true ;
public errorMessage = '' ;
public updateErrorMessage = '' ;
public modalErrorMessage = '' ;
2018-10-30 16:31:16 +01:00
public isPortalAdministrator = null ;
2019-03-07 16:43:10 +01:00
2018-10-30 16:31:16 +01:00
ngOnInit() {
this . formGroup = this . formComponent . form ;
this . route . data
. subscribe ( ( data : { envSpecific : EnvProperties } ) = > {
this . properties = data . envSpecific ;
this . route . queryParams . subscribe ( params = > {
2019-04-24 13:44:46 +02:00
HelperFunctions . scroll ( ) ;
2018-12-03 12:33:03 +01:00
2018-10-30 16:31:16 +01:00
this . selectedCommunityPid = params [ 'communityId' ] ;
this . applyCommunityFilter ( this . selectedCommunityPid ) ;
this . isPortalAdministrator = Session . isPortalAdministrator ( ) ;
} ) ;
} ) ;
}
2019-03-07 16:43:10 +01:00
constructor ( private element : ElementRef , private route : ActivatedRoute ,
private _router : Router ,
private _helpContentService : HelpContentService ) { }
2018-10-30 16:31:16 +01:00
getEntities ( community_pid : string ) {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-11-13 16:26:49 +01:00
} else {
2018-10-30 16:31:16 +01:00
this . showLoading = true ;
2019-03-07 16:43:10 +01:00
this . updateErrorMessage = '' ;
this . errorMessage = '' ;
2018-10-30 16:31:16 +01:00
this . _helpContentService . getCommunityEntities ( community_pid , this . properties . adminToolsAPIURL ) . subscribe (
entities = > {
this . entities = entities ;
this . checkboxes = [ ] ;
let self = this ;
entities . forEach ( _ = > {
self . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } ) ;
} ) ;
this . showLoading = false ;
} ,
error = > this . handleError ( 'System error retrieving entities' , error ) ) ;
2018-11-13 16:26:49 +01:00
}
2018-10-30 16:31:16 +01:00
}
public toggleCheckBoxes ( event ) {
this . checkboxes . forEach ( _ = > _ . checked = event . target . checked ) ;
}
2019-03-07 16:43:10 +01:00
public applyCheck ( flag : boolean ) {
2018-10-30 16:31:16 +01:00
this . checkboxes . forEach ( _ = > _ . checked = flag ) ;
}
2019-03-07 16:43:10 +01:00
public getSelectedEntities ( ) : string [ ] {
return this . checkboxes . filter ( entity = > entity . checked === true ) . map ( checkedEntity = > checkedEntity . entity ) . map ( res = > res . _id ) ;
2018-10-30 16:31:16 +01:00
}
2019-03-07 16:43:10 +01:00
private deleteEntitiesFromArray ( ids : string [ ] ) : void {
for ( let id of ids ) {
const i = this . checkboxes . findIndex ( _ = > _ . entity . _id === id ) ;
2018-10-30 16:31:16 +01:00
this . checkboxes . splice ( i , 1 ) ;
}
}
2019-03-07 16:43:10 +01:00
public confirmDeleteEntity ( id : string ) {
// this.deleteConfirmationModal.ids = [id];
// this.deleteConfirmationModal.showModal();
2018-10-30 16:31:16 +01:00
this . selectedEntities = [ id ] ;
this . confirmDeleteEntitiesModalOpen ( ) ;
}
public confirmDeleteSelectedEntities() {
2019-03-07 16:43:10 +01:00
// this.deleteConfirmationModal.ids = this.getSelectedEntities();
// this.deleteConfirmationModal.showModal();
2018-10-30 16:31:16 +01:00
this . selectedEntities = this . getSelectedEntities ( ) ;
this . confirmDeleteEntitiesModalOpen ( ) ;
}
private confirmDeleteEntitiesModalOpen() {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-11-13 16:26:49 +01:00
} else {
this . alertModalDeleteEntities . cancelButton = true ;
this . alertModalDeleteEntities . okButton = true ;
2019-03-07 16:43:10 +01:00
this . alertModalDeleteEntities . alertTitle = 'Delete Confirmation' ;
this . alertModalDeleteEntities . message = 'Are you sure you want to delete the selected entity(-ies)?' ;
this . alertModalDeleteEntities . okButtonText = 'Yes' ;
2018-11-13 16:26:49 +01:00
this . alertModalDeleteEntities . open ( ) ;
}
2018-10-30 16:31:16 +01:00
}
public confirmedDeleteEntities ( data : any ) {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-11-13 16:26:49 +01:00
} else {
2018-10-30 16:31:16 +01:00
this . showLoading = true ;
2019-03-07 16:43:10 +01:00
this . updateErrorMessage = '' ;
2018-10-30 16:31:16 +01:00
this . _helpContentService . deleteEntities ( this . selectedEntities , this . properties . adminToolsAPIURL ) . subscribe (
_ = > {
this . deleteEntitiesFromArray ( this . selectedEntities ) ;
this . showLoading = false ;
} ,
error = > this . handleUpdateError ( 'System error deleting the selected entities' , error )
) ;
2018-11-13 16:26:49 +01:00
}
2018-10-30 16:31:16 +01:00
}
2019-03-07 16:43:10 +01:00
public editEntity ( i : number ) {
const entity : Entity = this . checkboxes [ i ] . entity ;
2018-10-30 16:31:16 +01:00
this . formGroup . patchValue ( entity ) ;
2019-03-07 16:43:10 +01:00
// this.updateModal.showModal();
this . modalErrorMessage = '' ;
this . entitiesModalOpen ( this . alertModalUpdateEntity , 'Update' , 'Update Entity' ) ;
2018-10-30 16:31:16 +01:00
}
public newEntity() {
this . formComponent . reset ( ) ;
2019-03-07 16:43:10 +01:00
this . modalErrorMessage = '' ;
this . entitiesModalOpen ( this . alertModalSaveEntity , 'Save' , 'Add a new Entity' ) ;
2018-10-30 16:31:16 +01:00
}
private entitiesModalOpen ( modal : any , title : string , yesBtn : string ) {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-11-13 16:26:49 +01:00
} else {
modal . cancelButton = true ;
modal . okButton = true ;
modal . alertTitle = title ;
modal . okButtonText = yesBtn ;
modal . open ( ) ;
}
2018-10-30 16:31:16 +01:00
}
public entitySaveConfirmed ( data : any ) {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-10-30 16:31:16 +01:00
} else {
2019-03-07 16:43:10 +01:00
if ( ! this . formGroup . valid ) {
this . entitiesModalOpen ( this . alertModalSaveEntity , 'Save' , 'Add a new Entity' ) ;
this . modalErrorMessage = 'Please fill in all required fields marked with *' ;
2018-11-13 16:26:49 +01:00
} else {
2019-03-07 16:43:10 +01:00
this . modalErrorMessage = '' ;
2018-11-13 16:26:49 +01:00
this . _helpContentService . saveEntity ( < Entity > this . formGroup . value , this . properties . adminToolsAPIURL ) . subscribe (
entity = > {
this . entitySavedSuccessfully ( entity ) ;
} ,
2019-03-07 16:43:10 +01:00
error = > this . handleUpdateError ( 'System error creating entity' , error )
2018-11-13 16:26:49 +01:00
) ;
}
2018-10-30 16:31:16 +01:00
}
}
public entityUpdateConfirmed ( data : any ) {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-10-30 16:31:16 +01:00
} else {
2018-11-13 16:26:49 +01:00
if ( ! this . formGroup . valid ) {
2019-03-07 16:43:10 +01:00
this . entitiesModalOpen ( this . alertModalUpdateEntity , 'Update' , 'Update Entity' ) ;
this . modalErrorMessage = 'Please fill in all required fields marked with *' ;
2018-11-13 16:26:49 +01:00
} else {
2019-03-07 16:43:10 +01:00
this . _helpContentService . updateEntity (
< Entity > this . formGroup . value , this . properties . adminToolsAPIURL ) . subscribe (
2018-11-13 16:26:49 +01:00
entity = > {
this . entityUpdatedSuccessfully ( entity ) ;
} ,
2019-03-07 16:43:10 +01:00
error = > this . handleUpdateError ( 'System error updating entity' , error )
2018-11-13 16:26:49 +01:00
) ;
}
2018-10-30 16:31:16 +01:00
}
}
public entitySavedSuccessfully ( entity : Entity ) {
this . checkboxes . push ( < CheckEntity > { entity : entity , checked : false } ) ;
this . applyCheck ( false ) ;
}
2019-03-07 16:43:10 +01:00
public entityUpdatedSuccessfully ( entity : Entity ) {
this . checkboxes . find ( checkItem = > checkItem . entity . _id === entity . _id ) . entity = entity ;
2018-10-30 16:31:16 +01:00
this . applyCheck ( false ) ;
}
2019-03-07 16:43:10 +01:00
public filterBySearch ( text : string ) {
this . searchText = new RegExp ( text , 'i' ) ;
2018-10-30 16:31:16 +01:00
this . applyFilter ( ) ;
}
public applyFilter() {
this . checkboxes = [ ] ;
this . entities . filter ( item = > this . filterEntities ( item ) ) . forEach (
_ = > this . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } )
) ;
}
2019-03-07 16:43:10 +01:00
public filterEntities ( entity : Entity ) : boolean {
const textFlag = this . searchText . toString ( ) === '' || ( entity . name ) . match ( this . searchText ) != null ;
2018-10-30 16:31:16 +01:00
return textFlag ;
}
handleError ( message : string , error ) {
this . errorMessage = message ;
console . log ( 'Server responded: ' + error ) ;
this . showLoading = false ;
}
handleUpdateError ( message : string , error ) {
2019-03-07 16:43:10 +01:00
if ( error == null ) {
2018-10-30 16:31:16 +01:00
this . formComponent . reset ( ) ;
} else {
this . updateErrorMessage = message ;
2019-03-07 16:43:10 +01:00
console . log ( 'Server responded: ' + error ) ;
2018-10-30 16:31:16 +01:00
}
this . showLoading = false ;
}
public applyCommunityFilter ( community_pid : string ) {
this . getEntities ( community_pid ) ;
}
2019-03-07 16:43:10 +01:00
public toggleEntities ( status : boolean , ids : string [ ] ) {
// this.okModal.showModal();
2018-10-30 16:31:16 +01:00
this . toggleIds = ids ;
this . toggleStatus = status ;
this . confirmRelatedPagesModalOpen ( ) ;
}
private confirmRelatedPagesModalOpen() {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-11-13 16:26:49 +01:00
} else {
this . alertModalRelatedPages . cancelButton = true ;
this . alertModalRelatedPages . okButton = true ;
2019-03-07 16:43:10 +01:00
this . alertModalRelatedPages . alertTitle = 'Warning' ;
2018-11-13 16:26:49 +01:00
this . alertModalRelatedPages . message = "This action will affect all search pages related to this entity! Pages' status will change to entity's status! Do you want to continue?" ;
2019-03-07 16:43:10 +01:00
this . alertModalRelatedPages . okButtonText = 'Yes' ;
2018-11-13 16:26:49 +01:00
this . alertModalRelatedPages . open ( ) ;
}
2018-10-30 16:31:16 +01:00
}
public continueToggling ( event : any ) {
2019-03-07 16:43:10 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
2018-11-13 16:26:49 +01:00
} else {
2019-03-07 16:43:10 +01:00
this . updateErrorMessage = '' ;
this . _helpContentService . toggleEntities (
this . selectedCommunityPid , this . toggleIds , this . toggleStatus , this . properties . adminToolsAPIURL ) . subscribe (
2018-11-13 16:26:49 +01:00
( ) = > {
2019-03-07 16:43:10 +01:00
for ( let id of this . toggleIds ) {
const i = this . checkboxes . findIndex ( _ = > _ . entity . _id === id ) ;
this . checkboxes [ i ] . entity . isEnabled = this . toggleStatus ;
2018-11-13 16:26:49 +01:00
}
this . applyCheck ( false ) ;
} ,
error = > this . handleUpdateError ( 'System error changing the status of the selected entity(-ies)' , error )
) ;
}
2018-10-30 16:31:16 +01:00
}
}