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

op-autocompleter appears broken

Benjamin Bädorf35b76c9a6266
diff --git a/frontend/src/app/features/invite-user-modal/invite-user-modal-augment.service.ts b/frontend/src/app/features/invite-user-modal/invite-user-modal-augment.service.ts
index 219a0ca4d6c..986edd490e4 100644
--- a/frontend/src/app/features/invite-user-modal/invite-user-modal-augment.service.ts
+++ b/frontend/src/app/features/invite-user-modal/invite-user-modal-augment.service.ts
@@ -41,10 +41,11 @@ const attributeSelector = '[invite-user-modal-augment]';
*/
@Injectable({ providedIn: 'root' })
export class OpInviteUserModalAugmentService {
- constructor(@Inject(DOCUMENT) protected documentElement:Document,
+ constructor(
+ @Inject(DOCUMENT) protected documentElement:Document,
protected opModalService:OpModalService,
- protected currentProjectService:CurrentProjectService) {
- }
+ protected currentProjectService:CurrentProjectService,
+ ) { }
/**
* Create initial listeners for Rails-rendered modals
diff --git a/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.html b/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.html
index 9eacb98d315..5eaf07cd0d0 100644
--- a/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.html
+++ b/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.html
@@ -10,7 +10,7 @@
required
>
<op-project-autocompleter
- [opFormBinding]="projectControl"
+ formControlName="project"
slot="input"
[APIFilters]="APIFiltersForProjects"
[mapResultsFn]="projectFilterFn.bind(this)"
diff --git a/frontend/src/app/shared/components/autocompleter/openproject-autocompleter.module.ts b/frontend/src/app/shared/components/autocompleter/openproject-autocompleter.module.ts
index b2630fb64a4..e544ce4ece7 100644
--- a/frontend/src/app/shared/components/autocompleter/openproject-autocompleter.module.ts
+++ b/frontend/src/app/shared/components/autocompleter/openproject-autocompleter.module.ts
@@ -17,7 +17,7 @@ import { OpAutocompleterLabelTemplateDirective } from 'core-app/shared/component
import { OpAutocompleterHeaderTemplateDirective } from 'core-app/shared/components/autocompleter/op-autocompleter/directives/op-autocompleter-header-template.directive';
import { CreateAutocompleterComponent } from 'core-app/shared/components/autocompleter/create-autocompleter/create-autocompleter.component';
import { OpAutocompleterFooterTemplateDirective } from 'core-app/shared/components/autocompleter/autocompleter-footer-template/op-autocompleter-footer-template.directive';
-import { FormsModule } from '@angular/forms';
+import { FormsModule, ReactiveFormsModule } from '@angular/forms';
export const OPENPROJECT_AUTOCOMPLETE_COMPONENTS = [
CreateAutocompleterComponent,
@@ -42,6 +42,7 @@ export const OPENPROJECT_AUTOCOMPLETE_COMPONENTS = [
NgSelectModule,
DragulaModule,
FormsModule,
+ ReactiveFormsModule,
DynamicModule.withComponents(OPENPROJECT_AUTOCOMPLETE_COMPONENTS),
],
diff --git a/frontend/src/app/shared/components/autocompleter/project-autocompleter/flatten-project-tree.ts b/frontend/src/app/shared/components/autocompleter/project-autocompleter/flatten-project-tree.ts
new file mode 100644
index 00000000000..4410dad603f
--- /dev/null
+++ b/frontend/src/app/shared/components/autocompleter/project-autocompleter/flatten-project-tree.ts
@@ -0,0 +1,22 @@
+import { IProjectAutocompleteItem, IProjectAutocompleteItemTree } from './project-autocomplete-item';
+
+export const flattenProjectTree = (
+ projectTreeItems:IProjectAutocompleteItemTree[],
+ depth = 0,
+):IProjectAutocompleteItem[] => projectTreeItems.reduce(
+ (fullList, projectTreeItem) => {
+ return [
+ ...fullList,
+ {
+ ...projectTreeItem,
+ numberOfAncestors: depth,
+ // The actual list of ancestors does not matter anymore from this point forward,
+ // but to keep typing straightforward for consumers of this component that use mapResultsFn,
+ // it is marked as required in the interface.
+ ancestors: [],
+ },
+ ...flattenProjectTree(projectTreeItem.children, depth + 1),
+ ];
+ },
+ [],
+);
diff --git a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html
index 45ba6eab8b4..f81dd76a78e 100644
--- a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html
+++ b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.html
@@ -1,20 +1,20 @@
<op-autocompleter
[items]="requests.output$ | async"
- bindLabel="name"
- bindValue="id"
- [ngModel]="initialSelection"
- [virtualScroll]="true"
+ [(ngModel)]="value"
[trackByFn]="projectTracker"
[typeahead]="requests.input$"
- (focus)="onFocus()"
- (change)="onModelChange($event)"
- [appendTo]="appendTo"
- [multiple]="multiple"
+
+ placeholder="My placeholder"
+ notFoundText="Nothing found"
+
+ bindLabel="name"
+ bindValue="id"
>
<ng-template
op-autocompleter-label-tmp
let-item="item"
>
+ This should show if it is actually working, no?
{{ item.name }}
</ng-template>
@@ -38,13 +38,4 @@
>{{ text.noInviteRights }}</div>
</div>
</ng-template>
-
- <ng-template
- op-autocompleter-notfound-tmp
- let-searchTerm="searchTerm"
- >
- <div class="ng-option disabled">
- {{ text.noResultsFound }}
- </div>
- </ng-template>
</op-autocompleter>
diff --git a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts
index 4526a5e4611..0c7cf48935c 100644
--- a/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts
+++ b/frontend/src/app/shared/components/autocompleter/project-autocompleter/project-autocompleter.component.ts
@@ -27,8 +27,14 @@
//++
import {
- Component, ElementRef, EventEmitter, Injector, Input, OnInit, Output, ViewChild,
+ Component,
+ ElementRef,
+ Injector,
+ Input,
+ OnInit,
+ forwardRef,
} from '@angular/core';
+import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { HalResourceService } from 'core-app/features/hal/services/hal-resource.service';
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
@@ -39,8 +45,6 @@ import {
errorNotificationHandler,
} from 'core-app/shared/helpers/rxjs/debounced-input-switchmap';
import { HalResourceNotificationService } from 'core-app/features/hal/services/hal-resource-notification.service';
-import { NgSelectComponent } from '@ng-select/ng-select';
-import { ProjectResource } from 'core-app/features/hal/resources/project-resource';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { DatasetInputs } from 'core-app/shared/components/dataset-inputs.decorator';
import { ApiV3ListFilter, listParamsString } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
@@ -49,46 +53,28 @@ import { IHALCollection } from 'core-app/core/apiv3/types/hal-collection.type';
import { IProject } from 'core-app/core/state/projects/project.model';
import { HttpClient } from '@angular/common/http';
+import { IProjectAutocompleteItem } from './project-autocomplete-item';
import { buildTree } from './insert-in-list';
import { recursiveSort } from './recursive-sort';
-import { IProjectAutocompleteItem, IProjectAutocompleteItemTree } from './project-autocomplete-item';
+import { flattenProjectTree } from './flatten-project-tree';
export const projectsAutocompleterSelector = 'op-project-autocompleter';
-const flattenProjectTree = (
- projectTreeItems:IProjectAutocompleteItemTree[],
- depth = 0,
-):IProjectAutocompleteItem[] => projectTreeItems.reduce(
- (fullList, projectTreeItem, i) => {
- return [
- ...fullList,
- {
- ...projectTreeItem,
- numberOfAncestors: depth,
- // The actual list of ancestors does not matter anymore from this point forward,
- // but to keep typing straightforward for consumers of this component that use mapResultsFn,
- // it is marked as required in the interface.
- ancestors: [],
- },
- ...flattenProjectTree(projectTreeItem.children, depth + 1),
- ];
- },
- [],
-);
-
@DatasetInputs
@Component({
templateUrl: './project-autocompleter.component.html',
selector: projectsAutocompleterSelector,
+ providers: [{
+ provide: NG_VALUE_ACCESSOR,
+ useExisting: forwardRef(() => ProjectAutocompleterComponent),
+ multi: true,
+ }],
})
-export class ProjectAutocompleterComponent implements OnInit {
- projectTracker = (item:any) => item.href || item.id;
-
- @ViewChild(NgSelectComponent, { static: true }) public ngSelectComponent:NgSelectComponent;
-
- @Output() public onChange = new EventEmitter<IProjectAutocompleteItem>();
-
- @Input() public clearAfterSelection = false;
+export class ProjectAutocompleterComponent implements OnInit, ControlValueAccessor {
+ projectTracker = (item:IProjectAutocompleteItem) => {
+ console.log(item);
+ return item.href || item.id;
+ }
// Load all projects as default
@Input() public url:string = this.apiV3Service.projects.path;
@@ -99,10 +85,11 @@ export class ProjectAutocompleterComponent implements OnInit {
@Input() public multiple = false;
- @Input() public initialSelection:number|null = null;
-
@Input() public APIFilters:ApiV3ListFilter[] = [];
+ // This function maps the API results to the internally used IProjectAutocompleteItems.
+ // By default it does not do much, but it is overwritable so additional filtering or
+ // transforming can be done on the API result set.
@Input()
public mapResultsFn:(projects:IProject[]) => IProjectAutocompleteItem[] = (projects) => projects.map((project) => ({
id: project.id,
@@ -114,8 +101,17 @@ export class ProjectAutocompleterComponent implements OnInit {
children: [],
}));
- // Update an input field after changing, used when externally loaded
- private updateInputField:HTMLInputElement|undefined;
+ @Input('value') public _value = '';
+
+ get value():string {
+ return this._value;
+ }
+
+ set value(value:string) {
+ this._value = value;
+ this.onChange(value);
+ this.onTouched(value);
+ }
/** Keep a switchmap for search term and loading state */
public requests = new DebouncedRequestSwitchmap<string, IProjectAutocompleteItem>(
@@ -134,31 +130,10 @@ export class ProjectAutocompleterComponent implements OnInit {
readonly injector:Injector,
) { }
- ngOnInit() { }
-
- public onFocus() {
- if (!this.requests.lastRequestedValue) {
- this.requests.input$.next('');
- }
- }
-
- public onModelChange(project:any) {
- if (project) {
- this.onChange.emit(project);
- this.requests.input$.next('');
-
- if (this.clearAfterSelection) {
- this.ngSelectComponent.clearItem(project);
- }
-
- if (this.updateInputField) {
- if (this.multiple) {
- this.updateInputField.value = project.map((u:ProjectResource) => u.id);
- } else {
- this.updateInputField.value = project.id;
- }
- }
- }
+ ngOnInit() {
+ this.requests.output$.subscribe((projects) => {
+ console.log('new projects', projects);
+ })
}
protected getAvailableProjects(searchTerm:string):Observable<IProjectAutocompleteItem[]> {
@@ -193,13 +168,22 @@ export class ProjectAutocompleterComponent implements OnInit {
map((projects) => buildTree(projects)),
map((projects) => recursiveSort(projects)),
map((projectTreeItems) => flattenProjectTree(projectTreeItems)),
- );
+ );
+ }
+
+ writeValue(value:string) {
+ this.value = value;
+ }
+
+ onChange = (_:string):void => {};
+
+ onTouched = (_:string):void => {};
+
+ registerOnChange(fn:(_:string) => void):void {
+ this.onChange = fn;
}
- private setInitialSelection() {
- if (this.updateInputField) {
- const id = parseInt(this.updateInputField.value);
- this.initialSelection = isNaN(id) ? null : id;
- }
+ registerOnTouched(fn:(_:string) => void):void {
+ this.onTouched = fn;
}
}
diff --git a/frontend/src/app/shared/components/dynamic-forms/dynamic-forms.module.ts b/frontend/src/app/shared/components/dynamic-forms/dynamic-forms.module.ts
index baf85a1b89b..5eb40ca1b41 100644
--- a/frontend/src/app/shared/components/dynamic-forms/dynamic-forms.module.ts
+++ b/frontend/src/app/shared/components/dynamic-forms/dynamic-forms.module.ts
@@ -32,6 +32,7 @@ import { OPSharedModule } from 'core-app/shared/shared.module';
{ name: 'textInput', component: TextInputComponent },
{ name: 'dateInput', component: DateInputComponent },
{ name: 'selectInput', component: SelectInputComponent },
+ { name: 'projectInput', component: SelectInputComponent },
{ name: 'selectProjectStatusInput', component: SelectProjectStatusInputComponent },
{ name: 'formattableInput', component: FormattableTextareaInputComponent },
],
diff --git a/frontend/src/app/shared/components/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts b/frontend/src/app/shared/components/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts
index f5ba8ff63f6..710e0fae8c3 100644
--- a/frontend/src/app/shared/components/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts
+++ b/frontend/src/app/shared/components/dynamic-forms/services/dynamic-fields/dynamic-fields.service.ts
@@ -90,7 +90,31 @@ export class DynamicFieldsService {
},
useForFields: [
'Priority', 'Status', 'Type', 'User', 'Version', 'TimeEntriesActivity',
- 'Category', 'CustomOption', 'Project',
+ 'Category', 'CustomOption',
+ ],
+ },
+ {
+ config: {
+ type: 'projectInput',
+ defaultValue: this.selectDefaultValue,
+ templateOptions: {
+ locale: this.I18n.locale,
+ bindLabel: 'name',
+ searchable: true,
+ virtualScroll: true,
+ clearOnBackspace: false,
+ clearSearchOnAdd: false,
+ hideSelected: false,
+ text: {
+ add_new_action: this.I18n.t('js.label_create'),
+ },
+ },
+ expressionProperties: {
+ 'templateOptions.clearable': (model:any, formState:any, field:FormlyFieldConfig) => !field.templateOptions?.required,
+ },
+ },
+ useForFields: [
+ 'Project',
],
},
{
diff --git a/frontend/src/app/shared/components/dynamic-forms/typings.d.ts b/frontend/src/app/shared/components/dynamic-forms/typings.d.ts
index 6e805685d18..987e01762bf 100644
--- a/frontend/src/app/shared/components/dynamic-forms/typings.d.ts
+++ b/frontend/src/app/shared/components/dynamic-forms/typings.d.ts
@@ -29,7 +29,7 @@ export interface IOPFormlyTemplateOptions extends FormlyTemplateOptions {
}
type OPInputType = 'formattableInput'|'selectInput'|'textInput'|'integerInput'|
-'booleanInput'|'dateInput'|'formly-group'|'selectProjectStatusInput';
+'booleanInput'|'dateInput'|'formly-group'|'projectInput'|'selectProjectStatusInput';
export interface IOPDynamicInputTypeSettings {
config:IOPFormlyFieldSettings,