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

Change mapResultsFn interfacing

Benjamin Bädorfb22912fda577
diff --git a/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.ts b/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.ts
index 67cf09a023b..a3e0497f4a3 100644
--- a/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.ts
+++ b/frontend/src/app/features/invite-user-modal/project-selection/project-selection.component.ts
@@ -18,7 +18,6 @@ import { IOpOptionListOption } from 'core-app/shared/components/option-list/opti
import { ProjectResource } from 'core-app/features/hal/resources/project-resource';
import { PrincipalType } from '../invite-user.component';
import { ProjectAllowedValidator } from './project-allowed.validator';
-import { IProject } from 'core-app/core/state/projects/project.model';
import { map } from 'rxjs/operators';
import { CapabilityResource } from 'core-app/features/hal/resources/capability-resource';
import { IProjectAutocompleteItem } from 'core-app/shared/components/autocompleter/project-autocompleter/project-autocomplete-item';
@@ -139,15 +138,12 @@ export class ProjectSelectionComponent implements OnInit {
APIFiltersForProjects = [['active', '=', true]];
- projectFilterFn(projects:IProject[]):IProjectAutocompleteItem[] {
- const mapped = projects.map((project:IProject) => ({
- id: project.id,
- name: project.name,
- href: project._links.self.href,
- ancestors: project._links.ancestors,
+ projectFilterFn(projects:IProjectAutocompleteItem[]):IProjectAutocompleteItem[] {
+ const mapped = projects.map((project) => ({
+ ...project,
disabled: !this.projectInviteCapabilities.find((cap) => cap.context.id === project.id),
disabledReason: this.text.project.noInviteRights,
- }) as IProjectAutocompleteItem);
+ }));
mapped.sort(
(a, b) => (a.disabled ? 1 : 0) - (b.disabled ? 1 : 0),
diff --git a/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.html b/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.html
index 1b7c5f6cd09..6d49407983c 100644
--- a/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.html
+++ b/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.html
@@ -9,31 +9,16 @@
<span [textContent]="text.add_setting"></span>
</button>
- <op-autocompleter
- *ngIf="active"
- [placeholder]="text.please_select"
- (change)="selectProject($event)"
- (keydown.escape)="active = false"
- [fetchDataDirectly]="true"
- [filters]="autocompleterOptions.filters"
- [resource]="autocompleterOptions.resource"
- [getOptionsFn]="autocompleterOptions.getOptionsFn"
- appendTo="body"
- >
- <ng-template op-autocompleter-option-tmp let-item let-index="index" let-search="search">
- <div
- *ngIf="item"
- class="ng-option-label"
- >
- <!--Selectable option-->
- <div [ngOptionHighlight]="search">{{ item.name }}</div>
+ <op-project-autocompleter
+ *ngIf="active"
+
+ (change)="selectProject($event)"
+ (keydown.escape)="active = false"
+
+ [APIFilters]="APIFilters"
+ [mapResultsFn]="this.mapProjectsFn.bind(this)"
+ appendTo="body"
- <!-- Project already selected -->
- <div
- *ngIf="item.disabled"
- class="ellipsis"
- >{{ text.already_selected }}</div>
- </div>
- </ng-template>
- </op-autocompleter>
+ [placeholder]="text.please_select"
+ ></op-project-autocompleter>
</div>
diff --git a/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.ts b/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.ts
index d158ff4a80f..4658f44ec4d 100644
--- a/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.ts
+++ b/frontend/src/app/features/user-preferences/notifications-settings/inline-create/notification-setting-inline-create.component.ts
@@ -8,12 +8,8 @@ import {
import { FormArray } from '@angular/forms';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { Observable } from 'rxjs';
-import { map } from 'rxjs/operators';
-import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
-import { ApiV3FilterBuilder } from 'core-app/shared/helpers/api-v3/api-v3-filter-builder';
import { HalSourceLink } from 'core-app/features/hal/resources/hal-resource';
-import { UserPreferencesQuery } from 'core-app/features/user-preferences/state/user-preferences.query';
-import { UserPreferencesService } from 'core-app/features/user-preferences/state/user-preferences.service';
+import { IProjectAutocompleteItem } from 'core-app/shared/components/autocompleter/project-autocompleter/project-autocomplete-item';
export interface NotificationSettingProjectOption {
name:string;
@@ -42,44 +38,27 @@ export class NotificationSettingInlineCreateComponent {
already_selected: this.I18n.t('js.notifications.settings.project_specific.already_selected'),
};
- public autocompleterOptions = {
- filters: [],
- resource: 'default',
- getOptionsFn: (query:string):Observable<unknown[]> => this.autocomplete(query),
- };
+ public get APIFilters() {
+ return [
+ ['visible', '=', [this.userId]],
+ ];
+ }
constructor(
private I18n:I18nService,
- private apiV3Service:ApiV3Service,
- ) {
- }
+ ) { }
selectProject($event:NotificationSettingProjectOption):void {
this.selected.emit({ title: $event.name, href: $event.href });
this.active = false;
}
- private autocomplete(term:string|null):Observable<NotificationSettingProjectOption[]> {
- const filters = new ApiV3FilterBuilder()
- .add('visible', '=', [this.userId]);
-
- if (term) {
- filters.add('name_and_identifier', '~', [term]);
- }
-
- return this
- .apiV3Service
- .projects
- .filtered(filters, { pageSize: '-1' })
- .get()
- .pipe(
- map((collection) => collection.elements.map((project) => ({
- href: project.href || '',
- name: (project.parent ? '... ' : '') + project.name,
- disabled: !!this.settings.controls.find(
- (projectSetting) => (projectSetting.get('project')!.value as NotificationSettingProjectOption).href === project.href,
- ),
- }))),
- );
+ public mapProjectsFn(projects:IProjectAutocompleteItem[]):IProjectAutocompleteItem[] {
+ return projects.map((project) => ({
+ ...project,
+ disabled: !!this.settings.controls.find(
+ (projectSetting) => (projectSetting.get('project')!.value as NotificationSettingProjectOption).href === project.href,
+ ),
+ }));
}
}
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 5e8b6ee3eb6..55b8029ff16 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
@@ -3,10 +3,7 @@
[model]="value"
[trackByFn]="projectTracker"
- (change)="onChange($event)"
-
- bindLabel="name"
- bindValue="id"
+ (change)="writeValue($event)"
>
<ng-template
op-autocompleter-label-tmp
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 54b1c30a21c..b1103c030c2 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
@@ -33,13 +33,15 @@ import {
Input,
OnInit,
forwardRef,
+ EventEmitter,
+ Output,
} 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';
import { Observable } from 'rxjs';
-import { map, tap } from 'rxjs/operators';
+import { map } from 'rxjs/operators';
import { HalResourceNotificationService } from 'core-app/features/hal/services/hal-resource-notification.service';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { ApiV3ListFilter, listParamsString } from 'core-app/core/apiv3/paths/apiv3-list-resource.interface';
@@ -52,6 +54,7 @@ import { IProjectAutocompleteItem } from './project-autocomplete-item';
import { buildTree } from './insert-in-list';
import { recursiveSort } from './recursive-sort';
import { flattenProjectTree } from './flatten-project-tree';
+import { ID } from '@datorama/akita';
export const projectsAutocompleterSelector = 'op-project-autocompleter';
@@ -80,31 +83,29 @@ export class ProjectAutocompleterComponent implements OnInit, ControlValueAccess
@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.
+ // This function allows mapping of the results before they are fed to the tree
+ // structuring and destructuring algorithms used internally the this component
+ // to show the tree structure. 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,
- href: project._links.self.href,
- name: project.name,
- disabled: false,
- ancestors: project._links.ancestors,
- children: [],
- }));
-
- @Input('value') public _value = '';
-
- get value():string {
+ public mapResultsFn:(projects:IProjectAutocompleteItem[]) => IProjectAutocompleteItem[] = (projects) => projects;
+
+ @Input('value') public _value:ID|null = null;
+
+ get value():ID|null {
return this._value;
}
- set value(value:string) {
+ set value(value:ID|null) {
this._value = value;
this.onChange(value);
+ this.changeEmitter.emit(value);
this.onTouched(value);
}
+ @Output('change') changeEmitter = new EventEmitter();
+
constructor(
public elementRef:ElementRef,
protected halResourceService:HalResourceService,
@@ -145,6 +146,14 @@ export class ProjectAutocompleterComponent implements OnInit, ControlValueAccess
},
)
.pipe(
+ map((projects) => projects.map((project) => ({
+ id: project.id,
+ href: project._links.self.href,
+ name: project.name,
+ disabled: false,
+ ancestors: project._links.ancestors,
+ children: [],
+ }))),
map(this.mapResultsFn),
map((projects) => projects.sort((a, b) => a.ancestors.length - b.ancestors.length)),
map((projects) => buildTree(projects)),
@@ -153,23 +162,19 @@ export class ProjectAutocompleterComponent implements OnInit, ControlValueAccess
);
}
- change(val:any) {
- this.value = val;
- }
-
- writeValue(value:string) {
+ writeValue(value:ID|null) {
this.value = value;
}
- onChange = (_:string):void => {};
+ onChange = (_:ID|null):void => {};
- onTouched = (_:string):void => {};
+ onTouched = (_:ID|null):void => {};
- registerOnChange(fn:(_:string) => void):void {
+ registerOnChange(fn:(_:ID|null) => void):void {
this.onChange = fn;
}
- registerOnTouched(fn:(_:string) => void):void {
+ registerOnTouched(fn:(_:ID|null) => void):void {
this.onTouched = fn;
}
}