@intuitionrobotics/thunderstorm
Version:
161 lines • 6.08 kB
JavaScript
/*
* Thunderstorm is a full web app framework!
*
* Typescript & Express backend infrastructure that natively runs on firebase function
* Typescript & React frontend infrastructure
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as React from "react";
import { addAllItemToArray, Module, Second } from "@intuitionrobotics/ts-common";
// noinspection TypeScriptPreferShortImport
import { ThunderDispatcher } from "../../core/thunder-dispatcher.js";
import { StylableBuilder } from "../../tools/Stylable.js";
import {} from "../../components/types.js";
export var ToastType;
(function (ToastType) {
ToastType[ToastType["success"] = 0] = "success";
ToastType[ToastType["error"] = 1] = "error";
ToastType[ToastType["info"] = 2] = "info";
})(ToastType || (ToastType = {}));
const Interval_DefaultToast = 6 * Second;
export class ToastBuilder extends StylableBuilder {
bgColor = "#eeffef";
duration = Interval_DefaultToast;
type = ToastType.info;
positionVertical = "top";
positionHorizontal = "center";
actions = [];
content = "NO CONTENT";
setType(type) {
this.type = type;
return this;
}
setContent(content) {
this.content = content;
return this;
}
setBackground(bgColor) {
this.bgColor = bgColor;
return this;
}
setDuration(duration) {
this.duration = duration;
return this;
}
setActions(actions) {
this.actions = actions || [];
return this;
}
addActions(...actions) {
addAllItemToArray(this.actions, actions);
return this;
}
setVerticalPosition(positionVertical) {
this.positionVertical = positionVertical;
return this;
}
setHorizontalPosition(positionHorizontal) {
this.positionHorizontal = positionHorizontal;
return this;
}
show() {
const toast = {
duration: this.duration,
type: this.type,
content: this.content,
positionVertical: this.positionVertical,
positionHorizontal: this.positionHorizontal,
actions: this.actions,
bgColor: this.bgColor,
className: this.className,
style: this.style,
};
// @ts-expect-error TS struggles with this dynamic typing
ToastModule.toastImpl(toast);
}
}
const dispatch_showToast = new ThunderDispatcher("__showToast");
export class ToastModule_Class extends Module {
DefaultBuilder = new ToastBuilder();
constructor() {
super("ToastModule");
}
setDefaultBuilder(DefaultBuilder) {
this.DefaultBuilder = DefaultBuilder;
}
toastError(errorMessage, interval = Interval_DefaultToast, builder = this.DefaultBuilder) {
this.toast(errorMessage, ToastType.error, interval, builder);
}
toastSuccess(successMessage, interval = Interval_DefaultToast, builder = this.DefaultBuilder) {
this.toast(successMessage, ToastType.success, interval, builder);
}
toastInfo(infoMessage, interval = Interval_DefaultToast, builder = this.DefaultBuilder) {
this.toast(infoMessage, ToastType.info, interval, builder);
}
toast(_message, type, interval = Interval_DefaultToast, builder = this.DefaultBuilder) {
let color;
switch (type) {
case ToastType.success:
color = "#2ee06f";
break;
case ToastType.error:
color = "#ff4436";
break;
case ToastType.info:
color = "#49addb";
break;
default:
color = "#e8e8e8";
break;
}
let content = _message;
if (typeof _message === "string")
content = ToastModule.adjustStringMessage(_message);
// console.log("_message:", _message)
builder.setContent(content).setDuration(interval).setBackground(color).setType(type).show();
}
adjustStringMessage = (_message) => {
let message = _message;
message = message.replace(/\n#### (.*?)\n/g, "\n<h4>$1</h4>\n");
message = message.replace(/\n### (.*?)\n/g, "\n<h3>$1</h3>\n");
message = message.replace(/\n## (.*?)\n/g, "\n<h2>$1</h2>\n");
message = message.replace(/\n# (.*?)\n/g, "\n<h1>$1</h1>\n");
message = message.replace(/(<\/?.*?>)\n/g, "$1");
message = message.replace(/([^>]?)\n/g, "$1<br/> ");
const ignore = message.match(/`(.*?)`/g);
if (ignore && ignore.length > 0)
message = ignore.reduce((input, toEscape) => {
const replaceValue = toEscape.substring(1, toEscape.length - 1)
.replace(/([^\\]?)_/g, "$1\\_")
.replace(/([^\\]?)\*/g, "$1\\*");
return input.replace(toEscape, replaceValue);
}, message);
message = message.replace(/([^\\]?)_(.*?)([^\\])_/g, "$1<i>$2$3</i>");
message = message.replace(/([^\\]?)\*(.*?)([^\\])\*/g, "$1<b>$2$3</b>");
message = message.replace(/\\_/g, "_");
message = message.replace(/\\\*/g, "*");
return message;
};
hideToast = (_toast) => {
// in the future we can add more than one toast and manage a stack of them!!
dispatch_showToast.dispatchUI();
};
toastImpl(toast) {
dispatch_showToast.dispatchUI(toast);
}
}
export const ToastModule = new ToastModule_Class();
//# sourceMappingURL=ToasterModule.js.map