@@ -15,20 +15,26 @@ export function initAriaDropdownPatch() {
1515}
1616
1717// the patched `$.fn.dropdown` function, it passes the arguments to Fomantic's `$.fn.dropdown` function, and:
18- // * it does the one-time attaching on the first call
19- // * it delegates the `onLabelCreate` to the patched `onLabelCreate` to add necessary aria attributes
18+ // * it does the one-time element event attaching on the first call
19+ // * it delegates the module internal functions like `onLabelCreate` to the patched functions to add more features.
2020function ariaDropdownFn ( this : any , ...args : Parameters < FomanticInitFunction > ) {
2121 const ret = fomanticDropdownFn . apply ( this , args ) ;
2222
23- // if the `$().dropdown()` call is without arguments, or it has non-string (object) argument,
24- // it means that this call will reset the dropdown internal settings, then we need to re-delegate the callbacks.
25- const needDelegate = ( ! args . length || typeof args [ 0 ] !== 'string' ) ;
2623 for ( const el of this ) {
2724 if ( ! el [ ariaPatchKey ] ) {
28- attachInit ( el ) ;
25+ // the elements don't belong to the dropdown "module" and won't be reset
26+ // so we only need to initialize them once.
27+ attachInitElements ( el ) ;
2928 }
30- if ( needDelegate ) {
31- delegateOne ( $ ( el ) ) ;
29+
30+ // if the `$().dropdown()` call is without arguments, or it has non-string (object) argument,
31+ // it means that such call will reset the dropdown "module" including internal settings,
32+ // then we need to re-delegate the callbacks.
33+ const $dropdown = $ ( el ) ;
34+ const dropdownModule = $dropdown . data ( 'module-dropdown' ) ;
35+ if ( ! dropdownModule . giteaDelegated ) {
36+ dropdownModule . giteaDelegated = true ;
37+ delegateDropdownModule ( $dropdown ) ;
3238 }
3339 }
3440 return ret ;
@@ -68,7 +74,7 @@ function processMenuItems($dropdown: any, dropdownCall: any) {
6874}
6975
7076// delegate the dropdown's template functions and callback functions to add aria attributes.
71- function delegateOne ( $dropdown : any ) {
77+ function delegateDropdownModule ( $dropdown : any ) {
7278 const dropdownCall = fomanticDropdownFn . bind ( $dropdown ) ;
7379
7480 // If there is a "search input" in the "menu", Fomantic will only "focus the input" but not "toggle the menu" when the "dropdown icon" is clicked.
@@ -163,7 +169,7 @@ function attachStaticElements(dropdown: HTMLElement, focusable: HTMLElement, men
163169 }
164170}
165171
166- function attachInit ( dropdown : HTMLElement ) {
172+ function attachInitElements ( dropdown : HTMLElement ) {
167173 ( dropdown as any ) [ ariaPatchKey ] = { } ;
168174 if ( dropdown . classList . contains ( 'custom' ) ) return ;
169175
0 commit comments