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

Make principal component use DataInputs decorator

Oliver Günther228a7aa4e276
diff --git a/app/helpers/angular_helper.rb b/app/helpers/angular_helper.rb
index ef691608a81..091709e7249 100644
--- a/app/helpers/angular_helper.rb
+++ b/app/helpers/angular_helper.rb
@@ -40,7 +40,9 @@ module AngularHelper
.transform_values(&:to_json)
options[:data] = options.fetch(:data, {}).merge(inputs)
- options[:class] ||= "#{options.fetch(:class, '')} op-angular-component"
+ options[:class] ||= [options[:class], 'op-angular-component']
+ .compact
+ .join(' ')
content_tag(component, '', options)
end
diff --git a/frontend/src/app/core/global_search/input/global-search-input.component.html b/frontend/src/app/core/global_search/input/global-search-input.component.html
index ea6145241e4..0dd24a4e60e 100644
--- a/frontend/src/app/core/global_search/input/global-search-input.component.html
+++ b/frontend/src/app/core/global_search/input/global-search-input.component.html
@@ -54,7 +54,7 @@
style="line-height: 1"
>
<op-principal
- [hide-name]="true"
+ [hideName]="true"
[principal]="item.author"
class="hidden-for-mobile global-search--principal"
></op-principal>
diff --git a/frontend/src/app/features/boards/board/board-actions/assignee/assignee-board-header.html b/frontend/src/app/features/boards/board/board-actions/assignee/assignee-board-header.html
index 21f2fd94544..688c6952cf9 100644
--- a/frontend/src/app/features/boards/board/board-actions/assignee/assignee-board-header.html
+++ b/frontend/src/app/features/boards/board/board-actions/assignee/assignee-board-header.html
@@ -2,7 +2,7 @@
<op-principal
*ngIf="user.id"
[principal]="user"
- [hide-name]="true"
+ [hideName]="true"
size="mini"
></op-principal>
<h2 class="editable-toolbar-title--fixed">
diff --git a/frontend/src/app/features/in-app-notifications/entry/in-app-notification-entry.component.html b/frontend/src/app/features/in-app-notifications/entry/in-app-notification-entry.component.html
index dd0cbc6e738..c88ba4c39c3 100644
--- a/frontend/src/app/features/in-app-notifications/entry/in-app-notification-entry.component.html
+++ b/frontend/src/app/features/in-app-notifications/entry/in-app-notification-entry.component.html
@@ -95,8 +95,8 @@
<op-principal
class="op-ian-item--actors"
[principal]="actors[0]"
- [hide-name]="false"
- [hide-avatar]="true"
+ [hideName]="false"
+ [hideAvatar]="true"
[link]="!isMobile()"
></op-principal>
<span
diff --git a/frontend/src/app/features/work-packages/components/wp-activity/revision/revision-activity.component.html b/frontend/src/app/features/work-packages/components/wp-activity/revision/revision-activity.component.html
index 1dc5af0851f..29d257687ce 100644
--- a/frontend/src/app/features/work-packages/components/wp-activity/revision/revision-activity.component.html
+++ b/frontend/src/app/features/work-packages/components/wp-activity/revision/revision-activity.component.html
@@ -17,18 +17,15 @@
</div>
<op-principal
- *ngIf="activity.author && userName"
- [attr.data-principal-id]="userId"
- [attr.data-principal-name]="userName"
- data-principal-type="user"
- data-hide-name="true"
+ *ngIf="activity.author"
+ [principal]="activity.author"
+ [hideName]="true"
></op-principal>
<op-principal
*ngIf="!activity.author && userName"
- [attr.data-principal-name]="userName"
- data-principal-type="user"
- data-hide-name="true"
+ [principal]="{ name: userName }"
+ [hideName]="true"
></op-principal>
<span class="user" *ngIf="userActive">
diff --git a/frontend/src/app/features/work-packages/components/wp-activity/user/user-activity.component.html b/frontend/src/app/features/work-packages/components/wp-activity/user/user-activity.component.html
index 5ed72e0cefc..64daaa2fa10 100644
--- a/frontend/src/app/features/work-packages/components/wp-activity/user/user-activity.component.html
+++ b/frontend/src/app/features/work-packages/components/wp-activity/user/user-activity.component.html
@@ -9,7 +9,7 @@
<op-principal
*ngIf="userName"
[principal]="user"
- [hide-name]="true"
+ [hideName]="true"
class="op-user-activity--avatar"
></op-principal>
diff --git a/frontend/src/app/features/work-packages/components/wp-card-view/wp-single-card/wp-single-card.component.html b/frontend/src/app/features/work-packages/components/wp-card-view/wp-single-card/wp-single-card.component.html
index a943b595add..c5a4dcc7dea 100644
--- a/frontend/src/app/features/work-packages/components/wp-card-view/wp-single-card/wp-single-card.component.html
+++ b/frontend/src/app/features/work-packages/components/wp-card-view/wp-single-card/wp-single-card.component.html
@@ -78,7 +78,7 @@
<op-principal
*ngIf="workPackage.assignee"
[principal]="workPackage.assignee"
- [hide-name]="true"
+ [hideName]="true"
size="mini"
class="op-wp-single-card--content-assignee"
data-qa-selector="op-wp-single-card--content-assignee"
diff --git a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/wp-watcher-entry.component.html b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/wp-watcher-entry.component.html
index eab9fdce422..500518e244e 100644
--- a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/wp-watcher-entry.component.html
+++ b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/wp-watcher-entry.component.html
@@ -2,9 +2,8 @@
<span class="op-wp-watcher--form form--selected-value">
<op-principal
[principal]="watcher"
- data-link="true"
- data-principal-type="user"
- data-size="avatar-mini"
+ [link]="true"
+ size="mini"
data-qa-selector="op-wp-watcher-name"
></op-principal>
</span>
diff --git a/frontend/src/app/shared/components/autocompleter/create-autocompleter/create-autocompleter.component.html b/frontend/src/app/shared/components/autocompleter/create-autocompleter/create-autocompleter.component.html
index 45f7c1a28ca..61d1809ddb7 100644
--- a/frontend/src/app/shared/components/autocompleter/create-autocompleter/create-autocompleter.component.html
+++ b/frontend/src/app/shared/components/autocompleter/create-autocompleter/create-autocompleter.component.html
@@ -26,7 +26,7 @@
<ng-template ng-option-tmp let-item="item" let-search="searchTerm">
<op-principal
*ngIf="isPrincipal(item)"
- [hide-name]="true"
+ [hideName]="true"
[principal]="item"
size="mini"
></op-principal>
diff --git a/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.html b/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.html
index bb7d38b854f..f49dd74f3a8 100644
--- a/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.html
+++ b/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.html
@@ -123,7 +123,7 @@
class="op-autocompleter--option-principal"
*ngIf="item.author && item.author.href"
[principal]="item.author"
- [hide-name]="true"
+ [hideName]="true"
></op-principal>
<span
class="op-autocompleter--wp-subject"
@@ -165,7 +165,7 @@
<op-principal
*ngIf="item && item.href"
[principal]="item"
- [hide-name]="true"
+ [hideName]="true"
size="mini"
></op-principal>
<span [ngOptionHighlight]="search">{{ item.name }}</span>
diff --git a/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter.component.html b/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter.component.html
index 4d9cf5a70bb..f55499d1051 100644
--- a/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter.component.html
+++ b/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter.component.html
@@ -14,7 +14,7 @@
<op-principal
*ngIf="item && item.href"
[principal]="item"
- [hide-name]="true"
+ [hideName]="true"
size="mini"
></op-principal>
{{ item.name }}
diff --git a/frontend/src/app/shared/components/grids/widgets/news/news.component.html b/frontend/src/app/shared/components/grids/widgets/news/news.component.html
index 04c214fd588..756a21048e1 100644
--- a/frontend/src/app/shared/components/grids/widgets/news/news.component.html
+++ b/frontend/src/app/shared/components/grids/widgets/news/news.component.html
@@ -18,7 +18,7 @@
<op-principal
*ngIf="news.author"
[principal]="news.author"
- [hide-name]="true"
+ [hideName]="true"
class="news-author-avatar hidden-for-mobile"
></op-principal>
<div>
diff --git a/frontend/src/app/shared/components/principal/principal-list.component.html b/frontend/src/app/shared/components/principal/principal-list.component.html
index 62f40883086..2f08999129d 100644
--- a/frontend/src/app/shared/components/principal/principal-list.component.html
+++ b/frontend/src/app/shared/components/principal/principal-list.component.html
@@ -1,7 +1,7 @@
<op-principal
class="op-principal-list--principal"
*ngFor="let principal of principals"
- [hide-name]="true"
+ [hideName]="true"
[principal]="principal"
[link]="false"
size="mini"
diff --git a/frontend/src/app/shared/components/principal/principal.component.ts b/frontend/src/app/shared/components/principal/principal.component.ts
index 362d9cff382..483a7620636 100644
--- a/frontend/src/app/shared/components/principal/principal.component.ts
+++ b/frontend/src/app/shared/components/principal/principal.component.ts
@@ -27,31 +27,46 @@
//++
import {
- Component, ElementRef, Input, OnInit,
+ ChangeDetectionStrategy,
+ Component,
+ ElementRef,
+ Input,
+ OnInit,
} from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
import { APIV3Service } from 'core-app/core/apiv3/api-v3.service';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
-import { AvatarSize, PrincipalRendererService } from './principal-renderer.service';
+import {
+ AvatarSize,
+ PrincipalRendererService,
+} from './principal-renderer.service';
import { PrincipalLike } from './principal-types';
-import { PrincipalHelper } from './principal-helper';
+import { DatasetInputs } from 'core-app/shared/components/dataset-inputs.decorator';
+import { PrincipalHelper } from 'core-app/shared/components/principal/principal-helper';
import PrincipalPluralType = PrincipalHelper.PrincipalPluralType;
+import PrincipalType = PrincipalHelper.PrincipalType;
export const principalSelector = 'op-principal';
+export interface PrincipalInput {
+ type:PrincipalType;
+ id:string;
+}
+
+@DatasetInputs
@Component({
template: '',
selector: principalSelector,
+ changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OpPrincipalComponent implements OnInit {
- /** If coming from angular, pass a principal resource if available */
@Input() principal:PrincipalLike;
- @Input('hide-avatar') hideAvatar = false;
+ @Input() hideAvatar = false;
- @Input('hide-name') hideName = false;
+ @Input() hideName = false;
@Input() link = true;
@@ -67,18 +82,8 @@ export class OpPrincipalComponent implements OnInit {
}
ngOnInit() {
- const element = this.elementRef.nativeElement;
-
- if (!this.principal) {
- this.principal = this.principalFromDataset(element);
- this.hideAvatar = element.dataset.hideAvatar === 'true';
- this.hideName = element.dataset.hideName === 'true';
- this.link = element.dataset.link === 'true';
- this.size = element.dataset.size ?? 'default';
- }
-
this.principalRenderer.render(
- element,
+ this.elementRef.nativeElement as HTMLElement,
this.principal,
{
hide: this.hideName,
@@ -90,18 +95,4 @@ export class OpPrincipalComponent implements OnInit {
},
);
}
-
- private principalFromDataset(element:HTMLElement) {
- const id = element.dataset.principalId!;
- const name = element.dataset.principalName!;
- const type = element.dataset.principalType;
- const plural = `${type}s` as PrincipalPluralType;
- const href = this.apiV3Service[plural].id(id).toString();
-
- return {
- id,
- name,
- href,
- };
- }
}
diff --git a/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb b/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb
index b458324a831..ef2dce23348 100644
--- a/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb
+++ b/modules/avatars/lib/open_project/avatars/patches/avatar_helper_patch.rb
@@ -21,6 +21,7 @@ require 'avatar_helper'
AvatarHelper.class_eval do
include ::GravatarImageTag
+ include ::AngularHelper
GravatarImageTag.configure do |c|
c.include_size_attributes = false
@@ -83,16 +84,20 @@ AvatarHelper.class_eval do
def build_principal_avatar_tag(user, options = {})
tag_options = merge_default_avatar_options(user, options)
- content_tag 'op-principal',
- '',
- class: tag_options[:class],
- data: {
- 'principal-id': user.id,
- 'principal-name': user.name,
- 'principal-type': API::V3::Principals::PrincipalType.for(user),
- 'size': tag_options[:size],
- 'hide-name': tag_options[:hide_name]
- }
+ principal_type = API::V3::Principals::PrincipalType.for(user)
+ principal = {
+ href: API::V3::Utilities::PathHelper::ApiV3Path.send(principal_type, user.id),
+ name: user.name,
+ id: user.id
+ }
+
+ angular_component_tag 'op-principal',
+ class: tag_options[:class],
+ inputs: {
+ principal: principal,
+ size: tag_options[:size],
+ hideName: tag_options[:hide_name]
+ }
end
def merge_default_avatar_options(user, options)
diff --git a/modules/avatars/spec/helpers/avatar_helper_spec.rb b/modules/avatars/spec/helpers/avatar_helper_spec.rb
index e9e7ece6095..81e9c2312fc 100644
--- a/modules/avatars/spec/helpers/avatar_helper_spec.rb
+++ b/modules/avatars/spec/helpers/avatar_helper_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
+ include AngularHelper
+
let(:user) { FactoryBot.build_stubbed(:user) }
let(:mail_digest) { Digest::MD5.hexdigest(user.mail) }
let(:avatar_stub) { FactoryBot.build_stubbed(:avatar_attachment) }
@@ -17,35 +19,30 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
before do
allow(Setting)
.to receive(:plugin_openproject_avatars)
- .and_return(plugin_settings)
+ .and_return(plugin_settings)
allow(user).to receive(:local_avatar_attachment).and_return avatar_stub
end
- def local_expected_user_avatar_tag(user)
- tag_options = { 'data-principal-id': user.id,
- 'data-principal-name': user.name,
- 'data-principal-type': 'user',
- 'data-hide-name': 'true',
- 'data-size': 'default' }
+ def expected_user_avatar_tag(user)
+ principal = {
+ href: "/api/v3/users/#{user.id}",
+ name: user.name,
+ id: user.id
+ }
- content_tag 'op-principal', '', tag_options
+ angular_component_tag 'op-principal',
+ inputs: {
+ principal: principal,
+ hideName: true,
+ size: 'default'
+ }
end
def local_expected_url(user)
user_avatar_url(user)
end
- def default_expected_user_avatar_tag(user)
- tag_options = { 'data-hide-name': 'true',
- 'data-principal-id': user.id,
- 'data-principal-name': user.name,
- 'data-principal-type': 'user',
- 'data-size': 'default'}
-
- content_tag 'op-principal', '', tag_options
- end
-
def gravatar_expected_url(digest, options = {})
ssl = !!options[:ssl]
@@ -64,13 +61,13 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
let(:enable_gravatars) { true }
let(:enable_local_avatars) { true }
it "should return the image attached to the user" do
- expect(helper.avatar(user)).to be_html_eql(local_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
it "should return the gravatar image if no image uploaded for the user" do
allow(user).to receive(:local_avatar_attachment).and_return nil
- expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
end
@@ -78,7 +75,7 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
let(:enable_gravatars) { false }
let(:enable_local_avatars) { true }
it "should return blank if image attached to the user but gravatars disabled" do
- expect(helper.avatar(user)).to be_html_eql(local_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
end
@@ -87,7 +84,7 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
let(:enable_local_avatars) { false }
it "should return blank" do
- expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
end
end
@@ -145,7 +142,7 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
context 'with http', with_settings: { protocol: 'http' } do
it 'should return a gravatar image tag if a user is provided' do
- expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
it 'should return a gravatar url if a user is provided' do
@@ -155,7 +152,7 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
context 'with https', with_settings: { protocol: 'https' } do
it 'should return a gravatar image tag with ssl if the request was ssl required' do
- expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
it 'should return a gravatar image tag with ssl if the request was ssl required' do
@@ -192,7 +189,7 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
let(:enable_local_avatars) { false }
it 'should return an empty string if gravatar is disabled' do
- expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
it 'should return an empty string if gravatar is disabled' do
@@ -204,7 +201,7 @@ describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
let(:user) { User.system }
it 'renders the avatar as user type (Regression #37278)' do
- expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
+ expect(helper.avatar(user)).to be_html_eql(expected_user_avatar_tag(user))
end
end
end