From 914bb1e8d66955365a6d4e78b388bc11d74b5b7b Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Thu, 14 Aug 2025 18:42:17 +0000 Subject: [PATCH] Bug 40654: Fix how rank/priority is adjusted when changing from the UI To test: 0 - APPLY PATCH 1 - Place 50 holds on a bib record https://github.com/kidclamp/handy-koha-script/blob/main/randhold.pl 2 - View the holds page for the record 3 - Sort the holds alphabetically 4 - Change the dropdown priortiy for the alphabetically first hold to 1 5 - All the priorities should now ajust accordingly but they will NOT be resorted on the fly 6 - Play with adjusting some priorities, making sure everything correctly adjusts. 7 - Click 'Update holds' to make sure the priorities adjust right 8 - Make sure you test with the 'del' option and the adjustments still work. Signed-off-by: David Flater --- .../prog/en/modules/reserve/request.tt | 61 ++++++++++++++----- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt index f0e3e2c18c1..56bb9aa8d71 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt @@ -1756,27 +1756,56 @@ }); var prev_rank_request; $("select[name=rank-request]").on("focus", function() { - prev_rank_request = $(this).val(); - var row = $(this).parents("tr:first"); + prev_rank_request = parseInt($(this).val()); }).change(function() { - var row = $(this).parents("tr:first"); - var value = parseInt($(this).val()); - var found_holds = $("select[name='rank-request'][disabled='disabled']").length ; //Count how many are found - if( !isNaN(value) ) { //If moved to 'del' - var after = row.parent().find(`tr:nth-child(${value+found_holds})`); //Go to the row 1 after the new value (and skip found holds) - if (prev_rank_request > value) { - row.insertBefore(after); - } else { - row.insertAfter(after); + var this_rank = $(this); + var new_rank = parseInt(this_rank.val()); + var old_rank = prev_rank_request; + + //if new_rank is NaN, bail + if ( isNaN( new_rank ) ) return; + + if ( isNaN( old_rank ) ) { + $("select[name=rank-request]").not('[disabled]').not(this_rank).each(function() { + var current_rank = parseInt($(this).val()); + if ( !isNaN(current_rank) && current_rank >= new_rank ) { + $(this).val(current_rank + 1); + } + }); + prev_rank_request = new_rank; + return; + } + + if ( isNaN( new_rank ) ) { + if ( !isNaN( old_rank ) ) { + $("select[name=rank-request]").not('[disabled]').not(this).each(function() { + var current_rank = parseInt($(this).val()); + if ( !isNaN(current_rank) && current_rank > old_rank ) { + $(this).val(current_rank - 1); + } + }); } + return; } - var next_priority = 1; - $("select[name=rank-request]").each(function () { - if( isNaN( $(this).val() ) ){ return true; } //Don't reset found or del holds - $(this).val(next_priority); - next_priority++; + // Adjust ranks + $("select[name=rank-request]").not('[disabled]').not(this).each(function() { + var current_rank = parseInt($(this).val()); + //if current_rank is NaN, bail + if ( isNaN( current_rank ) ) return; + + if ( old_rank > new_rank ) { + if ( current_rank >= new_rank && current_rank < old_rank ) { + $(this).val(current_rank + 1); + } + } else { + if ( current_rank > old_rank && current_rank <= new_rank ) { + $(this).val(current_rank - 1); + } + } }); + + prev_rank_request = new_rank; }); $(".clear-date").on("click",function(e){ -- 2.39.5