Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Remove data from global search service

Oliver Günther20a70ffc2a32
diff --git a/frontend/src/app/core/global_search/input/global-search-input.component.ts b/frontend/src/app/core/global_search/input/global-search-input.component.ts
index c14e90cc4b3..bdcf6749e69 100644
--- a/frontend/src/app/core/global_search/input/global-search-input.component.ts
+++ b/frontend/src/app/core/global_search/input/global-search-input.component.ts
@@ -132,7 +132,7 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
/** Remember the current value */
public currentValue = '';
- public isFocusedDirectly = this.globalSearchService.searchTerm.length > 0 && this.selectedItem instanceof HalResource;
+ public isFocusedDirectly = !!this.currentQuery && this.selectedItem instanceof HalResource;
private unregisterGlobalListener:(() => unknown)|undefined;
@@ -164,7 +164,7 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
ngAfterViewInit():void {
// check searchterm on init, expand / collapse search bar and set correct classes
- this.searchTerm = this.globalSearchService.searchTerm;
+ this.searchTerm = this.currentQuery || '';
this.currentValue = '';
this.toggleTopMenuClass();
}
@@ -206,7 +206,7 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
} else if (this.searchTerm?.length === 0) {
this.ngSelectComponent.ngSelectInstance.focus();
} else {
- this.submitNonEmptySearch();
+ this.submitNonEmptySearch('');
}
}
}
@@ -290,8 +290,6 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
if (item instanceof HalResource) {
window.location.href = this.wpPath(item.id as string);
} else if (item) {
- // update embedded table and title when new search is submitted
- this.globalSearchService.searchTerm = this.currentValue;
this.searchInScope(item.projectScope);
}
}
@@ -382,7 +380,7 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
searchOptions.push('current_project_and_all_descendants');
searchOptions.push('current_project');
}
- if (this.globalSearchService.projectScope === 'current_project') {
+ if (this.currentScope === 'current_project') {
searchOptions.reverse();
}
searchOptions.push('all_projects');
@@ -426,23 +424,15 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
private searchInScope(scope:string):void {
switch (scope) {
case 'all_projects': {
- let forcePageLoad = false;
- if (this.globalSearchService.projectScope !== 'all') {
- forcePageLoad = true;
- this.globalSearchService.resultsHidden = true;
- }
- this.globalSearchService.projectScope = 'all';
- this.submitNonEmptySearch(forcePageLoad);
+ this.submitNonEmptySearch('all');
break;
}
case 'current_project': {
- this.globalSearchService.projectScope = 'current_project';
- this.submitNonEmptySearch();
+ this.submitNonEmptySearch('current_project');
break;
}
case 'current_project_and_all_descendants': {
- this.globalSearchService.projectScope = '';
- this.submitNonEmptySearch();
+ this.submitNonEmptySearch('');
break;
}
default: // Do nothing
@@ -450,32 +440,24 @@ export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
}
}
- public submitNonEmptySearch(forcePageLoad = false):void {
- this.globalSearchService.searchTerm = this.currentValue;
+ public submitNonEmptySearch(scope:string):void {
if (this.currentValue.length > 0) {
this.ngSelectComponent.ngSelectInstance.close();
- // Work package results can update without page reload.
- if (!forcePageLoad
- && this.globalSearchService.isAfterSearch()
- && this.globalSearchService.currentTab === 'work_packages') {
- window.history
- .replaceState(
- {},
- `${I18n.t('global_search.search')}: ${this.searchTerm}`,
- this.globalSearchService.searchPath(),
- );
-
- return;
- }
- this.globalSearchService.submitSearch();
+ this.globalSearchService.submitSearch(this.currentValue, scope);
}
}
private get currentScope():string {
- const serviceScope = this.globalSearchService.projectScope;
+ const params = new URLSearchParams(window.location.search);
+ const serviceScope = params.get('scope') || '';
return (serviceScope === '') ? 'current_project_and_all_descendants' : serviceScope;
}
+ private get currentQuery():string|null {
+ const params = new URLSearchParams(window.location.search);
+ return params.get('q');
+ }
+
private unregister():void {
if (this.unregisterGlobalListener) {
this.unregisterGlobalListener();
diff --git a/frontend/src/app/core/global_search/services/global-search.service.spec.ts b/frontend/src/app/core/global_search/services/global-search.service.spec.ts
deleted file mode 100644
index 69addabc6cb..00000000000
--- a/frontend/src/app/core/global_search/services/global-search.service.spec.ts
+++ /dev/null
@@ -1,105 +0,0 @@
-//-- copyright
-// OpenProject is an open source project management software.
-// Copyright (C) the OpenProject GmbH
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License version 3.
-//
-// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-// Copyright (C) 2006-2013 Jean-Philippe Lang
-// Copyright (C) 2010-2013 the ChiliProject Team
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-//
-// See COPYRIGHT and LICENSE files for more details.
-//++
-
-/* jshint expr: true */
-
-import { CurrentProjectService } from 'core-app/core/current-project/current-project.service';
-import { GlobalSearchService } from 'core-app/core/global_search/services/global-search.service';
-import { I18nService } from 'core-app/core/i18n/i18n.service';
-import { TestBed, waitForAsync } from '@angular/core/testing';
-import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
-import { States } from 'core-app/core/states/states.service';
-import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
-
-describe('Global search service', () => {
- let service:GlobalSearchService;
- let CurrentProject:CurrentProjectService;
- let CurrentProjectSpy;
-
- beforeEach(waitForAsync(() => {
- // noinspection JSIgnoredPromiseFromCall
- TestBed.configureTestingModule({
- providers: [
- I18nService,
- PathHelperService,
- States,
- ApiV3Service,
- CurrentProjectService,
- GlobalSearchService,
- ],
- })
- .compileComponents()
- .then(() => {
- CurrentProject = TestBed.inject(CurrentProjectService);
- service = TestBed.inject(GlobalSearchService);
- });
- }));
-
- describe('outside a project', () => {
- beforeEach(() => {
- CurrentProjectSpy = spyOnProperty(CurrentProject, 'path', 'get').and.returnValue(null);
- });
-
- it('searchPath returns a correct path', () => {
- service.searchTerm = 'hello';
- expect(service.searchPath()).toEqual('/search?q=hello&work_packages=1');
- });
-
- it('searchPath encodes the search term', () => {
- service.searchTerm = '<%';
- expect(service.searchPath()).toEqual('/search?q=%3C%25&work_packages=1');
- });
-
- it('searchPath entails the current tab', () => {
- service.currentTab = 'wiki_pages';
- expect(service.searchPath()).toEqual('/search?q=&wiki_pages=1');
- });
-
- it('when currentTab is "all" searchPath does not add it as a params key', () => {
- service.currentTab = 'all';
- expect(service.searchPath()).toEqual('/search?q=');
- });
- });
-
- describe('within a project', () => {
- beforeEach(() => {
- CurrentProjectSpy = spyOnProperty(CurrentProject, 'path', 'get')
- .and
- .returnValue('/projects/myproject');
- });
-
- it('returns correct path containing the project', () => {
- expect(service.searchPath()).toEqual('/projects/myproject/search?q=&work_packages=1');
- });
-
- it('returns correct path containing the project scope', () => {
- service.projectScope = 'current_project';
- expect(service.searchPath()).toEqual('/projects/myproject/search?q=&work_packages=1&scope=current_project');
- });
- });
-});
diff --git a/frontend/src/app/core/global_search/services/global-search.service.ts b/frontend/src/app/core/global_search/services/global-search.service.ts
index 5a838f0767c..de9eb6250d4 100644
--- a/frontend/src/app/core/global_search/services/global-search.service.ts
+++ b/frontend/src/app/core/global_search/services/global-search.service.ts
@@ -27,7 +27,6 @@
//++
import { Injectable, Injector } from '@angular/core';
-import { BehaviorSubject } from 'rxjs';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { CurrentProjectService } from 'core-app/core/current-project/current-project.service';
@@ -35,135 +34,33 @@ import { PathHelperService } from 'core-app/core/path-helper/path-helper.service
@Injectable()
export class GlobalSearchService {
- private _searchTerm = new BehaviorSubject<string>('');
-
- public searchTerm$ = this._searchTerm.asObservable();
-
- // Default selected tab is Work Packages
- private _currentTab = new BehaviorSubject<any>('work_packages');
-
- public currentTab$ = this._currentTab.asObservable();
-
- // Default project scope is "this project and all subprojects"
- private _projectScope = new BehaviorSubject<any>('');
-
- public projectScope$ = this._projectScope.asObservable();
-
- private _tabs = new BehaviorSubject<any>([]);
-
- public tabs$ = this._tabs.asObservable();
-
- // Sometimes we need to be able to hide the search results altogether, i.e. while expecting a full page reload.
- private _resultsHidden = new BehaviorSubject<any>(false);
-
- public resultsHidden$ = this._resultsHidden.asObservable();
-
constructor(
protected I18n:I18nService,
protected injector:Injector,
protected PathHelper:PathHelperService,
protected currentProjectService:CurrentProjectService,
) {
- this.initialize();
}
- private initialize():void {
- const initialData = this.loadGonData();
- if (initialData) {
- if (initialData.available_search_types) {
- this._tabs.next(initialData.available_search_types);
- }
- if (initialData.search_term) {
- this._searchTerm.next(initialData.search_term);
- }
- if (initialData.current_tab) {
- this._currentTab.next(initialData.current_tab);
- }
-
- if (initialData.project_scope) {
- this._projectScope.next(initialData.project_scope);
- } else if (!this.currentProjectService.path) {
- this._projectScope.next('all');
- }
- }
- }
-
- private loadGonData():{ available_search_types:string[],
- search_term:string,
- project_scope:string,
- current_tab:string }|null {
- try {
- return (window as any).gon.global_search;
- } catch (e) {
- return null;
- }
- }
-
- public submitSearch():void {
- window.location.href = this.searchPath();
+ public submitSearch(query:string, scope:string):void {
+ const path = this.searchPath(scope);
+ const params = this.searchQueryParams(query, scope);
+ window.location.href = `${path}?${params}`;
}
- public searchPath() {
+ public searchPath(scope:string) {
let searchPath:string = this.PathHelper.staticBase;
- if (this.currentProjectService.path && this.projectScope !== 'all') {
+ if (this.currentProjectService.path && scope !== 'all') {
searchPath = this.currentProjectService.path;
}
- searchPath += `/search?${this.searchQueryParams()}`;
- return searchPath;
- }
-
- public set searchTerm(searchTerm:string) {
- this._searchTerm.next(searchTerm);
- }
-
- public get searchTerm():string {
- return this._searchTerm.value;
+ return `${searchPath}/search`;
}
- public get tabs():string {
- return this._tabs.value;
- }
-
- public get currentTab():string {
- return this._currentTab.value;
- }
-
- public set currentTab(tab:string) {
- this._currentTab.next(tab);
- }
-
- public get projectScope():string {
- return this._projectScope.value;
- }
-
- public set projectScope(value:string) {
- this._projectScope.next(value);
- }
-
- public get resultsHidden():boolean {
- return this._resultsHidden.value;
- }
-
- public set resultsHidden(value:boolean) {
- this._resultsHidden.next(value);
- }
-
- private searchQueryParams():string {
- let params:string;
-
- params = `q=${encodeURIComponent(this.searchTerm)}`;
-
- if (this.currentTab.length > 0 && this.currentTab !== 'all') {
- params = `${params}&filter=${this.currentTab}`;
- }
- if (this.projectScope.length > 0) {
- params = `${params}&scope=${this.projectScope}`;
- }
-
- return params;
- }
+ private searchQueryParams(query:string, scope:string):string {
+ const params = new URLSearchParams(window.location.search);
+ params.set('q', query);
+ params.set('scope', scope);
- public isAfterSearch():boolean {
- return (jQuery('body.controller-search').length > 0);
+ return params.toString();
}
}