11// OpenUI5's Control.js subset
22import getSharedResource from "../getSharedResource.js" ;
33import insertOpenUI5PopupStyles from "./insertOpenUI5PopupStyles.js" ;
4+ import VersionInfo from "../generated/VersionInfo.js" ;
5+ import { compareVersions } from "../Runtimes.js" ;
6+
7+ type PatchedFunctionRecord = {
8+ version : VersionInfo | undefined ;
9+ originalFn : ( ...args : any [ ] ) => any ;
10+ } ;
11+
12+ type PatchedFunctionsRegistry = Record < string , PatchedFunctionRecord > ;
13+
14+ const PatchedFunctions = getSharedResource < PatchedFunctionsRegistry > ( "PatchedFunctions" , { } ) ;
15+
16+ const shouldRepatch = ( key : string ) : boolean => {
17+ const existing = PatchedFunctions [ key ] ;
18+ if ( ! existing ) {
19+ return true ;
20+ }
21+
22+ if ( ! existing . version ) {
23+ return true ;
24+ }
25+ return compareVersions ( VersionInfo , existing . version ) > 0 ;
26+ } ;
427
528type Control = {
629 getDomRef : ( ) => HTMLElement | null ,
@@ -173,7 +196,11 @@ const isNativePopoverOpen = (root: Document | ShadowRoot = document): boolean =>
173196} ;
174197
175198const patchDialog = ( Dialog : OpenUI5DialogClass ) => {
176- const origOnsapescape = Dialog . prototype . onsapescape ;
199+ const key = "Dialog.prototype.onsapescape" ;
200+ if ( shouldRepatch ( key ) ) {
201+ PatchedFunctions [ key ] = { version : VersionInfo , originalFn : PatchedFunctions [ key ] ?. originalFn ?? Dialog . prototype . onsapescape } ;
202+ }
203+ const origOnsapescape = PatchedFunctions [ key ] . originalFn ;
177204 Dialog . prototype . onsapescape = function onsapescape ( ...args : any [ ] ) {
178205 if ( hasWebComponentPopupAbove ( this . oPopup ) ) {
179206 return ;
@@ -184,7 +211,11 @@ const patchDialog = (Dialog: OpenUI5DialogClass) => {
184211} ;
185212
186213const patchOpen = ( Popup : OpenUI5PopupClass ) => {
187- const origOpen = Popup . prototype . open ;
214+ const key = "Popup.prototype.open" ;
215+ if ( shouldRepatch ( key ) ) {
216+ PatchedFunctions [ key ] = { version : VersionInfo , originalFn : PatchedFunctions [ key ] ?. originalFn ?? Popup . prototype . open } ;
217+ }
218+ const origOpen = PatchedFunctions [ key ] . originalFn ;
188219 Popup . prototype . open = function open ( ...args : any [ ] ) {
189220 origOpen . apply ( this , args ) ; // call open first to initiate opening
190221 openNativePopoverForOpenUI5 ( this ) ;
@@ -197,7 +228,11 @@ const patchOpen = (Popup: OpenUI5PopupClass) => {
197228} ;
198229
199230const patchClosed = ( Popup : OpenUI5PopupClass ) => {
200- const _origClosed = Popup . prototype . _closed ;
231+ const key = "Popup.prototype._closed" ;
232+ if ( shouldRepatch ( key ) ) {
233+ PatchedFunctions [ key ] = { version : VersionInfo , originalFn : PatchedFunctions [ key ] ?. originalFn ?? Popup . prototype . _closed } ;
234+ }
235+ const _origClosed = PatchedFunctions [ key ] . originalFn ;
201236 Popup . prototype . _closed = function _closed ( ...args : any [ ] ) {
202237 closeNativePopoverForOpenUI5 ( this ) ;
203238 _origClosed . apply ( this , args ) ; // only then call _close
@@ -206,7 +241,11 @@ const patchClosed = (Popup: OpenUI5PopupClass) => {
206241} ;
207242
208243const patchFocusEvent = ( Popup : OpenUI5PopupClass ) => {
209- const origFocusEvent = Popup . prototype . onFocusEvent ;
244+ const key = "Popup.prototype.onFocusEvent" ;
245+ if ( shouldRepatch ( key ) ) {
246+ PatchedFunctions [ key ] = { version : VersionInfo , originalFn : PatchedFunctions [ key ] ?. originalFn ?? Popup . prototype . onFocusEvent } ;
247+ }
248+ const origFocusEvent = PatchedFunctions [ key ] . originalFn ;
210249 Popup . prototype . onFocusEvent = function onFocusEvent ( ...args : any [ ] ) {
211250 if ( ! hasWebComponentPopupAbove ( this ) ) {
212251 origFocusEvent . apply ( this , args ) ;
0 commit comments