Skip to content

Commit 0ffca82

Browse files
authored
refactor(ui5-popup): Events do not bubble (#1981)
1 parent 47b38cc commit 0ffca82

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

packages/base/src/UI5Element.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,11 @@ class UI5Element extends HTMLElement {
644644
* @param name - name of the event
645645
* @param data - additional data for the event
646646
* @param cancelable - true, if the user can call preventDefault on the event object
647+
* @param bubbles - true, if the event bubbles
647648
* @returns {boolean} false, if the event was cancelled (preventDefault called), true otherwise
648649
*/
649-
fireEvent(name, data, cancelable) {
650-
const eventResult = this._fireEvent(name, data, cancelable);
650+
fireEvent(name, data, cancelable = false, bubbles = true) {
651+
const eventResult = this._fireEvent(name, data, cancelable, bubbles);
651652
const camelCaseEventName = kebabToCamelCase(name);
652653

653654
if (camelCaseEventName !== name) {
@@ -657,13 +658,13 @@ class UI5Element extends HTMLElement {
657658
return eventResult;
658659
}
659660

660-
_fireEvent(name, data, cancelable) {
661+
_fireEvent(name, data, cancelable = false, bubbles = true) {
661662
let compatEventResult = true; // Initialized to true, because if the event is not fired at all, it should be considered "not-prevented"
662663

663664
const noConflictEvent = new CustomEvent(`ui5-${name}`, {
664665
detail: data,
665666
composed: false,
666-
bubbles: true,
667+
bubbles,
667668
cancelable,
668669
});
669670

@@ -677,7 +678,7 @@ class UI5Element extends HTMLElement {
677678
const customEvent = new CustomEvent(name, {
678679
detail: data,
679680
composed: false,
680-
bubbles: true,
681+
bubbles,
681682
cancelable,
682683
});
683684

packages/main/src/Popup.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,34 @@ const metadata = {
8989
events: /** @lends sap.ui.webcomponents.main.Popup.prototype */ {
9090

9191
/**
92-
* Fired before the component is opened.
92+
* Fired before the component is opened. This event can be cancelled, which will prevent the popup from opening. This event does not bubble.
9393
*
9494
* @public
9595
* @event sap.ui.webcomponents.main.Popup#before-open
9696
*/
97-
9897
"before-open": {},
98+
9999
/**
100-
* Fired after the component is opened.
100+
* Fired after the component is opened. This event does not bubble.
101101
*
102102
* @public
103103
* @event sap.ui.webcomponents.main.Popup#after-open
104104
*/
105-
106105
"after-open": {},
106+
107107
/**
108-
* Fired before the component is closed.
108+
* Fired before the component is closed. This event can be cancelled, which will prevent the popup from closing. This event does not bubble.
109109
*
110110
* @public
111111
* @event sap.ui.webcomponents.main.Popup#before-close
112112
* @param {Boolean} escPressed Indicates that <code>ESC</code> key has triggered the event.
113113
*/
114-
115114
"before-close": {
116115
escPressed: { type: Boolean },
117116
},
118117

119118
/**
120-
* Fired after the component is closed.
119+
* Fired after the component is closed. This event does not bubble.
121120
*
122121
* @public
123122
* @event sap.ui.webcomponents.main.Popup#after-close
@@ -297,6 +296,11 @@ class Popup extends UI5Element {
297296
* @public
298297
*/
299298
open(preventInitialFocus) {
299+
const prevented = !this.fireEvent("before-open", {}, true, false);
300+
if (prevented) {
301+
return;
302+
}
303+
300304
if (this.isModal) {
301305
// create static area item ref for block layer
302306
this.getStaticAreaItemDomRef();
@@ -306,9 +310,7 @@ class Popup extends UI5Element {
306310

307311
this._zIndex = getNextZIndex();
308312
this.style.zIndex = this._zIndex;
309-
310313
this._focusedElementBeforeOpen = getFocusedElement();
311-
this.fireEvent("before-open", {});
312314
this.show();
313315

314316
if (!this._disableInitialFocus && !preventInitialFocus) {
@@ -318,7 +320,7 @@ class Popup extends UI5Element {
318320
this._addOpenedPopup();
319321

320322
this.opened = true;
321-
this.fireEvent("after-open", {});
323+
this.fireEvent("after-open", {}, false, false);
322324
}
323325

324326
/**
@@ -334,8 +336,12 @@ class Popup extends UI5Element {
334336
* @public
335337
*/
336338
close(escPressed = false, preventRegistryUpdate = false, preventFocusRestore = false) {
337-
const prevented = !this.fireEvent("before-close", { escPressed }, true);
338-
if (prevented || !this.opened) {
339+
if (!this.opened) {
340+
return;
341+
}
342+
343+
const prevented = !this.fireEvent("before-close", { escPressed }, true, false);
344+
if (prevented) {
339345
return;
340346
}
341347

@@ -355,7 +361,7 @@ class Popup extends UI5Element {
355361
this.resetFocus();
356362
}
357363

358-
this.fireEvent("after-close", {});
364+
this.fireEvent("after-close", {}, false, false);
359365
}
360366

361367
/**

0 commit comments

Comments
 (0)