ts: Convert common module to TypeScript.

We also update the tippy.js mock in tests to expect a element
(zjquery FakeElement function) instead of zjquery instance, and get
rid of setting _tippy property itself to a noop function.
This commit is contained in:
Priyank Patel 2021-06-06 20:22:22 +00:00 committed by Tim Abbott
parent 9418ae51fa
commit 8a6f57d750
1 changed files with 24 additions and 12 deletions

View File

@ -1,4 +1,5 @@
import $ from "jquery";
import type {ReferenceElement} from "tippy.js";
import tippy from "tippy.js";
import {$t} from "./i18n";
@ -6,13 +7,13 @@ import {$t} from "./i18n";
export const status_classes = "alert-error alert-success alert-info alert-warning";
// TODO: Move this to the portico codebase.
export function autofocus(selector) {
export function autofocus(selector: string): void {
$(() => {
$(selector).trigger("focus");
});
}
export function phrase_match(query, phrase) {
export function phrase_match(query: string, phrase: string): boolean {
// match "tes" to "test" and "stream test" but not "hostess"
let i;
query = query.toLowerCase();
@ -31,7 +32,7 @@ export function phrase_match(query, phrase) {
return false;
}
export function copy_data_attribute_value(elem, key) {
export function copy_data_attribute_value(elem: JQuery, key: string): void {
// function to copy the value of data-key
// attribute of the element to clipboard
const temp = $(document.createElement("input"));
@ -43,11 +44,11 @@ export function copy_data_attribute_value(elem, key) {
elem.fadeIn(1000);
}
export function has_mac_keyboard() {
export function has_mac_keyboard(): boolean {
return /mac/i.test(navigator.platform);
}
export function adjust_mac_shortcuts(key_elem_class, require_cmd_style) {
export function adjust_mac_shortcuts(key_elem_class: string, require_cmd_style = false): void {
if (!has_mac_keyboard()) {
return;
}
@ -83,10 +84,14 @@ export function adjust_mac_shortcuts(key_elem_class, require_cmd_style) {
// See https://zulip.readthedocs.io/en/latest/development/authentication.html#password-form-implementation
// for design details on this feature.
function set_password_toggle_label(password_selector, label, tippy_tooltips) {
function set_password_toggle_label(
password_selector: string,
label: string,
tippy_tooltips: boolean,
): void {
$(password_selector).attr("aria-label", label);
if (tippy_tooltips) {
const element = $(password_selector)[0];
const element: ReferenceElement = $(password_selector)[0];
const tippy_instance = element._tippy ?? tippy(element);
tippy_instance.setContent(label);
} else {
@ -94,7 +99,11 @@ function set_password_toggle_label(password_selector, label, tippy_tooltips) {
}
}
function toggle_password_visibility(password_field_id, password_selector, tippy_tooltips) {
function toggle_password_visibility(
password_field_id: string,
password_selector: string,
tippy_tooltips: boolean,
): void {
let label;
const password_field = $(password_field_id);
@ -110,7 +119,10 @@ function toggle_password_visibility(password_field_id, password_selector, tippy_
set_password_toggle_label(password_selector, label, tippy_tooltips);
}
export function reset_password_toggle_icons(password_field, password_selector) {
export function reset_password_toggle_icons(
password_field: string,
password_selector: string,
): void {
$(password_field).attr("type", "password");
$(password_selector).removeClass("fa-eye").addClass("fa-eye-slash");
const label = $t({defaultMessage: "Show password"});
@ -118,10 +130,10 @@ export function reset_password_toggle_icons(password_field, password_selector) {
}
export function setup_password_visibility_toggle(
password_field_id,
password_selector,
password_field_id: string,
password_selector: string,
{tippy_tooltips = false} = {},
) {
): void {
const label = $t({defaultMessage: "Show password"});
set_password_toggle_label(password_selector, label, tippy_tooltips);
$(password_selector).on("click", (e) => {