Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

records: add link to detail on brief view title #29

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions projects/ng-core-tester/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { NgModule } from '@angular/core';
import { Routes, RouterModule, UrlSegment } from '@angular/router';
import { of } from 'rxjs';
import { of, Observable } from 'rxjs';

import { HomeComponent } from './home/home.component';
import { DocumentComponent } from './record/document/document.component';
Expand All @@ -41,6 +41,10 @@ const canDelete = (record) => {
});
};

const canRead = (record: any): Observable<boolean> => {
return of(Math.random() >= 0.5);
};

const aggregations = (agg: object) => {
return of(agg);
};
Expand Down Expand Up @@ -132,7 +136,7 @@ const routes: Routes = [
}
},
{
key: 'patrons',
key: 'users',
label: 'Utilisateurs'
}
]
Expand Down Expand Up @@ -198,6 +202,7 @@ const routes: Routes = [
canAdd,
canUpdate,
canDelete,
canRead,
aggregations
},
{
Expand All @@ -206,7 +211,7 @@ const routes: Routes = [
component: InstitutionComponent
},
{
key: 'patrons',
key: 'users',
label: 'Utilisateurs'
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ <h1>{{ record.metadata.title }}</h1>
<p>
<span class="badge badge-primary mr-1" *ngFor="let author of record.metadata.authors">{{ author.name }}</span>
</p>
<div class="text-justify">
<div class="text-justify" *ngIf="record.metadata.abstracts">
{{ record.metadata.abstracts[0].value }}
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<h5>{{ record.metadata.title }}</h5>
<h5>
<a href [routerLink]="detailUrl" *ngIf="detailUrl; else titleWithoutLink">{{ record.metadata.title }}</a>
<ng-template #titleWithoutLink>
{{ record.metadata.title }}
</ng-template>
</h5>
<p>
<span class="badge badge-primary mr-1" *ngFor="let author of record.metadata.authors">{{ author.name }}</span>
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export class DocumentComponent implements ResultItem {

@Input()
type: string;

@Input()
detailUrl: string;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<h5>{{ record.metadata.name }}</h5>
<p class="mb-0"><span class="badge badge-primary">{{ record.metadata.pid }}</span></p>
<h5>
<a href [routerLink]="detailUrl" *ngIf="detailUrl; else titleWithoutLink">{{ record.metadata.name }}</a>
<ng-template #titleWithoutLink>
{{ record.metadata.name }}
</ng-template>
</h5>
<p class="mb-0"><span class="badge badge-primary">{{ record.metadata.pid }}</span></p>
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export class InstitutionComponent implements ResultItem {

@Input()
type: string;

@Input()
detailUrl: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<li class="list-group-item px-0 py-4" *ngFor="let record of records">
<ng-core-record-search-result [adminMode]="adminMode" [record]="record" [type]="currentType"
[itemViewComponent]="getResultItemComponentView()" [canUpdate]="canUpdateRecord(record)"
[inRouting]="inRouting" [detailUrl]="detailUrl" [canDelete]="canDeleteRecord(record)"
[inRouting]="inRouting" [canDelete]="canDeleteRecord(record)" [detailUrl$]="resolveDetailUrl(record)"
(deletedRecord)="deleteRecord($event)">
</ng-core-record-search-result>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('RecordSearchComponent', () => {
snapshot: {
data: {
linkPrefix: '/records',
detailUrl: '/custom/url/for/detail',
detailUrl: '/custom/url/for/detail/:type/:pid',
types: [
{
key: 'documents',
Expand Down Expand Up @@ -290,4 +290,31 @@ describe('RecordSearchComponent', () => {
expect(component.aggFilters.length).toBe(1);
expect(component.aggFilters[0].values).toEqual(['Filippini, Massimo']);
});

it('should resolve detail url', async(() => {
component.resolveDetailUrl({ metadata: { pid: 100 } }).subscribe((result: any) => {
expect(result).toBe('/custom/url/for/detail/documents/100');
});

component.detailUrl = null;

component.resolveDetailUrl({ metadata: { pid: 100 } }).subscribe((result: any) => {
expect(result).toBe('detail/100');
});

component.types = [
{
key: 'documents',
label: 'Documents',
canRead: () => of(false)
}
];
/* tslint:disable:no-string-literal */
component['loadResourceConfig']();

component.resolveDetailUrl({ metadata: { pid: 100 } }).subscribe((result: any) => {
expect(result).toBe(null);
});
}));

});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
*/
import { Component, OnInit, Input } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { of, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ToastrService } from 'ngx-toastr';
import { TranslateService } from '@ngx-translate/core';
import { of, Observable } from 'rxjs';

import { RecordService } from '../record.service';
import { DialogService } from '../../dialog/dialog.service';
Expand Down Expand Up @@ -96,6 +98,7 @@ export class RecordSearchComponent implements OnInit {
canAdd?: any,
canUpdate?: any,
canDelete?: any,
canRead?: any,
aggregations?: any
}[] = [{ key: 'documents', label: 'Documents' }];

Expand Down Expand Up @@ -465,4 +468,29 @@ export class RecordSearchComponent implements OnInit {

this.router.navigate([this.linkPrefix + '/' + this.currentType], { queryParams });
}

/**
* Returns an observable which emits the URL value for given record.
* In case record cannot be read, returns null.
* @param record - Generate detail URL for this record.
*/
resolveDetailUrl(record: any) {
const url = this.detailUrl ?
this.detailUrl.replace(':type', this.currentType).replace(':pid', record.metadata.pid) :
`detail/${record.metadata.pid}`;

if (!this.config.canRead) {
return of(url);
}

return this.config.canRead(record).pipe(
map((canReadResult: boolean) => {
if (canReadResult === false) {
return null;
}

return url;
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ export class JsonComponent implements ResultItem {

@Input()
type: string;

@Input()
detailUrl: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
export interface ResultItem {
record: any;
type: string;
detailUrl: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
<ng-template ngCoreRecordSearchResult></ng-template>
</div>
<div class="col-2 text-right">
<a [href]="formattedDetailUrl" *ngIf="detailUrl; else routingDetailLink">
<a href [routerLink]="detailUrl" *ngIf="detailUrl">
<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-file-o"></i>
</a>
</ng-template>
<a class="ml-2" [title]="'Edit'|translate" routerLink="edit/{{ record.metadata.pid }}"
*ngIf="inRouting && adminMode && canUpdate">
<i class="fa fa-pencil"></i>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,4 @@ describe('RecordSearchResultComponent', () => {
});
component.deleteRecord(new Event('click'), '1');
});

it('should resolve custom detail URL', () => {
component.record = {
id: '1',
metadata: {
pid: '1'
}
};
component.type = 'documents';
component.detailUrl = '/custom/url/to/detail/:type/:pid';
expect(component.formattedDetailUrl).toBe('/custom/url/to/detail/documents/1');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RecordSearchResultDirective } from './record-search-result.directive';
import { JsonComponent } from './item/json.component';
import { DialogService } from '../../../dialog/dialog.service';
import { DeleteRecordStatus } from '../../record-status';
import { ResultItem } from './item/result-item';

@Component({
selector: 'ng-core-record-search-result',
Expand All @@ -35,6 +36,11 @@ export class RecordSearchResultComponent implements OnInit {

canDeleteResult: DeleteRecordStatus;

/**
* Detail URL value, resolved by observable property detailUrl$.
*/
detailUrl: string;

/**
* Record to display
*/
Expand Down Expand Up @@ -84,17 +90,10 @@ export class RecordSearchResultComponent implements OnInit {
inRouting = false;

/**
* URL to notice detail
* Observable emitting current value of record URL.
*/
@Input()
detailUrl: string = null;

/**
* Return detail URL with replaced placeholders.
*/
get formattedDetailUrl() {
return this.detailUrl.replace(':type', this.type).replace(':pid', this.record.id);
}
detailUrl$: Observable<string> = null;

/**
* Event emitted when a record is deleted
Expand All @@ -121,13 +120,19 @@ export class RecordSearchResultComponent implements OnInit {
* Component init
*/
ngOnInit() {
this.loadItemView();

if (this.canDelete) {
this.canDelete.subscribe((result: DeleteRecordStatus) => {
this.canDeleteResult = result;
});
}

if (this.detailUrl$) {
this.detailUrl$.subscribe((url: string) => {
this.detailUrl = url;
});
}

this.loadItemView();
}

/**
Expand All @@ -140,8 +145,9 @@ export class RecordSearchResultComponent implements OnInit {
viewContainerRef.clear();

const componentRef = viewContainerRef.createComponent(componentFactory);
(componentRef.instance as JsonComponent).record = this.record;
(componentRef.instance as JsonComponent).type = this.type;
(componentRef.instance as ResultItem).record = this.record;
(componentRef.instance as ResultItem).type = this.type;
(componentRef.instance as ResultItem).detailUrl = this.detailUrl;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Directive, ViewContainerRef, Input } from '@angular/core';
import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
selector: '[ngCoreRecordSearchResult]',
})
export class RecordSearchResultDirective {
/**
* Record to display
*/
@Input()
record: object = {};

constructor(public viewContainerRef: ViewContainerRef) { }
}