@@ -237,7 +237,47 @@ <h6 class="filter-header">
237237 } ) ;
238238 } ) ;
239239
240- // Add clear all functionality
240+ // Reordering function to pin selected checkboxes to the top
241+ function reorderList ( containerSelector , checkboxSelector ) {
242+ const container = document . querySelector ( containerSelector ) ;
243+ if ( ! container ) return ;
244+ const items = Array . from ( container . children ) ;
245+ items . sort ( ( a , b ) => {
246+ const aCheckbox = a . querySelector ( checkboxSelector ) ;
247+ const bCheckbox = b . querySelector ( checkboxSelector ) ;
248+
249+ // Check if one is selected and the other is not
250+ if ( aCheckbox . checked && ! bCheckbox . checked ) return - 1 ;
251+ if ( ! aCheckbox . checked && bCheckbox . checked ) return 1 ;
252+
253+ // If both are either checked or unchecked, sort alphabetically by label text
254+ const aLabelText = a . querySelector ( 'label' ) . textContent . trim ( ) . toLowerCase ( ) ;
255+ const bLabelText = b . querySelector ( 'label' ) . textContent . trim ( ) . toLowerCase ( ) ;
256+ return aLabelText . localeCompare ( bLabelText ) ;
257+ } ) ;
258+ // Clear and reappend in new order
259+ container . innerHTML = '' ;
260+ items . forEach ( item => container . appendChild ( item ) ) ;
261+ }
262+
263+ // Add event listeners to re-sort subjects whenever a subject checkbox changes
264+ document . querySelectorAll ( '.form-check-subjects' ) . forEach ( checkbox => {
265+ checkbox . addEventListener ( 'change' , ( ) => {
266+ reorderList ( '.subject-list' , '.form-check-subjects' ) ;
267+ } ) ;
268+ } ) ;
269+
270+ // Add event listeners to re-sort disciplines whenever a discipline checkbox changes
271+ document . querySelectorAll ( '.form-check-disciplines' ) . forEach ( checkbox => {
272+ checkbox . addEventListener ( 'change' , ( ) => {
273+ reorderList ( '.discipline-list' , '.form-check-disciplines' ) ;
274+ } ) ;
275+ } ) ;
276+
277+ // Call reorder on page load in case some items are selected by default
278+ reorderList ( '.subject-list' , '.form-check-subjects' ) ;
279+ reorderList ( '.discipline-list' , '.form-check-disciplines' ) ;
280+
241281 const resetButton = document . querySelector ( 'button[type="reset"]' ) ;
242282 resetButton . addEventListener ( 'click' , function ( e ) {
243283 e . preventDefault ( ) ; // Prevent default reset behavior
@@ -261,7 +301,10 @@ <h6 class="filter-header">
261301 document . getElementById ( 'to_time' ) . value = '' ;
262302
263303 updateButtonState ( ) ;
264-
304+
305+ // Reorder lists after clearing filters (so any selected items, if any, are sorted correctly)
306+ reorderList ( '.subject-list' , '.form-check-subjects' ) ;
307+ reorderList ( '.discipline-list' , '.form-check-disciplines' ) ;
265308 } ) ;
266309
267310 // Add weekdays handling
@@ -276,7 +319,7 @@ <h6 class="filter-header">
276319 document . querySelectorAll ( '.day-checkbox' ) . forEach ( checkbox => {
277320 checkbox . addEventListener ( 'change' , ( ) => {
278321 updateWeekdays ( ) ;
279- updateFilterSummary ( ) ;
322+ updateButtonState ( ) ;
280323 } ) ;
281324 } ) ;
282325
0 commit comments