openaire-library/searchPages/searchUtils/searchPageTableView.compone...

273 lines
8.9 KiB
TypeScript
Raw Normal View History

import {Component, Input, ViewChild, Output, EventEmitter, ChangeDetectorRef} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Location} from '@angular/common';
import { Filter, Value} from './searchHelperClasses.class';
import {SearchResult} from '../../utils/entities/searchResult';
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
import {SearchUtilsClass} from './searchUtils.class';
import {DOI, StringUtils} from '../../utils/string-utils.class';
import {ModalLoading} from '../../utils/modal/loading.component';
import { Meta} from '../../../angular2-meta';
import{SearchFilterComponent} from './searchFilter.component';
import {SearchFilterModalComponent} from './searchFilterModal.component';
import { ErrorCodes} from '../../utils/properties/openaireProperties';
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
import {ContentProvidersDatatablePipe} from '../../utils/pipes/contentProvidersDatatable.pipe';
import {PiwikService} from '../../utils/piwik/piwik.service';
@Component({
selector: 'search-page-table',
templateUrl:'searchPageTableView.component.html'
})
export class SearchPageTableViewComponent {
@Input() pageTitle = "";
@Input() results;
@Input() filters = [];
@Input() type:string = "";
@Input() entityType: string = "";
@Input() searchUtils:SearchUtilsClass;// = new SearchUtilsClass();
//@Output() downloadClick = new EventEmitter();
@Input() showResultCount:boolean = true;
@Input() showRefine:boolean = true;
@Input() refineFields = [];
//@Input() csvParams: string;
//@Input() csvPath: string;
@Input() searchViewLink: string;
@Input() disableForms: boolean = false;
@Input() searchFormClass: string = "searchForm";
@Input() formPlaceholderText = "Type Keywords...";
@ViewChild (ModalLoading) loading : ModalLoading ;
private searchFieldsHelper:SearchFields = new SearchFields();
private queryParameters: Map<string, string> = new Map<string,string>();
private sub: any;
public countFilters= 0;
public parameterNames:string[] =[];
public parameterValues:string[] =[];
public triggerPipe: boolean = false;
public rowsOnPage:number = 10;
@ViewChild('mf') table: any;//DataTable;
@ViewChild (SearchFilterModalComponent) searchFilterModal : SearchFilterModalComponent ;
public currentFilter: Filter;
public errorCodes:ErrorCodes = new ErrorCodes();
piwiksub: any;
constructor (private location: Location , private _meta: Meta, private _piwikService:PiwikService, public cd: ChangeDetectorRef) { }
ngOnInit() {
this.updateTitle(this.pageTitle);
this.updateDescription("Openaire, search, repositories, open access, type, content provider, funder, project, " + this.type + "," +this.pageTitle);
if(typeof window !== 'undefined') {
this.updateUrl(OpenaireProperties.getBaseLink()+location.pathname);
}
if(OpenaireProperties.isPiwikTrackEnabled() && (typeof document !== 'undefined')){
this.piwiksub = this._piwikService.trackView(this.pageTitle).subscribe();
}
}
ngOnDestroy() {
if(this.piwiksub){
this.piwiksub.unsubscribe();
}
}
public sortByOrganization = (dataprovider: any) => {
if(dataprovider.organizations && dataprovider.organizations.length > 0) {
return dataprovider.organizations[0].name.toUpperCase();
}
return "-";
}
totalPages(): number {
let totalPages:any = this.searchUtils.totalResults/(this.rowsOnPage);
if(!(Number.isInteger(totalPages))) {
totalPages = (parseInt(totalPages, 10) + 1);
}
return totalPages;
}
refreshTable(page:number) {
this.searchUtils.page=page;
//this.table.mfActivePage=$event.value;
this.table.setPage(this.searchUtils.page, this.rowsOnPage);
}
toggleModal($event) {
this.currentFilter = $event.value;
this.searchFilterModal.open();
}
updateDescription(description:string){
this._meta.updateMeta("description", description);
this._meta.updateProperty("og:description", description);
}
updateTitle(title:string){
var _prefix ="OpenAIRE | ";
var _title = _prefix + ((title.length> 50 ) ?title.substring(0,50):title);
this._meta.setTitle(_title );
this._meta.updateProperty("og:title",_title);
}
updateUrl(url:string){
this._meta.updateProperty("og:url", url);
}
public getParametersFromUrl(params) {
for(var i=0; i< this.refineFields.length ; i++) {
var filterId = this.refineFields[i];
if(params[filterId] != undefined) {
if(this.queryParameters == undefined){
this.queryParameters = new Map<string,string>();
}
this.queryParameters[filterId]=decodeURIComponent(params[filterId]);
}
}
}
/*
* Mark as check the new filters that are selected, when you get them from search
*/
public checkSelectedFilters(filters:Filter[]){
this.filters = filters;
for(var i=0; i< filters.length ; i++){
var filter:Filter = filters[i];
filter.countSelectedValues = 0;
if(this.queryParameters[filter.filterId] != undefined) {
let values = (decodeURIComponent(this.queryParameters[filter.filterId])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/,-1);
for(let filterValue of filter.values) {
if(values.indexOf(StringUtils.quote(filterValue.id)) > -1) {
filterValue.selected = true;
filter.countSelectedValues++;
}else{
filterValue.selected = false;
}
}
}else{
for(let filterValue of filter.values) {
filterValue.selected = false;
}
}
}
return filters;
}
/*
*
*/
private createUrlParameters(filters:Filter[], includePage:boolean){
var allLimits="";//location.search.slice(1);
this.parameterNames.splice(0,this.parameterNames.length);
this.parameterValues.splice(0,this.parameterValues.length);
for (let filter of filters){
var filterLimits="";
if(filter.countSelectedValues > 0){
for (let value of filter.values){
if(value.selected == true){
filterLimits+=((filterLimits.length == 0)?'':',') +'"'+ StringUtils.URIEncode(value.id)+'"';
}
}
this.queryParameters[filter.filterId]=filterLimits;
if(filterLimits.length > 0){
this.parameterNames.push(filter.filterId);
this.parameterValues.push(filterLimits);
}
allLimits+=(allLimits.length==0?"?":"&")+((filterLimits.length == 0 )?'':filter.filterId + '='+ filterLimits) ;
}
}
if(this.searchUtils.keyword.length > 0 ){
allLimits+=(allLimits.length==0?"?":"&")+'keyword=' + this.searchUtils.keyword;
this.parameterNames.push("keyword");
this.parameterValues.push(this.searchUtils.keyword);
}
//if(this.searchUtils.page != 1 && includePage){
// allLimits+=((allLimits.length == 0)?'?':'&') + 'page=' + this.searchUtils.page;
//}
return allLimits;
}
public isFiltered(){
var filtered=false;
for (let filter of this.filters){
if(filter.countSelectedValues > 0){
filtered = true;
break;
}
}
if(this.searchUtils.keyword.length > 0 ){
filtered = true;
}
return filtered;
}
private clearKeywords(){
if(this.searchUtils.keyword.length > 0 ){
this.searchUtils.keyword ='';
}
this.goTo(1, true);
}
private clearFilters(){
for (var i =0 ; i < this.filters.length; i++) {
for (var j=0; j < this.filters[i].values.length; j++) {
if(this.filters[i].values[j].selected) {
this.filters[i].values[j].selected = false;
}
}
this.filters[i].countSelectedValues = 0;
}
this.clearKeywords();
}
private removeFilter(value:Value,filter:Filter){
filter.countSelectedValues--;
if(value.selected == true){
value.selected = false;
}
this.goTo(1, true);
}
goTo(page:number = 1, triggerPipe:boolean = true){
this.refreshTable(page);
var urlParameters = this.createUrlParameters(this.filters,true);
this.location.go(location.pathname,urlParameters);
if(triggerPipe) {
this.triggerPipe = !this.triggerPipe;
}
}
filterChanged($event){
this.goTo(1, true);
}
keywordChanged($event) {
this.searchUtils.keyword = $event.value;
this.goTo(1, true);
}
/*
downloadClicked($event) {
if($event.value == true) {
var queryParameters = this.createSearchQueryParameters(this.filters);
this.downloadClick.emit({
value: queryParameters
});
}
}
*/
getSelectedValues(filter):any{
var selected = [];
if(filter.countSelectedValues >0){
for (var i=0; i < filter.values.length; i++){
if(filter.values[i].selected){
selected.push(filter.values[i]);
}
}
}
return selected;
}
}