import { Injectable } from '@angular/core' ; import { MatDialog , MatDialogRef } from '@angular/material/dialog' ; import { CreateFromTemplateDialogComponent } from '../dialogs/node-template/create-from-template.dialog' ; import { Subject , from , of } from 'rxjs' ; import { Node , MinimalNode , MinimalNodeEntryEntity , ResultNode , PathElement , SearchApi } from '@alfresco/js-api' ; import { AlfrescoApiService , TranslationService } from '@alfresco/adf-core' ; import { switchMap , catchError } from 'rxjs/operators' ; import { Store } from '@ngrx/store' ; import { AppStore , SnackbarErrorAction } from '@alfresco/aca-shared/store' ; import { ContentNodeSelectorComponent , ContentNodeSelectorComponentData , ShareDataRow , NodeAction } from '@alfresco/adf-content-services' ; export interface TemplateDialogConfig { primaryPathName : string ; selectionType : string ; } @ Injectable ( { providedIn : 'root' } ) export class NodeTemplateService { private currentTemplateConfig : TemplateDialogConfig = null ; private rootNode : ResultNode ; _searchApi : SearchApi ; get searchApi ( ) : SearchApi { this . _searchApi = this . _searchApi ?? new SearchApi ( this . alfrescoApiService . getInstance ( ) ) ; return this . _searchApi ; } constructor ( private store , private alfrescoApiService , private translation , public dialog ) { } selectTemplateDialog ( config ) { this . currentTemplateConfig = config ; const select = new Subject < Node [ ] > ( ) ; select . subscribe ( { complete : this . close . bind ( this ) } ) ; const data = { selectionMode : 'single' , title : this . title ( config . selectionType ) , actionName : NodeAction . NEXT , dropdownHideMyFiles : true , currentFolderId : null , dropdownSiteList : null , breadcrumbTransform : this . transformNode . bind ( this ) , select , showSearch : false , showDropdownSiteList : false , isSelectionValid : this . isSelectionValid . bind ( this ) , rowFilter : this . rowFilter . bind ( this ) } ; const query = { query : { query : `PATH:"//${ config . primaryPathName }"` , language : 'afts' } , include : [ 'path' , 'properties' , 'allowableOperations' , 'permissions' ] } ; from ( this . searchApi . search ( query ) ) . pipe ( switchMap ( ( response ) => { const entry = response . list . entries [ 0 ] . entry ; this . rootNode = entry ; data . currentFolderId = entry . id ; return this . dialog . open ( ContentNodeSelectorComponent , { data , panelClass : [ 'adf-content-node-selector-dialog' , 'aca-template-node-selector-dialog' ] , width : '630px' } ) . afterClosed ( ) ; } ) , catchError ( ( error ) => { this . store . dispatch ( new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.GENERIC' ) ) ; return of ( error ) ; } ) ) . subscribe ( { next : ( ) => select . complete ( ) } ) ; return select ; } createTemplateDialog ( node ) { return this . dialog . open ( CreateFromTemplateDialogComponent , { data : node , panelClass : 'aca-create-from-template-dialog' , width : '630px' } ) ; } private transformNode ( node ) { if ( node && node . path && node . path && node . path . elements instanceof Array ) { node . path . elements = this . getPathElements ( node ) ; } return node ; } private isSelectionValid ( node ) { if ( ! node . path . elements . length ) { return false ; } if ( this . currentTemplateConfig . selectionType === 'folder' ) { return node . isFolder ; } return node . isFile ; } private close ( ) { this . dialog . closeAll ( ) ; } private title ( selectionType ) { if ( selectionType === 'file' ) { return this . translation . instant ( 'NODE_SELECTOR.SELECT_FILE_TEMPLATE_TITLE' ) ; } return this . translation . instant ( 'NODE_SELECTOR.SELECT_FOLDER_TEMPLATE_TITLE' ) ; } private rowFilter ( row ) { const node = row . node . entry ; return node . nodeType !== 'app:filelink' && node . nodeType !== 'app:folderlink' ; } private getPathElements ( node ) { return node . path . elements . filter ( ( pathElement ) => ! this . rootNode . path . elements . some ( ( rootPathElement ) => pathElement . id === rootPathElement . id ) ) ; } }