list_cursor: Convert module to TypeScript.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-09-22 01:42:54 -07:00 committed by Tim Abbott
parent fb765bd057
commit fcb212721a
3 changed files with 24 additions and 30 deletions

View File

@ -3,26 +3,25 @@ import $ from "jquery";
import * as blueslip from "./blueslip";
import * as scroll_util from "./scroll_util";
export class ListCursor {
constructor({highlight_class, list}) {
const config_ok =
highlight_class &&
list &&
list.scroll_container_sel &&
list.find_li &&
list.first_key &&
list.prev_key &&
list.next_key;
if (!config_ok) {
blueslip.error("Programming error");
return;
}
type List<Key> = {
scroll_container_sel: string;
find_li(opts: {key: Key; force_render: boolean}): JQuery;
first_key(): Key | undefined;
prev_key(key: Key): Key | undefined;
next_key(key: Key): Key | undefined;
};
export class ListCursor<Key> {
highlight_class: string;
list: List<Key>;
curr_key?: Key;
constructor({highlight_class, list}: {highlight_class: string; list: List<Key>}) {
this.highlight_class = highlight_class;
this.list = list;
}
clear() {
clear(): void {
if (this.curr_key === undefined) {
return;
}
@ -33,11 +32,11 @@ export class ListCursor {
this.curr_key = undefined;
}
get_key() {
get_key(): Key | undefined {
return this.curr_key;
}
get_row(key) {
get_row(key: Key | undefined): {highlight(): void; clear(): void} | undefined {
// TODO: The list class should probably do more of the work
// here, so we're not so coupled to jQuery, and
// so we instead just get back a widget we can say
@ -69,12 +68,12 @@ export class ListCursor {
};
}
adjust_scroll($li) {
adjust_scroll($li: JQuery): void {
const $scroll_container = $(this.list.scroll_container_sel);
scroll_util.scroll_element_into_container($li, $scroll_container);
}
redraw() {
redraw(): void {
// We should only call this for situations like the buddy
// list where we redraw the whole list without necessarily
// changing it, so we just want to re-highlight the current
@ -88,7 +87,7 @@ export class ListCursor {
row.highlight();
}
go_to(key) {
go_to(key: Key | undefined): void {
if (key === undefined) {
blueslip.error("Caller is not checking keys for ListCursor.go_to");
return;
@ -106,7 +105,7 @@ export class ListCursor {
row.highlight();
}
reset() {
reset(): void {
this.clear();
const key = this.list.first_key();
if (key === undefined) {
@ -116,7 +115,7 @@ export class ListCursor {
this.go_to(key);
}
prev() {
prev(): void {
if (this.curr_key === undefined) {
return;
}
@ -128,7 +127,7 @@ export class ListCursor {
this.go_to(key);
}
next() {
next(): void {
if (this.curr_key === undefined) {
// This is sort of a special case where we went from
// an empty filter to having data.

View File

@ -691,8 +691,8 @@ export function initialize_stream_cursor() {
scroll_container_sel: "#left_sidebar_scroll_container",
find_li(opts) {
const stream_id = opts.key;
const li = get_stream_li(stream_id);
return li;
const $li = get_stream_li(stream_id);
return $li;
},
first_key: stream_list_sort.first_stream_id,
prev_key: stream_list_sort.prev_stream_id,

View File

@ -9,11 +9,6 @@ const $ = require("./lib/zjquery");
const {ListCursor} = zrequire("list_cursor");
run_test("config errors", () => {
blueslip.expect("error", "Programming error");
new ListCursor({});
});
function basic_conf({first_key, prev_key, next_key}) {
const list = {
scroll_container_sel: "whatever",