From 7bcb79955dc054ada983228802733cf380bfc5b5 Mon Sep 17 00:00:00 2001 From: "argiro.kokogiannaki" Date: Tue, 4 Feb 2020 16:33:46 +0000 Subject: [PATCH] [Library|Trunk] New search page: Clean up the code remove queryParams - use parameterNames/Values instead buildPageURLPArams: don't return URL path remove method updateBaseURLParam create method prepareSearchPage: gather in one method all initializations required for search page git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@58055 d315682c-612b-4755-9ff5-7f18f6832af3 --- .../searchResearchResults.component.ts | 12 +- .../searchUtils/newSearchPage.component.ts | 120 +++++++++--------- 2 files changed, 62 insertions(+), 70 deletions(-) diff --git a/searchPages/searchResearchResults.component.ts b/searchPages/searchResearchResults.component.ts index 96ab3584..b29f0a0a 100644 --- a/searchPages/searchResearchResults.component.ts +++ b/searchPages/searchResearchResults.component.ts @@ -126,15 +126,8 @@ export class SearchResearchResultsComponent { this.searchUtils.sortBy = ""; } - this.searchPage.fieldIds = this.fieldIds; this.selectedFields = []; - this.searchPage.selectedFields = this.selectedFields; - this.searchPage.refineFields = this.refineFields; - this.searchPage.fieldIdsMap = this.fieldIdsMap; - this.searchPage.customFilter = this.customFilter; - this.searchPage.unknownFilters = this.searchPage.getEmptyRefineFilters(params); - - this.searchPage.createAdvancedSearchSelectedFiltersFromURLParameters(params); + this.searchPage.prepareSearchPage(this.fieldIds, this.selectedFields, this.refineFields, this.fieldIdsMap,this.customFilter,params); this._getResults(this.searchPage.getSearchAPIQueryForAdvancedSearhFields(), this.searchUtils.page, this.searchUtils.size, this.searchUtils.sortBy, refine, this.searchPage.getSearchAPIQueryForRefineFields(params)); }); } @@ -169,9 +162,10 @@ export class SearchResearchResultsComponent { this.results = data[1]; if (refine) { this.filters = this.searchPage.prepareFiltersToShow(data[2]); + }else{ + this.searchPage.buildPageURLParameters(this.filters, false); } - this.searchPage.updateBaseUrlWithParameters(); this.searchUtils.status = this.errorCodes.DONE; if (this.searchUtils.totalResults == 0) { this.searchUtils.status = this.errorCodes.NONE; diff --git a/searchPages/searchUtils/newSearchPage.component.ts b/searchPages/searchUtils/newSearchPage.component.ts index 4c9ae271..6047dd80 100644 --- a/searchPages/searchUtils/newSearchPage.component.ts +++ b/searchPages/searchUtils/newSearchPage.component.ts @@ -48,11 +48,10 @@ export class NewSearchPageComponent { @Input() refineFields = []; @Input() filters = []; selectedFilters:number = 0; - private queryParameters: Map = new Map(); private searchFieldsHelper: SearchFields = new SearchFields(); @Input() newQueryButton: boolean = true; public showUnknownFilters: boolean = false; // when a filter exists in query but has no results, so no filters returned from the query - unknownFilters: Filter[] = []; + URLCreatedFilters: Filter[] = []; @Input() showRefine: boolean = true; @Input() tableViewLink: string; @Input() mapUrl: string = ""; @@ -68,8 +67,6 @@ export class NewSearchPageComponent { public parameterNames: string[] = []; public parameterValues: string[] = []; - public baseURLWithParameters: string = ''; - public csvLimit: number = 0; public pagingLimit: number = 0; public resultsPerPage: number = 0; @@ -117,8 +114,6 @@ export class NewSearchPageComponent { this.updateDescription(description); this.searchUtils.baseUrl = "/" + this.searchUtils.baseUrl; - - this.updateBaseUrlWithParameters(); this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this.router.url, false); @@ -179,7 +174,7 @@ export class NewSearchPageComponent { goTo(page: number = 1) { this.searchUtils.page = page; - this.buildPageURLParameters(true); + this.buildPageURLParameters(this.filters, true); this.router.navigate([this.searchUtils.baseUrl], {queryParams: this.routerHelper.createQueryParams(this.parameterNames, this.parameterValues)}); /* Code For Piwik*/ if (typeof localStorage !== 'undefined') { @@ -212,12 +207,6 @@ export class NewSearchPageComponent { this.goTo(1); } - /** - * Update the url with proper parameters. This is used as base url in Paging Component - */ - public updateBaseUrlWithParameters() { - this.baseURLWithParameters = this.searchUtils.baseUrl + this.buildPageURLParameters(false); - } getOperatorParameter(parameter: string): string { for (let id of this.fieldIds) { @@ -246,10 +235,8 @@ export class NewSearchPageComponent { */ private getSelectedParameters(): string[] { var selected: string[] = []; - var params: string[] = Object.keys(this.queryParameters); - for (var i = 0; i < this.refineFields.length; i++) { - if (this.queryParameters.get(this.refineFields[i]) != undefined) { + if (this.parameterNames.indexOf(this.refineFields[i]) != -1) { selected.push(this.refineFields[i]); } } @@ -298,8 +285,9 @@ export class NewSearchPageComponent { for (var i = 0; i < filters.length; i++) { var filter: Filter = filters[i]; filter.countSelectedValues = 0; - if (this.queryParameters.get(filter.filterId) != undefined) { - let values = (decodeURIComponent(this.queryParameters.get(filter.filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1); + if (this.parameterNames.indexOf(filter.filterId) != -1) { + console.log(filter.filterId + " "+this.parameterNames.indexOf(filter.filterId) ) + let values = (decodeURIComponent(this.parameterValues[this.parameterNames.indexOf(filter.filterId)])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1); for (let filterValue of filter.values) { if (values.indexOf(StringUtils.quote(filterValue.id)) > -1) { filterValue.selected = true; @@ -329,8 +317,8 @@ export class NewSearchPageComponent { var filter: Filter = filters[i]; // console.log(filter.filterId); - if (filter.filterId.indexOf("funder") != -1 && this.queryParameters.get(filter.filterId) != undefined) { - let funders = (decodeURIComponent(this.queryParameters.get(filter.filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1); + if (filter.filterId.indexOf("funder") != -1 && this.parameterNames.indexOf(filter.filterId) != -1) { + let funders = (decodeURIComponent(this.parameterValues[this.parameterNames.indexOf(filter.filterId)])).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1); for (let funder of funders) { funder_prefix.push(StringUtils.unquote(funder).split("____")[0]); } @@ -516,7 +504,6 @@ export class NewSearchPageComponent { if (this.customFilter) { params += (countParams == 0 ? "" : " and ") + this.customFilter.queryFieldName + " exact " + StringUtils.quote((this.customFilter.valueId)); } -console.log(params); return params; } @@ -527,14 +514,11 @@ console.log(params); getSearchAPIQueryForRefineFields(URLparams) { var allFqs = ""; - - this.queryParameters = new Map(); for (var i = 0; i < this.refineFields.length; i++) { var filterId = this.refineFields[i]; if (URLparams[filterId] != undefined) { - this.queryParameters.set(filterId, StringUtils.URIDecode(URLparams[filterId])); - let values = (StringUtils.URIDecode(this.queryParameters.get(filterId))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1); + let values = (StringUtils.URIDecode(StringUtils.URIDecode(URLparams[filterId]))).split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/, -1); var countvalues = 0; var fq = ""; let filterOp: string = this.searchFieldsHelper.getFieldOperator(filterId); @@ -592,15 +576,19 @@ console.log(params); // } // } //TODO add DOI? + // console.log(this.parameterNames) + // console.log(this.parameterValues) return allFqs; } /** - * + * Sets parameterNames and parameterValues arrays + * used in paging, advanced search link, Goto() + * @param filters either query filters or filters defined by the URL parameters + * @param includePage */ - buildPageURLParameters(includePage: boolean) { - var params = ""; + buildPageURLParameters(filters:Filter[],includePage: boolean) { this.parameterNames.splice(0, this.parameterNames.length); this.parameterValues.splice(0, this.parameterValues.length); var fields: { [key: string]: { values: string[], operators: string[] } } = {}; @@ -626,9 +614,6 @@ console.log(params); } for (var i = 0; i < this.fieldIds.length; i++) { if (fields[this.fieldIds[i]]) { - - params += "&" + this.fieldIdsMap[this.fieldIds[i]].param + "=" + fields[this.fieldIds[i]].values.join() + - "&" + this.fieldIdsMap[this.fieldIds[i]].operator + "=" + fields[this.fieldIds[i]].operators.join() this.parameterNames.push(this.fieldIdsMap[this.fieldIds[i]].param); this.parameterValues.push(fields[this.fieldIds[i]].values.join()); this.parameterNames.push(this.fieldIdsMap[this.fieldIds[i]].operator); @@ -636,22 +621,20 @@ console.log(params); } } if (includePage && this.searchUtils.page != 1) { - params += "&page=" + this.searchUtils.page; + this.parameterNames.push("page"); + this.parameterValues.push("" + this.searchUtils.page); } - if (this.searchUtils.size != 10) { - params += ((params.length == 0) ? '' : '&') + 'size=' + this.searchUtils.size; + if (this.searchUtils.size != this.resultsPerPage) { this.parameterNames.push("size"); this.parameterValues.push("" + this.searchUtils.size); } if (this.sort && this.searchUtils.sortBy) { - params += ((params.length == 0) ? '' : '&') + 'sortBy=' + this.searchUtils.sortBy; this.parameterNames.push("sortBy"); this.parameterValues.push(this.searchUtils.sortBy); } - var allLimits = "";//location.search.slice(1); - for (let filter of this.filters) { + for (let filter of filters) { var filterLimits = ""; if (filter.countSelectedValues > 0) { for (let value of filter.values) { @@ -659,38 +642,35 @@ console.log(params); filterLimits += ((filterLimits.length == 0) ? '' : ',') + '"' + (value.id) + '"'; } } - this.queryParameters.set(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); - //this.parameterValues.push(StringUtils.quote(this.searchUtils.keyword)); - } - if (this.searchUtils.page != 1 && includePage) { - allLimits += ((allLimits.length == 0) ? '?' : '&') + 'page=' + this.searchUtils.page; - } - if (this.searchUtils.size != this.resultsPerPage) { - allLimits += ((allLimits.length == 0) ? '?' : '&') + 'size=' + this.searchUtils.size; - this.parameterNames.push("size"); - this.parameterValues.push("" + this.searchUtils.size); - } - if (this.sort && this.searchUtils.sortBy) { - allLimits += ((allLimits.length == 0) ? '?' : '&') + 'sortBy=' + this.searchUtils.sortBy; - this.parameterNames.push("sortBy"); - this.parameterValues.push(this.searchUtils.sortBy); } + // if (this.searchUtils.size != this.resultsPerPage) { + // // allLimits += ((allLimits.length == 0) ? '?' : '&') + 'size=' + this.searchUtils.size; + // this.parameterNames.push("size"); + // this.parameterValues.push("" + this.searchUtils.size); + // } + // if (this.sort && this.searchUtils.sortBy) { + // // allLimits += ((allLimits.length == 0) ? '?' : '&') + 'sortBy=' + this.searchUtils.sortBy; + // this.parameterNames.push("sortBy"); + // this.parameterValues.push(this.searchUtils.sortBy); + // } - return params + allLimits; } - getEmptyRefineFilters(URLparams) { + + /** + * create filters based on URL params + * @param URLparams + */ + getRefineFiltersFromURL(URLparams) { let fields = new SearchFields(); let filters: Filter[] = []; for (let i = 0; i < this.refineFields.length; i++) { @@ -717,7 +697,8 @@ console.log(params); } // console.log("Empty Filters"); // console.log(filters); - return filters; + this.URLCreatedFilters = filters; + // return filters; } @@ -728,18 +709,35 @@ console.log(params); * @param filters */ public prepareFiltersToShow(filters:Filter[]):Filter[]{ - if (this.unknownFilters.length > 0 && this.searchUtils.totalResults == 0) { + if (this.URLCreatedFilters.length > 0 && this.searchUtils.totalResults == 0) { this.showUnknownFilters = true; - this.filters = this.unknownFilters; + this.filters = this.URLCreatedFilters; } else if (this.searchUtils.totalResults != 0 ) { this.showUnknownFilters = false; this.filters = filters; } + this.buildPageURLParameters(this.URLCreatedFilters, true); this.checkSelectedFilters(this.filters); this.countSelectedFilters(this.filters); return this.filters; } - - + /** + * Used to set the variables and search page, and prepare it before the getResults query + * @param fieldIds + * @param selectedFields + * @param refineFields + * @param fieldIdsMap + * @param customFilter + * @param params + */ + prepareSearchPage(fieldIds, selectedFields, refineFields,fieldIdsMap, customFilter, params){ + this.fieldIds = fieldIds; + this.selectedFields = selectedFields; + this.refineFields = refineFields; + this.fieldIdsMap = fieldIdsMap; + this.customFilter = customFilter; + this.getRefineFiltersFromURL(params); + this.createAdvancedSearchSelectedFiltersFromURLParameters(params); +} }