settings_users: Extract user sort comparators to new user_sort module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-10-03 01:16:10 -07:00 committed by Tim Abbott
parent 30ae936793
commit 0efd9f7bdd
7 changed files with 60 additions and 59 deletions

View File

@ -241,6 +241,7 @@ EXEMPT_FILES = make_set(
"web/src/user_groups_settings_ui.js", "web/src/user_groups_settings_ui.js",
"web/src/user_profile.js", "web/src/user_profile.js",
"web/src/user_settings.ts", "web/src/user_settings.ts",
"web/src/user_sort.js",
"web/src/user_status.js", "web/src/user_status.js",
"web/src/user_status_ui.js", "web/src/user_status_ui.js",
"web/src/user_topics.ts", "web/src/user_topics.ts",

View File

@ -20,6 +20,7 @@ import * as settings_panel_menu from "./settings_panel_menu";
import * as timerender from "./timerender"; import * as timerender from "./timerender";
import * as user_deactivation_ui from "./user_deactivation_ui"; import * as user_deactivation_ui from "./user_deactivation_ui";
import * as user_profile from "./user_profile"; import * as user_profile from "./user_profile";
import * as user_sort from "./user_sort";
const section = { const section = {
active: {}, active: {},
@ -27,45 +28,12 @@ const section = {
bots: {}, bots: {},
}; };
function compare_a_b(a, b) {
if (a > b) {
return 1;
} else if (a === b) {
return 0;
}
return -1;
}
export function sort_email(a, b) {
const email_a = a.delivery_email;
const email_b = b.delivery_email;
if (email_a === null && email_b === null) {
// If both the emails are hidden, we sort the list by name.
return compare_a_b(a.full_name.toLowerCase(), b.full_name.toLowerCase());
}
if (email_a === null) {
// User with hidden should be at last.
return 1;
}
if (email_b === null) {
// User with hidden should be at last.
return -1;
}
return compare_a_b(email_a.toLowerCase(), email_b.toLowerCase());
}
function sort_bot_email(a, b) { function sort_bot_email(a, b) {
function email(bot) { function email(bot) {
return (bot.display_email || "").toLowerCase(); return (bot.display_email || "").toLowerCase();
} }
return compare_a_b(email(a), email(b)); return user_sort.compare_a_b(email(a), email(b));
}
function sort_role(a, b) {
return compare_a_b(a.role, b.role);
} }
function sort_bot_owner(a, b) { function sort_bot_owner(a, b) {
@ -73,20 +41,16 @@ function sort_bot_owner(a, b) {
return (bot.bot_owner_full_name || "").toLowerCase(); return (bot.bot_owner_full_name || "").toLowerCase();
} }
return compare_a_b(owner_name(a), owner_name(b)); return user_sort.compare_a_b(owner_name(a), owner_name(b));
} }
function sort_last_active(a, b) { function sort_last_active(a, b) {
return compare_a_b( return user_sort.compare_a_b(
presence.last_active_date(a.user_id) || 0, presence.last_active_date(a.user_id) || 0,
presence.last_active_date(b.user_id) || 0, presence.last_active_date(b.user_id) || 0,
); );
} }
export function sort_user_id(a, b) {
return compare_a_b(a.user_id, b.user_id);
}
function get_user_info_row(user_id) { function get_user_info_row(user_id) {
return $(`tr.user_row[data-user-id='${CSS.escape(user_id)}']`); return $(`tr.user_row[data-user-id='${CSS.escape(user_id)}']`);
} }
@ -301,7 +265,7 @@ section.bots.create_table = () => {
sort_fields: { sort_fields: {
email: sort_bot_email, email: sort_bot_email,
bot_owner: sort_bot_owner, bot_owner: sort_bot_owner,
role: sort_role, role: user_sort.sort_role,
...ListWidget.generic_sort_functions("alphabetic", ["full_name", "bot_type"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name", "bot_type"]),
}, },
$simplebar_container: $("#admin-bot-list .progressive-table-wrapper"), $simplebar_container: $("#admin-bot-list .progressive-table-wrapper"),
@ -328,10 +292,10 @@ section.active.create_table = (active_users) => {
$parent_container: $("#admin-user-list").expectOne(), $parent_container: $("#admin-user-list").expectOne(),
init_sort: "full_name_alphabetic", init_sort: "full_name_alphabetic",
sort_fields: { sort_fields: {
email: sort_email, email: user_sort.sort_email,
last_active: sort_last_active, last_active: sort_last_active,
role: sort_role, role: user_sort.sort_role,
id: sort_user_id, id: user_sort.sort_user_id,
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
}, },
$simplebar_container: $("#admin-user-list .progressive-table-wrapper"), $simplebar_container: $("#admin-user-list .progressive-table-wrapper"),
@ -358,9 +322,9 @@ section.deactivated.create_table = (deactivated_users) => {
$parent_container: $("#admin-deactivated-users-list").expectOne(), $parent_container: $("#admin-deactivated-users-list").expectOne(),
init_sort: "full_name_alphabetic", init_sort: "full_name_alphabetic",
sort_fields: { sort_fields: {
email: sort_email, email: user_sort.sort_email,
role: sort_role, role: user_sort.sort_role,
id: sort_user_id, id: user_sort.sort_user_id,
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
}, },
$simplebar_container: $("#admin-deactivated-users-list .progressive-table-wrapper"), $simplebar_container: $("#admin-deactivated-users-list .progressive-table-wrapper"),

View File

@ -7,8 +7,8 @@ import * as add_subscribers_pill from "./add_subscribers_pill";
import * as ListWidget from "./list_widget"; import * as ListWidget from "./list_widget";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as settings_users from "./settings_users";
import * as stream_create_subscribers_data from "./stream_create_subscribers_data"; import * as stream_create_subscribers_data from "./stream_create_subscribers_data";
import * as user_sort from "./user_sort";
let pill_widget; let pill_widget;
let all_users_list_widget; let all_users_list_widget;
@ -97,8 +97,8 @@ export function build_widgets() {
return render_new_stream_user(item); return render_new_stream_user(item);
}, },
sort_fields: { sort_fields: {
email: settings_users.sort_email, email: user_sort.sort_email,
id: settings_users.sort_user_id, id: user_sort.sort_user_id,
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
}, },
filter: { filter: {

View File

@ -16,11 +16,11 @@ import {page_params} from "./page_params";
import * as peer_data from "./peer_data"; import * as peer_data from "./peer_data";
import * as people from "./people"; import * as people from "./people";
import * as scroll_util from "./scroll_util"; import * as scroll_util from "./scroll_util";
import * as settings_users from "./settings_users";
import * as stream_data from "./stream_data"; import * as stream_data from "./stream_data";
import * as stream_settings_containers from "./stream_settings_containers"; import * as stream_settings_containers from "./stream_settings_containers";
import * as sub_store from "./sub_store"; import * as sub_store from "./sub_store";
import * as subscriber_api from "./subscriber_api"; import * as subscriber_api from "./subscriber_api";
import * as user_sort from "./user_sort";
export let pill_widget; export let pill_widget;
let current_stream_id; let current_stream_id;
@ -127,8 +127,8 @@ function make_list_widget({$parent_container, name, user_ids, user_can_remove_su
}, },
$parent_container: $("#stream_members_list").expectOne(), $parent_container: $("#stream_members_list").expectOne(),
sort_fields: { sort_fields: {
email: settings_users.sort_email, email: user_sort.sort_email,
id: settings_users.sort_user_id, id: user_sort.sort_user_id,
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
}, },
$simplebar_container, $simplebar_container,

View File

@ -7,8 +7,8 @@ import * as add_subscribers_pill from "./add_subscribers_pill";
import * as ListWidget from "./list_widget"; import * as ListWidget from "./list_widget";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as settings_users from "./settings_users";
import * as user_group_create_members_data from "./user_group_create_members_data"; import * as user_group_create_members_data from "./user_group_create_members_data";
import * as user_sort from "./user_sort";
let pill_widget; let pill_widget;
let all_users_list_widget; let all_users_list_widget;
@ -95,8 +95,8 @@ export function build_widgets() {
$parent_container: $add_people_container, $parent_container: $add_people_container,
get_item: people.get_by_user_id, get_item: people.get_by_user_id,
sort_fields: { sort_fields: {
email: settings_users.sort_email, email: user_sort.sort_email,
id: settings_users.sort_user_id, id: user_sort.sort_user_id,
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
}, },
modifier_html(user) { modifier_html(user) {

View File

@ -15,8 +15,8 @@ import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as scroll_util from "./scroll_util"; import * as scroll_util from "./scroll_util";
import * as settings_data from "./settings_data"; import * as settings_data from "./settings_data";
import * as settings_users from "./settings_users";
import * as user_groups from "./user_groups"; import * as user_groups from "./user_groups";
import * as user_sort from "./user_sort";
export let pill_widget; export let pill_widget;
let current_group_id; let current_group_id;
@ -67,8 +67,8 @@ function make_list_widget({$parent_container, name, user_ids}) {
get_item: ListWidget.default_get_item, get_item: ListWidget.default_get_item,
$parent_container, $parent_container,
sort_fields: { sort_fields: {
email: settings_users.sort_email, email: user_sort.sort_email,
id: settings_users.sort_user_id, id: user_sort.sort_user_id,
...ListWidget.generic_sort_functions("alphabetic", ["full_name"]), ...ListWidget.generic_sort_functions("alphabetic", ["full_name"]),
}, },
modifier_html(item) { modifier_html(item) {

36
web/src/user_sort.js Normal file
View File

@ -0,0 +1,36 @@
export function compare_a_b(a, b) {
if (a > b) {
return 1;
} else if (a === b) {
return 0;
}
return -1;
}
export function sort_email(a, b) {
const email_a = a.delivery_email;
const email_b = b.delivery_email;
if (email_a === null && email_b === null) {
// If both the emails are hidden, we sort the list by name.
return compare_a_b(a.full_name.toLowerCase(), b.full_name.toLowerCase());
}
if (email_a === null) {
// User with hidden should be at last.
return 1;
}
if (email_b === null) {
// User with hidden should be at last.
return -1;
}
return compare_a_b(email_a.toLowerCase(), email_b.toLowerCase());
}
export function sort_role(a, b) {
return compare_a_b(a.role, b.role);
}
export function sort_user_id(a, b) {
return compare_a_b(a.user_id, b.user_id);
}