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

[35354] Harmonize work package autocompleters (replacing global search box) (#9289)

Behrokh Satarnejad06cd178f3dc6
diff --git a/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.html b/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.html
index e6dea35901f..0a8f0890c8e 100644
--- a/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.html
+++ b/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.html
@@ -19,6 +19,7 @@
[openOnEnter]="openOnEnter"
[disabled]="disabled"
[name]="name"
+ [id]="id"
bindLabel="name"
[bindValue]="bindValue"
[markFirst]="markFirst"
@@ -55,6 +56,7 @@
(focus)="focused($event)"
(clear)="cleared($event)"
(keydown)="keydowned($event)"
+ (search)="searched($event)"
(scroll)="scrolled($event)"
(scrollToEnd)="scrolledToEnd($event)"
(add)="added($event)"
@@ -66,7 +68,8 @@
</ng-container>
</ng-template>
- <ng-template ng-label-tmp let-item="item">
+
+ <ng-template ng-label-tmp let-item="item" *ngIf="labelRequired">
<ng-container [ngTemplateOutlet]="labelTemplate ? labelTemplate : defaultLabel" [ngTemplateOutletContext]="{$implicit:item}">
</ng-container>
</ng-template>
@@ -91,7 +94,7 @@
<div class="op-autocompleter--option-wrapper">
- <op-principal
+ <op-principal *ngIf="item.author && item.author.href"
data-class-list="avatar op-autocompleter-author-avatar hidden-for-mobile"
[principal]="item.author"
[hide-name]="true"
@@ -139,7 +142,7 @@
<ng-template let-item #defaultLabel [ngSwitch]="resource">
<ng-container *ngSwitchCase="'work_packages'">
- {{item.type.name }} #{{ item.id }} {{ item.subject }}
+ {{item.type?.name }} #{{ item.id }} {{ item.subject }}
</ng-container>
<ng-container *ngSwitchDefault>
diff --git a/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.ts b/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.ts
index 321b609a86f..c05f90a6763 100644
--- a/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.ts
+++ b/frontend/src/app/modules/autocompleter/op-autocompleter/op-autocompleter.component.ts
@@ -36,15 +36,16 @@ export class OpAutocompleterComponent extends UntilDestroyedMixin implements Aft
@Input() public model?:any;
@Input() public searchKey?:string = '';
@Input() public defaulData?:boolean = false;
+ @Input() public focusDirectly?:boolean = true;
+ @Input() public labelRequired?:boolean = true;
@Input() public name?:string;
@Input() public required?:boolean = false;
@Input() public disabled?:string;
@Input() public searchable?:boolean = true;
@Input() public clearable?:boolean = true;
@Input() public addTag?:boolean = false;
+ @Input() public id?:string;
@Input() public configOptions?:IOPAutocompleterOptions[];
-
-
@Input() public clearSearchOnAdd?:boolean = true;
@Input() public classes?:string;
@Input() public multiple?:boolean = false;
@@ -135,7 +136,6 @@ export class OpAutocompleterComponent extends UntilDestroyedMixin implements Aft
}
ngAfterViewInit():void {
-
if (!this.ngSelectInstance) {
return;
}
@@ -157,9 +157,15 @@ export class OpAutocompleterComponent extends UntilDestroyedMixin implements Aft
distinctUntilChanged(),
switchMap(queryString => this.getOptionsFn(queryString))
));
+
+ if(this.openDirectly) {
+ this.ngSelectInstance.focus();
+ this.repositionDropdown();
+ }
+ else if (this.focusDirectly) {
+ this.ngSelectInstance.focus();
+ }
- this.ngSelectInstance.focus();
- this.repositionDropdown();
}, 25);
});
@@ -210,13 +216,15 @@ export class OpAutocompleterComponent extends UntilDestroyedMixin implements Aft
public changed(val:any) {
this.change.emit(val);
}
+ public searched(val:any) {
+ this.search.emit(val);
+ }
public blured(val:any) {
this.blur.emit(val);
}
public focused(val:any) {
-
this.focus.emit(val);
}
diff --git a/frontend/src/app/modules/global_search/input/global-search-input.component.html b/frontend/src/app/modules/global_search/input/global-search-input.component.html
index b1ce2e53e97..4414d8e3e6e 100644
--- a/frontend/src/app/modules/global_search/input/global-search-input.component.html
+++ b/frontend/src/app/modules/global_search/input/global-search-input.component.html
@@ -1,87 +1,95 @@
<div class="top-menu-search">
- <a *ngIf="expanded"
- (click)="toggleMobileSearch()"
- class="top-menu-search--back-button"
- href="#"
- title="{{text.close_search}}">
- <i class="icon-arrow-left1" aria-hidden="true"></i>
- </a>
- <ng-select #select
- [items]="requests.output$ | async"
- name="global-search-input"
- id="global-search-input"
- placeholder="{{text.search_dots}}"
- accesskey="4"
- [ngClass]="{'top-menu-search--input': true, '-markable': markable }"
- [(ngModel)]="selectedItem"
- [class.-expanded]="expanded"
- [openOnEnter]="false"
- [searchFn]="customSearchFn"
- [typeahead]="requests.input$"
- [loading]="requests.loading$ | async"
- (focus)="onFocus()"
- (blur)="onFocusOut()"
- (search)="search($event)"
- (open)="openCloseMenu(currentValue)"
- (close)="select.searchTerm = currentValue"
- (change)="followSelectedItem()"
- (keydown.enter)="onEnterBeforeResultsLoaded()"
- (keydown.escape)="blur()"
- (clear)="clearSearch()">
- <ng-template ng-option-tmp let-item="item" let-index="index" let-search="searchTerm">
- <div *ngIf="!item.id; else workPackageItemTemplate">
- <div tabindex="-1" class="global-search--option"(click)="followItem(item)">
- <div class="global-search--option-wrapper">
- <span class="global-search--search-term"> {{currentValue}} </span>
- <span class="global-search--project-scope" title="{{item.projectScope}}"> {{item.text}} ↵ </span>
- </div>
- </div>
+ <a *ngIf="expanded"
+ (click)="toggleMobileSearch()"
+ class="top-menu-search--back-button"
+ href="#"
+ title="{{text.close_search}}">
+ <i class="icon-arrow-left1" aria-hidden="true"></i>
+ </a>
+
+ <op-autocompleter #select
+ [hasDefaultContent]="true"
+ [defaulData]="false"
+ [name]="'global-search-input'"
+ [id]="'global-search-input'"
+ [placeholder]="text.search_dots"
+ [classes]="{'top-menu-search--input': true, '-markable': markable, '-expanded': expanded }"
+ [openOnEnter]="false"
+ [labelRequired]="false"
+ [focusDirectly]="isFocusedDirectly"
+ [model]="selectedItem"
+ [searchFn]="customSearchFn"
+ [loading]="isLoading"
+ (focus)="onFocus()"
+ (blur)="onFocusOut()"
+ (search)="search($event)"
+ (open)="openCloseMenu(currentValue)"
+ (close)="select.searchTerm = currentValue"
+ (change)="followSelectedItem()"
+ (keydown.enter)="onEnterBeforeResultsLoaded()"
+ (keydown.escape)="blur()"
+ (clear)="clearSearch()"
+ [filters]="autocompleterOptions.filters"
+ [resource]="autocompleterOptions.resource"
+ [searchKey]="autocompleterOptions.searchKey"
+ [getOptionsFn]="autocompleterOptions.getOptionsFn">
+
+ <ng-template op-autocompleter-option-tmp let-item let-index="index" let-search="searchTerm">
+ <div *ngIf="!item.id; else workPackageItemTemplate">
+ <div tabindex="-1" class="global-search--option"(click)="followItem(item)">
+ <div class="global-search--option-wrapper">
+ <span class="global-search--search-term"> {{currentValue}} </span>
+ <span class="global-search--project-scope" title="{{item.projectScope}}"> {{item.text}} ↵ </span>
</div>
- <ng-template #workPackageItemTemplate>
- <a tabindex="-1"
- class="global-search--option"
- [href]="wpPath(item.id)"
- (click)="redirectToWp(item.id, $event)"
- style="line-height: 1">
- <div class="global-search--option-wrapper">
- <op-principal
- [hide-name]="true"
- [principal]="item.author"
- class="hidden-for-mobile"
- ></op-principal>
+ </div>
+ </div>
+ <ng-template #workPackageItemTemplate>
+ <a tabindex="-1"
+ class="global-search--option"
+ [href]="wpPath(item.id)"
+ (click)="redirectToWp(item.id, $event)"
+ style="line-height: 1">
+ <div class="global-search--option-wrapper">
+ <op-principal
+ [hide-name]="true"
+ [principal]="item.author"
+ class="hidden-for-mobile"
+ ></op-principal>
- <span class="global-search--wp-subject">
- {{item.subject}}
- </span>
+ <span class="global-search--wp-subject">
+ {{item.subject}}
+ </span>
- <div class="global-search--wp-content">
- <span [textContent]="item.project"
- class="global-search--wp-project">
- </span>
+ <div class="global-search--wp-content">
+ <span [textContent]="item.project"
+ class="global-search--wp-project">
+ </span>
- <a class="global-search--wp-id"
- [ngClass]="uiStateLinkClass">
- #{{item.id}}
- </a>
- <span [textContent]="item.status"
- [ngClass]="statusHighlighting(item.statusId)"
- class="global-search--wp-status">
- </span>
+ <a class="global-search--wp-id"
+ [ngClass]="uiStateLinkClass">
+ #{{item.id}}
+ </a>
+ <span [textContent]="item.status"
+ [ngClass]="statusHighlighting(item.statusId)"
+ class="global-search--wp-status">
+ </span>
- </div>
- </div>
- </a>
- </ng-template>
+ </div>
+ </div>
+ </a>
</ng-template>
- </ng-select>
- <a #btn
- id="top-menu-search-button"
- class="top-menu-search--button search-form-normal"
- title="{{text.search}}"
- tabindex="0"
- role="button"
- [class.-input-focused]="expanded"
- (accessibleClick)="handleClick($event)">
- <op-icon icon-classes="icon5 icon-search ellipsis"></op-icon>
- </a>
+ </ng-template>
+ </op-autocompleter>
+
+
+ <a #btn
+ id="top-menu-search-button"
+ class="top-menu-search--button search-form-normal"
+ title="{{text.search}}"
+ tabindex="0"
+ role="button"
+ [class.-input-focused]="expanded"
+ (accessibleClick)="handleClick($event)">
+ <op-icon icon-classes="icon5 icon-search ellipsis"></op-icon>
+ </a>
</div>
diff --git a/frontend/src/app/modules/global_search/input/global-search-input.component.ts b/frontend/src/app/modules/global_search/input/global-search-input.component.ts
index c999d193ad7..01d55296823 100644
--- a/frontend/src/app/modules/global_search/input/global-search-input.component.ts
+++ b/frontend/src/app/modules/global_search/input/global-search-input.component.ts
@@ -35,7 +35,8 @@ import {
OnDestroy,
OnInit,
ViewChild,
- ViewEncapsulation
+ ViewEncapsulation,
+ NgZone, AfterViewInit
} from '@angular/core';
import { ContainHelpers } from 'core-app/modules/focus/contain-helpers';
import { I18nService } from 'core-app/modules/common/i18n/i18n.service';
@@ -54,6 +55,7 @@ import { LinkHandling } from "core-app/modules/common/link-handling/link-handlin
import { filter, map, take, tap } from "rxjs/operators";
import { APIV3Service } from "../../apiv3/api-v3.service";
import { HalResource } from 'core-app/modules/hal/resources/hal-resource';
+import { OpAutocompleterComponent } from "core-app/modules/autocompleter/op-autocompleter/op-autocompleter.component";
export const globalSearchSelector = 'global-search-input';
@@ -80,25 +82,28 @@ interface SearchOptionItem {
// Necessary because of ng-select
encapsulation: ViewEncapsulation.None
})
-export class GlobalSearchInputComponent implements OnInit, OnDestroy {
+export class GlobalSearchInputComponent implements AfterViewInit, OnDestroy {
@ViewChild('btn', { static: true }) btn:ElementRef;
- @ViewChild(NgSelectComponent, { static: true }) public ngSelectComponent:NgSelectComponent;
+ @ViewChild(OpAutocompleterComponent, { static: true }) public ngSelectComponent:OpAutocompleterComponent;
public expanded = false;
public markable = false;
+ public isLoading = false;
- /** Keep a switchmap for search term and loading state */
- public requests = new DebouncedRequestSwitchmap<string, SearchResultItem|SearchOptionItem>(
- (searchTerm:string) => this.autocompleteWorkPackages(searchTerm).pipe(
- tap(() => {
- setTimeout(() => this.setMarkedOption(), 50);
- })
- ),
- errorNotificationHandler(this.halNotification)
- );
+ getAutocompleterData = (query:string):Observable<any[]> => {
+ return this.autocompleteWorkPackages(query);
+ };
+
+ public autocompleterOptions = {
+ filters:[],
+ resource:'work_packages',
+ searchKey:'subjectOrId',
+ getOptionsFn: this.getAutocompleterData
+ };
/** Remember the current value */
public currentValue = '';
+ public isFocusedDirectly = (this.globalSearchService.searchTerm.length > 0);
/** Remember the item that best matches the query.
* That way, it will be highlighted (as we manually mark the selected item) and we can handle enter.
@@ -107,6 +112,7 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
private unregisterGlobalListener:Function|undefined;
+ private isInitialized :boolean = false;
public text:{ [key:string]:string } = {
all_projects: this.I18n.t('js.global_search.all_projects'),
current_project: this.I18n.t('js.global_search.current_project'),
@@ -125,17 +131,18 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
readonly currentProjectService:CurrentProjectService,
readonly deviceService:DeviceService,
readonly cdRef:ChangeDetectorRef,
- readonly halNotification:HalResourceNotificationService) {
+ readonly halNotification:HalResourceNotificationService,
+ readonly ngZone:NgZone) {
}
- ngOnInit() {
+ ngAfterViewInit():void {
// check searchterm on init, expand / collapse search bar and set correct classes
- this.ngSelectComponent.searchTerm = this.currentValue = this.globalSearchService.searchTerm;
- this.expanded = (this.ngSelectComponent.searchTerm.length > 0);
+ this.ngSelectComponent.ngSelectInstance.searchTerm = this.currentValue = this.globalSearchService.searchTerm;
+ this.expanded = (this.ngSelectComponent.ngSelectInstance.searchTerm.length > 0);
this.toggleTopMenuClass();
}
- ngOnDestroy() {
+ ngOnDestroy():void {
this.unregister();
}
@@ -151,8 +158,8 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
this.toggleMobileSearch();
// open ng-select menu on default
jQuery('.ng-input input').focus();
- } else if (this.ngSelectComponent.searchTerm.length === 0) {
- this.ngSelectComponent.focus();
+ } else if (this.ngSelectComponent.ngSelectInstance.searchTerm.length === 0) {
+ this.ngSelectComponent.ngSelectInstance.focus();
} else {
this.submitNonEmptySearch();
}
@@ -181,13 +188,14 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
}
public search($event:any) {
- this.currentValue = this.ngSelectComponent.searchTerm;
+ this.currentValue = this.ngSelectComponent.ngSelectInstance.searchTerm;
this.openCloseMenu($event.term);
}
// close menu when input field is empty
public openCloseMenu(searchedTerm:string) {
- this.ngSelectComponent.isOpen = (searchedTerm.trim().length > 0);
+
+ this.ngSelectComponent.ngSelectInstance.isOpen = (searchedTerm.trim().length > 0);
}
public onFocus() {
@@ -198,14 +206,15 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
public onFocusOut() {
if (!this.deviceService.isMobile) {
- this.expanded = (this.ngSelectComponent.searchTerm.length > 0);
- this.ngSelectComponent.isOpen = false;
+ this.expanded = (this.ngSelectComponent.ngSelectInstance.searchTerm !== null && this.ngSelectComponent.ngSelectInstance.searchTerm.length > 0);
+ this.ngSelectComponent.ngSelectInstance.isOpen = false;
+ this.selectedItem =null;
this.toggleTopMenuClass();
}
}
public clearSearch() {
- this.currentValue = this.ngSelectComponent.searchTerm = '';
+ this.currentValue = this.ngSelectComponent.ngSelectInstance.searchTerm = '';
this.openCloseMenu(this.currentValue);
}
@@ -213,17 +222,11 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
// in and then decide what to do. If a direct hit is present, follow that. Otherwise,
// go to the search in the current scope.
public onEnterBeforeResultsLoaded() {
- this.requests.loading$.pipe(
- filter(value => value === false),
- take(1)
- )
- .subscribe(() => {
- if (this.selectedItem) {
- this.followSelectedItem();
- } else {
- this.searchInScope(this.currentScope);
- }
- });
+ if (this.selectedItem) {
+ this.followSelectedItem();
+ } else {
+ this.searchInScope(this.currentScope);
+ }
}
public statusHighlighting(statusId:string) {
@@ -269,13 +272,18 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
const hashFreeQuery = this.queryWithoutHash(query);
+
+ this.isLoading = true
return this
.fetchSearchResults(hashFreeQuery, hashFreeQuery !== query)
.get()
.pipe(
map((collection) => {
return this.searchResultsToOptions(collection.elements, hashFreeQuery);
- })
+ }),
+ tap(() => {
+ this.isLoading = false;
+ this.setMarkedOption();})
);
}
@@ -304,9 +312,9 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
statusId: wp.status.idFromLink,
href: wp.href,
project: wp.project.name,
- author: wp.author
+ author: wp.author,
+ type: wp.type
} as SearchResultItem;
-
// If we have a direct hit, we choose it to be the selected element.
if (query === wp.id!.toString()) {
this.selectedItem = item;
@@ -371,7 +379,7 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
*/
private setMarkedOption() {
this.markable = true;
- this.ngSelectComponent.itemsList.markItem(this.ngSelectComponent.itemsList.selectedItems[0]);
+ this.ngSelectComponent.ngSelectInstance.itemsList.markItem(this.ngSelectComponent.ngSelectInstance.itemsList.selectedItems[0]);
this.cdRef.detectChanges();
}
@@ -404,14 +412,14 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
public submitNonEmptySearch(forcePageLoad = false) {
this.globalSearchService.searchTerm = this.currentValue;
if (this.currentValue.length > 0) {
- this.ngSelectComponent.close();
+ 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.ngSelectComponent.searchTerm}`,
+ `${I18n.t('global_search.search')}: ${this.ngSelectComponent.ngSelectInstance.searchTerm}`,
this.globalSearchService.searchPath());
return;
@@ -421,7 +429,7 @@ export class GlobalSearchInputComponent implements OnInit, OnDestroy {
}
public blur() {
- this.ngSelectComponent.searchTerm = '';
+ this.ngSelectComponent.ngSelectInstance.searchTerm = '';
(<HTMLInputElement>document.activeElement).blur();
}
diff --git a/frontend/src/app/modules/global_search/openproject-global-search.module.ts b/frontend/src/app/modules/global_search/openproject-global-search.module.ts
index 654332a35a6..6b57652b972 100644
--- a/frontend/src/app/modules/global_search/openproject-global-search.module.ts
+++ b/frontend/src/app/modules/global_search/openproject-global-search.module.ts
@@ -35,11 +35,13 @@ import { GlobalSearchTabsComponent } from "core-app/modules/global_search/tabs/g
import { GlobalSearchTitleComponent } from "core-app/modules/global_search/title/global-search-title.component";
import { GlobalSearchService } from "core-app/modules/global_search/services/global-search.service";
import { GlobalSearchWorkPackagesEntryComponent } from "core-app/modules/global_search/global-search-work-packages-entry.component";
+import { OpenprojectAutocompleterModule } from "core-app/modules/autocompleter/openproject-autocompleter.module";
@NgModule({
imports: [
OpenprojectCommonModule,
- OpenprojectWorkPackagesModule
+ OpenprojectWorkPackagesModule,
+ OpenprojectAutocompleterModule
],
providers: [
GlobalSearchService,
diff --git a/spec/features/search_spec.rb b/spec/features/search_spec.rb
index e3040adee07..e851ceca129 100644
--- a/spec/features/search_spec.rb
+++ b/spec/features/search_spec.rb
@@ -270,9 +270,6 @@ describe 'Search', type: :feature, js: true, with_settings: { per_page_options:
table.expect_work_package_subject(work_packages[1].subject)
- # Expect that changing the advanced filters will not affect the global search input.
- expect(global_search.input.value).to eq query
-
# Expect that a fresh global search will reset the advanced filters, i.e. that they are closed
global_search.search work_packages[6].subject, submit: true