From 3f0110d9c0a5730c1d1da5c20ee1bcbafbe0f276 Mon Sep 17 00:00:00 2001 From: Priyank Patel Date: Mon, 14 Jun 2021 18:29:04 +0000 Subject: [PATCH] ts: Convert ui_util module to TypeScript. --- static/js/global.d.ts | 1 + static/js/{ui_util.js => ui_util.ts} | 18 ++++++++++-------- tools/test-js-with-node | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) rename static/js/{ui_util.js => ui_util.ts} (67%) diff --git a/static/js/global.d.ts b/static/js/global.d.ts index 6f5b20e2d5..900e1119d7 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -7,4 +7,5 @@ declare let zulip_test: any; interface JQuery { expectOne(): JQuery; + tab(action?: string): this; // From static/third/bootstrap } diff --git a/static/js/ui_util.js b/static/js/ui_util.ts similarity index 67% rename from static/js/ui_util.js rename to static/js/ui_util.ts index 6ac5b3d7c9..4edc6093e2 100644 --- a/static/js/ui_util.js +++ b/static/js/ui_util.ts @@ -3,28 +3,30 @@ import $ from "jquery"; // Add functions to this that have no non-trivial // dependencies other than jQuery. -export function change_tab_to(tabname) { +export function change_tab_to(tabname: string): void { $(`#gear-menu a[href="${CSS.escape(tabname)}"]`).tab("show"); } // https://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser -export function place_caret_at_end(el) { +export function place_caret_at_end(el: HTMLElement): void { el.focus(); const range = document.createRange(); range.selectNodeContents(el); range.collapse(false); const sel = window.getSelection(); - sel.removeAllRanges(); - sel.addRange(range); + sel?.removeAllRanges(); + sel?.addRange(range); } -export function blur_active_element() { +export function blur_active_element(): void { // this blurs anything that may perhaps be actively focused on. - document.activeElement.blur(); + if (document.activeElement instanceof HTMLElement) { + document.activeElement.blur(); + } } -export function convert_enter_to_click(e) { +export function convert_enter_to_click(e: JQuery.KeyDownEvent): void { if (e.key === "Enter") { e.preventDefault(); e.stopPropagation(); @@ -32,7 +34,7 @@ export function convert_enter_to_click(e) { } } -export function update_unread_count_in_dom(unread_count_elem, count) { +export function update_unread_count_in_dom(unread_count_elem: JQuery, count: number): void { // This function is used to update unread count in top left corner // elements. const unread_count_span = unread_count_elem.find(".unread_count"); diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 192f15d766..5a6b6bbd75 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -166,7 +166,7 @@ EXEMPT_FILES = { "static/js/ui_init.js", "static/js/ui.js", "static/js/ui_report.ts", - "static/js/ui_util.js", + "static/js/ui_util.ts", "static/js/unread_ops.js", "static/js/unread_ui.js", "static/js/upload_widget.js",