Farmbot-Web-App/frontend/toast/toast.ts

91 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-06-26 09:59:24 -06:00
import { createToast, createToastOnce } from "./toast_internal_support";
2019-07-02 11:03:16 -06:00
import { t } from "../i18next_wrapper";
2019-06-25 15:26:02 -06:00
2019-06-25 12:39:05 -06:00
/**
2019-07-15 17:33:45 -06:00
* Orange message with "Warning" as the default title.
2019-06-25 12:39:05 -06:00
*/
2020-04-20 15:07:40 -06:00
export const warning = (
message: string,
title = t("Warning"),
color = "orange",
idPrefix = "",
) => {
createToastOnce(message, title, color, idPrefix, console.warn);
};
2019-06-25 12:39:05 -06:00
/**
* Red message with "Error" as the default title.
*/
2020-04-20 15:07:40 -06:00
export const error = (
message: string,
title = t("Error"),
color = "red",
idPrefix = "",
) => {
createToastOnce(message, title, color, idPrefix, console.error);
2019-06-25 12:39:05 -06:00
};
/**
* Green message with "Success" as the default title.
*/
2020-04-20 15:07:40 -06:00
export const success = (
message: string,
title = t("Success"),
color = "green",
idPrefix = "",
) =>
createToast(message, title, color, idPrefix);
2019-06-25 12:39:05 -06:00
/**
2019-07-15 17:33:45 -06:00
* Blue message with "FYI" as the default title.
2019-06-25 12:39:05 -06:00
*/
2020-04-20 15:07:40 -06:00
export const info = (
message: string,
title = t("FYI"),
color = "blue",
idPrefix = "",
) =>
createToast(message, title, color, idPrefix);
2019-06-25 12:39:05 -06:00
/**
2019-07-15 17:33:45 -06:00
* Yellow message with "Busy" as the default title.
*/
2020-04-20 15:07:40 -06:00
export const busy = (
message: string,
title = t("Busy"),
color = "yellow",
idPrefix = "",
) =>
createToast(message, title, color, idPrefix);
2019-07-15 17:33:45 -06:00
/**
* Dark blue message with "Did you know?" as the default title.
2019-06-25 12:39:05 -06:00
*/
2020-04-20 15:07:40 -06:00
export const fun = (
message: string,
title = t("Did you know?"),
color = "dark-blue",
idPrefix = "",
) =>
createToast(message, title, color, idPrefix);
2019-06-25 12:39:05 -06:00
/**
* Adds a hidden container div for holding toast messages.
*/
export const init = () => {
const toastContainer = document.createElement("div");
toastContainer.classList.add("toast-container");
document.body.appendChild(toastContainer);
};
2020-04-20 15:07:40 -06:00
/** Remove all toast messages that match the provided id prefix. */
export const removeToast = (idPrefix: string) => {
const parent = document.querySelector(".toast-container");
const toasts = document.querySelectorAll(`[id^="${idPrefix}-"]`);
if (parent) {
Object.values(toasts).map(toast => parent.removeChild(toast));
} else {
console.error("toast-container is null.");
}
};