2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 00:36:14 +01:00
|
|
|
import render_loader from "../templates/loader.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-06-17 19:39:35 +02:00
|
|
|
export function make_indicator(
|
2022-01-25 11:36:19 +01:00
|
|
|
$outer_container: JQuery,
|
2021-06-17 19:39:35 +02:00
|
|
|
{abs_positioned = false, text}: {abs_positioned?: boolean; text?: string} = {},
|
|
|
|
): void {
|
2022-01-25 11:36:19 +01:00
|
|
|
let $container = $outer_container;
|
2017-04-06 02:18:00 +02:00
|
|
|
|
|
|
|
// TODO: We set white-space to 'nowrap' because under some
|
|
|
|
// unknown circumstances (it happens on Keegan's laptop) the text
|
|
|
|
// width calculation, above, returns a result that's a few pixels
|
|
|
|
// too small. The container's div will be slightly too small,
|
|
|
|
// but that's probably OK for our purposes.
|
2022-01-25 11:36:19 +01:00
|
|
|
$outer_container.css({"white-space": "nowrap"});
|
2017-04-06 02:18:00 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.empty();
|
2014-03-13 15:03:01 +01:00
|
|
|
|
2021-06-25 22:08:05 +02:00
|
|
|
if (abs_positioned) {
|
2014-03-13 15:03:01 +01:00
|
|
|
// Create some additional containers to facilitate absolutely
|
|
|
|
// positioned spinners.
|
2022-01-25 11:36:19 +01:00
|
|
|
const container_id = $container.attr("id")!;
|
|
|
|
let $inner_container = $("<div>", {id: `${container_id}_box_container`});
|
|
|
|
$container.append($inner_container);
|
|
|
|
$container = $inner_container;
|
|
|
|
$inner_container = $("<div>", {id: `${container_id}_box`});
|
|
|
|
$container.append($inner_container);
|
|
|
|
$container = $inner_container;
|
2014-03-13 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 00:31:13 +01:00
|
|
|
const $spinner_elem = $("<div>", {class: "loading_indicator_spinner", ["aria-hidden"]: "true"});
|
2022-01-25 11:36:19 +01:00
|
|
|
$spinner_elem.html(render_loader({container_id: $outer_container.attr("id")}));
|
|
|
|
$container.append($spinner_elem);
|
2019-11-02 00:06:25 +01:00
|
|
|
let text_width = 0;
|
2014-03-13 15:03:01 +01:00
|
|
|
|
2021-06-25 22:08:05 +02:00
|
|
|
if (text !== undefined) {
|
2022-03-17 00:31:13 +01:00
|
|
|
const $text_elem = $("<span>", {class: "loading_indicator_text"});
|
2022-01-25 11:36:19 +01:00
|
|
|
$text_elem.text(text);
|
|
|
|
$container.append($text_elem);
|
2014-03-13 15:03:01 +01:00
|
|
|
// See note, below
|
2021-06-25 22:08:05 +02:00
|
|
|
if (!abs_positioned) {
|
2022-01-25 11:36:19 +01:00
|
|
|
text_width = 20 + ($text_elem.width() ?? 0);
|
2017-04-05 03:55:28 +02:00
|
|
|
}
|
2014-03-13 15:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// These width calculations are tied to the spinner width and
|
|
|
|
// margins defined via CSS
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.css({width: 38 + text_width, height: 0});
|
2014-03-13 15:03:01 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$outer_container.data("destroying", false);
|
2021-02-28 00:36:14 +01:00
|
|
|
}
|
2014-03-13 15:03:01 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function destroy_indicator($container: JQuery): void {
|
|
|
|
if ($container.data("destroying")) {
|
2014-03-13 15:03:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.data("destroying", true);
|
2014-03-13 15:03:01 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const spinner = $container.data("spinner_obj");
|
2014-03-13 15:03:01 +01:00
|
|
|
if (spinner !== undefined) {
|
|
|
|
spinner.stop();
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.removeData("spinner_obj");
|
|
|
|
$container.empty();
|
|
|
|
$container.css({width: 0, height: 0});
|
2021-02-28 00:36:14 +01:00
|
|
|
}
|
2021-07-16 08:45:23 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function show_button_spinner($elt: JQuery, using_dark_theme: boolean): void {
|
2021-07-16 08:45:23 +02:00
|
|
|
if (!using_dark_theme) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$elt.attr("src", "/static/images/loader-black.svg");
|
2021-07-16 08:45:23 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
$elt.attr("src", "/static/images/loader-white.svg");
|
2021-07-16 08:45:23 +02:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$elt.css("display", "inline-block");
|
2021-07-16 08:45:23 +02:00
|
|
|
}
|