-
Notifications
You must be signed in to change notification settings - Fork 279
fix(OpenUI5Support): improve focus handling for OpenUI5 and Web Component popups #12230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2f8d538
f0f9822
38c7fa5
a1d5fa9
add4321
aeee6e5
acd4165
23c2d62
9771b81
7d90f19
ba2f615
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,22 +16,49 @@ type OpenUI5Popup = { | |
} | ||
}; | ||
|
||
type PopupInfo = { | ||
type: "OpenUI5" | "WebComponent"; | ||
instance: object; | ||
}; | ||
|
||
// contains all OpenUI5 and Web Component popups that are currently opened | ||
const AllOpenedPopupsRegistry = getSharedResource<{ openedRegistry: Array<object> }>("AllOpenedPopupsRegistry", { openedRegistry: [] }); | ||
const AllOpenedPopupsRegistry = getSharedResource<{ openedRegistry: Array<PopupInfo> }>("AllOpenedPopupsRegistry", { openedRegistry: [] }); | ||
|
||
const addOpenedPopup = (popup: object) => { | ||
AllOpenedPopupsRegistry.openedRegistry.push(popup); | ||
const addOpenedPopup = (popupInfo: PopupInfo) => { | ||
AllOpenedPopupsRegistry.openedRegistry.push(popupInfo); | ||
}; | ||
|
||
const removeOpenedPopup = (popup: object) => { | ||
const index = AllOpenedPopupsRegistry.openedRegistry.indexOf(popup); | ||
const index = AllOpenedPopupsRegistry.openedRegistry.findIndex(el => el.instance === popup); | ||
if (index > -1) { | ||
AllOpenedPopupsRegistry.openedRegistry.splice(index, 1); | ||
} | ||
}; | ||
|
||
const getTopmostPopup = () => { | ||
return AllOpenedPopupsRegistry.openedRegistry[AllOpenedPopupsRegistry.openedRegistry.length - 1]; | ||
return AllOpenedPopupsRegistry.openedRegistry[AllOpenedPopupsRegistry.openedRegistry.length - 1].instance; | ||
}; | ||
|
||
/** | ||
* Original OpenUI5 popup focus event is triggered only | ||
* if there are no Web Component popups opened on top of it. | ||
* | ||
TeodorTaushanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param {object} popup - The popup instance to check. | ||
* @returns {boolean} True if the focus event should be triggered, false otherwise. | ||
*/ | ||
const shouldCallOpenUI5FocusEvent = (popup: object) => { | ||
for (let i = AllOpenedPopupsRegistry.openedRegistry.length - 1; i >= 0; i--) { | ||
const popupInfo = AllOpenedPopupsRegistry.openedRegistry[i]; | ||
if (popupInfo.type !== "OpenUI5") { | ||
return false; | ||
} | ||
|
||
if (popupInfo.instance === popup) { | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const openNativePopover = (domRef: HTMLElement) => { | ||
|
@@ -73,7 +100,10 @@ const patchOpen = (Popup: OpenUI5Popup) => { | |
} | ||
} | ||
|
||
addOpenedPopup(this); | ||
addOpenedPopup({ | ||
type: "OpenUI5", | ||
instance: this, | ||
}); | ||
}; | ||
}; | ||
|
||
|
@@ -94,9 +124,7 @@ const patchClosed = (Popup: OpenUI5Popup) => { | |
const patchFocusEvent = (Popup: OpenUI5Popup) => { | ||
const origFocusEvent = Popup.prototype.onFocusEvent; | ||
Popup.prototype.onFocusEvent = function onFocusEvent(e: FocusEvent) { | ||
// If the popup is the topmost one, we call the original focus event handler from the OpenUI5 Popup, | ||
// otherwise the focus event is handled by the Web Component Popup. | ||
if (this === getTopmostPopup()) { | ||
if (shouldCallOpenUI5FocusEvent(this)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an alternative is to patch only the logic for topmost popup - see 54781ca and https://git.wdf.sap.corp/c/openui5/+/6794573 |
||
origFocusEvent.call(this, e); | ||
} | ||
}; | ||
|
@@ -122,4 +150,4 @@ export { | |
getTopmostPopup, | ||
}; | ||
|
||
export type { OpenUI5Popup }; | ||
export type { OpenUI5Popup, PopupInfo }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,13 +21,14 @@ | |
data-sap-ui-compatVersion="edge"></script> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need more samples
|
||
<script> | ||
function onOpenUI5Init() { | ||
sap.ui.require(["sap/m/Button", "sap/m/Dialog"], (Button, Dialog) => { | ||
sap.ui.require(["sap/m/Button", "sap/m/Dialog", "sap/m/Input"], (Button, Dialog, Input) => { | ||
new Button("openUI5Button", { | ||
text: "Open OpenUI5 Dialog", | ||
press: function () { | ||
new Dialog({ | ||
title: "OpenUI5 Dialog", | ||
content: [ | ||
new Input(), | ||
new Button({ | ||
text: "Focus stop" | ||
}), | ||
|
@@ -47,12 +48,41 @@ | |
}); | ||
} | ||
|
||
function openUI5Dialog() { | ||
sap.ui.require(["sap/m/Button", "sap/m/Dialog"], (Button, Dialog) => { | ||
new Dialog({ | ||
title: "OpenUI5 Dialog", | ||
content: [ | ||
new Button({ | ||
text: "Focus stop" | ||
}), | ||
new Button("openUI5DialogButton", { | ||
text: "Open WebC Dialog", | ||
press: function () { | ||
document.getElementById("newDialog1").open = true; | ||
} | ||
}) | ||
], | ||
afterClose: function () { | ||
this.destroy(); | ||
} | ||
}).open(); | ||
}); | ||
} | ||
|
||
function init() { | ||
document.getElementById("myButton").addEventListener("click", function() { | ||
document.getElementById("dialog1").open = true; | ||
}); | ||
|
||
sap.ui.require(["sap/m/Select", "sap/ui/core/Item"], (Select, Item) => { | ||
sap.ui.require(["sap/m/Select", | ||
"sap/m/Button", | ||
"sap/ui/core/Item", | ||
"sap/ui/core/ShortcutHintsMixin"], | ||
(Select, | ||
Button, | ||
Item, | ||
ShortcutHintsMixin) => { | ||
new Select({ | ||
items: [ | ||
new Item({ text: "Item 1" }), | ||
|
@@ -62,29 +92,26 @@ | |
change: function (oEvent) { | ||
console.error("Selected item:", oEvent.getParameter("selectedItem").getText()); | ||
} | ||
}).placeAt("dilog1content"); | ||
}).placeAt("dialog1content"); | ||
|
||
const button = new Button({ | ||
text: "OpenUI5 with Shortcut (Ctrl+S)", | ||
press: function () { | ||
openUI5Dialog(); | ||
} | ||
}).placeAt("dialog1content"); | ||
|
||
|
||
ShortcutHintsMixin.addConfig(button, { | ||
event: "press", | ||
position: "0 0", | ||
addAccessibilityLabel: true, | ||
message: "Save" | ||
}, button); | ||
}); | ||
|
||
document.getElementById("dialogButton").addEventListener("click", function () { | ||
sap.ui.require(["sap/m/Button", "sap/m/Dialog"], (Button, Dialog) => { | ||
new Dialog({ | ||
title: "OpenUI5 Dialog", | ||
content: [ | ||
new Button({ | ||
text: "Focus stop" | ||
}), | ||
new Button("openUI5DialogButton", { | ||
text: "Open WebC Dialog", | ||
press: function () { | ||
document.getElementById("newDialog1").open = true; | ||
} | ||
}) | ||
], | ||
afterClose: function () { | ||
this.destroy(); | ||
} | ||
}).open(); | ||
}); | ||
openUI5Dialog(); | ||
}); | ||
} | ||
</script> | ||
|
@@ -94,7 +121,7 @@ | |
<ui5-button id="myButton">Open WebC Dialog</ui5-button> | ||
</div> | ||
<ui5-dialog id="dialog1" header-text="This is an WebC Dialog 1"> | ||
<div id="dilog1content"></div> | ||
<div id="dialog1content"></div> | ||
<ui5-button id="dialogButton">Open UI5 dialog</ui5-button> | ||
</ui5-dialog> | ||
<ui5-dialog id="newDialog1" header-text="This is an WebC Dialog 2"> | ||
|
Uh oh!
There was an error while loading. Please reload this page.