Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions frontend/src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,9 @@ type Untyped = any;
/** Leaflet, loaded from a CDN by sqlpage_map when a page holds a map. */
declare const L: Untyped;

/**
* A Bootstrap a page loaded for itself, preferred over the bundled copy. Its
* widgets are untyped: naming a few of them here would only claim more than
* this file knows.
*/
interface PageBootstrap {
[widget: string]: Untyped;
}

interface Window {
/** Every chart rendered on the page, in the order they were built. */
charts?: unknown[];
bootstrap?: PageBootstrap;
/** A Bootstrap a page loaded for itself, preferred over the bundled copy. */
bootstrap?: typeof import("@tabler/core").bootstrap;
}
17 changes: 14 additions & 3 deletions frontend/src/sqlpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { add_init_fn } from "./init.js";
// A page may load its own Bootstrap; prefer it over the bundled copy.
const page_bootstrap = () => window.bootstrap ?? bundled_bootstrap;

/**
* Bootstrap declares getOrCreateInstance on the base class, which returns a
* BaseComponent and so loses show().
* @typedef {InstanceType<typeof bundled_bootstrap.Toast>} ToastWidget
* @typedef {InstanceType<typeof bundled_bootstrap.Modal>} ModalWidget
*/

const nonce = /** @type {HTMLScriptElement} */ (document.currentScript).nonce;

function sqlpage_card() {
Expand Down Expand Up @@ -368,7 +375,7 @@ function open_toasts_for_hash(toasts) {
if (!hash) return;
for (const toast of toasts) {
if (normalize_hash(toast.dataset.toastTrigger) === hash) {
Toast.getOrCreateInstance(toast).show();
/** @type {ToastWidget} */ (Toast.getOrCreateInstance(toast)).show();
}
}
}
Expand Down Expand Up @@ -413,7 +420,9 @@ function sqlpage_toast() {
}

toast.removeAttribute("data-pre-init");
const instance = Toast.getOrCreateInstance(toast);
const instance = /** @type {ToastWidget} */ (
Toast.getOrCreateInstance(toast)
);
initialized_toasts.push(toast);
toast.addEventListener("hidden.bs.toast", () => {
restore_focus_after_toast(toast, container);
Expand Down Expand Up @@ -488,7 +497,9 @@ function open_modal_for_hash() {
if (!hash) return;
const modal = document.getElementById(hash);
if (!modal?.classList.contains("modal")) return;
const bootstrap_modal = page_bootstrap().Modal.getOrCreateInstance(modal);
const bootstrap_modal = /** @type {ModalWidget} */ (
page_bootstrap().Modal.getOrCreateInstance(modal)
);
bootstrap_modal.show();
modal.addEventListener(
"hidden.bs.modal",
Expand Down
10 changes: 1 addition & 9 deletions tests/end-to-end/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
// What the browser tests reach for on the page: widgets that the scripts under
// test attach to elements at runtime.

interface TomSelectInstance {
getValue(): string | string[];
setTextboxValue(value: string): void;
focus(): void;
open(): void;
options: Record<string, { label?: string } | undefined>;
}

interface HTMLElement {
/** Attached by sqlpage_select_dropdown to every select it takes over. */
tomselect?: TomSelectInstance;
tomselect?: import("tom-select/popular").default;
}
Loading