import {Component, Input} from '@angular/core'; import {ClaimEntity} from './claimHelper.class'; declare var UIkit: any; @Component({ selector: 'claim-results', template: `
{{(!entity.result) ? entity.type : ((entity.result && entity.result.source == 'openaire') ? entity.type : (entity.result && entity.result.source + ' result'))}}
`, }) export class ClaimResultsComponent { @Input() results: ClaimEntity[]; @Input() selectedResults: ClaimEntity[]; @Input() localStoragePrefix: string = ""; @Input() basketLimit; private isSelected(result: ClaimEntity) { let found: boolean = false; const id = result.id; for (let _i = 0; _i < this.selectedResults.length; _i++) { let item = this.selectedResults[_i]; if (item.id && item.id == id) { found = true; break; } } return found; // indexOf doesn't work when results came from // return this.selectedResults.indexOf(entity)!=-1; } add(item: ClaimEntity) { // if (this.selectedResults.length > this.basketLimit) { // UIkit.notification({ // message: 'Your basket exceeds the number of allowed results (150)', // status: 'warning', // timeout: 1500, // pos: 'top-center' // }); // return; // } let found: boolean = this.isSelected(item); if (!found) { this.selectedResults.push(item); if (this.selectedResults != null) { localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.selectedResults)); } UIkit.notification(item.type + ' added in your basket!', { status: 'success', timeout: 4000, pos: 'bottom-right' }); } } remove(item: any) { const index: number = this.selectedResults.indexOf(item); if (index > -1) { this.selectedResults.splice(index, 1); if (this.selectedResults != null) { localStorage.setItem(this.localStoragePrefix, JSON.stringify(this.selectedResults)); } UIkit.notification(item.type + ' removed from your basket!', { status: 'warning', timeout: 4000, pos: 'bottom-right' }); } } }