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

View File

@ -691,8 +691,8 @@ export function initialize_stream_cursor() {
scroll_container_sel: "#left_sidebar_scroll_container", scroll_container_sel: "#left_sidebar_scroll_container",
find_li(opts) { find_li(opts) {
const stream_id = opts.key; const stream_id = opts.key;
const li = get_stream_li(stream_id); const $li = get_stream_li(stream_id);
return li; return $li;
}, },
first_key: stream_list_sort.first_stream_id, first_key: stream_list_sort.first_stream_id,
prev_key: stream_list_sort.prev_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"); 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}) { function basic_conf({first_key, prev_key, next_key}) {
const list = { const list = {
scroll_container_sel: "whatever", scroll_container_sel: "whatever",