import { Injectable, RendererFactory2, Inject } from '@angular/core'; import { AppConstants } from '../helpers/constants/app.constants'; import { IAppConstants } from '../helpers/constants/app-constants.interface'; import { GlobalDataService } from './global-data.service'; import { Router } from '@angular/router'; import { ScriptService } from '../helpers/script/script.helper'; import { NumberParsePipe } from '../helpers/pipes/parse-numeric.pipe'; import { SharedService } from './shared.service'; import { QueryParamsModel } from '../models/query-params.model'; import { SlugGeneratePipe } from '../helpers/pipes/slug-generate.pipe'; import { ArticlePageFilterModel } from '../models/page-filter.model'; import { UrlSearchFilter } from '../models/url-search-filter.model'; import { SubjectQueryParamsModel } from '../models/subject-query-params.model'; @Injectable() export class BaseService { // #region Constructor constructor( public router: Router, public rendererFactory: RendererFactory2, public scriptService: ScriptService, public numberParsePipe: NumberParsePipe, public sharedService: SharedService, public slugGeneratePipe: SlugGeneratePipe, public globalData: GlobalDataService, @Inject(AppConstants) private constants: IAppConstants) { } // #region Constructor // #region Public Methods public ManageFilterParams(params: any, queryParams: QueryParamsModel): QueryParamsModel { queryParams.Domain = this.GetFilteredQueryParams(params[this.constants.QUERY_PARAMETER_DOMAIN]); queryParams.Journal = this.GetNumericQueryParams(params[this.constants.QUERY_PARAMETER_JOURNAL]); queryParams.Section = this.GetNumericQueryParams(params[this.constants.QUERY_PARAMETER_SECTION]); queryParams.Type = this.GetFilteredQueryParams(params[this.constants.QUERY_PARAMETER_TYPE]); queryParams.Date = this.GetFilteredQueryParams(params[this.constants.QUERY_PARAMETER_DATE]); queryParams.Sort = this.GetFilteredQueryParams(params[this.constants.QUERY_PARAMETER_SORT]); queryParams.PartOfResearchTopic = this.GetBooleanQueryParams(params[this.constants.QUERY_PARAMETER_PART_OF_RESEARCH_TOPICS]); const query = params[this.constants.QUERY_PARAMETER_SEARCH] || this.constants.DEFAULT_STRING_VALUE; queryParams.Query = query.trim(); return queryParams; } // #region Public Methods // #region Private Methods public GetFilteredQueryParams(item?: any, isReturnArray?: boolean): any { isReturnArray = isReturnArray || false; item = item || this.constants.DEFAULT_STRING_VALUE; const filter = Array.isArray(item) ? item[0] : item; const lstFilter = filter.split(','); return isReturnArray ? lstFilter : lstFilter[0]; } public GetNumericQueryParams(item?: any): number { const filter = this.GetFilteredQueryParams(item); return filter ? this.sharedService.GetParsedInput(filter) : 0; } public GetBooleanQueryParams(item?: any): any { let filterValue = null; const filter = this.GetFilteredQueryParams(item); if (filter && filter === this.constants.FILTER_TOKEN_TRUE || filter === this.constants.FILTER_TOKEN_FALSE) { filterValue = (filter === this.constants.FILTER_TOKEN_TRUE) ? this.constants.FILTER_TOKEN_YES : this.constants.FILTER_TOKEN_NO; } return filterValue; } public GetUpdatedUrl(queryParams: QueryParamsModel): string { const pageNumber = this.globalData.PageNumber || 0; const articleIds = queryParams.ArticleIds.toString() || this.constants.DEFAULT_STRING_VALUE; const rootUrl = `/${this.constants.ARTICLES_ROUTE_PREFIX}`; let articleUrl = this.constants.DEFAULT_STRING_VALUE; if (articleIds.length > 0) { articleUrl = `${this.constants.QUERY_PARAMETER_ARTICLE_IDS}=${articleIds}`; } else if (pageNumber !== 0 || !this.globalData.IsNextTagVisible) { this.globalData.QueryPageNumber = pageNumber; articleUrl = `${this.constants.QUERY_PARAMETER_PAGE}=${queryParams.Page}`; } else { const domain = this.GetActiveFilterSlug(this.globalData.Settings.ActiveFilters.Domains, this.constants.FILTER_TOKEN_DOMAIN, false); if (domain.length > 0) { articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_DOMAIN, domain); } if (queryParams.Query.length > 0) { const query = queryParams.Query; articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_SEARCH, query); } if (this.IsFilterActive(this.globalData.FiltersEnum.Journals)) { // tslint:disable-next-line: max-line-length articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_JOURNAL, this.globalData.Settings.ActiveFilters.Journals.Id); } if (this.IsFilterActive(this.globalData.FiltersEnum.Sections)) { // tslint:disable-next-line: max-line-length articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_SECTION, this.globalData.Settings.ActiveFilters.Sections.Id); } if (this.IsFilterActive(this.globalData.FiltersEnum.Type)) { const articleType = this.GetActiveFilterSlug(this.globalData.Settings.ActiveFilters.Type, this.constants.FILTER_TOKEN_TYPE, false); articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_TYPE, articleType); } if (this.IsFilterActive(this.globalData.FiltersEnum.Date)) { const date = this.GetActiveFilterSlug(this.globalData.Settings.ActiveFilters.Date, this.constants.FILTER_TOKEN_DATE, true); articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_DATE, date); } if (this.IsFilterActive(this.globalData.FiltersEnum.PartOfResearchTopic)) { // tslint:disable-next-line: max-line-length let isPartOfResearchTopic = this.GetActiveFilterSlug(this.globalData.Settings.ActiveFilters.PartOfResearchTopic, this.constants.DEFAULT_STRING_VALUE); // tslint:disable-next-line: max-line-length isPartOfResearchTopic = isPartOfResearchTopic === this.constants.FILTER_TOKEN_YES ? this.constants.FILTER_TOKEN_TRUE : this.constants.FILTER_TOKEN_FALSE; articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_PART_OF_RESEARCH_TOPICS, isPartOfResearchTopic); } if (this.IsFilterActive(this.globalData.FiltersEnum.Sort)) { const sort = this.GetActiveFilterSlug(this.globalData.Settings.ActiveFilters.Sort, this.constants.FILTER_TOKEN_SORT, true); articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_SORT, sort); } } const siteUrl = articleUrl.length > 0 ? `${rootUrl}?${articleUrl.toLowerCase()}` : `${rootUrl}`; return siteUrl; } public GetPageUrlFilters(queryParams: QueryParamsModel): ArticlePageFilterModel { const filterData = new ArticlePageFilterModel(); for (const key of Object.keys(this.globalData.UrlFilters)) { const filter = this.globalData.UrlFilters[key]; if (filter) { if (filter.Key === this.globalData.FiltersEnum.Domains) { filterData.Domains = this.GetTokenFilteredValue(filter.Value, queryParams.Domain, this.constants.FILTER_TOKEN_DOMAIN); } if (filter.Key === this.globalData.FiltersEnum.Journals) { filterData.Journals = this.GetSelectedFilter(filter.Value, queryParams.Journal); } if (filter.Key === this.globalData.FiltersEnum.Type) { filterData.Type = this.GetTokenFilteredValue(filter.Value, queryParams.Type, this.constants.FILTER_TOKEN_TYPE); } if (filter.Key === this.globalData.FiltersEnum.Sections) { filterData.Sections = this.GetSelectedFilter(filter.Value, queryParams.Section); } if (filter.Key === this.globalData.FiltersEnum.Date) { filterData.Date = this.GetTokenFilteredValue(filter.Value, queryParams.Date, this.constants.FILTER_TOKEN_DATE); } if (filter.Key === this.globalData.FiltersEnum.Sort) { filterData.Sort = this.GetTokenFilteredValue(filter.Value, queryParams.Sort, this.constants.FILTER_TOKEN_SORT, true); } if (filter.Key === this.globalData.FiltersEnum.PartOfResearchTopic) { // tslint:disable-next-line: max-line-length filterData.PartOfResearchTopic = this.GetTokenFilteredValue(filter.Value, queryParams.PartOfResearchTopic, this.constants.DEFAULT_STRING_VALUE, true); } } } return filterData; } public GetActiveFilterSlug(item: any, token: string, replaceAll?: boolean): string { let field = null; if (replaceAll) { field = this.slugGeneratePipe.transform(item.Name, token, this.constants.DEFAULT_STRING_VALUE).toLowerCase(); } else { field = item.Id === 0 ? this.slugGeneratePipe.transform(item.Name, token, this.constants.DEFAULT_STRING_VALUE) : item.Name; } return this.slugGeneratePipe.transform(field, this.constants.REGEX_PATTERN_WHITE_SPACE, this.constants.TOKEN_HYPHEN); } public GetTokenFilteredValue(lstFilters: any, value: any, token: string, replaceAll?: boolean): any { let filterData = lstFilters[0]; replaceAll = replaceAll || false; // tslint:disable-next-line: prefer-for-of for (let i = 0; i < lstFilters.length; i++) { const item = lstFilters[i]; let field = null; if (replaceAll) { field = this.slugGeneratePipe.transform(item.Name, token, this.constants.DEFAULT_STRING_VALUE).toLowerCase(); } else { field = item.Id === 0 ? this.slugGeneratePipe.transform(item.Name, token, this.constants.DEFAULT_STRING_VALUE) : item.Name; } const slug = this.slugGeneratePipe.transform(field, this.constants.REGEX_PATTERN_WHITE_SPACE, this.constants.TOKEN_HYPHEN); if (slug === value) { filterData = item; break; } } return filterData; } public GetSelectedFilter(lstFilters: any, value: any): any { let filterData = lstFilters[0]; // tslint:disable-next-line: prefer-for-of for (let i = 0; i < lstFilters.length; i++) { const item = lstFilters[i]; if (item.Id === value) { filterData = item; break; } } return filterData; } public GetUrlPageFilterParams(queryParams: QueryParamsModel): UrlSearchFilter { const urlParams = new UrlSearchFilter(); urlParams.DomainName = queryParams.Domain.toLowerCase() === this.constants.FILTER_TOKEN_ALL ? this.constants.DEFAULT_STRING_VALUE : this.slugGeneratePipe.transform(queryParams.Domain, this.constants.REGEX_PATTERN_UNDERSCORE, this.constants.FILTER_TOKEN_SPACE); urlParams.JournalId = queryParams.Journal; urlParams.SectionId = queryParams.Section; urlParams.SearchTerm = queryParams.Query; // tslint:disable-next-line: max-line-length urlParams.ArticleType = this.slugGeneratePipe.transform(queryParams.Type, this.constants.REGEX_PATTERN_UNDERSCORE, this.constants.FILTER_TOKEN_SPACE); return urlParams; } // #region Public Methods // #region Article Subjects public GetUpdatedSubjectPageUrl(queryParams: SubjectQueryParamsModel): string { const rootUrl = `/${this.constants.SUBJECTS_ROUTE_PREFIX}`; let articleUrl = this.constants.DEFAULT_STRING_VALUE; const domain = this.GetActiveFilterSlug(this.globalData.Settings.ActiveFilters.Domains, this.constants.FILTER_TOKEN_DOMAIN, false); if (domain.length > 0) { articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_DOMAIN, domain); } if (queryParams.Query.length > 0) { const query = queryParams.Query; articleUrl = this.GetUpdatedUrlValue(articleUrl, this.constants.QUERY_PARAMETER_SEARCH, query); } const siteUrl = articleUrl.length > 0 ? `${rootUrl}?${articleUrl.toLowerCase()}` : `${rootUrl}`; return siteUrl; } public ManageArticleRedirection() { if (this.globalData.SelectedPageType === this.globalData.ArticlePageType.Subjects) { const querParam = new QueryParamsModel(); querParam.Query = this.globalData.SearchTerm; const siteUrl = this.GetUpdatedUrl(querParam); this.router.navigateByUrl(siteUrl); } } // #endregion Article Subjects // #region Private Methods private IsFilterActive(key: any): boolean { const filter = this.globalData.Filters.find(x => x.Key === key); const selectedFilter = this.globalData.Settings.ActiveFilters[key]; return (selectedFilter.Id !== filter.DefaultValue) ? true : false; } private GetUpdatedUrlValue(articleUrl: string, param: string, input: any): string { return articleUrl = articleUrl.length > 0 ? `${articleUrl}&${param}=${input}` : `${param}=${input}`; } // #region Private Methods }