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

[#61126] go stimulus all the way

Tobias Dillmann77e2c354c667
diff --git a/frontend/src/app/features/invite-user-modal/principal/principal-search.component.html b/frontend/src/app/features/invite-user-modal/principal/principal-search.component.html
index 480bdd7f874..a905438c872 100644
--- a/frontend/src/app/features/invite-user-modal/principal/principal-search.component.html
+++ b/frontend/src/app/features/invite-user-modal/principal/principal-search.component.html
@@ -22,7 +22,7 @@
>
<!--Selectable option-->
<span
- class="op-hover-card--preview-trigger"
+ data-hover-card-trigger-target="trigger"
[attr.data-hover-card-url]="getHoverCardUrl(item.principal)"
[ngOptionHighlight]="search">{{ item.principal.name }}</span>
<span
diff --git a/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter-template.component.html b/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter-template.component.html
index 2cc05b55147..1820675eb25 100644
--- a/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter-template.component.html
+++ b/frontend/src/app/shared/components/autocompleter/user-autocompleter/user-autocompleter-template.component.html
@@ -12,7 +12,8 @@
></op-principal>
<span [opSearchHighlight]="search"
[attr.data-hover-card-url]="getHoverCardUrl(item)"
- class="op-user-autocompleter--name op-hover-card--preview-trigger">
+ data-hover-card-trigger-target="trigger"
+ class="op-user-autocompleter--name">
{{ item.name }}
</span>
<span
diff --git a/frontend/src/app/shared/components/fields/macros/work-package-quickinfo-macro.html b/frontend/src/app/shared/components/fields/macros/work-package-quickinfo-macro.html
index 32fd8bd05d3..0c8abdb01a0 100644
--- a/frontend/src/app/shared/components/fields/macros/work-package-quickinfo-macro.html
+++ b/frontend/src/app/shared/components/fields/macros/work-package-quickinfo-macro.html
@@ -9,7 +9,7 @@
[displayFieldOptions]="{ writable: false }"
fieldName="type">
</display-field>
- <a class="op-hover-card--preview-trigger"
+ <a data-hover-card-trigger-target="trigger"
target="_top"
[href]="workPackageLink"
[attr.data-work-package-id]="workPackage.id"
diff --git a/frontend/src/app/shared/components/principal/principal-renderer.service.ts b/frontend/src/app/shared/components/principal/principal-renderer.service.ts
index 2621d67f77a..13ba2461c89 100644
--- a/frontend/src/app/shared/components/principal/principal-renderer.service.ts
+++ b/frontend/src/app/shared/components/principal/principal-renderer.service.ts
@@ -268,7 +268,7 @@ export class PrincipalRendererService {
}
}
- element.classList.add('op-hover-card--preview-trigger');
+ element.setAttribute('data-hover-card-trigger-target', 'trigger');
element.setAttribute('data-hover-card-url', hoverCard.url);
}
}
diff --git a/frontend/src/app/shared/components/user-link/user-link.component.ts b/frontend/src/app/shared/components/user-link/user-link.component.ts
index e6c83eab9d4..e9402807844 100644
--- a/frontend/src/app/shared/components/user-link/user-link.component.ts
+++ b/frontend/src/app/shared/components/user-link/user-link.component.ts
@@ -35,7 +35,7 @@ import { PathHelperService } from 'core-app/core/path-helper/path-helper.service
selector: 'op-user-link',
template: `
<a *ngIf="href"
- class="op-hover-card--preview-trigger"
+ data-hover-card-trigger-target="trigger"
[attr.data-hover-card-url]="hoverCardUrl"
[attr.href]="href"
[attr.title]="label"
diff --git a/frontend/src/stimulus/controllers/hover-card-trigger.controller.ts b/frontend/src/stimulus/controllers/hover-card-trigger.controller.ts
index 77ccdcdeebb..ddbfb3b1a63 100644
--- a/frontend/src/stimulus/controllers/hover-card-trigger.controller.ts
+++ b/frontend/src/stimulus/controllers/hover-card-trigger.controller.ts
@@ -34,10 +34,13 @@ import { computePosition, flip, limitShift, shift } from '@floating-ui/dom';
/**
* This controller is responsible for showing a hover card when hovering over a trigger element.
- * You can define a trigger element by adding the class `op-hover-card--preview-trigger` to it.
+ *
+ * You can define a trigger element by adding the `data-hover-card-trigger-target="trigger"` to it.
* To have hover cards available everywhere, add this controller to the body tag.
*/
export default class HoverCardTriggerController extends ApplicationController {
+ static targets = ['trigger', 'card'];
+
private mouseInModal = false;
private hoverTimeout:number|null = null;
private closeTimeout:number|null = null;
@@ -52,60 +55,82 @@ export default class HoverCardTriggerController extends ApplicationController {
// The time you need to keep away from trigger/hover card before an opened card is closed
CLOSE_DELAY_IN_MS = 250;
- connect() {
- super.connect();
+ private boundTriggerMouseOver:EventListenerOrEventListenerObject;
+ private boundTriggerMouseLeave:EventListenerOrEventListenerObject;
+
+ private boundCardMouseLeave:EventListenerOrEventListenerObject;
+ private boundCardMouseEnter:EventListenerOrEventListenerObject;
+
+ initialize() {
+ super.initialize();
- this.setupListener();
+ this.boundTriggerMouseOver = this.onMouseOver.bind(this);
+ this.boundTriggerMouseLeave = this.onMouseLeave.bind(this);
+
+ this.boundCardMouseLeave = this.onMouseLeave.bind(this);
+ this.boundCardMouseEnter = this.onMouseEnter.bind(this);
}
- setupListener() {
- jQuery(this.element).on('mouseover', '.op-hover-card--preview-trigger', (e) => {
- e.preventDefault();
- e.stopPropagation();
-
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
- const el = e.target as HTMLElement;
- if (!el) { return; }
-
- if (this.previousTarget && this.previousTarget === el) {
- // Re-entering the trigger counts as hovering over the card:
- this.mouseInModal = true;
- // But we will not re-render the same card, abort here
- return;
- }
-
- // Hovering over a new target. Close the old one (if any).
- this.close(true);
-
- const turboFrameUrl = this.parseHoverCardUrl(el);
- if (!turboFrameUrl) { return; }
-
- // Reset close timer for when hovering over multiple triggers in quick succession.
- // A timer from a previous hover card might still be running. We do not want it to
- // close the new (i.e. this) hover card.
- this.clearCloseTimer();
-
- // Set a delay before showing the hover card
- this.hoverTimeout = window.setTimeout(() => {
- this.showHoverCard(el, turboFrameUrl);
- }, this.OPEN_DELAY_IN_MS);
- });
+ private triggerTargetConnected(trigger:Element) {
+ trigger.addEventListener('mouseover', this.boundTriggerMouseOver);
+ trigger.addEventListener('mouseleave', this.boundTriggerMouseLeave);
+ }
- jQuery(this.element).on('mouseleave', '.op-hover-card--preview-trigger', () => {
- this.clearHoverTimer();
- this.mouseInModal = false;
- this.closeAfterTimeout();
- });
+ private triggerTargetDisconnected(trigger:Element) {
+ trigger.removeEventListener('mouseover', this.boundTriggerMouseOver);
+ trigger.removeEventListener('mouseleave', this.boundTriggerMouseLeave);
+ }
- jQuery(this.element).on('mouseleave', '.op-hover-card', () => {
- this.clearHoverTimer();
- this.mouseInModal = false;
- this.closeAfterTimeout();
- });
+ private cardTargetConnected(card:Element) {
+ card.addEventListener('mouseleave', this.boundCardMouseLeave);
+ card.addEventListener('mouseenter', this.boundCardMouseEnter);
+ }
- jQuery(this.element).on('mouseenter', '.op-hover-card', () => {
+ private cardTargetDisconnected(card:Element) {
+ card.removeEventListener('mouseleave', this.boundCardMouseLeave);
+ card.removeEventListener('mouseenter', this.boundCardMouseEnter);
+ }
+
+ private onMouseLeave() {
+ this.clearHoverTimer();
+ this.mouseInModal = false;
+ this.closeAfterTimeout();
+ }
+
+ private onMouseEnter() {
+ this.mouseInModal = true;
+ }
+
+ private onMouseOver(e:MouseEvent) {
+ e.preventDefault();
+ e.stopPropagation();
+
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
+ const el = e.target as HTMLElement;
+ if (!el) { return; }
+
+ if (this.previousTarget && this.previousTarget === el) {
+ // Re-entering the trigger counts as hovering over the card:
this.mouseInModal = true;
- });
+ // But we will not re-render the same card, abort here
+ return;
+ }
+
+ // Hovering over a new target. Close the old one (if any).
+ this.close(true);
+
+ const turboFrameUrl = this.parseHoverCardUrl(el);
+ if (!turboFrameUrl) { return; }
+
+ // Reset close timer for when hovering over multiple triggers in quick succession.
+ // A timer from a previous hover card might still be running. We do not want it to
+ // close the new (i.e. this) hover card.
+ this.clearCloseTimer();
+
+ // Set a delay before showing the hover card
+ this.hoverTimeout = window.setTimeout(() => {
+ this.showHoverCard(el, turboFrameUrl);
+ }, this.OPEN_DELAY_IN_MS);
}
private showHoverCard(el:HTMLElement, turboFrameUrl:string) {
@@ -229,6 +254,7 @@ export default class HoverCardTriggerController extends ApplicationController {
const popover = document.createElement('div');
popover.className = 'op-hover-card';
popover.setAttribute('popover', 'auto');
+ popover.setAttribute('data-hover-card-trigger-target', 'card');
const turboFrame = document.createElement('turbo-frame');
turboFrame.id = 'op-hover-card-body';
diff --git a/lib/open_project/object_linking.rb b/lib/open_project/object_linking.rb
index 8a928315d9e..80c34212216 100644
--- a/lib/open_project/object_linking.rb
+++ b/lib/open_project/object_linking.rb
@@ -148,11 +148,11 @@ module OpenProject
# You can use this method if you want to render a link and apply a user hover card to it.
def add_hover_card_options(user, options, only_path: true, clazz_key: :classes)
if options.delete(:hover_card) { true } && user.is_a?(User)
- options[clazz_key] = [options[clazz_key], "op-hover-card--preview-trigger"].compact.join(" ")
options[:data] ||= {}
hover_card_url = hover_card_user_url(user, only_path:)
options[:data][:hover_card_url] = hover_card_url
+ options[:data][:hover_card_trigger_target] = "trigger"
end
options
diff --git a/lib/open_project/text_formatting/filters/mention_filter.rb b/lib/open_project/text_formatting/filters/mention_filter.rb
index 9ced1b421ca..c5deaaf0c35 100644
--- a/lib/open_project/text_formatting/filters/mention_filter.rb
+++ b/lib/open_project/text_formatting/filters/mention_filter.rb
@@ -75,8 +75,11 @@ module OpenProject::TextFormatting
def work_package_mention(work_package)
link_to("##{work_package.id}",
work_package_path_or_url(id: work_package.id, only_path: context[:only_path]),
- class: "issue work_package op-hover-card--preview-trigger",
- data: { "hover-card-url": hover_card_work_package_path(work_package.id) })
+ class: "issue work_package",
+ data: {
+ hover_card_trigger_target: "trigger",
+ hover_card_url: hover_card_work_package_path(work_package.id)
+ })
end
def class_from_mention(mention)
diff --git a/lib/open_project/text_formatting/matchers/link_handlers/work_packages.rb b/lib/open_project/text_formatting/matchers/link_handlers/work_packages.rb
index 42128b5a873..f4b58eaff9d 100644
--- a/lib/open_project/text_formatting/matchers/link_handlers/work_packages.rb
+++ b/lib/open_project/text_formatting/matchers/link_handlers/work_packages.rb
@@ -66,8 +66,11 @@ module OpenProject::TextFormatting::Matchers
def render_work_package_link(wp_id)
link_to("##{wp_id}",
work_package_path_or_url(id: wp_id, only_path: context[:only_path]),
- class: "issue work_package op-hover-card--preview-trigger",
- data: { "hover-card-url": hover_card_work_package_path(wp_id) })
+ class: "issue work_package",
+ data: {
+ hover_card_trigger_target: "trigger",
+ hover_card_url: hover_card_work_package_path(wp_id)
+ })
end
end
end