Skip to content

Commit

Permalink
DSP-1132 Advanced search limit to project (#238)
Browse files Browse the repository at this point in the history
* feat(search): Add limit to project to advanced search

* refactor(search): Use limitToProject in fulltext and panel and emit to advanced

* fix(search): Bug fix

* test(search): Bug fix in tests

* docs(search): Update jsDocs

* chore(playground): Update paramater in search playground
  • Loading branch information
André Kilchenmann committed Dec 3, 2020
1 parent e9b6b1d commit 56949b1
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 32 deletions.
Expand Up @@ -2,6 +2,7 @@ import {
Component,
EventEmitter,
Inject,
Input,
OnDestroy,
OnInit,
Output,
Expand Down Expand Up @@ -43,6 +44,13 @@ const typeGuard = <T>(o: any, className: Constructor<T>): o is T => {
})
export class AdvancedSearchComponent implements OnInit, OnDestroy {

/**
* Filter ontologies by specified project IRI
*
* @param limitToProject
*/
@Input() limitToProject?: string;

/**
* The data event emitter of type SearchParams
*
Expand Down Expand Up @@ -94,7 +102,6 @@ export class AdvancedSearchComponent implements OnInit, OnDestroy {
this.formChangesSubscription = this.form.statusChanges.subscribe((data) => {
this.formValid = this._validateForm();
});

// initialize ontologies to be used for the ontologies selection in the search form
this.initializeOntologies();
}
Expand Down Expand Up @@ -129,17 +136,34 @@ export class AdvancedSearchComponent implements OnInit, OnDestroy {
* @returns void
*/
initializeOntologies(): void {
this._dspApiConnection.v2.onto.getOntologiesMetadata().subscribe(
(response: OntologiesMetadata) => {
// filter out system ontologies
response.ontologies = response.ontologies.filter(onto => onto.attachedToProject !== Constants.SystemProjectIRI);

this.ontologiesMetadata = response;
},
(error: ApiResponseError) => {
this._notification.openSnackBar(error);
this.errorMessage = error;
});
if (this.limitToProject) {
this._dspApiConnection.v2.onto.getOntologiesByProjectIri(this.limitToProject).subscribe(
(response: OntologiesMetadata) => {
// filter out system ontologies
response.ontologies = response.ontologies.filter(onto => onto.attachedToProject !== Constants.SystemProjectIRI);

this.ontologiesMetadata = response;
},
(error: ApiResponseError) => {
this._notification.openSnackBar(error);
this.errorMessage = error;
});
} else {
this._dspApiConnection.v2.onto.getOntologiesMetadata().subscribe(
(response: OntologiesMetadata) => {
// filter out system ontologies
response.ontologies = response.ontologies.filter(onto => onto.attachedToProject !== Constants.SystemProjectIRI);

this.ontologiesMetadata = response;
},
(error: ApiResponseError) => {
this._notification.openSnackBar(error);
this.errorMessage = error;
});
}


}

/**
Expand Down
Expand Up @@ -25,7 +25,7 @@ import { FulltextSearchComponent } from './fulltext-search.component';
template: `
<dsp-fulltext-search #fulltextSearch
[projectfilter]="projectfilter"
[filterbyproject]="filterbyproject">
[limitToProject]="limitToProject">
</dsp-fulltext-search>
`
})
Expand All @@ -36,7 +36,8 @@ class TestHostFulltextSearchComponent implements OnInit {
sortingService: SortingService = new SortingService();

projectfilter?: boolean = true;
filterbyproject?: string;

limitToProject?: string;

ngOnInit() {
}
Expand Down
Expand Up @@ -6,6 +6,7 @@ import {
EventEmitter,
Inject,
Input,
OnChanges,
OnInit,
Output,
TemplateRef,
Expand Down Expand Up @@ -33,12 +34,14 @@ export interface PrevSearchItem {
query: string;
}

const resolvedPromise = Promise.resolve(null);

@Component({
selector: 'dsp-fulltext-search',
templateUrl: './fulltext-search.component.html',
styleUrls: ['./fulltext-search.component.scss']
})
export class FulltextSearchComponent implements OnInit {
export class FulltextSearchComponent implements OnInit, OnChanges {

/**
*
Expand All @@ -48,20 +51,33 @@ export class FulltextSearchComponent implements OnInit {
@Input() projectfilter?: boolean = false;

/**
* @deprecated Use `limitToProject` instead
*
* @param [filterbyproject] If the full-text search should be
* filtered by one project, you can define it with project iri.
*/
@Input() filterbyproject?: string;

/**
* Filter ontologies in advanced search or query in fulltext search by specified project IRI
*
* @param limitToProject
*/
@Input() limitToProject?: string;


/**
* Emits selected project in case of projectfilter
*/
@Output() limitToProjectChange = new EventEmitter<string>();

/**
* The data event emitter of type SearchParams
*
* @param search
*/
@Output() search = new EventEmitter<SearchParams>();


@ViewChild('fulltextSearchPanel', { static: false }) searchPanel: ElementRef;

@ViewChild('fulltextSearchInput', { static: false }) searchInput: ElementRef;
Expand All @@ -80,7 +96,7 @@ export class FulltextSearchComponent implements OnInit {
// list of projects, in case of filterproject is true
projects: ReadProject[];

// selected project, in case of filterbyproject and/or projectfilter is true
// selected project, in case of limitToProject and/or projectfilter is true
project: ReadProject;

defaultProjectLabel = 'All projects';
Expand Down Expand Up @@ -116,6 +132,10 @@ export class FulltextSearchComponent implements OnInit {
) { }

ngOnInit(): void {
// filterbyproject is set as deprecated. To avoid breaking changes we still support it
if (this.filterbyproject) {
this.limitToProject = this.filterbyproject;
}

// initialise prevSearch
const prevSearchOption = JSON.parse(localStorage.getItem('prevSearch'));
Expand All @@ -125,21 +145,29 @@ export class FulltextSearchComponent implements OnInit {
this.prevSearch = [];
}

if (this.filterbyproject) {
this.getProject(this.filterbyproject);
if (this.limitToProject) {
this.getProject(this.limitToProject);
}

if (this.projectfilter) {
this.getAllProjects();

if (localStorage.getItem('currentProject') !== null) {
this.setProject(
JSON.parse(localStorage.getItem('currentProject'))
);
}
}
}

ngOnChanges() {
// resource classes have been reinitialized
// reset form
resolvedPromise.then(() => {

if (localStorage.getItem('currentProject') !== null) {
this.setProject(
JSON.parse(localStorage.getItem('currentProject'))
);
}

});
}

/**
* Get all public projects from DSP-API
*/
Expand Down Expand Up @@ -186,11 +214,15 @@ export class FulltextSearchComponent implements OnInit {
// set default project: all
this.projectLabel = this.defaultProjectLabel;
this.projectIri = undefined;
this.limitToProject = undefined;
this.limitToProjectChange.emit(this.limitToProject);
localStorage.removeItem('currentProject');
} else {
// set current project shortname and id
this.projectLabel = project.shortname;
this.projectIri = project.id;
this.limitToProject = project.id;
this.limitToProjectChange.emit(this.limitToProject);
localStorage.setItem('currentProject', JSON.stringify(project));
}
}
Expand Down
@@ -1,6 +1,8 @@
<div class="dsp-search-panel" #fullSearchPanel cdkOverlayOrigin>

<dsp-fulltext-search [projectfilter]="projectfilter" [filterbyproject]="filterbyproject"
<dsp-fulltext-search
[projectfilter]="projectfilter"
[(limitToProject)]="limitToProject"
(search)="emitSearch($event)">
</dsp-fulltext-search>

Expand Down Expand Up @@ -33,7 +35,7 @@ <h4 *ngIf="!showAdvanced">Expert search</h4>
</span>
</div>
<div class="dsp-menu-content">
<dsp-advanced-search *ngIf="showAdvanced" (search)="emitSearch($event)"></dsp-advanced-search>
<dsp-advanced-search *ngIf="showAdvanced" [limitToProject]="limitToProject" (search)="emitSearch($event)"></dsp-advanced-search>
<dsp-expert-search *ngIf="!showAdvanced" (search)="emitSearch($event)"></dsp-expert-search>
</div>
</div>
Expand Down
Expand Up @@ -13,7 +13,7 @@ import { SearchPanelComponent } from './search-panel.component';
class TestFulltextSearchComponent implements OnInit {

@Input() projectfilter?: boolean = false;
@Input() filterbyproject?: string;
@Input() limitToProject?: string;
@Input() show: boolean;
@Output() showState = new EventEmitter();

Expand Down
@@ -1,33 +1,54 @@
import { ConnectionPositionPair, Overlay, OverlayConfig, OverlayRef, PositionStrategy } from '@angular/cdk/overlay';
import { TemplatePortal } from '@angular/cdk/portal';
import { Component, ElementRef, EventEmitter, Input, Output, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core';
import {
Component,
ElementRef,
EventEmitter,
Input, OnInit,
Output,
TemplateRef,
ViewChild,
ViewContainerRef
} from '@angular/core';
import { SearchParams } from '../../viewer/views/list-view/list-view.component';

@Component({
selector: 'dsp-search-panel',
templateUrl: './search-panel.component.html',
styleUrls: ['./search-panel.component.scss']
})
export class SearchPanelComponent {
export class SearchPanelComponent implements OnInit {

/**
* @param [projectfilter] If true it shows the selection of projects to filter by one of them
* Default value: false
*/
@Input() projectfilter?: boolean = false;

/**
* @deprecated Use `limitToProject` instead
*
* @param [filterbyproject] If your full-text search should be filtered by one project, you can define it with project
* iri in the parameter filterbyproject.
*/
@Input() filterbyproject?: string;

/**
* Filter ontologies in advanced search or query in fulltext search by specified project IRI
*
* @param limitToProject
*/
@Input() limitToProject?: string;

/**
* @param [advanced] Adds the extended / advanced search to the panel
* Default value: false
*/
@Input() advanced?: boolean = false;

/**
* @param [expert] Adds the expert search / gravsearch editor to the panel
* Default value: false
*/
@Input() expert?: boolean = false;

Expand All @@ -54,6 +75,13 @@ export class SearchPanelComponent {
private _viewContainerRef: ViewContainerRef
) { }

ngOnInit() {
// filterbyproject is set as deprecated. To avoid breaking changes we still support the parameter
if (this.filterbyproject) {
this.limitToProject = this.filterbyproject;
}
}

openPanelWithBackdrop(type: string) {

this.showAdvanced = (type === 'advanced');
Expand Down Expand Up @@ -87,6 +115,10 @@ export class SearchPanelComponent {
return overlayPosition;
}

updateLimitToProject(id: string) {
this.limitToProject = id;
}

/**
* Emit the search parameters
*
Expand All @@ -103,7 +135,7 @@ export class SearchPanelComponent {
closeMenu(): void {
this.showAdvanced = false;
this.showExpert = false;
if(this.overlayRef) {
if (this.overlayRef) {
this.overlayRef.detach();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/search-playground/search-playground.component.html
Expand Up @@ -58,7 +58,7 @@ <h3>Configure the search panel with the following parameters</h3>
</mat-expansion-panel-header>
<pre>
&lt;dsp-search-panel
[filterbyproject]="{{limitToProject}}"
[limitToProject]="{{limitToProject}}"
[projectfilter]="{{projectFilter}}"
[advanced]="{{advancedSearch}}"
[expert]="{{expertSearch}}"
Expand All @@ -73,7 +73,7 @@ <h3>Configure the search panel with the following parameters</h3>
<!-- simulate header -->
<div class="header">
<dsp-search-panel *ngIf="!loading" class="center" [projectfilter]="projectFilter"
[filterbyproject]="limitToProject" [advanced]="advancedSearch" [expert]="expertSearch" (search)="doSearch($event)">
[limitToProject]="limitToProject" [advanced]="advancedSearch" [expert]="expertSearch" (search)="doSearch($event)">
</dsp-search-panel>
<!-- -->

Expand Down

0 comments on commit 56949b1

Please sign in to comment.