From 34534c022a25edc3870e2874043d05df1c4d859b Mon Sep 17 00:00:00 2001 From: Merci Jacob Date: Thu, 30 Jan 2025 17:12:02 +0200 Subject: [PATCH] keep the sorting client-side for the search page --- modules/core/js_modules/actions/search.js | 3 +- modules/core/js_modules/route_handlers.js | 6 ++++ modules/core/message_list_functions.php | 8 ++++++ modules/core/site.js | 34 ++++++++--------------- modules/imap/functions.php | 4 ++- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/modules/core/js_modules/actions/search.js b/modules/core/js_modules/actions/search.js index 057ff2afe..e7fe5a138 100644 --- a/modules/core/js_modules/actions/search.js +++ b/modules/core/js_modules/actions/search.js @@ -12,8 +12,7 @@ function performSearch(routeParams) { Object.values(response.formatted_message_list).forEach((message) => { Hm_Utils.tbody().append(message['0']); }); - // sort by arrival date - Hm_Message_List.sort(4); + Hm_Message_List.sort(getParam('sort') || 'arrival'); } Hm_Message_List.check_empty_list(); } diff --git a/modules/core/js_modules/route_handlers.js b/modules/core/js_modules/route_handlers.js index 230f55592..e488b8b5f 100644 --- a/modules/core/js_modules/route_handlers.js +++ b/modules/core/js_modules/route_handlers.js @@ -38,6 +38,7 @@ function applySearchPageHandlers(routeParams) { Hm_Message_List.select_combined_view(); sortHandlerForMessageListAndSearchPage(); $('.search_reset').on("click", Hm_Utils.reset_search_form); + $('.combined_sort').on("change", function() { Hm_Message_List.sort($(this).val()); }); performSearch(routeParams); @@ -86,6 +87,11 @@ function applyMessaleListPageHandlers(routeParams) { if (routeParams.list_path === 'github_all') { return applyGithubMessageListPageHandler(routeParams); } + + + $('.combined_sort').on("change", function() { + sortCombinedLists($(this).val()); + }); // TODO: Refactor this handler to be more modular(applicable only for the imap list type) return applyImapMessageListPageHandlers(routeParams); diff --git a/modules/core/message_list_functions.php b/modules/core/message_list_functions.php index fea3bb64d..c580ae339 100644 --- a/modules/core/message_list_functions.php +++ b/modules/core/message_list_functions.php @@ -343,6 +343,14 @@ function date_callback($vals, $style, $output_mod) { return sprintf('%s', $snooze_class, $output_mod->html_safe(date('r', $vals[1])), $output_mod->html_safe($vals[0]), $output_mod->html_safe($vals[1])); }} +function dates_holders_callback($vals) { + $res = ''; + $res .= ''; + $res .= ''; + $res .= ''; + return $res; +} + /** * Callback for an icon in a message list row * @subpackage core/functions diff --git a/modules/core/site.js b/modules/core/site.js index 3f19f27ec..7ba13cd7e 100644 --- a/modules/core/site.js +++ b/modules/core/site.js @@ -528,31 +528,22 @@ function Message_List() { var aval; var bval; var sort_result = listitems.sort(function(a, b) { - switch (Math.abs(fld)) { - case 1: - case 2: - case 3: - aval = $($('td', a)[Math.abs(fld)]).text().replace(/^\s+/g, ''); - bval = $($('td', b)[Math.abs(fld)]).text().replace(/^\s+/g, ''); - break; - case 4: - default: - aval = $('input', $($('td', a)[Math.abs(fld)])).val(); - bval = $('input', $($('td', b)[Math.abs(fld)])).val(); - break; - } - if (fld == 4 || fld == -4 || !fld) { - if (fld == -4) { + const sortField = fld.replace('-', ''); + if (['arrival', 'date'].includes(sortField)) { + aval = new Date($(`input.${sortField}`, $('td.dates', a)).val()); + bval = new Date($(`input.${sortField}`, $('td.dates', b)).val()); + if (fld.startsWith('-')) { return aval - bval; } return bval - aval; } - else { - if (fld && fld < 0) { - return bval.toUpperCase().localeCompare(aval.toUpperCase()); - } - return aval.toUpperCase().localeCompare(bval.toUpperCase()); + + aval = $(`td.${sortField}`, a).text().replace(/^\s+/g, ''); + bval = $(`td.${sortField}`, b).text().replace(/^\s+/g, ''); + if (fld.startsWith('-')) { + return bval.toUpperCase().localeCompare(aval.toUpperCase()); } + return aval.toUpperCase().localeCompare(bval.toUpperCase()); }); this.sort_fld = fld; Hm_Utils.tbody().html(''); @@ -1810,9 +1801,6 @@ var hasLeadingOrTrailingSpaces = function(str) { var Hm_Message_List = new Message_List(); function sortHandlerForMessageListAndSearchPage() { - $('.combined_sort').on("change", function() { - sortCombinedLists($(this).val()); - }); $('.source_link').on("click", function() { $('.list_sources').toggle(); $('#list_controls_menu').hide(); return false; }); if (getListPathParam() == 'unread' && $('.menu_unread > a').css('font-weight') == 'bold') { $('.menu_unread > a').css('font-weight', 'normal'); diff --git a/modules/imap/functions.php b/modules/imap/functions.php index f1d718ad6..34d4d1068 100644 --- a/modules/imap/functions.php +++ b/modules/imap/functions.php @@ -316,6 +316,7 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false, array('safe_output_callback', 'source', $source), array('safe_output_callback', 'from'.$nofrom, $from, null, str_replace(array($from, '<', '>'), '', $msg['from'])), array('date_callback', $date, $timestamp), + array('dates_holders_callback', $msg['internal_date'], $msg['date']), ), $id, $style, @@ -330,7 +331,8 @@ function format_imap_message_list($msg_list, $output_module, $parent_list=false, array('safe_output_callback', 'from'.$nofrom, $from, null, str_replace(array($from, '<', '>'), '', $msg['from'])), array('subject_callback', $subject, $url, $flags, null, $preview_msg), array('date_callback', $date, $timestamp, $is_snoozed), - array('icon_callback', $flags) + array('icon_callback', $flags), + array('dates_holders_callback', $msg['internal_date'], $msg['date']), ), $id, $style,