@@ -9,6 +9,7 @@ const fomanticDropdownFn = $.fn.dropdown;
99// use our own `$().dropdown` function to patch Fomantic's dropdown module
1010export function initAriaDropdownPatch ( ) {
1111 if ( $ . fn . dropdown === ariaDropdownFn ) throw new Error ( 'initAriaDropdownPatch could only be called once' ) ;
12+ $ . fn . dropdown . settings . onAfterFiltered = onAfterFiltered ;
1213 $ . fn . dropdown = ariaDropdownFn ;
1314 $ . fn . fomanticExt . onResponseKeepSelectedItem = onResponseKeepSelectedItem ;
1415 ( ariaDropdownFn as FomanticInitFunction ) . settings = fomanticDropdownFn . settings ;
@@ -67,8 +68,9 @@ function updateSelectionLabel(label: HTMLElement) {
6768 }
6869}
6970
70- function processMenuItems ( $dropdown : any , dropdownCall : any ) {
71- const hideEmptyDividers = dropdownCall ( 'setting' , 'hideDividers' ) === 'empty' ;
71+ function onAfterFiltered ( this : any ) {
72+ const $dropdown = $ ( this ) ;
73+ const hideEmptyDividers = $dropdown . dropdown ( 'setting' , 'hideDividers' ) === 'empty' ;
7274 const itemsMenu = $dropdown [ 0 ] . querySelector ( '.scrolling.menu' ) || $dropdown [ 0 ] . querySelector ( '.menu' ) ;
7375 if ( hideEmptyDividers ) hideScopedEmptyDividers ( itemsMenu ) ;
7476}
@@ -77,18 +79,6 @@ function processMenuItems($dropdown: any, dropdownCall: any) {
7779function delegateDropdownModule ( $dropdown : any ) {
7880 const dropdownCall = fomanticDropdownFn . bind ( $dropdown ) ;
7981
80- const oldFilterItems = dropdownCall ( 'internal' , 'filterItems' ) ;
81- dropdownCall ( 'internal' , 'filterItems' , function ( this : any , ...args : any [ ] ) {
82- oldFilterItems . call ( this , ...args ) ;
83- processMenuItems ( $dropdown , dropdownCall ) ;
84- } ) ;
85-
86- const oldShow = dropdownCall ( 'internal' , 'show' ) ;
87- dropdownCall ( 'internal' , 'show' , function ( this : any , ...args : any [ ] ) {
88- oldShow . call ( this , ...args ) ;
89- processMenuItems ( $dropdown , dropdownCall ) ;
90- } ) ;
91-
9282 // the "template" functions are used for dynamic creation (eg: AJAX)
9383 const dropdownTemplates = { ...dropdownCall ( 'setting' , 'templates' ) , t : performance . now ( ) } ;
9484 const dropdownTemplatesMenuOld = dropdownTemplates . menu ;
@@ -301,9 +291,11 @@ export function hideScopedEmptyDividers(container: Element) {
301291 const visibleItems : Element [ ] = [ ] ;
302292 const curScopeVisibleItems : Element [ ] = [ ] ;
303293 let curScope : string = '' , lastVisibleScope : string = '' ;
304- const isScopedDivider = ( item : Element ) => item . matches ( '.divider' ) && item . hasAttribute ( 'data-scope' ) ;
294+ const isDivider = ( item : Element ) => item . classList . contains ( 'divider' ) ;
295+ const isScopedDivider = ( item : Element ) => isDivider ( item ) && item . hasAttribute ( 'data-scope' ) ;
305296 const hideDivider = ( item : Element ) => item . classList . add ( 'hidden' , 'transition' ) ; // dropdown has its own classes to hide items
306-
297+ const showDivider = ( item : Element ) => item . classList . remove ( 'hidden' , 'transition' ) ;
298+ const isHidden = ( item : Element ) => item . classList . contains ( 'hidden' ) || item . classList . contains ( 'filtered' ) || item . classList . contains ( 'tw-hidden' ) ;
307299 const handleScopeSwitch = ( itemScope : string ) => {
308300 if ( curScopeVisibleItems . length === 1 && isScopedDivider ( curScopeVisibleItems [ 0 ] ) ) {
309301 hideDivider ( curScopeVisibleItems [ 0 ] ) ;
@@ -319,34 +311,37 @@ export function hideScopedEmptyDividers(container: Element) {
319311 curScopeVisibleItems . length = 0 ;
320312 } ;
321313
314+ // reset hidden dividers
315+ queryElems ( container , '.divider' , showDivider ) ;
316+
322317 // hide the scope dividers if the scope items are empty
323318 for ( const item of container . children ) {
324319 const itemScope = item . getAttribute ( 'data-scope' ) || '' ;
325320 if ( itemScope !== curScope ) {
326321 handleScopeSwitch ( itemScope ) ;
327322 }
328- if ( ! item . classList . contains ( 'filtered' ) && ! item . classList . contains ( 'tw-hidden' ) ) {
323+ if ( ! isHidden ( item ) ) {
329324 curScopeVisibleItems . push ( item as HTMLElement ) ;
330325 }
331326 }
332327 handleScopeSwitch ( '' ) ;
333328
334329 // hide all leading and trailing dividers
335330 while ( visibleItems . length ) {
336- if ( ! visibleItems [ 0 ] . matches ( '.divider' ) ) break ;
331+ if ( ! isDivider ( visibleItems [ 0 ] ) ) break ;
337332 hideDivider ( visibleItems [ 0 ] ) ;
338333 visibleItems . shift ( ) ;
339334 }
340335 while ( visibleItems . length ) {
341- if ( ! visibleItems [ visibleItems . length - 1 ] . matches ( '.divider' ) ) break ;
336+ if ( ! isDivider ( visibleItems [ visibleItems . length - 1 ] ) ) break ;
342337 hideDivider ( visibleItems [ visibleItems . length - 1 ] ) ;
343338 visibleItems . pop ( ) ;
344339 }
345340 // hide all duplicate dividers, hide current divider if next sibling is still divider
346341 // no need to update "visibleItems" array since this is the last loop
347- for ( const item of visibleItems ) {
348- if ( ! item . matches ( '.divider' ) ) continue ;
349- if ( item . nextElementSibling ?. matches ( '.divider' ) ) hideDivider ( item ) ;
342+ for ( let i = 0 ; i < visibleItems . length - 1 ; i ++ ) {
343+ if ( ! visibleItems [ i ] . matches ( '.divider' ) ) continue ;
344+ if ( visibleItems [ i + 1 ] . matches ( '.divider' ) ) hideDivider ( visibleItems [ i + 1 ] ) ;
350345 }
351346}
352347
0 commit comments