import * as vscode from 'vscode' ; import { ExtensionComponent } from '../extensionComponent' ; import { BuildsNode } from '../treeDataProviders/dependenciesTree/ciNodes/buildsTree' ; import { DependenciesTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesTreeNode' ; import { TreesManager } from '../treeDataProviders/treesManager' ; import { LicensesFilter } from './licensesFilter' ; import { ScopesFilter } from './scopeFilter' ; import { SeverityFilter as SeveritiesFilter } from './severitiesFilter' ; enum FilterTypes { SEVERITY = '$(alert)   Issues severity' , LICENSE = '$(law)   Licenses' , SCOPE = '$(telescope)   Scope' , CLEAR_ALL = '$(trashcan)   Reset filters' } /**
 * Manage the filters of the components tree.
 */ export class FilterManager implements ExtensionComponent { private _severitiesFilter : SeveritiesFilter ; private _licensesFilter : LicensesFilter ; private _scopeFilter : ScopesFilter ; constructor ( private _treesManager ) { this . _severitiesFilter = new SeveritiesFilter ( _treesManager . scanCacheManager ) ; this . _licensesFilter = new LicensesFilter ( _treesManager ) ; this . _scopeFilter = new ScopesFilter ( _treesManager ) ; } public activate ( ) { return this ; } public async showFilterMenu ( ) { let choice = await vscode . window . showQuickPick ( this . getFilters ( ) , < vscode . QuickPickOptions > { placeHolder : 'Filter' , canPickMany : false } ) ; switch ( choice ) { case FilterTypes . SEVERITY : this . _severitiesFilter . showFilterMenu ( this ) ; break ; case FilterTypes . LICENSE : this . _licensesFilter . showFilterMenu ( this ) ; break ; case FilterTypes . SCOPE : this . _scopeFilter . showFilterMenu ( this ) ; break ; case FilterTypes . CLEAR_ALL : this . _severitiesFilter . clearFilters ( ) ; this . _licensesFilter . clearFilters ( ) ; this . _scopeFilter . clearFilters ( ) ; this . _treesManager . treeDataProviderManager . applyFilters ( undefined ) ; } } public getFilters ( ) { let results = [ ] ; Object . values ( FilterTypes ) . forEach ( filterType => { if ( filterType === FilterTypes . SCOPE && this . _scopeFilter . getValues ( ) . length === 0 ) { return ; } results . push ( filterType ) ; } ) ; return results ; } public applyFilters ( ) { let unfilteredRoot = this . _treesManager . treeDataProviderManager . getDisplayedDependenciesTree ( ) ; if ( ! ( unfilteredRoot instanceof DependenciesTreeNode ) ) { return ; } let filteredRoot = unfilteredRoot . shallowClone ( ) ; this . _applyFilters ( unfilteredRoot , filteredRoot , { nodeSelected : true } ) ; this . _treesManager . treeDataProviderManager . applyFilters ( filteredRoot ) ; } private _applyFilters ( unfilteredNode , filteredNode , picked ) { picked . nodeSelected = ( this . _severitiesFilter . isNodePicked ( unfilteredNode ) && this . _licensesFilter . isNodePicked ( unfilteredNode ) && this . _scopeFilter . isNodePicked ( unfilteredNode ) ) || unfilteredNode instanceof BuildsNode ; for ( let unfilteredChild of unfilteredNode . children ) { let filteredNodeChild = unfilteredChild . shallowClone ( ) ; let childSelected = { nodeSelected : false } ; this . _applyFilters ( unfilteredChild , filteredNodeChild , childSelected ) ; if ( childSelected . nodeSelected ) { picked . nodeSelected = true ; filteredNode . addChild ( filteredNodeChild ) ; filteredNodeChild . parent = filteredNode ; } } } }