Skip to content

Commit

Permalink
many: improvements
Browse files Browse the repository at this point in the history
* Changes icon on show document action
* Adds parameter aggregations on routing configuration
* Adds translation on language aggregation

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr authored and Sébastien Délèze committed Oct 23, 2019
1 parent f9a60af commit 1cbdc3b
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 16 deletions.
7 changes: 6 additions & 1 deletion projects/ng-core-tester/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const canDelete = (record) => {
});
};

const aggregations = (agg: object) => {
return of(agg);
};

export function matchedUrl(url: UrlSegment[]) {
const segments = [
new UrlSegment(url[0].path, {}),
Expand Down Expand Up @@ -193,7 +197,8 @@ const routes: Routes = [
detailComponent: DetailComponent,
canAdd,
canUpdate,
canDelete
canDelete,
aggregations
},
{
key: 'institutions',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<div class="card mb-2" *ngIf="aggregation.value.buckets && aggregation.value.buckets.length">
<div class="card-header p-0">
<h5 class="mb-0">
<button class="btn btn-link" type="button" (click)="show = !show;">
{{ aggregation.key|translate|ucfirst }}
<button class="btn btn-link" type="button" (click)="expand = !expand;">
{{ aggregation.key | translate | ucfirst }}
</button>
</h5>
</div>
Expand All @@ -30,7 +30,8 @@ <h5 class="mb-0">
(click)="updateFilter(bucket.key) ">
<label class="form-check-label">
<span *ngIf="bucket.name">{{ bucket.name }}</span>
<span *ngIf="!bucket.name">{{ bucket.key }}</span> ({{ bucket.doc_count }})
<span *ngIf="!bucket.name && aggregation.key != 'language'">{{ bucket.key | translate }}</span>
<span *ngIf="!bucket.name && aggregation.key == 'language'">{{ bucket.key | translateLanguage:language }}</span> ({{ bucket.doc_count }})
</label>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { TranslateModule, TranslateLoader, TranslateFakeLoader } from '@ngx-tran

import { RecordSearchAggregationComponent } from './aggregation.component';
import { UpperCaseFirstPipe } from '../../../pipe/ucfirst.pipe';
import { TranslateLanguagePipe } from '../../../translate-language/translate-language.pipe';

describe('RecordSearchAggregationComponent', () => {
let component: RecordSearchAggregationComponent;
Expand All @@ -28,7 +29,8 @@ describe('RecordSearchAggregationComponent', () => {
TestBed.configureTestingModule({
declarations: [
RecordSearchAggregationComponent,
UpperCaseFirstPipe
UpperCaseFirstPipe,
TranslateLanguagePipe
],
imports: [
TranslateModule.forRoot({
Expand Down Expand Up @@ -72,7 +74,7 @@ describe('RecordSearchAggregationComponent', () => {
it('should show aggregation filter', () => {
expect(component.showAggregation()).toBe(true);

component.show = false;
component.expand = false;
expect(component.showAggregation()).toBe(true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'ng-core-record-search-aggregation',
Expand All @@ -37,14 +38,27 @@ export class RecordSearchAggregationComponent {
* Show or hide filter items
*/
@Input()
public show = true;
public expand = true;

/**
* Emit event to parent when a value is clicked
*/
@Output()
public updateAggregationFilter = new EventEmitter<{ term: string, values: string[] }>();

/**
* Constructor
* @param translate TranslateService
*/
constructor(private translate: TranslateService) {}

/**
* Interface language
*/
get language() {
return this.translate.currentLang;
}

/**
* Check if a value is already registered in filters.
* @param value - string, filter value
Expand All @@ -71,6 +85,6 @@ export class RecordSearchAggregationComponent {
* Show filter values
*/
showAggregation() {
return this.show || this.selectedValues.length > 0;
return this.expand || this.selectedValues.length > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<div *ngFor="let item of aggregations | keyvalue">
<ng-core-record-search-aggregation [aggregation]="item"
[selectedValues]="getFilterSelectedValues(item.key)"
[expand]="expandFacet(item.key)"
(updateAggregationFilter)="updateAggregationFilter($event)">
</ng-core-record-search-aggregation>
</div>
Expand All @@ -78,4 +79,4 @@
<div class="text-center my-5">
<i class="fa fa-spin fa-spinner fa-2x"></i>
</div>
</ng-template>
</ng-template>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { RecordService } from '../record.service';
import { DialogComponent } from '../../dialog/dialog.component';
import { Nl2brPipe } from '../../pipe/nl2br.pipe';
import { DialogService } from '../../dialog/dialog.service';
import { TranslateLanguagePipe } from '../../translate-language/translate-language.pipe';

describe('RecordSearchComponent', () => {
let component: RecordSearchComponent;
Expand Down Expand Up @@ -93,7 +94,8 @@ describe('RecordSearchComponent', () => {
DefaultPipe,
UpperCaseFirstPipe,
Nl2brPipe,
DialogComponent
DialogComponent,
TranslateLanguagePipe
],
imports: [
BrowserAnimationsModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class RecordSearchComponent implements OnInit {
total?: number,
canAdd?: any,
canUpdate?: any,
canDelete?: any
canDelete?: any,
aggregations?: any
}[] = [{ key: 'documents', label: 'Documents' }];

/**
Expand Down Expand Up @@ -405,11 +406,41 @@ export class RecordSearchComponent implements OnInit {
).subscribe(records => {
this.records = records.hits.hits;
this.total = records.hits.total;
this.aggregations = records.aggregations;
this.aggregationsFilters(records.aggregations).subscribe((aggr: any) => {
this.aggregations = aggr;
});
this.isLoading = false;
});
}

/**
* Aggregations filters (facets)
* @param records - Result records
*/
aggregationsFilters(aggregations: object) {
if (this.config.aggregations) {
return this.config.aggregations(aggregations);
} else {
return of(aggregations);
}
}

/**
* Show or hide facet section
* @param key facet key
*/
expandFacet(key: string) {
if ('_settings' in this.aggregations) {
const settings = this.aggregations._settings;
const keyExtand = 'expand';
if (keyExtand in settings && settings[keyExtand].indexOf(key) > -1) {
return true;
}
return false;
}
return true;
}

/**
* Update route parameters when search criteria are changed.
* Only applied if component is integrated in routing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
</div>
<div class="col-2 text-right">
<a [href]="formattedDetailUrl" *ngIf="detailUrl; else routingDetailLink">
<i class="fa fa-search"></i>
<i class="fa fa-file-o"></i>
</a>
<ng-template #routingDetailLink>
<a href="#" [title]="'Show'|translate" [routerLink]="['detail/', record.metadata.pid]" *ngIf="inRouting">
<i class="fa fa-search"></i>
<i class="fa fa-file-o"></i>
</a>
</ng-template>
<a class="ml-2" [title]="'Edit'|translate" routerLink="edit/{{ record.metadata.pid }}"
Expand All @@ -40,8 +40,8 @@
<i class="fa fa-trash"></i>
</a>
</ng-template>

</span>

</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export class RecordSearchResultComponent implements OnInit {
@Input()
canDelete: Observable<DeleteRecordStatus>;

/**
* Aggregations
*/
@Input()
aggregations: Array<object>;

/**
* Indicates if the component is included in angular routes
*/
Expand Down
1 change: 1 addition & 0 deletions projects/rero/ng-core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export * from './lib/validator/time.validator';
export * from './lib/translate/translate-loader';
export * from './lib/translate/translate-service';
export * from './lib/translate/date-translate-pipe';
export * from './lib/record/record-status';

0 comments on commit 1cbdc3b

Please sign in to comment.