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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Initial modal service refactoring
Benjamin Bädorf1207a40e5cdb
diff --git a/frontend/src/app/features/boards/board/configuration-modal/board-configuration.modal.ts b/frontend/src/app/features/boards/board/configuration-modal/board-configuration.modal.tsindex bcd4e4f9b84..e8ab4d4c41b 100644--- a/frontend/src/app/features/boards/board/configuration-modal/board-configuration.modal.ts+++ b/frontend/src/app/features/boards/board/configuration-modal/board-configuration.modal.ts@@ -61,7 +61,7 @@ export class BoardConfigurationModalComponent extends OpModalComponent implement}ngOnInit() {- this.$element = jQuery(this.elementRef.nativeElement);+ this.$element = this.elementRef.nativeElement;this.tabPortalHost = new TabPortalOutlet(this.boardConfigurationService.tabs,@@ -116,7 +116,7 @@ export class BoardConfigurationModalComponent extends OpModalComponent implementreturn true;}- protected get afterFocusOn():JQuery {+ protected get afterFocusOn() {return this.$element;}}diff --git a/frontend/src/app/features/work-packages/components/wp-table/configuration-modal/wp-table-configuration.modal.ts b/frontend/src/app/features/work-packages/components/wp-table/configuration-modal/wp-table-configuration.modal.tsindex c688f3d0e18..55568a4e8a1 100644--- a/frontend/src/app/features/work-packages/components/wp-table/configuration-modal/wp-table-configuration.modal.ts+++ b/frontend/src/app/features/work-packages/components/wp-table/configuration-modal/wp-table-configuration.modal.ts@@ -45,8 +45,6 @@ export class WpTableConfigurationModalComponent extends OpModalComponent impleme/* Close on outside click */public closeOnOutsideClick = false;- public $element:JQuery;-public text = {title: this.I18n.t('js.work_packages.table_configuration.modal_title'),closePopup: this.I18n.t('js.close_popup_title'),@@ -94,7 +92,7 @@ export class WpTableConfigurationModalComponent extends OpModalComponent impleme}ngOnInit() {- this.$element = jQuery(this.elementRef.nativeElement);+ this.$element = this.elementRef.nativeElement;this.tabPortalHost = new TabPortalOutlet(this.wpTableConfigurationService.tabs,@@ -148,7 +146,7 @@ export class WpTableConfigurationModalComponent extends OpModalComponent implemereturn true;}- protected get afterFocusOn():JQuery {+ protected get afterFocusOn() {return this.$element;}diff --git a/frontend/src/app/shared/components/grids/widgets/time-entries/current-user/configuration-modal/configuration.modal.ts b/frontend/src/app/shared/components/grids/widgets/time-entries/current-user/configuration-modal/configuration.modal.tsindex 9af4d6a40e6..3117d4c3e96 100644--- a/frontend/src/app/shared/components/grids/widgets/time-entries/current-user/configuration-modal/configuration.modal.ts+++ b/frontend/src/app/shared/components/grids/widgets/time-entries/current-user/configuration-modal/configuration.modal.ts@@ -21,8 +21,6 @@ export class TimeEntriesCurrentUserConfigurationModalComponent extends OpModalCo/* Close on outside click */public closeOnOutsideClick = true;- public $element:JQuery;-public text = {displayedDays: this.I18n.t('js.grid.widgets.time_entries_current_user.displayed_days'),closePopup: this.I18n.t('js.close_popup_title'),diff --git a/frontend/src/app/shared/components/modal/modal-portal.component.html b/frontend/src/app/shared/components/modal/modal-portal.component.htmlnew file mode 100644index 00000000000..ca4dda29521--- /dev/null+++ b/frontend/src/app/shared/components/modal/modal-portal.component.html@@ -0,0 +1,17 @@+<div+ class="spot-modal-overlay"+ [ngClass]="{+ 'spot-modal-overlay_active': isActive,+ 'spot-modal-overlay_not-full-screen': notFullscreen+ }"+ (click)="close()"+>+ <button+ type="button"+ class="spot-button spot-modal-overlay--close-button"+ (click)="close()"+ >+ <span class="spot-icon spot-icon_close"></span>+ </button>+ <ng-template [ckdPortalOutlet]="modalPortal$" #portalOutlet></ng-template>+</div>diff --git a/frontend/src/app/shared/components/modal/modal-portal.component.ts b/frontend/src/app/shared/components/modal/modal-portal.component.tsnew file mode 100644index 00000000000..6a42b59f502--- /dev/null+++ b/frontend/src/app/shared/components/modal/modal-portal.component.ts@@ -0,0 +1,59 @@+// -- copyright+// OpenProject is an open source project management software.+// Copyright (C) 2012-2022 the OpenProject GmbH+//+// This program is free software; you can redistribute it and/or+// modify it under the terms of the GNU General Public License version 3.+//+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+// Copyright (C) 2006-2013 Jean-Philippe Lang+// Copyright (C) 2010-2013 the ChiliProject Team+//+// This program is free software; you can redistribute it and/or+// modify it under the terms of the GNU General Public License+// as published by the Free Software Foundation; either version 2+// of the License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU General Public License for more details.+//+// You should have received a copy of the GNU General Public License+// along with this program; if not, write to the Free Software+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+//+// See COPYRIGHT and LICENSE files for more details.+//++++import {+ ChangeDetectionStrategy,+ ChangeDetectorRef, Component, ElementRef, OnInit, ViewChild,+} from '@angular/core';+import { I18nService } from 'core-app/core/i18n/i18n.service';+import { OpModalService } from 'core-app/shared/components/modal/modal.service';++@Component({+ templateUrl: './modal-portal.component.html',+ changeDetection: ChangeDetectionStrategy.OnPush,+})+export class OpModalPortalComponent implements OnInit {+ // override superclass+ // Allowing outside clicks to close the modal leads to the user involuntarily closing+ // the modal when removing error messages or clicking on labels e.g. in the registration modal.+ public closeOnOutsideClick = false;++ @ViewChild('portalOutlet') portalOutlet: ElementRef<HTMLElement>;++ constructor(+ readonly modalService:OpModalService,+ readonly elementRef:ElementRef,+ readonly cdRef:ChangeDetectorRef,+ readonly I18n:I18nService,+ ) { }++ ngOnInit():void {+ }++ modalPortal$ = this.modalService.activeModal$;+}diff --git a/frontend/src/app/shared/components/modal/modal-wrapper-augment.service.ts b/frontend/src/app/shared/components/modal/modal-wrapper-augment.service.tsindex e6cea7faaba..6861f6eda60 100644--- a/frontend/src/app/shared/components/modal/modal-wrapper-augment.service.ts+++ b/frontend/src/app/shared/components/modal/modal-wrapper-augment.service.ts@@ -39,10 +39,11 @@ const iframeSelector = '.iframe-target-wrapper';*/@Injectable({ providedIn: 'root' })export class OpModalWrapperAugmentService {- constructor(@Inject(DOCUMENT) protected documentElement:Document,+ constructor(+ @Inject(DOCUMENT) protected documentElement:Document,protected injector:Injector,- protected opModalService:OpModalService) {- }+ protected opModalService:OpModalService,+ ) {}/*** Create initial listeners for Rails-rendered modalsdiff --git a/frontend/src/app/shared/components/modal/modal.component.ts b/frontend/src/app/shared/components/modal/modal.component.tsindex 921003ef866..17a66a766bd 100644--- a/frontend/src/app/shared/components/modal/modal.component.ts+++ b/frontend/src/app/shared/components/modal/modal.component.ts@@ -18,7 +18,7 @@ export abstract class OpModalComponent extends UntilDestroyedMixin implements On/* Reference to service */protected service:OpModalService = this.locals.service;- public $element:JQuery;+ public $element:HTMLElement;/** Closing event called from the service when closing this modal */public closingEvent = new EventEmitter<this>();@@ -28,14 +28,16 @@ export abstract class OpModalComponent extends UntilDestroyedMixin implements On/* Data to be return from this modal instance */public data:unknown;- protected constructor(public locals:OpModalLocalsMap,+ protected constructor(+ public locals:OpModalLocalsMap,readonly cdRef:ChangeDetectorRef,- readonly elementRef:ElementRef) {+ readonly elementRef:ElementRef,+ ) {super();}ngOnInit() {- this.$element = jQuery(this.elementRef.nativeElement);+ this.$element = this.elementRef.nativeElement;}ngOnDestroy() {@@ -67,7 +69,7 @@ export abstract class OpModalComponent extends UntilDestroyedMixin implements Onthis.cdRef.detectChanges();}- protected get afterFocusOn():JQuery {+ protected get afterFocusOn():HTMLElement {return this.$element;}}diff --git a/frontend/src/app/shared/components/modal/modal.service.ts b/frontend/src/app/shared/components/modal/modal.service.tsindex 843fab968ce..e6861e3c938 100644--- a/frontend/src/app/shared/components/modal/modal.service.ts+++ b/frontend/src/app/shared/components/modal/modal.service.ts@@ -37,27 +37,19 @@ import {import {ComponentPortal,ComponentType,- DomPortalOutlet,PortalInjector,} from '@angular/cdk/portal';import { TransitionService } from '@uirouter/core';import { OpModalComponent } from 'core-app/shared/components/modal/modal.component';+import { ReplaySubject } from 'rxjs';+import { take } from 'rxjs/operators';export const OpModalLocalsToken = new InjectionToken<any>('OP_MODAL_LOCALS');@Injectable({ providedIn: 'root' })export class OpModalService {- public active:OpModalComponent|null = null;-- // Hold a reference to the DOM node we're using as a host- private readonly portalHostElement:HTMLElement;-- // And a reference to the actual portal host interface on top of the element- private bodyPortalHost:DomPortalOutlet;-- // Remember when we're opening a new modal to avoid the outside click bubbling up.- private opening = false;+ public activeModal$ = new ReplaySubject<ComponentPortal<OpModalComponent>|null>();constructor(private readonly componentFactoryResolver:ComponentFactoryResolver,@@ -69,42 +61,18 @@ export class OpModalService {hostElement.classList.add('spot-modal-overlay');document.body.appendChild(hostElement);- const closeButton = document.createElement('button');- closeButton.classList.add('spot-button', 'spot-modal-close-button');- closeButton.innerHTML = '<span class="spot-icon spot-icon_close"></span>';- hostElement.appendChild(closeButton);-// Listen to keystrokes on window to close context menuswindow.addEventListener('keydown', (evt:KeyboardEvent) => {- if (this.active && this.active.closeOnEscape && evt.key === 'Escape') {- this.active.closeOnEscapeFunction();- }- return true;- });-- // Listen to any click on the modal overlay (backdrop click)- hostElement.addEventListener('click', (evt:MouseEvent) => {- if (this.active- && !this.opening- && this.portalHostElement === evt.target as Element) {- this.close();+ if (evt.key !== 'Escape') {+ return;}- });- closeButton.addEventListener('click', () => {- if (this.active && !this.opening) {- this.close();- }+ this.activeModal$.pipe(take(1)).subscribe((activeModal) => {+ if (activeModal) {+ this.activeModal$.next(null);+ }+ });});-- this.bodyPortalHost = new DomPortalOutlet(- hostElement,- this.componentFactoryResolver,- this.appRef,- this.injector,- );-- this.portalHostElement = hostElement;}/**@@ -134,12 +102,6 @@ export class OpModalService {// Create a portal for the given component class and render itconst portal = new ComponentPortal(modal, null, this.injectorFor(injector, locals));- const ref:ComponentRef<OpModalComponent> = this.bodyPortalHost.attach(portal) as ComponentRef<OpModalComponent>;- this.active = ref.instance as T;- this.portalHostElement.classList.add('spot-modal-overlay_active');- if (notFullScreen) {- this.portalHostElement.classList.add('spot-modal-overlay_not-full-screen');- }setTimeout(() => {// Focus on the first element@@ -164,6 +126,8 @@ export class OpModalService {*/public close():void {// Detach any component currently in the portal+ this.activeModal$.next(null);+ /*if (this.active && this.active.onClose()) {this.active.closingEvent.emit(this.active);this.bodyPortalHost.detach();@@ -171,6 +135,7 @@ export class OpModalService {this.portalHostElement.classList.remove('spot-modal-overlay_not-full-screen');this.active = null;}+ */}/**diff --git a/frontend/src/app/shared/components/modals/export-modal/wp-table-export.modal.ts b/frontend/src/app/shared/components/modals/export-modal/wp-table-export.modal.tsindex aa985790bde..dcbfc444e4d 100644--- a/frontend/src/app/shared/components/modals/export-modal/wp-table-export.modal.ts+++ b/frontend/src/app/shared/components/modals/export-modal/wp-table-export.modal.ts@@ -35,7 +35,7 @@ export class WpTableExportModalComponent extends OpModalComponent implements OnI/* Close on outside click */public closeOnOutsideClick = true;- public $element:JQuery;+ public $element:HTMLElement;public exportOptions:{ identifier:string, label:string, url:string }[];@@ -46,7 +46,8 @@ export class WpTableExportModalComponent extends OpModalComponent implements OnIcancelButton: this.I18n.t('js.button_cancel'),};- constructor(@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,+ constructor(+ @Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,readonly I18n:I18nService,readonly elementRef:ElementRef,readonly querySpace:IsolatedQuerySpace,@@ -54,7 +55,8 @@ export class WpTableExportModalComponent extends OpModalComponent implements OnIreadonly httpClient:HttpClient,readonly wpTableColumns:WorkPackageViewColumnsService,readonly loadingIndicator:LoadingIndicatorService,- readonly toastService:ToastService) {+ readonly toastService:ToastService,+ ) {super(locals, cdRef, elementRef);}@@ -136,7 +138,7 @@ export class WpTableExportModalComponent extends OpModalComponent implements OnIreturn url.toString();}- protected get afterFocusOn():JQuery {- return jQuery('#work-packages-settings-button');+ protected get afterFocusOn():HTMLElement {+ return document.getElementById('work-packages-settings-button')!;}}diff --git a/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.html b/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.htmlindex 0841ea9e823..1589434941d 100644--- a/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.html+++ b/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.html@@ -1,4 +1,5 @@<divclass="spot-modal dynamic-content-modal--wrapper loading-indicator--location"+ #wrapperdata-indicator-name="modal"></div>diff --git a/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.ts b/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.tsindex cdf9a4360ba..78b96d67f16 100644--- a/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.ts+++ b/frontend/src/app/shared/components/modals/modal-wrapper/dynamic-content.modal.ts@@ -28,7 +28,7 @@import {ChangeDetectionStrategy,- ChangeDetectorRef, Component, ElementRef, Inject, OnDestroy, OnInit,+ ChangeDetectorRef, Component, ElementRef, Inject, OnDestroy, OnInit, ViewChild,} from '@angular/core';import { OpModalLocalsToken } from 'core-app/shared/components/modal/modal.service';import { OpModalLocalsMap } from 'core-app/shared/components/modal/modal.types';@@ -45,10 +45,14 @@ export class DynamicContentModalComponent extends OpModalComponent implements On// the modal when removing error messages or clicking on labels e.g. in the registration modal.public closeOnOutsideClick = false;- constructor(readonly elementRef:ElementRef,+ @ViewChild('wrapper') wrapper:HTMLElement;++ constructor(+ readonly elementRef:ElementRef,@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,readonly cdRef:ChangeDetectorRef,- readonly I18n:I18nService) {+ readonly I18n:I18nService,+ ) {super(locals, cdRef, elementRef);}@@ -56,13 +60,11 @@ export class DynamicContentModalComponent extends OpModalComponent implements Onsuper.ngOnInit();// Append the dynamic body- this.$element- .find('.dynamic-content-modal--wrapper')- .addClass(this.locals.modalClassName)- .append(this.locals.modalBody);+ this.wrapper.classList.add(this.locals.modalClassName);+ this.wrapper.appendChild(this.locals.modalBody);const modal = document.querySelector('.spot-modal') as HTMLElement;- const closeButton = modal.querySelector('[dynamic-content-modal-close-button]') as HTMLMetaElement;+ const closeButton = modal.querySelector('[dynamic-content-modal-close-button]') as HTMLButtonElement;closeButton.addEventListener('click', () => this.closeMe());}diff --git a/frontend/src/app/shared/components/modals/save-modal/save-query.modal.ts b/frontend/src/app/shared/components/modals/save-modal/save-query.modal.tsindex 5475f6f87bf..85bbafdae25 100644--- a/frontend/src/app/shared/components/modals/save-modal/save-query.modal.ts+++ b/frontend/src/app/shared/components/modals/save-modal/save-query.modal.ts@@ -66,7 +66,8 @@ export class SaveQueryModalComponent extends OpModalComponent {close_popup: this.I18n.t('js.close_popup_title'),};- constructor(readonly elementRef:ElementRef,+ constructor(+ readonly elementRef:ElementRef,@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,readonly I18n:I18nService,readonly states:States,@@ -74,7 +75,8 @@ export class SaveQueryModalComponent extends OpModalComponent {readonly wpListService:WorkPackagesListService,readonly halNotification:HalResourceNotificationService,readonly cdRef:ChangeDetectorRef,- readonly toastService:ToastService) {+ readonly toastService:ToastService,+ ) {super(locals, cdRef, elementRef);}@@ -88,7 +90,7 @@ export class SaveQueryModalComponent extends OpModalComponent {}public get afterFocusOn() {- return jQuery('#work-packages-settings-button');+ return document.getElementById('work-packages-settings-button')!;}public saveQueryAs($event:Event):void {diff --git a/frontend/src/app/shared/components/modals/share-modal/query-sharing.modal.ts b/frontend/src/app/shared/components/modals/share-modal/query-sharing.modal.tsindex 4609d7b089b..88e0d94f498 100644--- a/frontend/src/app/shared/components/modals/share-modal/query-sharing.modal.ts+++ b/frontend/src/app/shared/components/modals/share-modal/query-sharing.modal.ts@@ -64,7 +64,8 @@ export class QuerySharingModalComponent extends OpModalComponent implements OnInclose_popup: this.I18n.t('js.close_popup_title'),};- constructor(readonly elementRef:ElementRef,+ constructor(+ readonly elementRef:ElementRef,@Inject(OpModalLocalsToken) public locals:OpModalLocalsMap,readonly I18n:I18nService,readonly states:States,@@ -72,7 +73,8 @@ export class QuerySharingModalComponent extends OpModalComponent implements OnInreadonly cdRef:ChangeDetectorRef,readonly wpListService:WorkPackagesListService,readonly halNotification:HalResourceNotificationService,- readonly toastService:ToastService) {+ readonly toastService:ToastService,+ ) {super(locals, cdRef, elementRef);}@@ -91,7 +93,7 @@ export class QuerySharingModalComponent extends OpModalComponent implements OnIn}public get afterFocusOn() {- return jQuery('#work-packages-settings-button');+ return document.getElementById('work-packages-settings-button')!;}public saveQuery($event:Event):void {diff --git a/frontend/src/app/shared/components/work-package-graphs/configuration-modal/wp-graph-configuration.modal.ts b/frontend/src/app/shared/components/work-package-graphs/configuration-modal/wp-graph-configuration.modal.tsindex 0ee311944ee..bbf25b4b33a 100644--- a/frontend/src/app/shared/components/work-package-graphs/configuration-modal/wp-graph-configuration.modal.ts+++ b/frontend/src/app/shared/components/work-package-graphs/configuration-modal/wp-graph-configuration.modal.ts@@ -43,7 +43,7 @@ export class WpGraphConfigurationModalComponent extends OpModalComponent impleme/* Close on outside click */public closeOnOutsideClick = false;- public $element:JQuery;+ public $element:HTMLElement;public text = {title: this.I18n.t('js.chart.modal_title'),@@ -79,7 +79,7 @@ export class WpGraphConfigurationModalComponent extends OpModalComponent impleme}ngOnInit():void {- this.$element = jQuery(this.elementRef.nativeElement);+ this.$element = this.elementRef.nativeElement;this.loadingIndicator.indicator('modal').promise = this.graphConfiguration.loadForms().then(() => {@@ -134,7 +134,7 @@ export class WpGraphConfigurationModalComponent extends OpModalComponent implemereturn true;}- protected get afterFocusOn():JQuery {+ protected get afterFocusOn():HTMLElement {return this.$element;}}