user_search: Convert module to typescript.

This commit is contained in:
evykassirer
2024-02-20 12:22:11 -08:00
committed by Tim Abbott
parent 860150525c
commit b30f6bb705

View File

@@ -1,4 +1,5 @@
import $ from "jquery"; import $ from "jquery";
import assert from "minimalistic-assert";
import * as buddy_data from "./buddy_data"; import * as buddy_data from "./buddy_data";
import * as popovers from "./popovers"; import * as popovers from "./popovers";
@@ -11,42 +12,53 @@ export class UserSearch {
// details of populating the list when we change. // details of populating the list when we change.
$widget = $("#user_search_section").expectOne(); $widget = $("#user_search_section").expectOne();
$input = $(".user-list-filter").expectOne(); $input = $<HTMLInputElement>(".user-list-filter").expectOne();
_reset_items: () => void;
_update_list: () => void;
_on_focus: () => void;
constructor(opts) { constructor(opts: {reset_items: () => void; update_list: () => void; on_focus: () => void}) {
this._reset_items = opts.reset_items; this._reset_items = opts.reset_items;
this._update_list = opts.update_list; this._update_list = opts.update_list;
this._on_focus = opts.on_focus; this._on_focus = opts.on_focus;
$("#clear_search_people_button").on("click", () => this.clear_search()); $("#clear_search_people_button").on("click", () => {
$("#userlist-header").on("click", () => this.toggle_filter_displayed()); this.clear_search();
});
$("#userlist-header").on("click", () => {
this.toggle_filter_displayed();
});
this.$input.on("input", () => { this.$input.on("input", () => {
buddy_data.set_is_searching_users(this.$input.val() !== ""); buddy_data.set_is_searching_users(this.$input.val() !== "");
opts.update_list(); opts.update_list();
}); });
this.$input.on("focus", (e) => this.on_focus(e)); this.$input.on("focus", (e) => {
this.on_focus(e);
});
} }
input_field() { input_field(): JQuery {
return this.$input; return this.$input;
} }
text() { text(): string {
return this.$input.val().trim(); const input_val = this.$input.val();
assert(input_val !== undefined);
return input_val.trim();
} }
searching() { searching(): boolean {
return this.$input.is(":focus"); return this.$input.is(":focus");
} }
empty() { empty(): boolean {
return this.text() === ""; return this.text() === "";
} }
// This clears search input but doesn't close // This clears search input but doesn't close
// the search widget unless it was already empty. // the search widget unless it was already empty.
clear_search() { clear_search(): void {
buddy_data.set_is_searching_users(false); buddy_data.set_is_searching_users(false);
if (this.empty()) { if (this.empty()) {
@@ -60,35 +72,35 @@ export class UserSearch {
} }
// This always clears and closes search. // This always clears and closes search.
clear_and_hide_search() { clear_and_hide_search(): void {
this.clear_search(); this.clear_search();
this._update_list(); this._update_list();
this.close_widget(); this.close_widget();
} }
hide_widget() { hide_widget(): void {
this.$widget.addClass("notdisplayed"); this.$widget.addClass("notdisplayed");
resize.resize_sidebars(); resize.resize_sidebars();
} }
show_widget() { show_widget(): void {
// Hide all the popovers. // Hide all the popovers.
popovers.hide_all(); popovers.hide_all();
this.$widget.removeClass("notdisplayed"); this.$widget.removeClass("notdisplayed");
resize.resize_sidebars(); resize.resize_sidebars();
} }
widget_shown() { widget_shown(): boolean {
return this.$widget.hasClass("notdisplayed"); return this.$widget.hasClass("notdisplayed");
} }
close_widget() { close_widget(): void {
this.$input.trigger("blur"); this.$input.trigger("blur");
this.hide_widget(); this.hide_widget();
this._reset_items(); this._reset_items();
} }
expand_column() { expand_column(): void {
const $column = this.$input.closest(".app-main [class^='column-']"); const $column = this.$input.closest(".app-main [class^='column-']");
if (!$column.hasClass("expanded")) { if (!$column.hasClass("expanded")) {
popovers.hide_all(); popovers.hide_all();
@@ -100,13 +112,13 @@ export class UserSearch {
} }
} }
initiate_search() { initiate_search(): void {
this.expand_column(); this.expand_column();
this.show_widget(); this.show_widget();
this.$input.trigger("focus"); this.$input.trigger("focus");
} }
toggle_filter_displayed() { toggle_filter_displayed(): void {
if (this.widget_shown()) { if (this.widget_shown()) {
this.initiate_search(); this.initiate_search();
} else { } else {
@@ -114,7 +126,7 @@ export class UserSearch {
} }
} }
on_focus(e) { on_focus(e: JQuery.FocusEvent): void {
this._on_focus(); this._on_focus();
e.stopPropagation(); e.stopPropagation();
} }