import { Injectable } from '@angular/core' ; import { File , Directory , instanceOfDirectory , DirectoryClientState , LabDirectory , isLabDirectory } from '@machinelabs/models' ; @ Injectable ( ) export class FileTreeService { collapseDirectory ( fileOrDirectory ) { this . setCollapsed ( fileOrDirectory , true ) ; } expandDirectory ( fileOrDirectory ) { this . setCollapsed ( fileOrDirectory , false ) ; } collapseAll ( fileOrDirectory ) { if ( isLabDirectory ( fileOrDirectory ) ) { return fileOrDirectory . map ( child => this . collapseAll ( child ) ) ; } this . collapseDirectory ( fileOrDirectory ) ; if ( instanceOfDirectory ( fileOrDirectory ) && fileOrDirectory . contents ) { return fileOrDirectory . contents . map ( child => this . collapseAll ( child ) ) ; } } selectFile ( fileOrDirectory ) { this . setSelected ( fileOrDirectory , true ) ; } unselectFile ( fileOrDirectory ) { this . setSelected ( fileOrDirectory , false ) ; } private setCollapsed ( fileOrDirectory , value ) { if ( fileOrDirectory ) { fileOrDirectory . clientState = this . updateClientState ( fileOrDirectory . clientState , { collapsed : value } ) ; } } private setSelected ( fileOrDirectory , value ) { if ( fileOrDirectory ) { fileOrDirectory . clientState = this . updateClientState ( fileOrDirectory . clientState , { selected : value } ) ; } } private updateClientState ( clientState , newState ) { return { ... clientState , ... newState } ; } }