2024-11-21 14:57:42 +01:00
import type { FomanticInitFunction } from '../../types.ts' ;
2025-07-05 23:21:53 +08:00
import { generateElemId , queryElems } from '../../utils/dom.ts' ;
2022-06-04 05:38:26 +08:00
2023-03-22 10:52:01 +08:00
const ariaPatchKey = '_giteaAriaPatchDropdown' ;
const fomanticDropdownFn = $ . fn . dropdown ;
2022-06-04 05:38:26 +08:00
2023-03-22 10:52:01 +08:00
// use our own `$().dropdown` function to patch Fomantic's dropdown module
export function initAriaDropdownPatch() {
if ( $ . fn . dropdown === ariaDropdownFn ) throw new Error ( 'initAriaDropdownPatch could only be called once' ) ;
$ . fn . dropdown = ariaDropdownFn ;
2025-01-02 01:21:13 +08:00
$ . fn . fomanticExt . onResponseKeepSelectedItem = onResponseKeepSelectedItem ;
2025-06-22 02:21:48 +08:00
$ . fn . fomanticExt . onDropdownAfterFiltered = onDropdownAfterFiltered ;
2024-11-21 14:57:42 +01:00
( ariaDropdownFn as FomanticInitFunction ) . settings = fomanticDropdownFn . settings ;
2022-06-04 05:38:26 +08:00
}
2023-03-22 10:52:01 +08:00
// the patched `$.fn.dropdown` function, it passes the arguments to Fomantic's `$.fn.dropdown` function, and:
2025-03-26 10:51:22 +08:00
// * it does the one-time element event attaching on the first call
// * it delegates the module internal functions like `onLabelCreate` to the patched functions to add more features.
2025-01-15 21:26:17 +01:00
function ariaDropdownFn ( this : any , . . . args : Parameters < FomanticInitFunction > ) {
2023-03-22 10:52:01 +08:00
const ret = fomanticDropdownFn . apply ( this , args ) ;
2025-03-27 00:01:43 +08:00
for ( let el of this ) {
// dropdown will replace '<select class="ui dropdown"/>' to '<div class="ui dropdown"><select (hidden)></select><div class="menu">...</div></div>'
// so we need to correctly find the closest '.ui.dropdown' element, it is the real fomantic dropdown module.
el = el . closest ( '.ui.dropdown' ) ;
2023-03-22 10:52:01 +08:00
if ( ! el [ ariaPatchKey ] ) {
2025-03-26 10:51:22 +08:00
// the elements don't belong to the dropdown "module" and won't be reset
// so we only need to initialize them once.
attachInitElements ( el ) ;
2023-03-22 10:52:01 +08:00
}
2025-03-26 10:51:22 +08:00
2025-03-27 00:01:43 +08:00
// if the `$().dropdown()` is called without arguments, or it has non-string (object) argument,
2025-03-26 10:51:22 +08:00
// it means that such call will reset the dropdown "module" including internal settings,
// then we need to re-delegate the callbacks.
const $dropdown = $ ( el ) ;
const dropdownModule = $dropdown . data ( 'module-dropdown' ) ;
if ( ! dropdownModule . giteaDelegated ) {
dropdownModule . giteaDelegated = true ;
delegateDropdownModule ( $dropdown ) ;
2023-03-22 10:52:01 +08:00
}
}
return ret ;
}
// make the item has role=option/menuitem, add an id if there wasn't one yet, make items as non-focusable
// the elements inside the dropdown menu item should not be focusable, the focus should always be on the dropdown primary element.
2024-11-21 14:57:42 +01:00
function updateMenuItem ( dropdown : HTMLElement , item : HTMLElement ) {
2025-07-05 23:21:53 +08:00
if ( ! item . id ) item . id = generateElemId ( '_aria_dropdown_item_' ) ;
2025-01-22 08:11:51 +01:00
item . setAttribute ( 'role' , ( dropdown as any ) [ ariaPatchKey ] . listItemRole ) ;
2023-03-22 10:52:01 +08:00
item . setAttribute ( 'tabindex' , '-1' ) ;
2024-03-13 21:44:46 +08:00
for ( const el of item . querySelectorAll ( 'a, input, button' ) ) el . setAttribute ( 'tabindex' , '-1' ) ;
2023-03-22 10:52:01 +08:00
}
2024-03-27 12:50:07 +02:00
/**
* make the label item and its "delete icon" have correct aria attributes
* @param {HTMLElement} label
*/
2024-11-21 14:57:42 +01:00
function updateSelectionLabel ( label : HTMLElement ) {
2023-03-22 10:52:01 +08:00
// the "label" is like this: "<a|div class="ui label" data-value="1">the-label-name <i|svg class="delete icon"/></a>"
2024-03-27 12:50:07 +02:00
if ( ! label . id ) {
2025-07-05 23:21:53 +08:00
label . id = generateElemId ( '_aria_dropdown_label_' ) ;
2024-03-27 12:50:07 +02:00
}
label . tabIndex = - 1 ;
const deleteIcon = label . querySelector ( '.delete.icon' ) ;
if ( deleteIcon ) {
deleteIcon . setAttribute ( 'aria-hidden' , 'false' ) ;
2026-01-24 13:52:13 +01:00
deleteIcon . setAttribute ( 'aria-label' , window . config . i18n . remove_label_str . replace ( '%s' , label . getAttribute ( 'data-value' ) ! ) ) ;
2024-03-27 12:50:07 +02:00
deleteIcon . setAttribute ( 'role' , 'button' ) ;
}
2023-03-22 10:52:01 +08:00
}
2025-06-22 02:21:48 +08:00
function onDropdownAfterFiltered ( this : any ) {
2025-06-17 19:19:08 +08:00
const $dropdown = $ ( this ) . closest ( '.ui.dropdown' ) ; // "this" can be the "ui dropdown" or "<select>"
2025-03-26 10:51:22 +08:00
const hideEmptyDividers = $dropdown . dropdown ( 'setting' , 'hideDividers' ) === 'empty' ;
2024-12-09 15:54:59 +08:00
const itemsMenu = $dropdown [ 0 ] . querySelector ( '.scrolling.menu' ) || $dropdown [ 0 ] . querySelector ( '.menu' ) ;
2025-06-17 19:19:08 +08:00
if ( hideEmptyDividers && itemsMenu ) hideScopedEmptyDividers ( itemsMenu ) ;
2024-12-09 15:54:59 +08:00
}
2023-03-22 10:52:01 +08:00
// delegate the dropdown's template functions and callback functions to add aria attributes.
2025-03-26 10:51:22 +08:00
function delegateDropdownModule ( $dropdown : any ) {
2023-03-22 10:52:01 +08:00
const dropdownCall = fomanticDropdownFn . bind ( $dropdown ) ;
// the "template" functions are used for dynamic creation (eg: AJAX)
const dropdownTemplates = { . . . dropdownCall ( 'setting' , 'templates' ) , t : performance.now ( ) } ;
const dropdownTemplatesMenuOld = dropdownTemplates . menu ;
2024-11-21 14:57:42 +01:00
dropdownTemplates . menu = function ( response : any , fields : any , preserveHTML : any , className : Record < string , string > ) {
2023-03-22 10:52:01 +08:00
// when the dropdown menu items are loaded from AJAX requests, the items are created dynamically
const menuItems = dropdownTemplatesMenuOld ( response , fields , preserveHTML , className ) ;
2024-03-16 15:25:27 +02:00
const div = document . createElement ( 'div' ) ;
div . innerHTML = menuItems ;
const $wrapper = $ ( div ) ;
2023-03-22 10:52:01 +08:00
const $items = $wrapper . find ( '> .item' ) ;
$items . each ( ( _ , item ) = > updateMenuItem ( $dropdown [ 0 ] , item ) ) ;
$dropdown [ 0 ] [ ariaPatchKey ] . deferredRefreshAriaActiveItem ( ) ;
return $wrapper . html ( ) ;
} ;
dropdownCall ( 'setting' , 'templates' , dropdownTemplates ) ;
// the `onLabelCreate` is used to add necessary aria attributes for dynamically created selection labels
const dropdownOnLabelCreateOld = dropdownCall ( 'setting' , 'onLabelCreate' ) ;
2025-01-15 21:26:17 +01:00
dropdownCall ( 'setting' , 'onLabelCreate' , function ( this : any , value : any , text : string ) {
2023-03-22 10:52:01 +08:00
const $label = dropdownOnLabelCreateOld . call ( this , value , text ) ;
2024-03-27 12:50:07 +02:00
updateSelectionLabel ( $label [ 0 ] ) ;
2023-03-22 10:52:01 +08:00
return $label ;
} ) ;
2024-06-12 16:58:03 +02:00
const oldSet = dropdownCall ( 'internal' , 'set' ) ;
const oldSetDirection = oldSet . direction ;
2024-11-21 14:57:42 +01:00
oldSet . direction = function ( $menu : any ) {
2024-06-12 16:58:03 +02:00
oldSetDirection . call ( this , $menu ) ;
const classNames = dropdownCall ( 'setting' , 'className' ) ;
$menu = $menu || $dropdown . find ( '> .menu' ) ;
const elMenu = $menu [ 0 ] ;
// detect whether the menu is outside the viewport, and adjust the position
// there is a bug in fomantic's builtin `direction` function, in some cases (when the menu width is only a little larger) it wrongly opens the menu at right and triggers the scrollbar.
elMenu . classList . add ( classNames . loading ) ;
if ( elMenu . getBoundingClientRect ( ) . right > document . documentElement . clientWidth ) {
elMenu . classList . add ( classNames . leftward ) ;
}
elMenu . classList . remove ( classNames . loading ) ;
} ;
2023-03-22 10:52:01 +08:00
}
// for static dropdown elements (generated by server-side template), prepare them with necessary aria attributes
2024-11-21 14:57:42 +01:00
function attachStaticElements ( dropdown : HTMLElement , focusable : HTMLElement , menu : HTMLElement ) {
2023-03-22 10:52:01 +08:00
// prepare static dropdown menu list popup
2024-03-27 12:50:07 +02:00
if ( ! menu . id ) {
2025-07-05 23:21:53 +08:00
menu . id = generateElemId ( '_aria_dropdown_menu_' ) ;
2024-03-27 12:50:07 +02:00
}
$ ( menu ) . find ( '> .item' ) . each ( ( _ , item ) = > updateMenuItem ( dropdown , item ) ) ;
2023-03-22 10:52:01 +08:00
// this role could only be changed after its content is ready, otherwise some browsers+readers (like Chrome+AppleVoice) crash
2025-01-22 08:11:51 +01:00
menu . setAttribute ( 'role' , ( dropdown as any ) [ ariaPatchKey ] . listPopupRole ) ;
2023-03-22 10:52:01 +08:00
// prepare selection label items
2024-11-21 14:57:42 +01:00
for ( const label of dropdown . querySelectorAll < HTMLElement > ( '.ui.label' ) ) {
2024-03-27 12:50:07 +02:00
updateSelectionLabel ( label ) ;
}
2023-03-22 10:52:01 +08:00
// make the primary element (focusable) aria-friendly
2025-01-22 08:11:51 +01:00
focusable . setAttribute ( 'role' , focusable . getAttribute ( 'role' ) ? ? ( dropdown as any ) [ ariaPatchKey ] . focusableRole ) ;
focusable . setAttribute ( 'aria-haspopup' , ( dropdown as any ) [ ariaPatchKey ] . listPopupRole ) ;
2024-03-27 12:50:07 +02:00
focusable . setAttribute ( 'aria-controls' , menu . id ) ;
focusable . setAttribute ( 'aria-expanded' , 'false' ) ;
2023-03-22 10:52:01 +08:00
// use tooltip's content as aria-label if there is no aria-label
2024-03-27 12:50:07 +02:00
const tooltipContent = dropdown . getAttribute ( 'data-tooltip-content' ) ;
if ( tooltipContent && ! dropdown . getAttribute ( 'aria-label' ) ) {
dropdown . setAttribute ( 'aria-label' , tooltipContent ) ;
2023-03-22 10:52:01 +08:00
}
}
2025-03-26 10:51:22 +08:00
function attachInitElements ( dropdown : HTMLElement ) {
2025-01-22 08:11:51 +01:00
( dropdown as any ) [ ariaPatchKey ] = { } ;
2022-06-04 05:38:26 +08:00
2023-03-17 11:08:05 +08:00
// Dropdown has 2 different focusing behaviors
// * with search input: the input is focused, and it works with aria-activedescendant pointing another sibling element.
2022-06-04 05:38:26 +08:00
// * without search input (but the readonly text), the dropdown itself is focused. then the aria-activedescendant points to the element inside dropdown
2023-03-17 11:08:05 +08:00
// Some desktop screen readers may change the focus, but dropdown requires that the focus must be on its primary element, then they don't work well.
2022-06-04 05:38:26 +08:00
2023-03-17 11:08:05 +08:00
// Expected user interactions for dropdown with aria support:
2022-06-04 05:38:26 +08:00
// * user can use Tab to focus in the dropdown, then the dropdown menu (list) will be shown
// * user presses Tab on the focused dropdown to move focus to next sibling focusable element (but not the menu item)
// * user can use arrow key Up/Down to navigate between menu items
// * when user presses Enter:
// - if the menu item is clickable (eg: <a>), then trigger the click event
// - otherwise, the dropdown control (low-level code) handles the Enter event, hides the dropdown menu
2023-03-22 10:52:01 +08:00
// TODO: multiple selection is only partially supported. Check and test them one by one in the future.
2022-06-04 05:38:26 +08:00
2024-11-21 14:57:42 +01:00
const textSearch = dropdown . querySelector < HTMLElement > ( 'input.search' ) ;
2024-03-27 12:50:07 +02:00
const focusable = textSearch || dropdown ; // the primary element for focus, see comment above
if ( ! focusable ) return ;
2022-06-04 05:38:26 +08:00
2023-04-07 08:11:02 +08:00
// as a combobox, the input should not have autocomplete by default
2024-03-27 12:50:07 +02:00
if ( textSearch && ! textSearch . getAttribute ( 'autocomplete' ) ) {
textSearch . setAttribute ( 'autocomplete' , 'off' ) ;
2023-04-07 08:11:02 +08:00
}
2024-03-27 12:50:07 +02:00
let menu = $ ( dropdown ) . find ( '> .menu' ) [ 0 ] ;
if ( ! menu ) {
2023-03-22 10:52:01 +08:00
// some "multiple selection" dropdowns don't have a static menu element in HTML, we need to pre-create it to make it have correct aria attributes
2024-03-27 12:50:07 +02:00
menu = document . createElement ( 'div' ) ;
menu . classList . add ( 'menu' ) ;
dropdown . append ( menu ) ;
2023-03-22 10:52:01 +08:00
}
2023-03-17 11:08:05 +08:00
// There are 2 possible solutions about the role: combobox or menu.
// The idea is that if there is an input, then it's a combobox, otherwise it's a menu.
// Since #19861 we have prepared the "combobox" solution, but didn't get enough time to put it into practice and test before.
2024-03-27 12:50:07 +02:00
const isComboBox = dropdown . querySelectorAll ( 'input' ) . length > 0 ;
2023-03-17 11:08:05 +08:00
2025-01-22 08:11:51 +01:00
( dropdown as any ) [ ariaPatchKey ] . focusableRole = isComboBox ? 'combobox' : 'menu' ;
( dropdown as any ) [ ariaPatchKey ] . listPopupRole = isComboBox ? 'listbox' : '' ;
( dropdown as any ) [ ariaPatchKey ] . listItemRole = isComboBox ? 'option' : 'menuitem' ;
2023-03-17 11:08:05 +08:00
2024-03-27 12:50:07 +02:00
attachDomEvents ( dropdown , focusable , menu ) ;
attachStaticElements ( dropdown , focusable , menu ) ;
2023-03-22 10:52:01 +08:00
}
2022-06-04 05:38:26 +08:00
2024-11-21 14:57:42 +01:00
function attachDomEvents ( dropdown : HTMLElement , focusable : HTMLElement , menu : HTMLElement ) {
2023-03-17 11:08:05 +08:00
// when showing, it has class: ".animating.in"
// when hiding, it has class: ".visible.animating.out"
2024-03-27 12:50:07 +02:00
const isMenuVisible = ( ) = > ( menu . classList . contains ( 'visible' ) && ! menu . classList . contains ( 'out' ) ) || menu . classList . contains ( 'in' ) ;
2022-06-04 05:38:26 +08:00
2023-03-17 11:08:05 +08:00
// update aria attributes according to current active/selected item
2023-03-22 10:52:01 +08:00
const refreshAriaActiveItem = ( ) = > {
2023-03-17 11:08:05 +08:00
const menuVisible = isMenuVisible ( ) ;
2024-03-27 12:50:07 +02:00
focusable . setAttribute ( 'aria-expanded' , menuVisible ? 'true' : 'false' ) ;
2023-03-17 11:08:05 +08:00
// if there is an active item, use it (the user is navigating between items)
// otherwise use the "selected" for combobox (for the last selected item)
2024-03-27 12:50:07 +02:00
const active = $ ( menu ) . find ( '> .item.active, > .item.selected' ) [ 0 ] ;
if ( ! active ) return ;
2023-03-17 11:08:05 +08:00
// if the popup is visible and has an active/selected item, use its id as aria-activedescendant
if ( menuVisible ) {
2024-03-27 12:50:07 +02:00
focusable . setAttribute ( 'aria-activedescendant' , active . id ) ;
2025-01-22 08:11:51 +01:00
} else if ( ( dropdown as any ) [ ariaPatchKey ] . listPopupRole === 'menu' ) {
2023-03-17 11:08:05 +08:00
// for menu, when the popup is hidden, no need to keep the aria-activedescendant, and clear the active/selected item
2024-03-27 12:50:07 +02:00
focusable . removeAttribute ( 'aria-activedescendant' ) ;
active . classList . remove ( 'active' , 'selected' ) ;
2023-03-17 11:08:05 +08:00
}
2022-06-04 05:38:26 +08:00
} ;
2024-11-21 14:57:42 +01:00
dropdown . addEventListener ( 'keydown' , ( e : KeyboardEvent ) = > {
2026-02-08 14:39:09 +08:00
if ( e . isComposing ) return ;
2022-06-04 05:38:26 +08:00
// here it must use keydown event before dropdown's keyup handler, otherwise there is no Enter event in our keyup handler
if ( e . key === 'Enter' ) {
2025-04-30 10:00:36 +08:00
const elItem = menu . querySelector < HTMLElement > ( ':scope > .item.selected, .menu > .item.selected' ) ;
2023-03-08 11:26:37 +08:00
// if the selected item is clickable, then trigger the click event.
// we can not click any item without check, because Fomantic code might also handle the Enter event. that would result in double click.
2025-04-30 10:00:36 +08:00
if ( elItem ? . matches ( 'a, .js-aria-clickable' ) && ! elItem . matches ( '.tw-hidden, .filtered' ) ) {
e . preventDefault ( ) ;
elItem . click ( ) ;
}
2022-06-04 05:38:26 +08:00
}
} ) ;
// use setTimeout to run the refreshAria in next tick (to make sure the Fomantic UI code has finished its work)
2023-03-17 11:08:05 +08:00
// do not return any value, jQuery has return-value related behaviors.
// when the popup is hiding, it's better to have a small "delay", because there is a Fomantic UI animation
// without the delay for hiding, the UI will be somewhat laggy and sometimes may get stuck in the animation.
2023-03-22 10:52:01 +08:00
const deferredRefreshAriaActiveItem = ( delay = 0 ) = > { setTimeout ( refreshAriaActiveItem , delay ) } ;
2025-01-22 08:11:51 +01:00
( dropdown as any ) [ ariaPatchKey ] . deferredRefreshAriaActiveItem = deferredRefreshAriaActiveItem ;
2024-03-27 12:50:07 +02:00
dropdown . addEventListener ( 'keyup' , ( e ) = > { if ( e . key . startsWith ( 'Arrow' ) ) deferredRefreshAriaActiveItem ( ) ; } ) ;
2023-03-17 11:08:05 +08:00
// if the dropdown has been opened by focus, do not trigger the next click event again.
// otherwise the dropdown will be closed immediately, especially on Android with TalkBack
// * desktop event sequence: mousedown -> focus -> mouseup -> click
// * mobile event sequence: focus -> mousedown -> mouseup -> click
// Fomantic may stop propagation of blur event, use capture to make sure we can still get the event
let ignoreClickPreEvents = 0 , ignoreClickPreVisible = 0 ;
2023-03-22 10:52:01 +08:00
dropdown . addEventListener ( 'mousedown' , ( ) = > {
2023-03-17 11:08:05 +08:00
ignoreClickPreVisible += isMenuVisible ( ) ? 1 : 0 ;
ignoreClickPreEvents ++ ;
} , true ) ;
2023-03-22 10:52:01 +08:00
dropdown . addEventListener ( 'focus' , ( ) = > {
2023-03-17 11:08:05 +08:00
ignoreClickPreVisible += isMenuVisible ( ) ? 1 : 0 ;
ignoreClickPreEvents ++ ;
2023-03-22 10:52:01 +08:00
deferredRefreshAriaActiveItem ( ) ;
2023-03-17 11:08:05 +08:00
} , true ) ;
2023-03-22 10:52:01 +08:00
dropdown . addEventListener ( 'blur' , ( ) = > {
2023-03-17 11:08:05 +08:00
ignoreClickPreVisible = ignoreClickPreEvents = 0 ;
2023-03-22 10:52:01 +08:00
deferredRefreshAriaActiveItem ( 100 ) ;
2023-03-17 11:08:05 +08:00
} , true ) ;
2023-03-22 10:52:01 +08:00
dropdown . addEventListener ( 'mouseup' , ( ) = > {
2023-03-17 11:08:05 +08:00
setTimeout ( ( ) = > {
ignoreClickPreVisible = ignoreClickPreEvents = 0 ;
2023-03-22 10:52:01 +08:00
deferredRefreshAriaActiveItem ( 100 ) ;
2023-03-17 11:08:05 +08:00
} , 0 ) ;
} , true ) ;
2024-11-21 14:57:42 +01:00
dropdown . addEventListener ( 'click' , ( e : MouseEvent ) = > {
2023-03-17 11:08:05 +08:00
if ( isMenuVisible ( ) &&
ignoreClickPreVisible !== 2 && // dropdown is switch from invisible to visible
ignoreClickPreEvents === 2 // the click event is related to mousedown+focus
) {
e . stopPropagation ( ) ; // if the dropdown menu has been opened by focus, do not trigger the next click event again
}
ignoreClickPreEvents = ignoreClickPreVisible = 0 ;
} , true ) ;
2022-06-04 05:38:26 +08:00
}
2024-12-09 15:54:59 +08:00
// Although Fomantic Dropdown supports "hideDividers", it doesn't really work with our "scoped dividers"
// At the moment, "label dropdown items" use scopes, a sample case is:
// * a-label
// * divider
// * scope/1
// * scope/2
// * divider
// * z-label
// when the "scope/*" are filtered out, we'd like to see "a-label" and "z-label" without the divider.
export function hideScopedEmptyDividers ( container : Element ) {
const visibleItems : Element [ ] = [ ] ;
const curScopeVisibleItems : Element [ ] = [ ] ;
let curScope : string = '' , lastVisibleScope : string = '' ;
2025-03-26 10:51:22 +08:00
const isDivider = ( item : Element ) = > item . classList . contains ( 'divider' ) ;
const isScopedDivider = ( item : Element ) = > isDivider ( item ) && item . hasAttribute ( 'data-scope' ) ;
2024-12-09 15:54:59 +08:00
const hideDivider = ( item : Element ) = > item . classList . add ( 'hidden' , 'transition' ) ; // dropdown has its own classes to hide items
2025-03-26 10:51:22 +08:00
const showDivider = ( item : Element ) = > item . classList . remove ( 'hidden' , 'transition' ) ;
const isHidden = ( item : Element ) = > item . classList . contains ( 'hidden' ) || item . classList . contains ( 'filtered' ) || item . classList . contains ( 'tw-hidden' ) ;
2024-12-09 15:54:59 +08:00
const handleScopeSwitch = ( itemScope : string ) = > {
if ( curScopeVisibleItems . length === 1 && isScopedDivider ( curScopeVisibleItems [ 0 ] ) ) {
hideDivider ( curScopeVisibleItems [ 0 ] ) ;
} else if ( curScopeVisibleItems . length ) {
if ( isScopedDivider ( curScopeVisibleItems [ 0 ] ) && lastVisibleScope === curScope ) {
hideDivider ( curScopeVisibleItems [ 0 ] ) ;
curScopeVisibleItems . shift ( ) ;
}
visibleItems . push ( . . . curScopeVisibleItems ) ;
lastVisibleScope = curScope ;
}
curScope = itemScope ;
curScopeVisibleItems . length = 0 ;
} ;
2025-03-26 10:51:22 +08:00
// reset hidden dividers
queryElems ( container , '.divider' , showDivider ) ;
2024-12-09 15:54:59 +08:00
// hide the scope dividers if the scope items are empty
for ( const item of container . children ) {
const itemScope = item . getAttribute ( 'data-scope' ) || '' ;
if ( itemScope !== curScope ) {
handleScopeSwitch ( itemScope ) ;
}
2025-03-26 10:51:22 +08:00
if ( ! isHidden ( item ) ) {
2024-12-09 15:54:59 +08:00
curScopeVisibleItems . push ( item as HTMLElement ) ;
}
}
handleScopeSwitch ( '' ) ;
// hide all leading and trailing dividers
while ( visibleItems . length ) {
2025-03-26 10:51:22 +08:00
if ( ! isDivider ( visibleItems [ 0 ] ) ) break ;
2024-12-09 15:54:59 +08:00
hideDivider ( visibleItems [ 0 ] ) ;
visibleItems . shift ( ) ;
}
while ( visibleItems . length ) {
2025-03-26 10:51:22 +08:00
if ( ! isDivider ( visibleItems [ visibleItems . length - 1 ] ) ) break ;
2024-12-09 15:54:59 +08:00
hideDivider ( visibleItems [ visibleItems . length - 1 ] ) ;
visibleItems . pop ( ) ;
}
// hide all duplicate dividers, hide current divider if next sibling is still divider
// no need to update "visibleItems" array since this is the last loop
2025-03-26 10:51:22 +08:00
for ( let i = 0 ; i < visibleItems . length - 1 ; i ++ ) {
if ( ! visibleItems [ i ] . matches ( '.divider' ) ) continue ;
if ( visibleItems [ i + 1 ] . matches ( '.divider' ) ) hideDivider ( visibleItems [ i ] ) ;
2024-12-09 15:54:59 +08:00
}
}
2025-01-02 01:21:13 +08:00
2025-09-14 18:15:06 +02:00
function onResponseKeepSelectedItem ( dropdown : typeof $ | HTMLElement , selectedValue : string ) {
2025-01-02 01:21:13 +08:00
// There is a bug in fomantic dropdown when using "apiSettings" to fetch data
// * when there is a selected item, the dropdown insists on hiding the selected one from the list:
// * in the "filter" function: ('[data-value="'+value+'"]').addClass(className.filtered)
//
// When user selects one item, and click the dropdown again,
// then the dropdown only shows other items and will select another (wrong) one.
// It can't be easily fix by using setTimeout(patch, 0) in `onResponse` because the `onResponse` is called before another `setTimeout(..., timeLeft)`
// Fortunately, the "timeLeft" is controlled by "loadingDuration" which is always zero at the moment, so we can use `setTimeout(..., 10)`
2025-01-22 08:11:51 +01:00
const elDropdown = ( dropdown instanceof HTMLElement ) ? dropdown : ( dropdown as any ) [ 0 ] ;
2025-01-02 01:21:13 +08:00
setTimeout ( ( ) = > {
queryElems ( elDropdown , ` .menu .item[data-value=" ${ CSS . escape ( selectedValue ) } "].filtered ` , ( el ) = > el . classList . remove ( 'filtered' ) ) ;
$ ( elDropdown ) . dropdown ( 'set selected' , selectedValue ? ? '' ) ;
} , 10 ) ;
}