Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

ts: Migrate `scroll_util.js` to TypeScript.

Lalitc7b8f0658b01
diff --git a/tools/test-js-with-node b/tools/test-js-with-node
index 89b1e5c7dc5..5b7cf58384b 100755
--- a/tools/test-js-with-node
+++ b/tools/test-js-with-node
@@ -149,7 +149,7 @@ EXEMPT_FILES = make_set(
"web/src/scheduled_messages.js",
"web/src/scheduled_messages_overlay_ui.js",
"web/src/scroll_bar.ts",
- "web/src/scroll_util.js",
+ "web/src/scroll_util.ts",
"web/src/search_pill_widget.js",
"web/src/sent_messages.js",
"web/src/sentry.ts",
diff --git a/web/src/scroll_util.js b/web/src/scroll_util.ts
similarity index 68%
rename from web/src/scroll_util.js
rename to web/src/scroll_util.ts
index 279a44711c9..a4be25b85ac 100644
--- a/web/src/scroll_util.js
+++ b/web/src/scroll_util.ts
@@ -1,7 +1,10 @@
import $ from "jquery";
import SimpleBar from "simplebar";
-export function get_content_element($element) {
+// This type is helpful for testing, where we may have a dummy object instead of an actual jquery object.
+type JQueryOrZJQuery = {__zjquery?: true} & JQuery;
+
+export function get_content_element($element: JQuery): JQuery {
const element = $element.expectOne()[0];
const sb = SimpleBar.instances.get(element);
if (sb) {
@@ -10,9 +13,9 @@ export function get_content_element($element) {
return $element;
}
-export function get_scroll_element($element) {
+export function get_scroll_element($element: JQueryOrZJQuery): JQuery {
// For testing we just return the element itself.
- if ($element && $element.__zjquery) {
+ if ($element?.__zjquery) {
return $element;
}
@@ -23,12 +26,12 @@ export function get_scroll_element($element) {
} else if ("simplebar" in element.dataset) {
// The SimpleBar mutation observer hasn’t processed this element yet.
// Create the SimpleBar early in case we need to add event listeners.
- return $(new SimpleBar(element).getScrollElement());
+ return $(new SimpleBar(element).getScrollElement()!);
}
return $element;
}
-export function reset_scrollbar($element) {
+export function reset_scrollbar($element: JQuery): void {
const element = $element.expectOne()[0];
const sb = SimpleBar.instances.get(element);
if (sb) {
@@ -38,7 +41,11 @@ export function reset_scrollbar($element) {
}
}
-export function scroll_delta(opts) {
+export function scroll_delta(opts: {
+ elem_top: number;
+ elem_bottom: number;
+ container_height: number;
+}): number {
const elem_top = opts.elem_top;
const container_height = opts.container_height;
const elem_bottom = opts.elem_bottom;
@@ -58,16 +65,19 @@ export function scroll_delta(opts) {
return delta;
}
-export function scroll_element_into_container($elem, $container, sticky_header_height = 0) {
+export function scroll_element_into_container(
+ $elem: JQuery,
+ $container: JQuery,
+ sticky_header_height = 0,
+): void {
// This does the minimum amount of scrolling that is needed to make
// the element visible. It doesn't try to center the element, so
// this will be non-intrusive to users when they already have
// the element visible.
-
$container = get_scroll_element($container);
const elem_top = $elem.position().top - sticky_header_height;
- const elem_bottom = elem_top + $elem.innerHeight();
- const container_height = $container.height() - sticky_header_height;
+ const elem_bottom = elem_top + ($elem.innerHeight() ?? 0);
+ const container_height = ($container.height() ?? 0) - sticky_header_height;
const opts = {
elem_top,
@@ -81,5 +91,5 @@ export function scroll_element_into_container($elem, $container, sticky_header_h
return;
}
- $container.scrollTop($container.scrollTop() + delta);
+ $container.scrollTop(($container.scrollTop() ?? 0) + delta);
}